Skip to content

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

Open
PascalThuet wants to merge 2 commits into
github:mainfrom
PascalThuet:split/extension-preset-downloads
Open

harden: safe ZIP extraction, checksums, and bounded reads for extension/preset downloads#3141
PascalThuet wants to merge 2 commits into
github:mainfrom
PascalThuet:split/extension-preset-downloads

Conversation

@PascalThuet

Copy link
Copy Markdown
Contributor

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

Add a shared _download_security module (read_response_limited,
is_https_or_localhost_http, size constants) and route the GitHub release
and Azure DevOps token network reads through bounded reads so an oversized
response can't exhaust memory.

Add a strict_redirects mode to authentication.open_url: the redirect handler
now rejects any redirect whose target isn't HTTPS (or HTTP to localhost),
composing with the existing per-hop redirect_validator and auth-stripping.
The Azure DevOps token POST is routed through that handler so a 307/308
cannot forward the client_secret body to a non-HTTPS host.
…on/preset downloads

Extend _download_security with safe_extract_zip (path + symlink + per-member
and total size + entry-count bounds), read_zip_member_limited, and
verify_sha256, and adopt them across the extension and preset install paths:

- install_from_zip extracts via safe_extract_zip (zip-slip + symlink + zip-bomb
  defense) instead of the previous path-only containment check.
- catalog JSON fetches and package downloads use bounded reads; the inline
  manifest pre-read in extension update uses read_zip_member_limited.
- downloaded packages are verified against their catalog sha256 when present.
- CatalogStackBase and the preset catalog validator reject hostless URLs
  before the scheme check.

The command-registration path-traversal guard the original change carried is
already on main (github#3088) and is intentionally omitted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant