Skip to content

fix(artifacts): admit ZIP archives after unclaimed leading bytes - #5803

Merged
kaoru0822-kitauji merged 3 commits into
mainfrom
fix/zip-executable-prefix
Oct 21, 2026
Merged

kaoru0822-kitauji merged 3 commits into
mainfrom
fix/zip-executable-prefix

Conversation

@kaoru0822-kitauji

@kaoru0822-kitauji kaoru0822-kitauji commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

What broke: inspect-artifact and extract-artifact refused a valid ZIP behind a script or self-extractor stub as "unsupported binary format", although Info-ZIP, bsdtar and Python read it (#5732).
Fix: target resolution and root inventory now locate a trailing single-disk ZIP from its end record, central directory and first local header. This applies only when no other format claims the leading bytes; executable-led files keep their executable route.
Risk: low to medium. It is additive for previously refused files. Classification stays unchanged for files any existing format recognizes, and Zip64, split and empty archives still fail closed.

Summary

Fixes #5732. A ZIP preceded by inert leading bytes, such as a #!/bin/sh stub or a self-extractor, was refused before inventory or extraction because ZIP recognition only checked the first four bytes. Both prefix styles in common use now inventory and extract with the same verified member bytes as the plain archive:

  • member offsets that include the leading bytes (zip -A, or Python's zipfile appending to the stub);
  • raw concatenation, where offsets were written before the stub was prepended.

Problem and expected behavior

Trigger: a 17-byte shell stub followed by a ZIP. Before this change, on main d672cc239 (macOS arm64), inspect-artifact returned target_unavailable / "Cannot open artifact: unsupported binary format." for both prefix styles, while the plain ZIP passed.

Expected: a file whose leading bytes are claimed by no other format, and which ends in a structurally consistent ZIP, is a ZIP archive. A file whose leading bytes begin an executable header keeps the executable classification. For example, /bin/ls with an appended ZIP stays mach-o-universal.

Change and scope

  • src/domain/zipPackageFormat.ts now owns ZIP recognition at both ends of a file: hasZipSignature for the start and the new locateTrailingZipArchive for a trailing archive. The locator takes an injected positional read and is bounded to the last 22 + 65,535 bytes, one 46-byte central-directory header and one 4-byte local-header signature. It requires:

    • an end record whose comment ends exactly at end of file;
    • a single disk with at least one entry and no Zip64 markers;
    • a central-directory header at the derived position;
    • a local header at the first entry's offset, using an offset shift that resolves both prefix styles.
  • src/domain/binaryTarget.ts adds claimsExecutableHeader, which recognizes the ELF, MZ, Mach-O and FAT families that parseExecutableHeader interprets, whether or not the header is valid. Both callers use it, so they make the same decision.

  • BinaryTargetResolver consults the locator only after native header detection fails and only when no executable family claims the leading bytes. Root inventory classification (classify.ts) consults it only when the bytes classify as file or unknown and no executable family claims them.

  • ZIP_NON_ENTRY_LIMITATION now names leading bytes before the first entry, such as a self-extractor stub, as non-entry bytes that are not occurrences.

  • Not included:

    • Zip64, split, and empty trailing archives, which stay refused;
    • trailing data after the end record;
    • nested members, whose classification still uses their own leading bytes;
    • executable-led archives such as PE or ELF self-extractors, which keep their executable route.

    Admitting any of these would need a separate decision.

  • Scope decision: not applicable. This is format recognition within the existing ZIP reader boundary. The reader (@zip.js/zip.js 2.15.0) already reads both prefix styles, as verified below.

Contract and boundary impact

  • Semantic owner and earliest changed stage: ZIP recognition in src/domain/zipPackageFormat.ts, consumed by target resolution and root inventory classification.
  • Contribution scope fit and linked maintainer decision: not applicable (ordinary fix).
  • CLI and MCP/tool-catalog contract: no schema change. Previously refused files with unclaimed leading bytes and a trailing ZIP now return zip artifact inventories and extractions.
  • Provider, bridge, target-format, or platform compatibility: classification is unchanged for every file another format recognizes. Executable-led files with appended archives keep their executable route.
  • Evidence, artifact, provenance, or reconstruction contract: root identity and digest cover the whole file, leading bytes included. Members keep their content identities. The ZIP limitation now names leading bytes as non-entry bytes.
  • Target selection, authority, upstream behavior, and cleanup details: no new process, write or network behavior. Extraction still uses the hardened ZIP reader and output tree.
  • Generated metadata, package, or installation impact: none.

Expected effects:

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated:
    • tests/acceptance/artifactZipLeadingBytes.test.ts: the public CLI inspect-artifact and extract-artifact on a script-stubbed ZIP. It checks zip root format, verified members, the non-entry limitation, and the extracted bytes. This fails on the base revision with target_unavailable.
    • tests/boundary/filesystem/zipDetection.test.ts:
      • Both prefix styles classify as zip through root classification and parseBinaryTarget, with member identities equal to the plain archive's.
      • ELF-, MZ- and Mach-O-led files with an appended ZIP stay off the ZIP route.
      • A trailing ZIP is not located with bytes after the end record, a corrupt central-directory header, a Zip64 directory offset, or an empty archive record.
    • tests/boundary/filesystem/artifactComparison.test.ts: updated for the widened limitation text.
  • Base reproduction (executed, macOS arm64, main d672cc239): plain.zip passed; prefixed.zip (offsets include the stub) and concat.zip (raw concatenation) failed with "unsupported binary format".
  • After the change (executed):
    • All three inventory a.txt, d, and d/b.txt as verified, and extract members with identical digests.
    • /bin/ls + ZIP stays mach-o-universal.
    • A corrupted central directory, trailing junk, and a text file ending in an end-record-shaped tail are still refused.
  • Reader check (executed): @zip.js/zip.js 2.15.0 reads both prefix styles. For raw concatenation it rebases entry offsets itself (a.txt at offset 17).
  • User-visible CLI output (inspect-artifact on the concatenated file): root format zip; occurrences ., a.txt, d, d/b.txt, all verified.
  • Performance (executed, warm, 200 iterations): locateTrailingZipArchive takes 0.112 ms per call on a 50 MB unrecognized random file and 0.023 ms on the prefixed ZIP. It runs only for files that no other format claims; the cold-cache cost of one ≤64 KiB tail read was not measured separately.
  • Remaining proof gaps:
    • Zip64 trailing archives are not located.
    • Real PE self-extractors were not exercised; the MZ-led synthetic case covers the routing rule.
    • Linux and Windows hosts were not run locally; CI covers them.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • npm run test:focused -- tests/acceptance/artifactZipLeadingBytes.test.ts tests/boundary/filesystem/zipDetection.test.ts tests/boundary/filesystem/artifactComparison.test.ts tests/acceptance/artifactZipCurrentDirectory.test.ts tests/acceptance/artifactZipExtractionPaths.test.ts tests/acceptance/pyinstallerArchive.test.ts tests/acceptance/godotPck.test.ts src/artifacts/ZipArtifactReader.test.ts src/artifacts/apple/machoContainer.test.ts: 76 passed.

  • npm run test:focused -- every test file that references parseBinaryTarget, classifyAndHashRoot, BinaryTargetResolver or parseExecutableHeader (46 files): 822 passed, 1 skipped.

  • npm run check:fast: passed (typecheck, lint).

  • npm run format:check: passed.

  • CLI reproduction and non-regression checks above: passed.

  • Review follow-up (93e5576): the root ZIP family mapping (.app → app-pack) now lives once in zipRootPackageFormat and is shared by both resolver routes and root inventory; a stub-prefixed root Example.app is app-pack from both. zipDetection + CLI acceptance: 25 passed; check:fast, format:check passed. The first CI run failed only peResources (30 s timeout, also seen on main on 2026-10-20); it passed locally in 9 s.

Compatibility, safety, and release

  • Breaking changes or migration steps: none. Only previously refused inputs change outcome.
  • Real Hopper/Ghidra, browser, or OS coverage: macOS arm64 only, locally. No provider is involved.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: the leading bytes are never executed or extracted. Extraction keeps the existing path-containment and output-tree checks, and the locator's reads are bounded.

Author readiness

Human owner: @kaoru0822-kitauji

  • 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.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

🤖 Generated with Claude Code

ZIP recognition required the archive at byte 0, so a ZIP behind a
script or self-extractor stub was refused as an unsupported binary
format although standard readers accept it. Target resolution and root
inventory now locate a trailing single-disk ZIP from its end record,
central directory and first local header, whether member offsets include
the leading bytes or not. Leading bytes any executable header family
claims keep their executable route.

Fixes #5732

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kaoru0822-kitauji kaoru0822-kitauji added bug Something isn't working track:issues Issue bug-fix track labels Oct 21, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T11:23:34.055814Z 63ba251 New commits
ℹ️ 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 41948cc6a5

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/application/BinaryTargetResolver.ts Resolved
@greptile-apps

greptile-apps Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no blocking issue was established.

Summary

Adds trailing ZIP recognition for files whose leading bytes are not claimed by another format.

  • Artifact tools recognize ZIPs after unclaimed leading bytes.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Root file] --> B{Existing format recognized?}
  B -->|Yes| C[Keep existing route]
  B -->|No| D{Executable header claimed?}
  D -->|Yes| C
  D -->|No| E[Locate trailing ZIP]
  E -->|Not found| F[Keep rejection or generic classification]
  E -->|Found| G[Shared root ZIP package mapping]
  G --> H[Existing ZIP reader and extraction checks]
Loading

Reviews (3) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile

Comment thread src/application/BinaryTargetResolver.ts Resolved
The trailing-ZIP fallback named a stub-prefixed root `.app` archive
`zip`, while root inventory and the leading-signature route name it
`app-pack`. The root mapping now lives once in `zipRootPackageFormat`
and both target resolution routes and root inventory use it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kaoru0822-kitauji
kaoru0822-kitauji merged commit 35d6d5b into main Oct 21, 2026
73 of 76 checks passed
@kaoru0822-kitauji
kaoru0822-kitauji deleted the fix/zip-executable-prefix branch October 21, 2026 12:06
@github-actions github-actions Bot mentioned this pull request Oct 21, 2026
3 tasks done
@morluto morluto added the area: evidence Evidence, artifacts, and reconstruction label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: evidence Evidence, artifacts, and reconstruction bug Something isn't working track:issues Issue bug-fix track

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Artifact inspection and extraction reject ZIPs with a valid executable prefix

2 participants