Compare commits

..

1 Commits

Author SHA1 Message Date
Stefan Agner 1e457600f1 Harden backup tar extraction with Python tar_filter (#172252) 2026-05-27 18:10:04 +02:00
5 changed files with 60 additions and 29 deletions
+1 -1
View File
@@ -39,7 +39,7 @@ on:
env:
CACHE_VERSION: 3
MYPY_CACHE_VERSION: 1
HA_SHORT_VERSION: "2026.7"
HA_SHORT_VERSION: "2026.6"
ADDITIONAL_PYTHON_VERSIONS: "[]"
# 10.3 is the oldest supported version
# - 10.3.32 is the version currently shipped with Synology (as of 17 Feb 2022)
+2 -4
View File
@@ -92,8 +92,7 @@ def _extract_backup(
):
ostf.tar.extractall(
path=Path(tempdir, "extracted"),
members=securetar.secure_path(ostf.tar),
filter="fully_trusted",
filter="tar",
)
backup_meta_file = Path(tempdir, "extracted", "backup.json")
backup_meta = json.loads(backup_meta_file.read_text(encoding="utf8"))
@@ -119,8 +118,7 @@ def _extract_backup(
) as istf:
istf.extractall(
path=Path(tempdir, "homeassistant"),
members=securetar.secure_path(istf),
filter="fully_trusted",
filter="tar",
)
if restore_content.restore_homeassistant:
keep = list(KEEP_BACKUPS)
+1 -1
View File
@@ -14,7 +14,7 @@ if TYPE_CHECKING:
APPLICATION_NAME: Final = "HomeAssistant"
MAJOR_VERSION: Final = 2026
MINOR_VERSION: Final = 7
MINOR_VERSION: Final = 6
PATCH_VERSION: Final = "0.dev0"
__short_version__: Final = f"{MAJOR_VERSION}.{MINOR_VERSION}"
__version__: Final = f"{__short_version__}.{PATCH_VERSION}"
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "homeassistant"
version = "2026.7.0.dev0"
version = "2026.6.0.dev0"
license = "Apache-2.0"
license-files = ["LICENSE*", "homeassistant/backports/LICENSE*"]
description = "Open-source home automation platform running on Python 3."
+55 -22
View File
@@ -1,5 +1,6 @@
"""Test methods in backup_restore."""
from io import BytesIO
import json
from pathlib import Path
import tarfile
@@ -386,8 +387,8 @@ def test_restore_backup(
}
def test_restore_backup_filter_files(tmp_path: Path) -> None:
"""Test filtering dangerous files when restoring a backup."""
def test_restore_backup_rejects_unsafe_files(tmp_path: Path) -> None:
"""Test that a backup with unsafe paths is rejected."""
backup_file_path = tmp_path / "backups" / "test.tar"
backup_file_path.parent.mkdir()
get_fixture_path(
@@ -410,7 +411,55 @@ def test_restore_backup_filter_files(tmp_path: Path) -> None:
"data/home-assistant_v2.db-wal",
}
real_extractone = tarfile.TarFile._extract_one
with (
mock.patch(
"homeassistant.backup_restore.restore_backup_file_content",
return_value=backup_restore.RestoreBackupFileContent(
backup_file_path=backup_file_path,
password=None,
remove_after_restore=False,
restore_database=True,
restore_homeassistant=True,
),
),
pytest.raises(tarfile.FilterError),
):
backup_restore.restore_backup(tmp_path.as_posix())
result = restore_result_file_content(tmp_path)
assert result is not None
assert result["success"] is False
assert result["error_type"] in {"AbsolutePathError", "OutsideDestinationError"}
def test_restore_backup_rejects_absolute_symlink(tmp_path: Path) -> None:
"""Test rejection of a symlink whose linkname escapes the destination.
A SYMTYPE entry followed by a regular file whose name traverses the
symlink would otherwise land attacker-controlled bytes outside the
extraction directory. The tar filter resolves the path after the
symlink and rejects the entry.
"""
backup_file_path = tmp_path / "backups" / "test.tar"
backup_file_path.parent.mkdir()
with tarfile.open(backup_file_path, "w") as tar:
backup_json = json.dumps(
{"homeassistant": {"version": "0.0.0"}, "compressed": False}
).encode()
info = tarfile.TarInfo(name="./backup.json")
info.size = len(backup_json)
tar.addfile(info, BytesIO(backup_json))
symlink = tarfile.TarInfo(name="pwn")
symlink.type = tarfile.SYMTYPE
symlink.linkname = "/tmp" # noqa: S108
tar.addfile(symlink)
payload = b"pwned"
evil = tarfile.TarInfo(name="pwn/ha_escape_target")
evil.size = len(payload)
tar.addfile(evil, BytesIO(payload))
with (
mock.patch(
@@ -423,27 +472,11 @@ def test_restore_backup_filter_files(tmp_path: Path) -> None:
restore_homeassistant=True,
),
),
mock.patch(
"tarfile.TarFile._extract_one", autospec=True, wraps=real_extractone
) as extractone_mock,
pytest.raises(tarfile.FilterError),
):
assert backup_restore.restore_backup(tmp_path.as_posix()) is True
backup_restore.restore_backup(tmp_path.as_posix())
# Check the unsafe files are not extracted, and that the safe files are extracted
extracted_files = {call.args[1].name for call in extractone_mock.mock_calls}
assert extracted_files == {
"./backup.json", # From the outer tar
"homeassistant.tar.gz", # From the outer tar
".",
"data",
"data/home-assistant_v2.db",
"data/home-assistant_v2.db-wal",
}
assert restore_result_file_content(tmp_path) == {
"error": None,
"error_type": None,
"success": True,
}
assert not Path("/tmp/ha_escape_target").exists() # noqa: S108
@pytest.mark.parametrize(("remove_after_restore"), [True, False])