Skip to content

fix(oauth): compare redirect URI paths in encoded form, reject any '#' - #296

Open
dpage wants to merge 1 commit into
mainfrom
security/oauth-redirect-escaped-path
Open

dpage wants to merge 1 commit into
mainfrom
security/oauth-redirect-escaped-path

Conversation

@dpage

@dpage dpage commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Follow-up hardening to the unreleased OAuth server (#287), mirroring pgEdge/pgedge-mcp-bridge@05bb3e20.

  • redirectURIAllowed compared the decoded url.Path, so a percent-encoded variant of an allowed path (https://claude.ai/api%2Fmcp/auth_callback, http://127.0.0.1:53211/call%62ack) was accepted, despite the function documenting a byte-for-byte match. It now compares EscapedPath().
  • A trailing empty fragment (.../auth_callback#) was accepted because url.Parse reports it as no fragment; RFC 6749 section 3.1.2 forbids fragments, so any URI containing # is now rejected.

The userinfo bypass the bridge fixed (http://localhost:@attacker.example/callback) did not apply here, as we already took the host from the parsed URI and rejected userinfo; a test case now pins that down.

Server and client Go suites pass; the live-database tests were skipped (no connection string set), and this touches no database code.

Summary by CodeRabbit

  • Bug Fixes
    • Redirect URI validation now rejects fragments, including empty fragments, and malformed localhost authorities.
    • Redirect paths with percent-encoded characters or separators are matched consistently, preventing encoded paths from being treated as their decoded equivalents.

redirectURIAllowed compared the decoded url.Path, so a percent-encoded
variant of an allowed path, such as https://claude.ai/api%2Fmcp/auth_callback
or http://127.0.0.1:53211/call%62ack, was accepted despite the function
documenting a byte-for-byte match. Compare EscapedPath() instead.

It also accepted a trailing empty fragment ("...callback#"), because
url.Parse reports that as no fragment at all; RFC 6749 section 3.1.2
forbids fragments in redirect URIs, so reject any URI containing '#'.

Both mirror the hardening in pgEdge/pgedge-mcp-bridge@05bb3e20. The
userinfo bypass that commit fixed does not apply here, since the host
was already taken from the parsed URI and userinfo rejected; a test
case now pins that down too.
@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: pgEdge/pgedge-postgres-mcp/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 74aeb48b-103a-4218-af3f-83890ad7f7c5
📥 Commits

Reviewing files that changed from the base of the PR and between d451081 and c59f2ef.

📒 Files selected for processing (2)
  • internal/oauth/redirect.go
  • internal/oauth/redirect_test.go

Included review availability: This review used your included allowance. 1 included review remains after this review. Your included PR review attempts over the past 7 days set your current allowance at 2 reviews per hour.

Walkthrough

The redirect URI validator rejects URIs containing # and compares percent-encoded paths for exact-host and loopback matches. Tests add rejection cases for malformed localhost authorities, encoded paths, and fragments.

Changes

OAuth Redirect URI Validation

Layer / File(s) Summary
Redirect URI matching and validation
internal/oauth/redirect.go, internal/oauth/redirect_test.go
The validator rejects fragment markers and compares escaped paths for exact-host and loopback matches. Tests cover malformed localhost authorities, encoded path characters, and callback URLs with fragments.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to c59f2

Redirect validation now distinguishes percent-encoded path spellings and rejects fragment markers as intended. No merge-blocking risk is established; the change is ready for normal checks.

Pre-merge checks · Passed 4 · Failed 1

Failed Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks (4 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes both main changes: encoded redirect URI path comparison and rejection of all fragments.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity

Metric Results
Complexity 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@AntTheLimey AntTheLimey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approving. The new cases fail on main (api%2Fmcp, call%62ack,
trailing #) and pass here. I probed 62 URIs against 10 allowed
entries. 14 went from accepted to rejected and none the other way.
The probe included %23, dot segments, backslash and userinfo host
tricks, punycode, encoded hosts, IPv6 loopback forms and control
characters. Go's EscapedPath() re-encoding fallback is not a bypass,
because handleAuthorize rebuilds the redirect with u.String(), so
every accepted URI lands on an allowed entry's exact path. make test,
make lint, -race, gofmt and go vet are clean. internal/oauth
coverage is unchanged at 89.6%. No changelog entry is needed while
OAuth sits under Unreleased. This merges cleanly with #293.

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.

2 participants