Skip to content

Commit 56aec8a

Browse files
marcelsafinCopilot
andauthored
fix: decode the zipped manifest as UTF-8 before parsing (github#3958)
Review follow-up: feeding PyYAML the byte stream let its Reader honour a UTF-16 BOM and accept a manifest yamlio.load_yaml rejects, so zip and directory sources diverged. Decode raw as UTF-8 (UnicodeError -> BundlerError 'Could not read ...') then parse, and cover a well-formed UTF-16 manifest in the regression tests. Assisted-by: GitHub Copilot (model: claude-fable-5, autonomous) Co-authored-by: Copilot <[email protected]>
1 parent 7346819 commit 56aec8a

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎src/specify_cli/commands/bundle/__init__.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,6 @@ def _local_manifest_source(arg: str):
772772
return BundleManifest.from_file(manifest_path)
773773

774774
if candidate.suffix == ".zip":
775-
import io
776-
777775
import yaml as _yaml
778776

779777
from ..._download_security import open_zip_bounded, read_zip_member_limited
@@ -791,8 +789,20 @@ def _local_manifest_source(arg: str):
791789
error_type=BundlerError,
792790
label="bundle manifest",
793791
)
792+
# The bounded-zip helpers above keep archive failures inside the
793+
# BundlerError contract, but the manifest bytes need the same
794+
# treatment as yamlio.load_yaml: decode as UTF-8 explicitly —
795+
# feeding PyYAML the byte stream would let its Reader auto-detect
796+
# a UTF-16 BOM and accept a manifest the directory and bundle.yml
797+
# sources reject.
798+
try:
799+
text = raw.decode("utf-8")
800+
except UnicodeError as exc:
801+
raise BundlerError(
802+
f"Could not read bundle.yml inside '{candidate}': {exc}"
803+
) from exc
794804
try:
795-
data = _yaml.safe_load(io.BytesIO(raw))
805+
data = _yaml.safe_load(text)
796806
except _yaml.YAMLError as exc:
797807
# The sibling directory/bundle.yml branches reach YAML through
798808
# load_yaml(), which turns a parse failure into a BundlerError. This

‎tests/integration/test_bundler_local_install.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,39 @@ def test_local_source_rejects_unknown_file(tmp_path: Path):
6262
_local_manifest_source(str(weird))
6363

6464

65+
def test_local_source_zip_non_utf8_manifest_raises_bundler_error(tmp_path: Path):
66+
"""Undecodable bundle.yml bytes inside a .zip must raise BundlerError.
67+
68+
The manifest bytes are decoded as UTF-8 explicitly, matching
69+
``yamlio.load_yaml``'s "Could not read ..." contract, instead of
70+
escaping as a raw ``UnicodeDecodeError``/``ReaderError`` traceback.
71+
"""
72+
artifact = tmp_path / "demo.zip"
73+
with zipfile.ZipFile(artifact, "w") as archive:
74+
archive.writestr("bundle.yml", b"\xff\xfe bundle \xc3\x28\n")
75+
76+
with pytest.raises(BundlerError, match="Could not read"):
77+
_local_manifest_source(str(artifact))
78+
79+
80+
def test_local_source_zip_utf16_manifest_rejected_like_directory(tmp_path: Path):
81+
"""A well-formed UTF-16 manifest must fail the same way in a .zip.
82+
83+
``yamlio.load_yaml`` decodes strictly as UTF-8, so a UTF-16 bundle.yml
84+
(the realistic PowerShell ``Out-File`` output) is rejected when read
85+
from a directory. Feeding the zip bytes straight to PyYAML would let
86+
its Reader honour the UTF-16 BOM and *accept* the same manifest,
87+
making zip and directory sources diverge.
88+
"""
89+
artifact = tmp_path / "demo.zip"
90+
manifest_text = "bundle:\n id: demo-bundle\n version: 1.0.0\n"
91+
with zipfile.ZipFile(artifact, "w") as archive:
92+
archive.writestr("bundle.yml", manifest_text.encode("utf-16"))
93+
94+
with pytest.raises(BundlerError, match="Could not read"):
95+
_local_manifest_source(str(artifact))
96+
97+
6598
def test_install_bundled_extension_from_zip_offline(tmp_path: Path):
6699
"""End-to-end: build → install (offline, local .zip) → list → remove."""
67100
project = make_project(tmp_path / "proj")

0 commit comments

Comments
 (0)