Skip to content

auth: reject encoded slashes/dots in redirect paths (CWE-601) - #107

Merged
aterga merged 3 commits into
mainfrom
claude/redirect-encoded-slash-fix
Jul 30, 2026
Merged

aterga merged 3 commits into
mainfrom
claude/redirect-encoded-slash-fix

Conversation

@aterga

@aterga aterga commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes a MEDIUM authorization-code-phishing bug (CWE-601): the hosted-redirect path pin could be bypassed with percent-encoded slashes.

redirect_uri_permitted pins a hosted redirect_uri to the vendor's callback path prefix, relying on url::Url to collapse ./.. traversal before the check. But the WHATWG parser only collapses a segment that is literally ./.. (raw or %2e-encoded); it does not decode %2f. So this payload:

https://chatgpt.com/connector/oauth/%2e%2e%2f%2e%2e%2fg%2fattacker

parses as one opaque segment under /connector/oauth/, passes path_within_prefix, is idempotent through the canonical re-parse, and was permitted. build_redirect appends ?code=… to the raw stored string, so the encoding reaches the browser; a vendor server/CDN that later decodes %2f (and resolves ..) delivers the code to /g/attacker, an attacker-controlled script-capable path on the trusted origin. With unauthenticated DCR + attacker-chosen PKCE, that captures a victim's code and mints a token as the victim.

Verified against the real url crate before fixing (probe test, since removed):

redirect_uri url.path() permitted (before)
…/connector/oauth/%2e%2e%2f%2e%2e%2fg%2fattacker /connector/oauth/%2e%2e%2f%2e%2e%2fg%2fattacker true ⚠️
…/connector/oauth/../../g/evil (real slashes) /g/evil false ✓

Changes

  • Add path_has_encoded_separator(path) — rejects a path carrying %2f/%2F, %5c/%5C, or %2e/%2E. No legitimate vendor callback path uses these encodings.
  • redirect_uri_permitted: reject encoded separators in the hosted branch before the prefix match, so the pin holds under every equivalent encoding. Replaced the now-incorrect comment that claimed WHATWG normalization alone was sufficient.
  • is_wellformed_hosted_redirect: reject them too, so such a payload is classified invalid_request rather than routed to the "request access" page (mirrors the MCP05 query/fragment handling).
  • Loopback is intentionally unaffected: a loopback code resolves on the consenter's own machine, not a shared trusted origin, so the confused-deputy vector doesn't apply.
  • Regression tests for the encoded-slash traversal the existing real-slash ../ tests missed (upper/lower %2f, backslash, within-prefix, and the ineligibility classification).

Testing

  • cargo test --locked (150 + 8 + 1 pass, 0 failed; new encoded-slash assertions included)
  • Confirmed the payload flips from permitted=true to permitted=false with the fix; real-slash ../ behavior and all legitimate vendor paths unchanged.

Checklist

  • Docs (comments) updated: the stale "no separate dot-segment guard needed" comment is corrected.
  • No secrets, credentials, or internal-only information are included.

🤖 Generated with Claude Code


Generated by Claude Code

The hosted-redirect path pin (redirect_uri_permitted) relied on url::Url
to collapse `.`/`..` traversal before the pinned-prefix check. But the
WHATWG URL parser only collapses a segment that is literally `.`/`..`
(raw or `%2e`-encoded); it does NOT decode `%2f`. So a redirect_uri like

    https://chatgpt.com/connector/oauth/%2e%2e%2f%2e%2e%2fg%2fattacker

parses as ONE opaque segment under the `/connector/oauth/` prefix, passes
the prefix check, is idempotent through the canonical re-parse, and so was
permitted. build_redirect then appends `?code=...` to the raw stored
string, so the encoding survives to the browser; a vendor server/CDN that
later decodes `%2f` (and resolves the `..`) delivers the code to
`/g/attacker`, an attacker-controlled script-capable path on the trusted
origin. Combined with unauthenticated DCR and attacker-chosen PKCE, that
captures a victim's authorization code (authorization-code phishing).

Fix: reject any hosted redirect whose path carries a percent-encoded
slash, backslash, or dot (`%2f`/`%2F`, `%5c`/`%5C`, `%2e`/`%2E`, via
path_has_encoded_separator) before the prefix match, so the pin holds
under every equivalent encoding. No legitimate vendor callback path uses
these encodings. is_wellformed_hosted_redirect rejects them too, so such a
payload is classified invalid_request rather than offered the
"request access" page (mirroring the MCP05 query/fragment handling).
Loopback is unaffected: its code resolves on the consenter's own machine,
not a shared trusted origin.

Adds regression tests for the encoded-slash traversal the existing
real-slash `../` tests missed, including the within-prefix and backslash
variants and the ineligibility classification.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym
@aterga
aterga requested a review from Copilot July 29, 2026 20:32
@aterga
aterga marked this pull request as ready for review July 29, 2026 20:32
@aterga
aterga requested a review from a team July 29, 2026 20:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens hosted OAuth redirect URI validation to prevent path-prefix (“path pin”) bypasses using percent-encoded separators/dot characters, closing an open-redirect/auth-code-phishing vector (CWE-601) on trusted vendor origins.

Changes:

  • Add a helper to detect encoded path metacharacters (%2f, %5c, %2e) that could be decoded downstream into separators/dot-segments.
  • Reject hosted redirects containing these encodings in both redirect_uri_permitted (enforcement) and is_wellformed_hosted_redirect (classification as invalid_request).
  • Add regression tests covering encoded-slash/backslash traversal and related edge cases.

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

Comment thread src/auth.rs Resolved
path_has_encoded_separator lowercased the whole caller-controlled path
(allocating a String per call) on the unauthenticated front-channel. Scan
the bytes for `%` + the two hex digits case-insensitively instead: no
allocation, short-circuits on the first match, same rejection set
(`%2f`/`%5c`/`%2e`, either case). Behavior is unchanged; the redirect
allow-list tests still pass.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym
Copilot AI review requested due to automatic review settings July 29, 2026 21:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/auth.rs:602

  • is_wellformed_hosted_redirect is used to decide whether to show the “request access” page vs classify the request as invalid_request, but it currently treats https://…:<non-443>/… as well-formed. redirect_uri_permitted will still reject non-default ports, so this misclassifies an ineligible redirect as an approval gap (and offers “request access” for something that can never be approved under the current policy). Consider requiring the HTTPS default port here too (mirroring the allow-list shape rules).
                && url.fragment().is_none()
                // An encoded slash/backslash/dot in the path is likewise ineligible
                // (CWE-601 pin bypass), not a real client awaiting approval, so it is
                // classified `invalid_request` rather than offered "request access".
                && !path_has_encoded_separator(url.path())

…ors (review)

Per review feedback, replace the enumerated dangerous-sequence check
(`%2f`/`%5c`/`%2e`) with a wholesale rejection of any percent-encoding in a
hosted redirect path. A legitimate vendor callback path is plain ASCII, so
refusing every `%` holds the CWE-601 pin under any encoding without having to
reason about which sequences a given downstream server/CDN happens to decode.

path_has_encoded_separator -> path_has_percent_encoding (path.contains('%')),
which is also allocation-free (superseding the earlier byte-scan). Adds tests
that non-separator escapes (`%20`, `%41`) are refused too; all redirect
allow-list tests still pass.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym
Copilot AI review requested due to automatic review settings July 30, 2026 11:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (4)

src/auth.rs:594

  • This new “ineligible ⇒ invalid_request” classification for percent-encoded paths is good, but is_wellformed_hosted_redirect still treats non-default ports (e.g. https://…:444/...) as “well-formed”, which will route those requests to the “request access” page even though ports are explicitly ineligible elsewhere (redirect_uri_permitted / parse_redirect_prefix). Consider also rejecting non-default ports here via url.port().is_none() to keep the classification consistent.
                // Percent-encoding in the path is likewise ineligible (CWE-601 pin
                // bypass), not a real client awaiting approval, so it is classified
                // `invalid_request` rather than offered "request access".
                && !path_has_percent_encoding(url.path())

src/auth.rs:479

  • PR description says the fix adds a helper that rejects only specific encoded separators/dot segments (e.g. %2f, %5c, %2e), but the implementation here rejects any % in the hosted redirect path (including otherwise-harmless escapes like %20/%41, as the tests confirm). Please align the PR description (or the code) so reviewers/operators aren’t surprised by the stricter behavior.
/// Whether `path` contains ANY percent-encoding (a literal `%`). A legitimate hosted
/// vendor callback path is plain ASCII with no percent-encoding, so rather than
/// enumerate the dangerous sequences we reject percent-encoding wholesale: it is the
/// only way an encoded path separator or dot-segment can hide from the pinned-prefix
/// check, and refusing all of it means never having to reason about which sequences a

src/auth.rs:557

  • path_has_percent_encoding is currently evaluated after the extra canonical re-parse, which means percent-encoded paths (now always rejected) still pay for format!(...) + a second Url::parse. Since this runs on unauthenticated front-channel requests, consider moving the percent check before the canonical re-parse and reusing the extracted path in the canonical string to short-circuit earlier.
    // A vendor callback path is plain ASCII, so reject ANY percent-encoding in the
    // path first (CWE-601), which holds the pin under every encoding without having to
    // enumerate the dangerous sequences (see [`path_has_percent_encoding`]).
    if path_has_percent_encoding(path) {
        return false;

src/auth.rs:480

  • With the new policy of rejecting any % in hosted redirect paths, OAUTH_ALLOWED_REDIRECT_PREFIXES entries containing percent-encoding will still parse and be cached (no warning) but can never match at runtime (all such redirects are now refused). Consider also rejecting/warning on percent-encoded paths in parse_redirect_prefix so operators don’t end up with allow-list entries that are silently ineffective.

This issue also appears in the following locations of the same file:

  • line 475
  • line 553
  • line 591
/// Whether `path` contains ANY percent-encoding (a literal `%`). A legitimate hosted
/// vendor callback path is plain ASCII with no percent-encoding, so rather than
/// enumerate the dangerous sequences we reject percent-encoding wholesale: it is the
/// only way an encoded path separator or dot-segment can hide from the pinned-prefix
/// check, and refusing all of it means never having to reason about which sequences a
/// given downstream server/CDN happens to decode.

@aterga
aterga merged commit aca7c65 into main Jul 30, 2026
7 checks passed
aterga pushed a commit that referenced this pull request Aug 3, 2026
Post-audit merges (#87-#115) that changed user-visible or documented
behavior, folded into the README:

- II's consent screen requires an explicit access-level choice (Questions
  only vs Actions & questions); it is not a read-only-by-default opt-out.
- Name the connect binding cookie correctly: mcp_connect, not sid, with
  Secure conditional on an https PUBLIC_URL.
- Document the official pages (/privacy-policy, /support, /terms) and the
  OpenAI domain-verification endpoint with its env-gated 404 behavior
  (#109, #112, #115).
- /version now reports the served II pairings via the instances array (#94).
- CWE-674: note the post-parse depth bounds on interface alias chains (#87)
  and the type-less reply decode/render path (#108).
- Companion-control note: percent-encoded slashes/dots in redirect paths are
  rejected (#107).
- Deploy: describe the reference native deployment pipeline (staging tracks
  main, release-* tags ship production with the binary attached) (#81, #93).

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TGhwpdTbUqQbR5AiiN8Ljw
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.

4 participants