Skip to content

fix(security): malformed URLs get a named refusal, not a raw codec error or a 500 (abilityai/trinity#3322) - #3906

Merged
obasilakis merged 2 commits into
devfrom
feature/3322-url-validation-named-refusals
Oct 23, 2026
Merged

obasilakis merged 2 commits into
devfrom
feature/3322-url-validation-named-refusals

Conversation

@obasilakis

Copy link
Copy Markdown
Contributor

Summary

  • Unencodable host (https://a..example.com/, a 64-char label): getaddrinfo's own idna encode raises UnicodeError, which the except socket.gaierror arms missed. _validate_public_https_url now raises PublicUrlRefusal("invalid", "<label> must have a valid hostname"), so the template-registry 400 carries our text and A2A callers get a real reason code. The trust-check resolver (return None site) treats it the same as gaierror.
  • Unbalanced bracket (https://[::1/x, x[/y, o]/r): reject_embedded_credentials no longer leaks urlparse's bare ValueError. It falls back to the shared userinfo regex for every shape (still refuses https://tok@[::1/x), and otherwise returns so the route's validate_skills_library_url answers 400 "Invalid URL format". Its shorthand branch now refuses an owner/repo urlparse cannot parse; balanced shorthands like [::1]/x are unchanged (the A8/A12 seam tests still hold).

Both inputs already failed closed; this only turns kind-less errors and a 500 into named 400s.

Changes

  • src/backend/utils/url_validation.py
  • tests/unit/test_ec_url_validation_edges.py: C12 and D4 xfail markers removed (C12 also asserts kind invalid and no codec text); the D4 precondition that pinned the bare ValueError is replaced by two tests: the validator refuses the malformed URL, and a userinfo is still refused when urlparse cannot parse.

Test Plan

  • cd tests && pytest unit/test_ec_url_validation_edges.py — the 9 C12/D4 cases now pass; no new failures
  • pytest unit -k "url or skill or template or a2a or registry" — 58 failures vs 67 on dev locally, none new. The remaining ones are IPv6-mapped / special-purpose range verdicts that differ on this machine's Python 3.11 (ipaddress); CI runs 3.13
  • CI green on 3.13

Fixes #3322

🤖 Generated with Claude Code

obasilakis and others added 2 commits October 22, 2026 17:20
…ror or a 500 (#3322)

A host the idna codec refuses (an empty or 64+ char label) passed
canonical_host's ASCII fallback, and getaddrinfo then raised UnicodeError,
which the gaierror arm missed: the template-registry route echoed the
codec's text and A2A callers had no reason code. Both resolve sites now
map it to PublicUrlRefusal("invalid") / a refusal.

An unbalanced bracket (https://[::1/x, x[/y) made urlparse raise a plain
ValueError inside reject_embedded_credentials, and the skills-source
routes catch only EmbeddedCredentialError, so a typo was a 500. It now
falls back to the userinfo regex (still refusing a token) and otherwise
leaves the URL to validate_skills_library_url, whose shorthand branch
now refuses an unparseable owner/repo with "Invalid URL format".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@obasilakis

Copy link
Copy Markdown
Contributor Author

/review + /validate-pr

Verdict: APPROVE once CI is green. No critical findings. Lane C (utils/url_validation.py), so the ui label is added for frontend-e2e.

Review

  • Fail-closed holds: 35 inputs probed through the skills routes, legacy adoption, A2A (with and without trusted entries) and template registry, on merge-base and head. Nothing refused on dev is accepted; every change turns a 500 or raw exception into a named 400 or refusal. Userinfo is still refused when urlparse cannot parse.
  • Mutation: reverting the source turns the 9 C12/D4 tests red; removing only the shorthand guard turns 2 red.

Fixed after review

  • The trusted-A2A UnicodeError arm now has a test (endpoint_not_https), red without the arm.
  • reject_embedded_credentials documents that it returns on unparseable input and callers must run their URL validator next.

Follow-ups filed

Local test note: ~21 IPv6-mapped / range cases fail on Python 3.11 locally on dev too; CI runs 3.13.

@dolho dolho 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.

/review: approve

Scope: clean. Both #3322 inputs now get named 400s instead of a codec error or a 500.

Execution coverage

changed symbol executed by live consumer verdict
_validate_public_https_url except UnicodeError test_C12_an_unencodable_ascii_host_is_a_named_refusal (asserts kind invalid, no codec text) template-registry and A2A validators ✅
_validate_trusted_a2a_url (gaierror, UnicodeError) test_C12_the_trusted_a2a_path_refuses_an_unencodable_host_by_name validate_a2a_endpoint_url ✅
reject_embedded_credentials fallback D4 parametrized + test_D4_a_userinfo_is_still_refused_when_urlparse_cannot_parse routers/skills.py create / update / _validated_source_url, skill_service legacy adoption ✅
validate_skills_library_url shorthand bracket check test_D4_the_validator_then_refuses_the_malformed_url same routes ✅

Fix mutation: the C12/D4 cases were xfail(strict=True) against the old code, so reverting the fix turns them red.

Checked

  • I traced every caller of reject_embedded_credentials. All three router call sites run validate_skills_library_url right after it and map ValueError to a 400. skill_service runs it inside except ValueError. So the new "return and let the validator refuse it" contract holds everywhere.
  • I ran the PR's functions directly. https://tok@[::1/x, x@[/y, //tok@[oops and tok:pw@[x/y all raise EmbeddedCredentialError. https://[::1/x, x[/y and o]/r fall through, and the validator then refuses them with "Invalid URL format". A balanced [::1]/x is unchanged.
  • The new refusal uses label, which matches the sibling invalid refusals.

Informational (non-blocking)

  • validate_skills_library_url("//[oops") still returns //[oops as an "owner/repo" shorthand, because urlparse("https:////[oops") doesn't raise. This is not caused by this PR and the input still fails later. Mentioning it only because it is the same family of bug.

No blocking findings.

@obasilakis
obasilakis merged commit 6f7f349 into dev Oct 23, 2026
23 checks passed
@obasilakis
obasilakis deleted the feature/3322-url-validation-named-refusals branch October 23, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ui PR touches the frontend UI — triggers Playwright e2e tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants