-
Notifications
You must be signed in to change notification settings - Fork 264
chore(web): guard OAuth API routes against 307/308 redirects #1163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
brendan-kellam
merged 2 commits into
main
from
brendan/oauth-307-redirect-guard-SOU-945
Apr 30, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
4 changes: 2 additions & 2 deletions
4
packages/web/src/app/api/(server)/ee/.well-known/oauth-authorization-server/route.ts
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { describe, expect, test, vi } from 'vitest'; | ||
| import { NextRequest } from 'next/server'; | ||
| import { oauthApiHandler } from './apiHandler'; | ||
|
|
||
| vi.mock('@/lib/posthog', () => ({ | ||
| captureEvent: vi.fn(), | ||
| })); | ||
|
|
||
| const makeRequest = () => new NextRequest('http://localhost/api/ee/oauth/test', { method: 'POST' }); | ||
|
|
||
| describe('oauthApiHandler', () => { | ||
| test('passes through a 200 response unchanged', async () => { | ||
| const handler = oauthApiHandler(async (_req: NextRequest) => Response.json({ ok: true }, { status: 200 })); | ||
| const res = await handler(makeRequest()); | ||
| expect(res.status).toBe(200); | ||
| }); | ||
|
|
||
| test('passes through a 400 response unchanged', async () => { | ||
| const handler = oauthApiHandler(async (_req: NextRequest) => Response.json({ error: 'bad' }, { status: 400 })); | ||
| const res = await handler(makeRequest()); | ||
| expect(res.status).toBe(400); | ||
| }); | ||
|
|
||
| test('passes through a 302 redirect unchanged', async () => { | ||
| const handler = oauthApiHandler(async (_req: NextRequest) => | ||
| new Response(null, { status: 302, headers: { Location: '/elsewhere' } }) | ||
| ); | ||
| const res = await handler(makeRequest()); | ||
| expect(res.status).toBe(302); | ||
| }); | ||
|
|
||
| test('passes through a 303 redirect unchanged (the spec-recommended status)', async () => { | ||
| const handler = oauthApiHandler(async (_req: NextRequest) => | ||
| new Response(null, { status: 303, headers: { Location: '/elsewhere' } }) | ||
| ); | ||
| const res = await handler(makeRequest()); | ||
| expect(res.status).toBe(303); | ||
| }); | ||
|
|
||
| test('throws when the inner handler returns 307', async () => { | ||
| const handler = oauthApiHandler(async (_req: NextRequest) => | ||
| new Response(null, { status: 307, headers: { Location: '/elsewhere' } }) | ||
| ); | ||
| await expect(handler(makeRequest())).rejects.toThrow(/RFC 9700/); | ||
| }); | ||
|
|
||
| test('throws when the inner handler returns 308', async () => { | ||
| const handler = oauthApiHandler(async (_req: NextRequest) => | ||
| new Response(null, { status: 308, headers: { Location: '/elsewhere' } }) | ||
| ); | ||
| await expect(handler(makeRequest())).rejects.toThrow(/RFC 9700/); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { apiHandler } from '@/lib/apiHandler'; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| type AnyHandler = (...args: any[]) => Promise<Response> | Response; | ||
|
|
||
| /** | ||
| * Wraps `apiHandler` for routes that are part of the OAuth Authorization Server | ||
| * (`/api/ee/oauth/*`). | ||
| * | ||
| * Per RFC 9700 §4.12, the authorization server MUST avoid forwarding user | ||
| * credentials accidentally on redirect. 307 and 308 preserve the request | ||
| * method and body, so they MUST NOT be used for authorization-server | ||
| * redirects. This wrapper asserts that handlers never emit either status, | ||
| * giving us a runtime guarantee. | ||
| * | ||
| * @see https://datatracker.ietf.org/doc/html/rfc9700#section-4.12 | ||
| */ | ||
| export function oauthApiHandler<H extends AnyHandler>(handler: H): H { | ||
| const wrapped = apiHandler(async (...args: Parameters<H>) => { | ||
| const response = await handler(...args); | ||
| if (response.status === 307 || response.status === 308) { | ||
| throw new Error( | ||
| `OAuth authorization server emitted HTTP ${response.status} redirect; ` + | ||
| `per RFC 9700 §4.12 the authorization server MUST NOT use 307/308. ` + | ||
| `Use 303 (See Other) instead.` | ||
| ); | ||
| } | ||
| return response; | ||
| }); | ||
|
|
||
| return wrapped as H; | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.