|
| 1 | +import { mockDeep } from 'jest-mock-extended'; |
| 2 | +import { |
| 3 | + generateCsrf, |
| 4 | + getSessionId, |
| 5 | + verifyCsrfToken, |
| 6 | + verifyCsrfTokenFull, |
| 7 | + getCsrfFormValue, |
| 8 | +} from '@utils/csrf-utils'; |
| 9 | +import { cookies } from 'next/headers'; |
| 10 | +import { sign } from 'jsonwebtoken'; |
| 11 | +import type { ReadonlyRequestCookies } from 'next/dist/server/web/spec-extension/adapters/request-cookies'; |
| 12 | +import { BinaryLike, BinaryToTextEncoding } from 'node:crypto'; |
| 13 | +import { getAccessTokenServer } from '@utils/amplify-utils'; |
| 14 | + |
| 15 | +class MockHmac { |
| 16 | + constructor() {} |
| 17 | + |
| 18 | + update(_: BinaryLike) { |
| 19 | + return this; |
| 20 | + } |
| 21 | + |
| 22 | + digest(_: BinaryToTextEncoding) { |
| 23 | + return 'hmac'; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const mockJwt = sign( |
| 28 | + { |
| 29 | + jti: 'jti', |
| 30 | + }, |
| 31 | + 'key' |
| 32 | +); |
| 33 | + |
| 34 | +jest.mock('next/headers'); |
| 35 | +jest.mock('node:crypto', () => ({ |
| 36 | + createHmac: () => new MockHmac(), |
| 37 | + randomBytes: () => 'salt', |
| 38 | +})); |
| 39 | +jest.mock('@utils/amplify-utils'); |
| 40 | + |
| 41 | +const OLD_ENV = { ...process.env }; |
| 42 | + |
| 43 | +beforeAll(() => { |
| 44 | + process.env.CSRF_SECRET = 'secret'; |
| 45 | +}); |
| 46 | + |
| 47 | +beforeEach(() => { |
| 48 | + jest.clearAllMocks(); |
| 49 | +}); |
| 50 | + |
| 51 | +afterAll(() => { |
| 52 | + process.env = OLD_ENV; |
| 53 | +}); |
| 54 | + |
| 55 | +describe('getCsrfFormValue', () => { |
| 56 | + test('cookie is present', async () => { |
| 57 | + jest.mocked(cookies).mockReturnValue( |
| 58 | + mockDeep<ReadonlyRequestCookies>({ |
| 59 | + get: (_: string) => ({ |
| 60 | + name: 'csrf_token', |
| 61 | + value: 'token', |
| 62 | + }), |
| 63 | + }) |
| 64 | + ); |
| 65 | + |
| 66 | + const csrfToken = await getCsrfFormValue(); |
| 67 | + |
| 68 | + expect(csrfToken).toEqual('token'); |
| 69 | + }); |
| 70 | + test('cookie is not present', async () => { |
| 71 | + jest.mocked(cookies).mockReturnValue( |
| 72 | + mockDeep<ReadonlyRequestCookies>({ |
| 73 | + get: (_: string) => undefined, |
| 74 | + }) |
| 75 | + ); |
| 76 | + |
| 77 | + const csrfToken = await getCsrfFormValue(); |
| 78 | + |
| 79 | + expect(csrfToken).toEqual('no_token'); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('getSessionId', () => { |
| 84 | + test('errors when access token not found', async () => { |
| 85 | + jest.mocked(getAccessTokenServer).mockResolvedValue(undefined); |
| 86 | + |
| 87 | + await expect(() => getSessionId()).rejects.toThrow( |
| 88 | + 'Could not get access token' |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + test('errors when session ID not found', async () => { |
| 93 | + const mockEmptyJwt = sign( |
| 94 | + { |
| 95 | + jti: undefined, |
| 96 | + }, |
| 97 | + 'key' |
| 98 | + ); |
| 99 | + |
| 100 | + jest.mocked(getAccessTokenServer).mockResolvedValue(mockEmptyJwt); |
| 101 | + |
| 102 | + await expect(() => getSessionId()).rejects.toThrow( |
| 103 | + 'Could not get session ID' |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + test('returns session id', async () => { |
| 108 | + jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt); |
| 109 | + |
| 110 | + const sessionId = await getSessionId(); |
| 111 | + |
| 112 | + expect(sessionId).toEqual('jti'); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +test('generateCsrf', async () => { |
| 117 | + const csrf = await generateCsrf(); |
| 118 | + expect(csrf).toEqual('hmac.salt'); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('verifyCsrfToken', () => { |
| 122 | + test('valid CSRF token', async () => { |
| 123 | + const csrfVerification = await verifyCsrfToken( |
| 124 | + 'hmac.salt', |
| 125 | + 'secret', |
| 126 | + 'salt' |
| 127 | + ); |
| 128 | + |
| 129 | + expect(csrfVerification).toEqual(true); |
| 130 | + }); |
| 131 | + |
| 132 | + test('invalid CSRF token', async () => { |
| 133 | + const csrfVerification = await verifyCsrfToken( |
| 134 | + 'hmac2.salt', |
| 135 | + 'secret', |
| 136 | + 'salt' |
| 137 | + ); |
| 138 | + |
| 139 | + expect(csrfVerification).toEqual(false); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +describe('verifyCsrfTokenFull', () => { |
| 144 | + test('missing CSRF cookie', async () => { |
| 145 | + const formData = mockDeep<FormData>(); |
| 146 | + |
| 147 | + jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt); |
| 148 | + |
| 149 | + await expect(() => verifyCsrfTokenFull(formData)).rejects.toThrow( |
| 150 | + 'missing CSRF cookie' |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + test('missing CSRF form field', async () => { |
| 155 | + const formData = mockDeep<FormData>({ |
| 156 | + get: () => null, |
| 157 | + }); |
| 158 | + |
| 159 | + jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt); |
| 160 | + jest.mocked(cookies).mockReturnValue( |
| 161 | + mockDeep<ReadonlyRequestCookies>({ |
| 162 | + get: (_: string) => ({ |
| 163 | + name: 'csrf_token', |
| 164 | + value: 'hmac.salt', |
| 165 | + }), |
| 166 | + }) |
| 167 | + ); |
| 168 | + |
| 169 | + await expect(() => verifyCsrfTokenFull(formData)).rejects.toThrow( |
| 170 | + 'missing CSRF form field' |
| 171 | + ); |
| 172 | + }); |
| 173 | + |
| 174 | + test('CSRF mismatch', async () => { |
| 175 | + const formData = mockDeep<FormData>({ |
| 176 | + get: () => 'hmac2.salt', |
| 177 | + }); |
| 178 | + |
| 179 | + jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt); |
| 180 | + jest.mocked(cookies).mockReturnValue( |
| 181 | + mockDeep<ReadonlyRequestCookies>({ |
| 182 | + get: (_: string) => ({ |
| 183 | + name: 'csrf_token', |
| 184 | + value: 'hmac.salt', |
| 185 | + }), |
| 186 | + }) |
| 187 | + ); |
| 188 | + |
| 189 | + await expect(() => verifyCsrfTokenFull(formData)).rejects.toThrow( |
| 190 | + 'CSRF mismatch' |
| 191 | + ); |
| 192 | + }); |
| 193 | + |
| 194 | + test('invalid CSRF form field', async () => { |
| 195 | + const formData = mockDeep<FormData>({ |
| 196 | + get: () => 'hmac2.salt', |
| 197 | + }); |
| 198 | + |
| 199 | + jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt); |
| 200 | + jest.mocked(cookies).mockReturnValue( |
| 201 | + mockDeep<ReadonlyRequestCookies>({ |
| 202 | + get: (_: string) => ({ |
| 203 | + name: 'csrf_token', |
| 204 | + value: 'hmac2.salt', |
| 205 | + }), |
| 206 | + }) |
| 207 | + ); |
| 208 | + |
| 209 | + await expect(() => verifyCsrfTokenFull(formData)).rejects.toThrow( |
| 210 | + 'CSRF error' |
| 211 | + ); |
| 212 | + }); |
| 213 | + |
| 214 | + test('valid CSRF', async () => { |
| 215 | + const formData = mockDeep<FormData>({ |
| 216 | + get: () => 'hmac.salt', |
| 217 | + }); |
| 218 | + |
| 219 | + jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt); |
| 220 | + jest.mocked(cookies).mockReturnValue( |
| 221 | + mockDeep<ReadonlyRequestCookies>({ |
| 222 | + get: (_: string) => ({ |
| 223 | + name: 'csrf_token', |
| 224 | + value: 'hmac.salt', |
| 225 | + }), |
| 226 | + }) |
| 227 | + ); |
| 228 | + |
| 229 | + const csrfVerification = await verifyCsrfTokenFull(formData); |
| 230 | + |
| 231 | + expect(csrfVerification).toEqual(true); |
| 232 | + }); |
| 233 | +}); |
0 commit comments