|
| 1 | +import cookiePlugin from '@fastify/cookie'; |
| 2 | +import jwtPlugin from '@fastify/jwt'; |
| 3 | +import Fastify, { type FastifyInstance } from 'fastify'; |
| 4 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 5 | + |
| 6 | +import { authRoutes } from '../routes/auth.js'; |
| 7 | + |
| 8 | +async function buildTestApp(): Promise<FastifyInstance> { |
| 9 | + const app = Fastify({ logger: false }); |
| 10 | + |
| 11 | + await app.register(cookiePlugin as any); |
| 12 | + await app.register(jwtPlugin as any, { |
| 13 | + secret: 'test-secret-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 14 | + cookie: { cookieName: 'access_Token', signed: false }, |
| 15 | + }); |
| 16 | + |
| 17 | + app.decorate('prisma', { |
| 18 | + user: { findUnique: vi.fn(), create: vi.fn(), update: vi.fn() }, |
| 19 | + userIdentity: { findUnique: vi.fn(), create: vi.fn() }, |
| 20 | + refreshToken: { create: vi.fn() }, |
| 21 | + } as any); |
| 22 | + |
| 23 | + app.decorate('redis', { |
| 24 | + set: vi.fn(), |
| 25 | + getdel: vi.fn(), |
| 26 | + } as any); |
| 27 | + |
| 28 | + app.decorate('authenticate', async () => {}); |
| 29 | + |
| 30 | + await app.register(authRoutes, { prefix: '/auth' }); |
| 31 | + await app.ready(); |
| 32 | + return app; |
| 33 | +} |
| 34 | + |
| 35 | +function cookieCleared(res: any): boolean { |
| 36 | + const raw = res.headers['set-cookie'] as string | string[] | undefined; |
| 37 | + const cookies = Array.isArray(raw) ? raw : raw ? [raw] : []; |
| 38 | + return cookies.some((c) => c.startsWith('oauth_state=;') || c.includes('oauth_state=; ')); |
| 39 | +} |
| 40 | + |
| 41 | +describe('GET /auth/github/callback — Zod validation', () => { |
| 42 | + let app: FastifyInstance; |
| 43 | + |
| 44 | + beforeEach(async () => { |
| 45 | + vi.clearAllMocks(); |
| 46 | + app = await buildTestApp(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(async () => { |
| 50 | + await app.close(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('400 — missing code rejects with validation error', async () => { |
| 54 | + const res = await app.inject({ |
| 55 | + method: 'GET', |
| 56 | + url: '/auth/github/callback?state=somestate', |
| 57 | + headers: { Cookie: 'oauth_state=somestate' }, |
| 58 | + }); |
| 59 | + |
| 60 | + expect(res.statusCode).toBe(400); |
| 61 | + expect(res.json().error).toBe('Invalid callback parameters'); |
| 62 | + expect(cookieCleared(res)).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it('400 — empty code rejects with validation error', async () => { |
| 66 | + const res = await app.inject({ |
| 67 | + method: 'GET', |
| 68 | + url: '/auth/github/callback?code=&state=somestate', |
| 69 | + headers: { Cookie: 'oauth_state=somestate' }, |
| 70 | + }); |
| 71 | + |
| 72 | + expect(res.statusCode).toBe(400); |
| 73 | + expect(res.json().error).toBe('Invalid callback parameters'); |
| 74 | + expect(cookieCleared(res)).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it('400 — missing state rejects with validation error', async () => { |
| 78 | + const res = await app.inject({ |
| 79 | + method: 'GET', |
| 80 | + url: '/auth/github/callback?code=validcode', |
| 81 | + }); |
| 82 | + |
| 83 | + expect(res.statusCode).toBe(400); |
| 84 | + expect(res.json().error).toBe('Invalid callback parameters'); |
| 85 | + expect(cookieCleared(res)).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('400 — empty state rejects with validation error', async () => { |
| 89 | + const res = await app.inject({ |
| 90 | + method: 'GET', |
| 91 | + url: '/auth/github/callback?code=validcode&state=', |
| 92 | + }); |
| 93 | + |
| 94 | + expect(res.statusCode).toBe(400); |
| 95 | + expect(res.json().error).toBe('Invalid callback parameters'); |
| 96 | + expect(cookieCleared(res)).toBe(true); |
| 97 | + }); |
| 98 | + |
| 99 | + it('400 — valid code and state but no cookie rejects with state error', async () => { |
| 100 | + const res = await app.inject({ |
| 101 | + method: 'GET', |
| 102 | + url: '/auth/github/callback?code=validcode&state=somestate', |
| 103 | + }); |
| 104 | + |
| 105 | + expect(res.statusCode).toBe(400); |
| 106 | + expect(res.json().error).toBe('Invalid or missing OAuth state — possible CSRF attack'); |
| 107 | + expect(cookieCleared(res)).toBe(true); |
| 108 | + }); |
| 109 | + |
| 110 | + it('400 — valid code and state but mismatched cookie rejects with state error', async () => { |
| 111 | + const res = await app.inject({ |
| 112 | + method: 'GET', |
| 113 | + url: '/auth/github/callback?code=validcode&state=somestate', |
| 114 | + headers: { Cookie: 'oauth_state=differentstate' }, |
| 115 | + }); |
| 116 | + |
| 117 | + expect(res.statusCode).toBe(400); |
| 118 | + expect(res.json().error).toBe('Invalid or missing OAuth state — possible CSRF attack'); |
| 119 | + expect(cookieCleared(res)).toBe(true); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe('GET /auth/google/callback — Zod validation', () => { |
| 124 | + let app: FastifyInstance; |
| 125 | + |
| 126 | + beforeEach(async () => { |
| 127 | + vi.clearAllMocks(); |
| 128 | + app = await buildTestApp(); |
| 129 | + }); |
| 130 | + |
| 131 | + afterEach(async () => { |
| 132 | + await app.close(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('400 — missing code rejects with validation error', async () => { |
| 136 | + const res = await app.inject({ |
| 137 | + method: 'GET', |
| 138 | + url: '/auth/google/callback?state=somestate', |
| 139 | + headers: { Cookie: 'oauth_state=somestate' }, |
| 140 | + }); |
| 141 | + |
| 142 | + expect(res.statusCode).toBe(400); |
| 143 | + expect(res.json().error).toBe('Invalid callback parameters'); |
| 144 | + expect(cookieCleared(res)).toBe(true); |
| 145 | + }); |
| 146 | + |
| 147 | + it('400 — empty code rejects with validation error', async () => { |
| 148 | + const res = await app.inject({ |
| 149 | + method: 'GET', |
| 150 | + url: '/auth/google/callback?code=&state=somestate', |
| 151 | + headers: { Cookie: 'oauth_state=somestate' }, |
| 152 | + }); |
| 153 | + |
| 154 | + expect(res.statusCode).toBe(400); |
| 155 | + expect(res.json().error).toBe('Invalid callback parameters'); |
| 156 | + expect(cookieCleared(res)).toBe(true); |
| 157 | + }); |
| 158 | + |
| 159 | + it('400 — missing state rejects with validation error', async () => { |
| 160 | + const res = await app.inject({ |
| 161 | + method: 'GET', |
| 162 | + url: '/auth/google/callback?code=validcode', |
| 163 | + }); |
| 164 | + |
| 165 | + expect(res.statusCode).toBe(400); |
| 166 | + expect(res.json().error).toBe('Invalid callback parameters'); |
| 167 | + expect(cookieCleared(res)).toBe(true); |
| 168 | + }); |
| 169 | + |
| 170 | + it('400 — empty state rejects with validation error', async () => { |
| 171 | + const res = await app.inject({ |
| 172 | + method: 'GET', |
| 173 | + url: '/auth/google/callback?code=validcode&state=', |
| 174 | + }); |
| 175 | + |
| 176 | + expect(res.statusCode).toBe(400); |
| 177 | + expect(res.json().error).toBe('Invalid callback parameters'); |
| 178 | + expect(cookieCleared(res)).toBe(true); |
| 179 | + }); |
| 180 | + |
| 181 | + it('400 — valid code and state but no cookie rejects with state error', async () => { |
| 182 | + const res = await app.inject({ |
| 183 | + method: 'GET', |
| 184 | + url: '/auth/google/callback?code=validcode&state=somestate', |
| 185 | + }); |
| 186 | + |
| 187 | + expect(res.statusCode).toBe(400); |
| 188 | + expect(res.json().error).toBe('Invalid or missing OAuth state — possible CSRF attack'); |
| 189 | + expect(cookieCleared(res)).toBe(true); |
| 190 | + }); |
| 191 | + |
| 192 | + it('400 — valid code and state but mismatched cookie rejects with state error', async () => { |
| 193 | + const res = await app.inject({ |
| 194 | + method: 'GET', |
| 195 | + url: '/auth/google/callback?code=validcode&state=somestate', |
| 196 | + headers: { Cookie: 'oauth_state=differentstate' }, |
| 197 | + }); |
| 198 | + |
| 199 | + expect(res.statusCode).toBe(400); |
| 200 | + expect(res.json().error).toBe('Invalid or missing OAuth state — possible CSRF attack'); |
| 201 | + expect(cookieCleared(res)).toBe(true); |
| 202 | + }); |
| 203 | +}); |
0 commit comments