|
| 1 | +import type { Request, Response, NextFunction } from 'express'; |
| 2 | +import { createCsrfMiddleware } from '@constructive-io/csrf'; |
| 3 | +import { parseCookieValue, SESSION_COOKIE_NAME } from '../cookie'; |
| 4 | + |
| 5 | +describe('CSRF middleware integration', () => { |
| 6 | + const csrf = createCsrfMiddleware({ |
| 7 | + cookieOptions: { |
| 8 | + httpOnly: false, |
| 9 | + secure: false, |
| 10 | + sameSite: 'lax', |
| 11 | + }, |
| 12 | + }); |
| 13 | + |
| 14 | + const createMockReq = (overrides: Partial<Request> = {}): Request => { |
| 15 | + return { |
| 16 | + method: 'POST', |
| 17 | + headers: {}, |
| 18 | + cookies: {}, |
| 19 | + body: {}, |
| 20 | + ...overrides, |
| 21 | + } as unknown as Request; |
| 22 | + }; |
| 23 | + |
| 24 | + const createMockRes = (): Response => { |
| 25 | + const res: Partial<Response> = { |
| 26 | + cookie: jest.fn(), |
| 27 | + status: jest.fn().mockReturnThis(), |
| 28 | + json: jest.fn().mockReturnThis(), |
| 29 | + }; |
| 30 | + return res as Response; |
| 31 | + }; |
| 32 | + |
| 33 | + describe('csrfSetToken', () => { |
| 34 | + it('sets csrf_token cookie on request', (done) => { |
| 35 | + const req = createMockReq(); |
| 36 | + const res = createMockRes(); |
| 37 | + |
| 38 | + csrf.setToken(req as any, res as any, (err?: Error) => { |
| 39 | + expect(err).toBeUndefined(); |
| 40 | + expect(res.cookie).toHaveBeenCalledWith( |
| 41 | + 'csrf_token', |
| 42 | + expect.any(String), |
| 43 | + expect.objectContaining({ |
| 44 | + httpOnly: false, |
| 45 | + }) |
| 46 | + ); |
| 47 | + done(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('does not overwrite existing csrf_token cookie', (done) => { |
| 52 | + const req = createMockReq({ |
| 53 | + cookies: { csrf_token: 'existing-token' }, |
| 54 | + }); |
| 55 | + const res = createMockRes(); |
| 56 | + |
| 57 | + csrf.setToken(req as any, res as any, (err?: Error) => { |
| 58 | + expect(err).toBeUndefined(); |
| 59 | + expect(res.cookie).not.toHaveBeenCalled(); |
| 60 | + done(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('csrfProtect', () => { |
| 66 | + it('allows GET requests without CSRF token', (done) => { |
| 67 | + const req = createMockReq({ method: 'GET' }); |
| 68 | + const res = createMockRes(); |
| 69 | + |
| 70 | + csrf.protect(req as any, res as any, (err?: Error) => { |
| 71 | + expect(err).toBeUndefined(); |
| 72 | + done(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('blocks POST without CSRF cookie', (done) => { |
| 77 | + const req = createMockReq({ method: 'POST' }); |
| 78 | + const res = createMockRes(); |
| 79 | + |
| 80 | + csrf.protect(req as any, res as any, (err?: Error) => { |
| 81 | + expect(err).toBeDefined(); |
| 82 | + expect((err as any).code).toBe('CSRF_TOKEN_MISSING'); |
| 83 | + done(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('blocks POST with cookie but no header', (done) => { |
| 88 | + const req = createMockReq({ |
| 89 | + method: 'POST', |
| 90 | + cookies: { csrf_token: 'valid-token' }, |
| 91 | + }); |
| 92 | + const res = createMockRes(); |
| 93 | + |
| 94 | + csrf.protect(req as any, res as any, (err?: Error) => { |
| 95 | + expect(err).toBeDefined(); |
| 96 | + expect((err as any).code).toBe('CSRF_TOKEN_INVALID'); |
| 97 | + done(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('allows POST with matching cookie and header', (done) => { |
| 102 | + const token = 'valid-csrf-token'; |
| 103 | + const req = createMockReq({ |
| 104 | + method: 'POST', |
| 105 | + cookies: { csrf_token: token }, |
| 106 | + headers: { 'x-csrf-token': token }, |
| 107 | + }); |
| 108 | + const res = createMockRes(); |
| 109 | + |
| 110 | + csrf.protect(req as any, res as any, (err?: Error) => { |
| 111 | + expect(err).toBeUndefined(); |
| 112 | + done(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('blocks POST with mismatched cookie and header', (done) => { |
| 117 | + const req = createMockReq({ |
| 118 | + method: 'POST', |
| 119 | + cookies: { csrf_token: 'token-a' }, |
| 120 | + headers: { 'x-csrf-token': 'token-b' }, |
| 121 | + }); |
| 122 | + const res = createMockRes(); |
| 123 | + |
| 124 | + csrf.protect(req as any, res as any, (err?: Error) => { |
| 125 | + expect(err).toBeDefined(); |
| 126 | + expect((err as any).code).toBe('CSRF_TOKEN_INVALID'); |
| 127 | + done(); |
| 128 | + }); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('CSRF skip conditions for server.ts', () => { |
| 133 | + const shouldSkipCsrf = (req: Request): boolean => { |
| 134 | + const auth = req.headers.authorization; |
| 135 | + if (auth && auth.toLowerCase().startsWith('bearer ')) return true; |
| 136 | + const sessionCookie = parseCookieValue(req, SESSION_COOKIE_NAME); |
| 137 | + if (!sessionCookie) return true; |
| 138 | + return false; |
| 139 | + }; |
| 140 | + |
| 141 | + it('skips CSRF for Bearer token auth', () => { |
| 142 | + const req = createMockReq({ |
| 143 | + headers: { authorization: 'Bearer some-token' }, |
| 144 | + }); |
| 145 | + expect(shouldSkipCsrf(req)).toBe(true); |
| 146 | + }); |
| 147 | + |
| 148 | + it('skips CSRF for anonymous requests (no session cookie)', () => { |
| 149 | + const req = createMockReq(); |
| 150 | + expect(shouldSkipCsrf(req)).toBe(true); |
| 151 | + }); |
| 152 | + |
| 153 | + it('enforces CSRF for cookie-authenticated requests', () => { |
| 154 | + const req = createMockReq({ |
| 155 | + headers: { cookie: `${SESSION_COOKIE_NAME}=some-session` }, |
| 156 | + }); |
| 157 | + expect(shouldSkipCsrf(req)).toBe(false); |
| 158 | + }); |
| 159 | + |
| 160 | + it('skips CSRF for Bearer even with session cookie', () => { |
| 161 | + const req = createMockReq({ |
| 162 | + headers: { |
| 163 | + authorization: 'Bearer some-token', |
| 164 | + cookie: `${SESSION_COOKIE_NAME}=some-session`, |
| 165 | + }, |
| 166 | + }); |
| 167 | + expect(shouldSkipCsrf(req)).toBe(true); |
| 168 | + }); |
| 169 | + }); |
| 170 | +}); |
0 commit comments