Skip to content

fix(desktop): read a Zip64 end record that carries an extensible data sector - #10618

Closed
XiaoBinGan wants to merge 1 commit into
morluto:mainfrom
XiaoBinGan:fix/zip64-extensible-end-record
Closed

XiaoBinGan wants to merge 1 commit into
morluto:mainfrom
XiaoBinGan:fix/zip64-extensible-end-record

Conversation

@XiaoBinGan

Copy link
Copy Markdown
Contributor

Summary

directoryEnd located the Zip64 end-of-central-directory record by subtracting its fixed 56 bytes from the locator. APPNOTE 4.3.14 lets that record end with a variable-size extensible data sector, so a conforming archive whose record carries one was refused as not a readable zip archive with no entries listed.

Measured

A one-member Zip64 archive whose record ends with N extensible bytes, on this host:

extensible bytes before after unzip -l
0 1 entry 1 entry lists a.txt
8 refused 1 entry lists a.txt
32 refused 1 entry lists a.txt
1024 refused 1 entry lists a.txt

Info-ZIP unzip lists the member of every one of those archives, so the refusal is an accept/reject flip against a reference reader, not a stricter reading of the format.

Approach

4.3.15 has the locator declare the record's offset, so the declared offset is read first. A record can only end where its locator begins, and only the record's own length field says where it begins — so that length must land exactly on the locator. That check is what distinguishes the real record from a coincidental signature, and it keeps the refusal when a declared offset points at something else.

When the declared offset does not land there, the fixed 56 bytes before the locator are read first, as zipfile and zip.js do, and then the bytes behind them are walked for the record's real start, bounded by ZIP64_EXTENSIBLE_LIMIT.

A declared archive offset cannot account for data prepended to the archive, so the walk also makes that case work:

prepended bytes extensible before after
0 0 1 entry 1 entry
0 8 refused 1 entry
16 0 1 entry 1 entry
16 8 refused 1 entry

Evidence

  • src/domain/desktop/zipDirectory.test.ts — new file, 2 cases covering both tables above. Reverting the source fails both (2 failed), and no test previously called scanZipDirectory at all.
  • The existing Zip64 tests (tests/boundary/providers/desktopZipPython.test.ts:388,405) build their archives with withZip64End, which always writes a 44-byte body hard against its locator — the shape that cannot see this defect.
  • src/domain/desktop, src/domain/apkSignature, src/domain/zipEndOfCentralDirectory.test.ts — 1012 passing. The single appWithheld failure reproduces on main on this Windows host.
  • npx tsc --noEmit -p . — 0 errors. npm run lint — 0 errors.

Unavailable coverage: desktopZipPython and desktopZipHeap exercise this path only through Python and a subprocess, and the former fails 26 tests on this Windows host at both revisions. The new test calls scanZipDirectory directly, so it runs everywhere.

Author readiness

Human owner: @XiaoBinGan

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-14T18:41:26.457058Z
We couldn’t complete this review. Comment @codex review to try again.
101cda6 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@greptile-apps

greptile-apps Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the previous boundary issue is fixed.

Summary

Reads Zip64 end records with an extensible data sector in desktop inspection and exported Python readers.

  • Desktop ZIP readers now list archives with Zip64 extensible data sectors.

Reviews (7) · Last reviewed commit: "fix(desktop): bound the Zip64 search on ..." · Reviewed by Greptile

Comment thread src/domain/desktop/zipDirectory.ts Resolved
@XiaoBinGan

Copy link
Copy Markdown
Contributor Author

Correct, and a real gap I introduced: the inspection reader could read the record while the exported Python reader still read exactly 56 bytes before the locator, so a document readable during inspection became unreadable in the exported environment.

_zip64_end_at now mirrors the same two steps the inspection read takes — the locator's declared offset, then the bytes behind the locator bounded by ZIP64_EXTENSIBLE_LIMIT — and accepts a record only when its own length field ends exactly at the locator, which is what identifies it however much data precedes the archive.

While writing it I also found the locator layout mattered: signature (4), the disk it names (4), the record's offset at locator + 8 (8), and the disk count (4). Reading the offset straight after the signature takes the disk number instead. That is fixed here too.

One gap I must record: no Python interpreter is installed on this host. tests/boundary/providers/desktopExportPython.test.ts fails 20 of 21 on main here for exactly that reason, so I could not run the exported reader. What I did instead: verified the extracted module's indentation and bracket balance, and compared its branches with the inspection read line by line. The cross-reader fixture you asked for is the right thing to add, and I have not claimed it as verified.

@XiaoBinGan

Copy link
Copy Markdown
Contributor Author

Correct. The search sought and read for every byte, so an unusable declared offset cost about a million reads through the exported reader's disk-backed handle.

The bytes before the locator are now read once and the search indexes into them, and begins_at refuses an offset outside that window before slicing, so a declared offset cannot drive a read either. The bound is the same one the inspection read uses, so both search the same window.

@XiaoBinGan

Copy link
Copy Markdown
Contributor Author

Correct. begins_at only looked inside the buffered window, so a declared offset pointing before it was refused — which is exactly what a record with a sector larger than the window needs, and the exported reader then answered "not a readable zip archive" for an archive both the previous reader and the inspection read accept.

The declared offset is now read where it points, in one fixed-size read, and the search window behind the locator is what ZIP64_EXTENSIBLE_LIMIT bounds. Both paths still require the record's own length field to end exactly at the locator, so neither is a guess.

I should also record the honest limit of my verification here: there is no Python interpreter on this host, so the exported reader is checked statically — module indentation and bracket balance, plus a line-by-line comparison with the inspection read, whose 8 case matrix (prepended × extensible) passes. The reader itself has not been executed.

@XiaoBinGan
XiaoBinGan force-pushed the fix/zip64-extensible-end-record branch from 614ba18 to edff236 Compare November 15, 2026 14:50
Comment thread src/domain/desktop/zipDirectory.ts Resolved
@XiaoBinGan

Copy link
Copy Markdown
Contributor Author

Right, and the boundary was off by exactly the record's own size.

ZIP64_EXTENSIBLE_LIMIT describes the extensible data sector, which begins after the record's 56
fixed bytes, but the walk counted those fixed bytes against it:

- locator - at <= ZIP64_EXTENSIBLE_LIMIT
+ locator - at - ZIP64_END_BYTES <= ZIP64_EXTENSIBLE_LIMIT

Measured on a prepended archive (16 leading bytes) against sector sizes around the bound:

sector before after
1 048 520 read read
1 048 575 undefined read
1 048 576 undefined read
1 048 577 undefined undefined

So 56 bytes of reach were lost, exactly as you described, and prepended data is what forces the
search instead of the direct read — with no prepended stub the locator's declared offset resolves the
record and the bound never applies.

Boundary case added in zipDirectory.test.ts, as suggested: prepended data plus sectors of
LIMIT - 56 and LIMIT, both now read, and LIMIT + 1 still undefined so the search cannot grow
without limit. Reverting the bound alone fails it with sector 1048576: expected undefined to be 1,
so it is load-bearing rather than a restatement.

I have not verified the Python comparison directly — there is no Python interpreter on this host — so
I am relying on your reading of zipfile for that side rather than claiming to have run it.

node scripts/verify/architecture-guards.mjs — passed across 2 843 source files.
src/domain/desktop + zipEndOfCentralDirectory — 889 passing. tsc --noEmit 0 errors.

… sector

APPNOTE 4.3.14 puts a variable-size extensible data sector after the Zip64 end
record's 56 fixed bytes, and 4.3.15 has its locator declare that record's offset. The
reader assumed the record was always the 56 bytes immediately before the locator, so
an archive written with a sector either lost its Zip64 record entirely or read the
classic fields the record exists to override.

Measured on a 1 MiB sector, the reader's reach went from "no record" to "record found"
for every sector up to the `1 << 20` bound, and the declared offset is taken first
when it points at a record whose own length ends exactly at the locator — so an
archive embedded after leading bytes, where the declared archive offset cannot be used
directly, is still found.

`appExportPythonZip.ts` had its own copy of the same assumption in the exported
reader; it now reads through the same scan.

Three reads that were spelled out are now `u32At` / `u64At`, which took this file from
14 counted raw reads to 12 and let the baseline shrink.

**Verification.** `src/domain/desktop/zipDirectory.test.ts` — 3 passing, covering a
record with a sector, one whose sector is exactly the bound, and one beyond it. `tsc
--noEmit` 0 errors. `architecture-guards` passed across 2 818 source files.
`npm run lint` 0 errors.

The branch was 700 commits behind and `scripts/verify/architecture-guard-baselines.json`
was split into per-guard files under `scripts/verify/baselines/` while it sat, so it is
rebased: the one line this PR lowered now lives in `baselines/raw-buffer-read.json`.
@morluto

morluto commented Nov 19, 2026

Copy link
Copy Markdown
Owner

The host and generated Python readers now follow the declared Zip64 end-record span, including the extensible sector, in #12157. Closing this superseded implementation.

@morluto morluto closed this Nov 19, 2026
@morluto morluto added the bug Something isn't working label Nov 22, 2026
@morluto morluto added the area: runtime Process capture and runtime observation label Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: runtime Process capture and runtime observation bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants