-
Notifications
You must be signed in to change notification settings - Fork 11
feat(auth): add explicit delegation with legacy compatibility #6155
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
Open
pandemicsyn
wants to merge
2
commits into
main
Choose a base branch
from
split/explicit-resource-delegation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { NextRequest } from 'next/server'; | ||
| import { createDelegatedResourceToken } from '@/lib/auth/resource-delegation'; | ||
| import { POST } from './route'; | ||
|
|
||
| jest.mock('@/lib/user/server', () => ({ | ||
| getUserFromAuth: jest.fn(async () => ({ user: { id: 'oauth/test-user' } })), | ||
| })); | ||
| jest.mock('@/lib/auth/resource-delegation', () => ({ | ||
| isDelegableResource: (value: string) => | ||
| ['api', 'gateway', 'attribution', 'html-deploy'].includes(value), | ||
| createDelegatedResourceToken: jest.fn(async () => ({ token: 'delegated', expiresAt: 'expiry' })), | ||
| })); | ||
|
|
||
| beforeEach(() => jest.clearAllMocks()); | ||
|
|
||
| it('rejects personal attribution issuance because the reader requires organization claims', async () => { | ||
| const response = await POST( | ||
| new NextRequest('https://example.test/api/auth/resource-token', { | ||
| method: 'POST', | ||
| headers: { origin: 'https://example.test' }, | ||
| body: JSON.stringify({ resource: 'attribution' }), | ||
| }) | ||
| ); | ||
| expect(response.status).toBe(403); | ||
| expect(createDelegatedResourceToken).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each(['api', 'gateway', 'html-deploy'])('retains personal %s negotiation', async resource => { | ||
| const response = await POST( | ||
| new NextRequest('https://example.test/api/auth/resource-token', { | ||
| method: 'POST', | ||
| headers: { origin: 'https://example.test' }, | ||
| body: JSON.stringify({ resource }), | ||
| }) | ||
| ); | ||
| expect(response.status).toBe(200); | ||
| expect(createDelegatedResourceToken).toHaveBeenCalledWith( | ||
| { id: 'oauth/test-user' }, | ||
| resource, | ||
| expect.objectContaining({ headers: expect.any(Headers) }) | ||
| ); | ||
| }); |
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,59 @@ | ||
| import type { NextRequest } from 'next/server'; | ||
| import { NextResponse } from 'next/server'; | ||
| import { getUserFromAuth } from '@/lib/user/server'; | ||
| import { | ||
| createDelegatedResourceToken, | ||
| isDelegableResource, | ||
| TypedResourceDelegationError, | ||
| } from '@/lib/auth/resource-delegation'; | ||
|
|
||
| function isSameOriginRequest(request: NextRequest): boolean { | ||
| const origin = request.headers.get('origin'); | ||
| return origin !== null && origin === request.nextUrl.origin; | ||
| } | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| let body: unknown; | ||
| try { | ||
| body = await request.json(); | ||
| } catch { | ||
| return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 }); | ||
| } | ||
| const resource = | ||
| body && typeof body === 'object' && 'resource' in body ? body.resource : undefined; | ||
| if (!isDelegableResource(resource)) { | ||
| return NextResponse.json({ error: 'Unsupported resource' }, { status: 400 }); | ||
| } | ||
| if (!request.headers.has('authorization') && !isSameOriginRequest(request)) { | ||
| return NextResponse.json({ error: 'Invalid request origin' }, { status: 403 }); | ||
| } | ||
| const { user, authFailedResponse, organizationId, tokenSource } = await getUserFromAuth({ | ||
| adminOnly: false, | ||
| }); | ||
| if (authFailedResponse) return authFailedResponse; | ||
| if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); | ||
| if (organizationId) { | ||
| return NextResponse.json( | ||
| { error: 'Organization credentials are not supported' }, | ||
| { status: 403 } | ||
| ); | ||
| } | ||
| if (resource === 'attribution') { | ||
| return NextResponse.json( | ||
| { error: 'Attribution requires an organization resource token' }, | ||
| { status: 403 } | ||
| ); | ||
| } | ||
| try { | ||
| const result = await createDelegatedResourceToken(user, resource, { | ||
| headers: request.headers, | ||
| tokenSource, | ||
| }); | ||
| return NextResponse.json({ token: result.token, expiresAt: result.expiresAt }); | ||
| } catch (error) { | ||
| if (error instanceof TypedResourceDelegationError) { | ||
| return NextResponse.json({ error: error.message }, { status: error.status }); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SUGGESTION: Same-origin guard is skipped by an empty
Authorizationheaderrequest.headers.has('authorization')istruefor an empty header value, so a request carryingAuthorization:bypassesisSameOriginRequest. Downstream,getUserFromAuth->resolveUserFromAuthreadsheadersList.get('Authorization')(an empty string is falsy) and falls back to session-cookie auth, so such a request is authenticated by cookie without the intended same-origin check. Browsers cannot set this header cross-site, so this is defense-in-depth rather than a live CSRF, but usingget('authorization')here would match the auth layer's semantics and close the gap.Reply with
@kilocode-bot fix itto have Kilo Code address this issue.