Restrict the loopback redirect_uri exception to the port (RFC 8252 §7.3) - #1878
Merged
nbulaj merged 1 commit intoJul 27, 2026
Merged
Conversation
RFC 8252 §7.3 lets only the port of a loopback redirect URI vary. The port was blanked with URI#port= before a string comparison, but on Ruby >= 4.0 URI#port= also clears the userinfo, so http://attacker@127.0.0.1/cb matched a registered http://127.0.0.1/cb. Compare the URIs component by component instead, keeping the port out of the comparison. The destination host is always the loopback interface, so this was not a cross-origin open redirect; non-loopback hosts take the exact-match branch and were never affected.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR tightens Doorkeeper’s RFC 8252 §7.3 “loopback redirect URI port variance” handling by avoiding URI#port= mutation (which can drop userinfo on Ruby >= 4.0) and instead comparing URI components while ignoring only the port. This prevents userinfo-based mismatches (e.g. attacker@127.0.0.1) from being accepted as valid loopback redirects.
Changes:
- Replace the previous “nil out port then compare
to_s” approach with an explicit component-by-component comparison that excludes the port. - Add regression specs ensuring
userinfois not ignored for loopback redirect URIs while preserving the port-only variance behavior. - Document the security fix in
CHANGELOG.md.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| spec/lib/oauth/helpers/uri_checker_spec.rb | Adds regression coverage for loopback redirect URI matching when userinfo differs, and confirms port-only variance still works. |
| lib/doorkeeper/oauth/helpers/uri_checker.rb | Implements loopback_match? to compare all URI components except port, preventing userinfo from being unintentionally ignored. |
| CHANGELOG.md | Records the loopback redirect URI userinfo/port matching fix and its Ruby >= 4.0 motivation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
nbulaj
reviewed
Jul 27, 2026
55728
force-pushed
the
fix/loopback-redirect-uri-userinfo
branch
from
July 27, 2026 10:28
bccd096 to
4678fcf
Compare
55728
force-pushed
the
fix/loopback-redirect-uri-userinfo
branch
from
July 27, 2026 10:30
4678fcf to
c8ab38e
Compare
Contributor
Author
|
Thank you 😊 |
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.
Problem
RFC 8252 §7.3 lets only the port of a loopback redirect URI vary at runtime. The check blanked the port with
URI#port=and compared the reassembled strings, but on Ruby >= 4.0URI#port=also clears the userinfo. Sohttp://attacker@127.0.0.1/cbmatched a registeredhttp://127.0.0.1/cb.Scope: the destination host is always the loopback interface (127.0.0.1 / ::1), so this is not a cross-origin open redirect. Non-loopback hosts take the exact-match branch and were never affected. Ruby-version dependent (surfaced by the
URI#port=behavior change in Ruby 4.0).Fix
Compare the two URIs component by component (scheme, userinfo, host, path, query, fragment), leaving the port out. This keeps the port-only exception while no longer ignoring userinfo differences, and preserves the previous property of not letting
URI#==normalize an empty path to"/".