Skip to content

fix(presets): re-validate catalog URL after redirects (HTTPS parity/security)#3523

Open
jawwad-ali wants to merge 2 commits into
github:mainfrom
jawwad-ali:fix/presets-catalog-redirect-revalidate
Open

fix(presets): re-validate catalog URL after redirects (HTTPS parity/security)#3523
jawwad-ali wants to merge 2 commits into
github:mainfrom
jawwad-ali:fix/presets-catalog-redirect-revalidate

Conversation

@jawwad-ali

Copy link
Copy Markdown
Contributor

What

PresetCatalog._fetch_single_catalog fetched the catalog JSON and trusted it without re-validating the post-redirect URL:

with self._open_url(entry.url, timeout=10) as response:
    catalog_data = json.loads(response.read())   # no response.geturl() check

_open_url follows redirects (stripping auth only on an HTTPS→HTTP downgrade), so an https:// catalog entry.url that 30x-redirects to http://attacker/catalog.json is still fetched and trusted. Because the catalog payload supplies each preset's download_url and sha256, an attacker-controlled redirected payload defeats verify_archive_sha256 and can drive installation of an arbitrary archive.

Fix

Re-validate response.geturl() after the redirect via the existing _validate_catalog_url, exactly mirroring the other catalog systems:

  • integrations/catalog.py (final_url = resp.geturl(); if final_url != entry.url: self._validate_catalog_url(final_url))
  • workflows/catalog.py, bundler/services/adapters.py
  • and presets/_commands.py itself, which already does redirect re-validation on its --from download path.

This was the one preset catalog-fetch site missing the guard — clearly an oversight, not a deliberate omission.

Test

test_fetch_single_catalog_revalidates_redirected_url: an HTTPS URL whose response.geturl() reports http:// is rejected with PresetValidationError (fails before: no revalidation). Four pre-existing fetch-test mocks were completed to report geturl() like a real urllib response.

uv run pytest tests/test_presets.py   # 339 passed
uvx ruff check  # clean

🤖 Written with the assistance of Claude Code (AI). Bug self-found; fix/tests verified locally (fail-before / pass-after).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds post-redirect URL validation for preset catalog fetching.

Changes:

  • Validates the final catalog URL before reading its payload.
  • Adds redirect regression coverage and updates response mocks.
Show a summary per file
File Description
src/specify_cli/presets/__init__.py Adds final URL validation.
tests/test_presets.py Tests insecure redirects and updates mocks.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Medium

Comment thread src/specify_cli/presets/__init__.py
Comment thread src/specify_cli/presets/__init__.py

@mnriem mnriem left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please address Copilot feedback

jawwad-ali and others added 2 commits July 17, 2026 15:42
PresetCatalog._fetch_single_catalog opened the catalog URL and trusted the
payload without re-validating response.geturl() after redirects. _open_url
follows redirects (stripping auth only on an HTTPS->HTTP downgrade), so an
https:// catalog entry that 30x-redirects to http://attacker/... was still
fetched and trusted. The catalog payload supplies each preset's download_url +
sha256, so a redirected payload can drive install of an arbitrary archive that
passes verify_archive_sha256.

Add the post-redirect geturl() re-validation via _validate_catalog_url,
mirroring integrations/catalog.py, workflows/catalog.py, and bundler adapters —
and presets/_commands.py, which already does this on its --from download path.
This is the lone preset catalog-fetch site missing the guard.

Test: an HTTPS URL whose response.geturl() reports http:// is rejected
(PresetValidationError). Completed four existing fetch-test mocks that predated
this behavior to report geturl() like a real urllib response.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…talog path

Two follow-ups to the catalog redirect hardening:

1. Validate every redirect hop, not just the terminal URL. A final-geturl-only
   check passes an https -> http -> attacker-controlled-https chain: the insecure
   intermediate hop lets a network attacker rewrite the next redirect. _open_url
   now forwards a redirect_validator to open_url (called before each hop), and
   _fetch_single_catalog passes _validate_catalog_url through it while retaining
   the final geturl() check — mirroring bundler/services/adapters.py.

2. The legacy public fetch_catalog() single-catalog path parsed response.read()
   with no redirect check at all. Give it the same redirect_validator + final
   geturl() validation.

Tests: a stubbed intermediate http hop is rejected (redirect_validator), and the
legacy fetch_catalog() rejects an HTTPS->http redirected payload (fail before:
no raise). Existing fetch-test mocks updated to accept the redirect_validator
kwarg and report geturl() like a real response. Full test_presets.py (365) green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jawwad-ali
jawwad-ali force-pushed the fix/presets-catalog-redirect-revalidate branch from 80464fb to 69ff566 Compare July 17, 2026 10:48
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.

3 participants