spec-kit icon indicating copy to clipboard operation
spec-kit copied to clipboard

harden: safe ZIP extraction, checksums, and bounded reads for extension/preset downloads

Open PascalThuet opened this issue 5 months ago • 1 comments

Part of splitting #2442 into smaller, dedicated PRs. Re-derived against current main and adapted to the extensions/ package layout (the original PR predated the module→package refactor #3014).

Stacked on #3140 (bounded HTTP reads). This PR extends the _download_security module introduced there, so its diff currently includes #3140's commit — review/merge #3140 first.

What

  • Extend src/specify_cli/_download_security.py with safe_extract_zip (path-traversal + symlink + per-member/total-size + entry-count bounds), read_zip_member_limited, and verify_sha256.
  • extensions/__init__.py + presets/__init__.py: install_from_zip now extracts via safe_extract_zip (zip-slip + symlink + zip-bomb defense) instead of the path-only containment check; catalog fetches and package downloads use bounded reads; downloads are checked against their catalog sha256 when present.
  • extensions/_commands.py: the inline extension.yml manifest pre-read (before extraction) uses read_zip_member_limited so a crafted manifest can't zip-bomb memory.
  • catalogs.py (CatalogStackBase) + the preset catalog validator: reject hostless URLs before the scheme check.

Deliberately omitted

The command-registration path-traversal guard the original #2442 change carried is already on main (landed via #3088, and more thoroughly) — so agents.py/test_registrar_path_traversal.py are not touched here.

Validation

  • test_download_security.py, test_extensions.py, test_presets.py, integrations/test_integration_catalog.py — 757 passed.
  • ruff check clean on all changed files.
  • Existing extension/preset tests pass unchanged except for mock/assertion updates: stream mocks made cursor-based (so they exercise the new bounded reads), and one catalog-URL message assertion updated for the host-before-scheme order. integrations/test_integration_catalog.py carries a one-line param fix because it asserts the shared CatalogStackBase behavior changed here (the rest of that file's bounded-read work is a later PR).

Catalog bounded-reads for the workflow/integration/bundler readers, and the tree-wide unbounded-read gate, come in the next PR.

PascalThuet avatar Jun 23 '26 22:06 PascalThuet

Converting to draft as https://github.com/github/spec-kit/pull/3140 has to land first

mnriem avatar Jun 24 '26 18:06 mnriem

Rebased this PR onto current main after #3140 and #3671, then force-updated the branch to b52570e (54d5f0c for the rebased feature wiring, b52570e for the independent audit fixes).

The refreshed diff removes the duplicated #3140 history, retains the shared redirect/checksum behavior from main, and adds bounded ZIP32 central-directory preflight, portable path/collision validation, bounded direct/catalog/update reads, safe catalog-derived output filenames, and manifest selection aligned with extraction. The trailing-backslash directory feedback remains covered.

Validation on the published tree: full Python 3.12 suite 5291 passed, 166 skipped; focused Python 3.13 suite 915 passed; Ruff 0.15.0 and git diff --check clean; Bandit found no medium/high issue in changed production files. ZIP64 is intentionally rejected, SHA-256 applies only where catalogs provide hashes, and atomic cache publication/local symlink races remain a separate follow-up.

Posted on behalf of @PascalThuet by OpenAI Codex (model: GPT-5).

PascalThuet avatar Jul 23 '26 21:07 PascalThuet

Please address Copilot feedback and fix test & lint errors

mnriem avatar Jul 24 '26 18:07 mnriem

Thank you!

mnriem avatar Jul 28 '26 12:07 mnriem