feat: add RFC 8252 loopback port flexibility for native apps#145
Merged
mattzcarey merged 6 commits intocloudflare:mainfrom Feb 26, 2026
Merged
Conversation
Per RFC 8252 Section 7.3, authorization servers MUST allow any port for loopback IP redirect URIs (127.0.0.0/8 and ::1). Native apps like VS Code, Cursor, and CLI tools obtain ephemeral ports from the OS at runtime. This change allows loopback redirect URIs to match on scheme, host, and path while ignoring the port. Non-loopback URIs still require exact match. Fixes cloudflare#35
commit: |
🦋 Changeset detectedLatest commit: 9a57612 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Add 22 tests covering all RFC 8252 Section 7.3 loopback port flexibility scenarios including IPv4/IPv6 loopback, port flexibility, path/scheme/host rejection, localhost exclusion, token exchange, completeAuthorization, full end-to-end flows, and multiple registered URI matching. Also include query string comparison in loopback URI validation per Codex review feedback - RFC 8252 only exempts port, not query.
deathbyknowledge
approved these changes
Feb 26, 2026
Merged
mattzcarey
pushed a commit
that referenced
this pull request
Feb 26, 2026
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @cloudflare/workers-oauth-provider@0.2.4 ### Patch Changes - [#136](#136) [`a8c5936`](a8c5936) Thanks [@mattzcarey](https://github.com/mattzcarey)! - Add `/.well-known/oauth-protected-resource` endpoint (RFC 9728) for OAuth 2.0 Protected Resource Metadata discovery, as required by the MCP authorization specification. The endpoint is always served with sensible defaults (request origin as resource and authorization server), and can be customized via the new `resourceMetadata` option. - [#151](#151) [`dbb150e`](dbb150e) Thanks [@mattzcarey](https://github.com/mattzcarey)! - Add `allowPlainPKCE` option to enforce S256-only PKCE as recommended by OAuth 2.1. When set to false, the plain PKCE method is rejected and only S256 is accepted. Defaults to true for backward compatibility. - [#140](#140) [`65d5cfa`](65d5cfa) Thanks [@mattzcarey](https://github.com/mattzcarey)! - Fix apiHandler route matching when set to '/' to use exact match instead of prefix match, preventing it from matching all routes and breaking OAuth endpoints - [#150](#150) [`734738c`](734738c) Thanks [@mattzcarey](https://github.com/mattzcarey)! - Fix TypeScript types by making OAuthProviderOptions generic over Env, eliminating the need for @ts-expect-error workarounds when using typed environments - [#145](#145) [`6ce5c10`](6ce5c10) Thanks [@mattzcarey](https://github.com/mattzcarey)! - Add RFC 8252 Section 7.3 compliance: allow any port for loopback redirect URIs (127.x.x.x, ::1) to support native apps that use ephemeral ports - [#143](#143) [`8909060`](8909060) Thanks [@mattzcarey](https://github.com/mattzcarey)! - Include `resource_metadata` URL in `WWW-Authenticate` headers on 401 responses per RFC 9728 §5.1, enabling clients to discover the protected resource metadata endpoint directly from authentication challenges. Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
5 tasks
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.
Summary
Background
Per RFC 8252 Section 7.3:
Native desktop applications like VS Code, Cursor, and CLI tools typically:
http://127.0.0.1:8080/callbackhttp://127.0.0.1:52431/callback)This change enables these native apps to work correctly by allowing any port for loopback redirect URIs.
Implementation
Added two helper functions:
isLoopbackUri()- Detects loopback addresses (127.0.0.0/8 and ::1)isValidRedirectUri()- Validates redirect URIs with RFC 8252 loopback supportReplaced all 3 occurrences of
redirectUris.includes(redirectUri)withisValidRedirectUri(redirectUri, clientInfo.redirectUris):Test Plan
Fixes #35