fix(presets): raise PresetValidationError, not raw ValueError, on malformed catalog URL#3576
Open
chuenchen309 wants to merge 1 commit into
Open
fix(presets): raise PresetValidationError, not raw ValueError, on malformed catalog URL#3576chuenchen309 wants to merge 1 commit into
chuenchen309 wants to merge 1 commit into
Conversation
…formed catalog URL `PresetCatalog._validate_catalog_url` called `urlparse(url).hostname` without guarding it. For a malformed authority such as an unterminated IPv6 bracket (`https://[::1`), `urlparse(...).hostname` raises `ValueError: Invalid IPv6 URL`, which escapes the method. Its docstring promises `PresetValidationError`, and its callers (`preset catalog add`, `preset catalog list` reading the `SPECKIT_PRESET_CATALOG_URL` env var / `.specify/preset-catalogs.yml`) only catch `PresetValidationError` -- so a malformed URL crashes the CLI with a traceback instead of a clean error message. The shared `CatalogStackBase` (github#3435), `workflows` (github#3484), `bundler` (github#3433) and `IntegrationCatalog` copies already wrap this in `try/except ValueError`; the preset validator was the remaining un-updated twin. Mirror the shared implementation: wrap `urlparse` + `.hostname`, re-raise as `PresetValidationError("Catalog URL is malformed: ...")`, and read the local `hostname` in the host check. Add a regression test mirroring `IntegrationCatalog`'s `test_malformed_url_rejected_cleanly`; it is red before the fix (raw `ValueError`) and green after. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
PresetCatalog._validate_catalog_urlleaks a rawValueErroron a malformed catalog URL, violating its documentedPresetValidationErrorcontract and crashing the CLI with a traceback.urlparse("https://[::1").hostnameraisesValueError: Invalid IPv6 URL(unterminated bracket). The preset validator accesses.hostnamewithout guarding it, so the exception escapes. Callers only catchPresetValidationError:specify preset catalog add <URL>validates the user'surlargument insideexcept PresetValidationError.specify preset catalog listvalidates URLs fromSPECKIT_PRESET_CATALOG_URLand.specify/preset-catalogs.yml, again catching onlyPresetValidationError.So a malformed URL from any of these sources crashes with a traceback instead of a clean error message.
This is the last un-updated copy of a pattern the project has already hardened elsewhere: the shared
CatalogStackBase(#3435),workflows(#3484),bundler(#3433), andIntegrationCatalogall wrap this intry/except ValueErrorand re-raise their domain error. This PR mirrors the sharedCatalogStackBaseimplementation exactly — wrapurlparse+.hostname, re-raise asPresetValidationError("Catalog URL is malformed: ..."), and read the localhostnamein the host check.Before:
After: raises
PresetValidationError: Catalog URL is malformed: https://[::1.Testing
uv run specify --helpuv sync && uv run pytestAdded
test_validate_catalog_url_malformed_rejectedtotests/test_presets.py, mirroringIntegrationCatalog's existingtest_malformed_url_rejected_cleanly. It is red onmain(rawValueError) and green with the fix.tests/test_presets.pypasses (365), preset-scoped suite passes (376), anduvx ruff check src/is clean. The valid / host-less / non-HTTPS control cases are unchanged.AI Disclosure
Correcting this disclosure — the original wording credited the human operator with work an AI actually did, which understates the extent and would lead you to apply less scrutiny than you should. The accurate version:
This contribution is fully AI-authored and autonomous (Claude Code, acting on this account). An AI found the leak, reproduced it against
main, wrote the failing regression test first, mirrored the sharedCatalogStackBasefix, ran the preset test suites andruff check, reviewed the diff, and wrote this description. The human operator of this account did not hand-review the diff or personally run the tests.What that means for your review, stated plainly against your "what we're looking for" list:
mainbefore any fix, regression test red→green (mirroringIntegrationCatalog'stest_malformed_url_rejected_cleanly),test_presets.py365 passed, preset-scoped 376 passed,uvx ruff check src/clean. You can re-run all of it from the diff.