-
Notifications
You must be signed in to change notification settings - Fork 120
feat(auth): add Zod validation for OAuth callback endpoints #593
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
Harxhit
merged 4 commits into
Dev-Card:main
from
ramnnn2006:fix/oauth-callback-zod-validation
Jun 17, 2026
+230
−23
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1c4c0ee
feat(auth): add Zod validation for OAuth callback endpoints
ramnnn2006 243404a
fix(auth): address review feedback on OAuth callback validation
ramnnn2006 86f4348
fix(auth): clear oauth_state cookie on validation failure and add tra…
ramnnn2006 57595a5
fix(auth): clear oauth_state cookie on all failure paths and drop det…
ramnnn2006 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,203 @@ | ||
| import cookiePlugin from '@fastify/cookie'; | ||
| import jwtPlugin from '@fastify/jwt'; | ||
| import Fastify, { type FastifyInstance } from 'fastify'; | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
|
|
||
| import { authRoutes } from '../routes/auth.js'; | ||
|
|
||
| async function buildTestApp(): Promise<FastifyInstance> { | ||
| const app = Fastify({ logger: false }); | ||
|
|
||
| await app.register(cookiePlugin as any); | ||
| await app.register(jwtPlugin as any, { | ||
| secret: 'test-secret-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | ||
| cookie: { cookieName: 'access_Token', signed: false }, | ||
| }); | ||
|
|
||
| app.decorate('prisma', { | ||
| user: { findUnique: vi.fn(), create: vi.fn(), update: vi.fn() }, | ||
| userIdentity: { findUnique: vi.fn(), create: vi.fn() }, | ||
| refreshToken: { create: vi.fn() }, | ||
| } as any); | ||
|
|
||
| app.decorate('redis', { | ||
| set: vi.fn(), | ||
| getdel: vi.fn(), | ||
| } as any); | ||
|
|
||
| app.decorate('authenticate', async () => {}); | ||
|
|
||
| await app.register(authRoutes, { prefix: '/auth' }); | ||
| await app.ready(); | ||
| return app; | ||
| } | ||
|
|
||
| function cookieCleared(res: any): boolean { | ||
| const raw = res.headers['set-cookie'] as string | string[] | undefined; | ||
| const cookies = Array.isArray(raw) ? raw : raw ? [raw] : []; | ||
| return cookies.some((c) => c.startsWith('oauth_state=;') || c.includes('oauth_state=; ')); | ||
| } | ||
|
|
||
| describe('GET /auth/github/callback — Zod validation', () => { | ||
| let app: FastifyInstance; | ||
|
|
||
| beforeEach(async () => { | ||
| vi.clearAllMocks(); | ||
| app = await buildTestApp(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await app.close(); | ||
| }); | ||
|
|
||
| it('400 — missing code rejects with validation error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/github/callback?state=somestate', | ||
| headers: { Cookie: 'oauth_state=somestate' }, | ||
| }); | ||
|
Harxhit marked this conversation as resolved.
|
||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid callback parameters'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
|
|
||
| it('400 — empty code rejects with validation error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/github/callback?code=&state=somestate', | ||
| headers: { Cookie: 'oauth_state=somestate' }, | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid callback parameters'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
|
|
||
| it('400 — missing state rejects with validation error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/github/callback?code=validcode', | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid callback parameters'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
|
|
||
| it('400 — empty state rejects with validation error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/github/callback?code=validcode&state=', | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid callback parameters'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
|
|
||
| it('400 — valid code and state but no cookie rejects with state error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/github/callback?code=validcode&state=somestate', | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid or missing OAuth state — possible CSRF attack'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
|
|
||
| it('400 — valid code and state but mismatched cookie rejects with state error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/github/callback?code=validcode&state=somestate', | ||
| headers: { Cookie: 'oauth_state=differentstate' }, | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid or missing OAuth state — possible CSRF attack'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('GET /auth/google/callback — Zod validation', () => { | ||
| let app: FastifyInstance; | ||
|
|
||
| beforeEach(async () => { | ||
| vi.clearAllMocks(); | ||
| app = await buildTestApp(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await app.close(); | ||
| }); | ||
|
|
||
| it('400 — missing code rejects with validation error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/google/callback?state=somestate', | ||
| headers: { Cookie: 'oauth_state=somestate' }, | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid callback parameters'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
|
|
||
| it('400 — empty code rejects with validation error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/google/callback?code=&state=somestate', | ||
| headers: { Cookie: 'oauth_state=somestate' }, | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid callback parameters'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
|
|
||
| it('400 — missing state rejects with validation error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/google/callback?code=validcode', | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid callback parameters'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
|
|
||
| it('400 — empty state rejects with validation error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/google/callback?code=validcode&state=', | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid callback parameters'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
|
|
||
| it('400 — valid code and state but no cookie rejects with state error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/google/callback?code=validcode&state=somestate', | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid or missing OAuth state — possible CSRF attack'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
|
|
||
| it('400 — valid code and state but mismatched cookie rejects with state error', async () => { | ||
| const res = await app.inject({ | ||
| method: 'GET', | ||
| url: '/auth/google/callback?code=validcode&state=somestate', | ||
| headers: { Cookie: 'oauth_state=differentstate' }, | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid or missing OAuth state — possible CSRF attack'); | ||
| expect(cookieCleared(res)).toBe(true); | ||
| }); | ||
| }); | ||
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
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.
Rejection coverage is strong and nicely symmetric across both providers (missing/empty
code, missing/emptystate, no-cookie, mismatched-cookie, field-leveldetails).Gap: there's no happy-path assertion — valid
code+ matchingstateproceeding past validation (e.g. mockingfetchto assertclearCookiefires / token exchange is attempted). Without it the suite can't catch a regression that wrongly rejects valid callbacks.buildTestAppalready wires the prisma/redis mocks, so it's set up for this. Optional for this PR's scope.