From 54ecaf8b389ae7ffbe22cdd410d93fd1f690677d Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 26 Oct 2026 17:25:15 +0000 Subject: [PATCH] Close backup download stream in google_drive when not fully read (#188686) --- homeassistant/components/google_drive/api.py | 7 +- .../components/google_drive/backup.py | 4 +- tests/components/google_drive/test_backup.py | 181 +++++++++++++++++- 3 files changed, 184 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/google_drive/api.py b/homeassistant/components/google_drive/api.py index 10d51a5fa292..c74dae070ba2 100644 --- a/homeassistant/components/google_drive/api.py +++ b/homeassistant/components/google_drive/api.py @@ -6,7 +6,7 @@ import json import logging from typing import Any, override -from aiohttp import ClientSession, ClientTimeout, StreamReader +from aiohttp import ClientSession, ClientTimeout from aiohttp.client_exceptions import ClientError, ClientResponseError from google_drive_api.api import AbstractAuth, GoogleDriveApi @@ -15,6 +15,7 @@ from homeassistant.config_entries import ConfigEntryState from homeassistant.const import CONF_ACCESS_TOKEN from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import config_entry_oauth2_flow +from homeassistant.util.async_iterator import ResourceChunkIterator from .const import DOMAIN @@ -283,9 +284,9 @@ class DriveClient: """Delete file.""" await self._api.delete_file(file_id) - async def async_download(self, file_id: str) -> StreamReader: + async def async_download(self, file_id: str) -> ResourceChunkIterator: """Download a file.""" resp = await self._api.get_file_content( file_id, timeout=ClientTimeout(total=_UPLOAD_AND_DOWNLOAD_TIMEOUT) ) - return resp.content + return ResourceChunkIterator(resp.content.readany, close=resp.close) diff --git a/homeassistant/components/google_drive/backup.py b/homeassistant/components/google_drive/backup.py index 47120c1d2479..52da6aa3949a 100644 --- a/homeassistant/components/google_drive/backup.py +++ b/homeassistant/components/google_drive/backup.py @@ -17,7 +17,6 @@ from homeassistant.components.backup import ( ) from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers.aiohttp_client import ChunkAsyncStreamIterator from homeassistant.util import slugify from . import DATA_BACKUP_AGENT_LISTENERS @@ -162,8 +161,7 @@ class GoogleDriveBackupAgent(BackupAgent): file_id = await self._client.async_get_backup_file_id(backup_id) if file_id: _LOGGER.debug("Downloading file_id: %s", file_id) - stream = await self._client.async_download(file_id) - return ChunkAsyncStreamIterator(stream) + return await self._client.async_download(file_id) except (GoogleDriveApiError, HomeAssistantError, TimeoutError) as err: _LOGGER.error("Failed to download backup: %s", err) raise BackupAgentError( diff --git a/tests/components/google_drive/test_backup.py b/tests/components/google_drive/test_backup.py index efa46e0d77a3..1186da101f9a 100644 --- a/tests/components/google_drive/test_backup.py +++ b/tests/components/google_drive/test_backup.py @@ -1,15 +1,21 @@ """Test the Google Drive backup platform.""" +from dataclasses import replace from io import StringIO import json from typing import Any from unittest.mock import AsyncMock, MagicMock, Mock, patch -from aiohttp import ClientResponse +from aiohttp import ( + ClientConnectionError, + ClientPayloadError, + ClientResponse, + StreamReader, +) from freezegun.api import FrozenDateTimeFactory from google_drive_api.exceptions import GoogleDriveApiError import pytest -from pytest_homeassistant.common import MockConfigEntry +from pytest_homeassistant.common import MockConfigEntry, get_fixture_path from pytest_homeassistant.test_util.aiohttp import mock_stream from pytest_homeassistant.typing import ClientSessionGenerator, WebSocketGenerator from syrupy.assertion import SnapshotAssertion @@ -65,6 +71,14 @@ TEST_AGENT_BACKUP_RESULT = { } +# Protected with password "hunter2", padded beyond the 4 MiB read buffer so the +# backup is not read to the end when checking the password or decrypting it +PROTECTED_BACKUP_DATA = ( + get_fixture_path("test_backups/c0cb53bd.tar", BACKUP_DOMAIN).read_bytes() + + b"\0" * 12 * 2**20 +) + + async def consume_stream( file_metadata: Any, open_stream: Any, @@ -639,3 +653,166 @@ async def test_agents_delete_not_found( assert response["result"] == {"agent_error_translations": {}, "agent_errors": {}} mock_api.delete_file.assert_not_called() + + +@pytest.fixture +def backup_data() -> bytes: + """Backup data, override in tests to change defaults.""" + return b"backup data" + + +@pytest.fixture +def backup_protected() -> bool: + """Backup protected, override in tests to change defaults.""" + return False + + +@pytest.fixture +def downloaded_responses( + mock_api: MagicMock, backup_data: bytes, backup_protected: bool +) -> list[AsyncMock]: + """Mock the backup and return the responses of downloaded backup archives.""" + backup = replace(TEST_AGENT_BACKUP, protected=backup_protected) + mock_api.list_files = AsyncMock( + return_value={ + "files": [ + {"id": "backup-file-id", "description": json.dumps(backup.as_dict())} + ] + } + ) + responses: list[AsyncMock] = [] + + def get_file_content(*args: Any, **kwargs: Any) -> AsyncMock: + response = AsyncMock(spec=ClientResponse) + response.content = mock_stream(backup_data) + responses.append(response) + return response + + mock_api.get_file_content = AsyncMock(side_effect=get_file_content) + return responses + + +@pytest.mark.parametrize( + ("backup_protected", "backup_data"), + [pytest.param(True, PROTECTED_BACKUP_DATA, id="protected_backup")], +) +@pytest.mark.parametrize( + ("password", "success"), + [ + pytest.param("hunter2", True, id="correct_password"), + pytest.param("wrong", False, id="wrong_password"), + ], +) +async def test_can_decrypt_on_download_closes_response( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + downloaded_responses: list[AsyncMock], + password: str, + success: bool, +) -> None: + """Test the download is closed after checking the password of a backup.""" + client = await hass_ws_client(hass) + + await client.send_json_auto_id( + { + "type": "backup/can_decrypt_on_download", + "backup_id": TEST_AGENT_BACKUP.backup_id, + "agent_id": TEST_AGENT_ID, + "password": password, + } + ) + assert (await client.receive_json())["success"] is success + + assert len(downloaded_responses) == 1 + downloaded_responses[0].close.assert_called_once() + + +@pytest.mark.parametrize( + ("backup_protected", "backup_data"), + [pytest.param(True, PROTECTED_BACKUP_DATA, id="protected_backup")], +) +@pytest.mark.parametrize( + "query", + [ + pytest.param("", id="no_password"), + pytest.param("&password=hunter2", id="correct_password"), + pytest.param("&password=wrong", id="wrong_password"), + ], +) +async def test_agents_download_closes_response( + hass_client: ClientSessionGenerator, + downloaded_responses: list[AsyncMock], + query: str, +) -> None: + """Test the download is closed when the backup is downloaded.""" + client = await hass_client() + + resp = await client.get( + f"/api/backup/download/{TEST_AGENT_BACKUP.backup_id}?agent_id={TEST_AGENT_ID}{query}" + ) + assert resp.status == 200 + await resp.read() + + assert len(downloaded_responses) == 1 + downloaded_responses[0].close.assert_called_once() + + +async def test_agents_download_closes_response_on_read_error( + hass_client: ClientSessionGenerator, + mock_api: MagicMock, + downloaded_responses: list[AsyncMock], +) -> None: + """Test the download is closed when reading the backup fails.""" + get_file_content = mock_api.get_file_content.side_effect + + def get_failing_file_content(*args: Any, **kwargs: Any) -> AsyncMock: + response = get_file_content(*args, **kwargs) + response.content = StreamReader(Mock(_reading_paused=False), limit=2**16) + response.content.set_exception(ClientConnectionError()) + return response + + mock_api.get_file_content.side_effect = get_failing_file_content + client = await hass_client() + + resp = await client.get( + f"/api/backup/download/{TEST_AGENT_BACKUP.backup_id}?agent_id={TEST_AGENT_ID}" + ) + with pytest.raises(ClientPayloadError): + await resp.read() + + assert len(downloaded_responses) == 1 + downloaded_responses[0].close.assert_called_once() + + +async def test_agents_download_empty_chunk_before_eof( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_api: MagicMock, + downloaded_responses: list[AsyncMock], +) -> None: + """Test an empty chunk at an HTTP chunk boundary does not end the download.""" + get_file_content = mock_api.get_file_content.side_effect + + def get_chunked_file_content(*args: Any, **kwargs: Any) -> AsyncMock: + response = get_file_content(*args, **kwargs) + response.content = StreamReader( + Mock(_reading_paused=False), 2**16, loop=hass.loop + ) + for data in (b"backup", b"", b" data"): + response.content.begin_http_chunk_receiving() + response.content.feed_data(data) + response.content.end_http_chunk_receiving() + response.content.feed_eof() + return response + + mock_api.get_file_content.side_effect = get_chunked_file_content + client = await hass_client() + + resp = await client.get( + f"/api/backup/download/{TEST_AGENT_BACKUP.backup_id}?agent_id={TEST_AGENT_ID}" + ) + assert resp.status == 200 + assert await resp.read() == b"backup data" + + assert len(downloaded_responses) == 1 + downloaded_responses[0].close.assert_called_once()