Repository navigation
assistant transfer: refuse malformed bundles with TransferError, not a traceback - #740
Conversation
…a traceback inspect_bundle(), stage_import() and install_bundle() let damaged or hand-edited bundles escape as BadZipFile, UnicodeDecodeError, OSError, KeyError, AttributeError, TypeError, RecursionError, ValueError, JSONDecodeError, FileExistsError, NotImplementedError or RuntimeError. The CLI catches only TransferError, so these printed a traceback. Map zipfile's errors to UnsafeArchive only where the archive is opened or a member is read, so a caller's own errors keep their type. Refuse encrypted members, members whose offset is not before the central directory, and compression other than stored or deflated. Parse member JSON through one helper that caps nesting at 32 levels (json.loads on 3.12+ accepts depths json.dumps and repr cannot walk), and parse the extracted lock the same way. Type-check created_at, sha256 strings, int (not bool) sizes, string ids and pair licenses, the lock chip object and the voice receipt, and refuse duplicate pair and voice pack ids. Check the file count before any per-path work, then refuse payload paths that are not canonical, have more than 64 parts, a part longer than 255 bytes or more than 2,048 bytes in all, collide with another path or a generated transfer-lock.json or transfer-receipt.json, or sit outside a payload directory; the collision check tests each path's ancestors against the declared names rather than collecting every ancestor. Report a runtime spawn that cannot start (a NUL byte or an oversized argument from the bundle) as TransferError, and decode child output with replacement so a failing child keeps its exit code and stderr in the error.
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (2 files)
Reviewed by free · Input: 83.2K · Output: 3K · Cached: 240.6K |
|
GREEN at 2fc5a9c Verified on main, not just read:
|
|
Independent second read, done on the head 2fc5a9c (the merge landed at 13:27 UTC as 7eea7f1 while this review was running; main carries the tree). The change is correct and does not weaken any existing check. Verified by hand:
Tests here: the transfer suite passes at this head except 7 app-transfer cases that need a repo checkout (install.sh / portable app bytes) - an artifact of running from a serve-only archive; CI runs the full checkout and is green on all four jobs. The new tests fail on main's pre-#740 code by construction (main had none of the wrappers these tests exercise). One follow-up, pre-existing and unchanged by this PR: the lock and receipt member reads in |
|
Independent second read of #740 at head 2fc5a9c - GREEN for the merge call (model: zai/glm-5.3-flash; same-model-family check: the author posts as only21mil, an outside machine account, so the reviewer-identity rule is met on my side). What I verified on a clean tree of this exact head (serve + scripts + tests), not a partial extract:
Findings that would block the merge: none. Two follow-ups, both pre-existing and both now covered by follow-up PRs already open by the same author (out of scope here, listed for the queue only):
I recommend the merge. Model: zai/glm-5.3-flash. |
…ming (#746) ## Problem When a bundle's pair lock or voice receipt declares a large size, import reads the whole member into memory before checking its size or hash. This follows up the review note on #740. `_json_member` joins every chunk of a member, then parses it. The only bound is the member's declared size in the zip's central directory, and nothing checks that size before this read. Deflate compresses a run of whitespace about a thousandfold, so a 520 KB bundle can carry a 512 MiB lock. `stage_import` buffers all of it while deriving the bundle's licenses, before the size and hash checks. ## Fix - `_member_chunks` takes an optional byte limit and raises `UnsafeArchive` as soon as the decompressed stream passes it, reading at most one byte past the limit. The count comes from bytes actually read, so neither header size field can raise the limit. - `_json_member` now requires `max_bytes`: 1 MiB for pair locks and voice receipts (real ones are a few KB), and 64 MiB for the manifest, the same limit its declared-size check already uses. - Payload extraction keeps the unlimited stream: each member's declared size must equal the manifest's before it starts, and the size and hash are checked after. ## Verification A new test, `test_oversized_lock_or_receipt_stops_at_the_byte_cap`, first checks that a normal bundle with a voice pack inspects and imports. Then, for both the lock and the receipt, it builds a member padded to 32 MiB and deflated to under 1 MiB, with the local header claiming 16 bytes. It expects `UnsafeArchive` with traced memory rising less than 8 MiB. | | base `2aef87273` | head | |---|---|---| | new test, Python 3.14 and 3.11 | 2 subtests fail (bundle accepted) | pass | | 512 MiB lock in a 520 KB bundle, peak RSS added during the license check | +1025 MiB, 1.6 s, accepted | +2 MiB, 3 ms, `UnsafeArchive` | All the Linux steps of `collectors.yml` pass on Python 3.14 and on 3.11, run one after the other. `privacy_check.py` reports clean. <details><summary>Repro output</summary> ``` == base (2aef872) py3.14 bundle 530922 bytes; everyday/lock.json: compressed 522256, central-directory size 536871775, local header size 16 provenance: mlx-omarchy None mx=None verified=no-mlx harness=2aef87273 files=unresolved outcome: accepted (whole lock read into memory) lock read: 1.580 s; peak RSS 1048 MiB (+1025 MiB over pre-read) == base (2aef872) py3.11 bundle 530921 bytes; everyday/lock.json: compressed 522255, central-directory size 536871775, local header size 16 provenance: mlx-omarchy None mx=None verified=no-mlx harness=2aef87273 files=unresolved outcome: accepted (whole lock read into memory) lock read: 1.547 s; peak RSS 1049 MiB (+1026 MiB over pre-read) == head (b707d094a) py3.14 bundle 530923 bytes; everyday/lock.json: compressed 522257, central-directory size 536871775, local header size 16 provenance: mlx-omarchy None mx=None verified=no-mlx harness=b707d094a files=unresolved outcome: UnsafeArchive: bundle member everyday/lock.json exceeds 1048576 bytes lock read: 0.003 s; peak RSS 24 MiB (+2 MiB over pre-read) == head (b707d094a) py3.11 bundle 530920 bytes; everyday/lock.json: compressed 522254, central-directory size 536871775, local header size 16 provenance: mlx-omarchy None mx=None verified=no-mlx harness=b707d094a files=unresolved outcome: UnsafeArchive: bundle member everyday/lock.json exceeds 1048576 bytes lock read: 0.003 s; peak RSS 25 MiB (+2 MiB over pre-read) ``` </details> Generated with Claude Opus 5.5 in T3 Code Nightly
Problem
When
transfer inspectortransfer installgets a damaged or hand-edited bundle, it prints a Python traceback instead of atransfer:error.main()catches onlyTransferError, butinspect_bundle(),stage_import()andinstall_bundle()raisedBadZipFile,KeyError,TypeError,OSErrorand others.Change
Contract: a malformed bundle raises
TransferError(UnsafeArchivefor archive damage), so the CLI exits 1 withtransfer: <reason>.UnsafeArchiveonly at archive open and member reads.TransferError.Scope
Crash fix. No issue: small and obvious.
Verification
Tested by: AI agent.
python -m mlx_omarchy_assistant.transfer inspect --bundle <file>→stage_import()with the bundle's platform.Before,
c4fe1ad0a(newest green main):After,
2fc5a9c99:inspectreads no payload bytes, so it passes the CRC-damaged bundle; install refuses it. Python 3.11 and the local merge withc4fe1ad0aprint the same. Nine of ten new test methods fail onc4fe1ad0a; one guards caller errors.Links
e34fd699f, head2fc5a9c99;git merge-treewithc4fe1ad0ais clean.tests/test_assistant_transfer.pyNot shown
macos-15legs ofcollectors.yml;tests/runs on Linux only.installon Apple Silicon.Checks run by AI agent
collectors.ymlLinux steps, plusmlx_provenance.py --self-test, on2fc5a9c99:scripts/test_collect.pyscripts/test_collect_macos.pyunittest discover -s tests -p test_mlx_provenance.pyscripts/mlx_provenance.py --self-testscripts/bench_matrix.py --self-testpytest -q qualification-kit/...pytest tests -qpytest -q tests/coreml/test_import_contract.py--no-depsclosure (3.14 leg only)Six more bundles split the same way:
bad-utf8-name,no-manifest,manifest-listandint-sha256(traceback before, clean after);dict-license(TypeErrorinstage_import()before, refused after);bom-lock(JSONDecodeErrorinstage_import()before, installs after). A local fuzz found no other escape on2fc5a9c99: 5,000 byte-level, zip-header-field and ZIP64 mutations (5,439 escapes onc4fe1ad0a, 0 after), and every JSON path in the manifest, a lock and a receipt set to 13 wrong-typed values or deleted (172 before, 0 after). On Python 3.14,json.loadsaccepts a lock, license, probe list orarchnested 40,000 to 55,000 levels deep, which later raisedRecursionErrorfromjson.dumpsorreprininspectorstage_import(); the 32-level cap refuses all five. A payload path 1,200 directories deep (RecursionError), one over 4,096 bytes (ENAMETOOLONG) and a duplicatepair_id(one lock activated twice) are refused too. A 606 KB bundle whose manifest alone declares 199,990 paths 60 levels deep is refused at about 300 MB peak RSS, as onc4fe1ad0a, in about 4 s against 1.6 s (19 s and 2.1 GB before this fix).Generated with Claude Opus 5.5 in T3 Code Nightly