|
| 1 | +import type { PayloadRequest } from '../types/index.js' |
| 2 | + |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | + |
| 5 | +import { getRequestOrigin } from './getRequestOrigin' |
| 6 | + |
| 7 | +type MinimalReq = Pick<PayloadRequest, 'headers' | 'payload' | 'url'> |
| 8 | + |
| 9 | +const makeReq = (url: string, hostOverride?: string): MinimalReq => { |
| 10 | + let host = hostOverride |
| 11 | + if (host === undefined) { |
| 12 | + try { |
| 13 | + host = new URL(url).host |
| 14 | + } catch { |
| 15 | + host = undefined |
| 16 | + } |
| 17 | + } |
| 18 | + return { |
| 19 | + url, |
| 20 | + headers: new Headers(host !== undefined ? { host } : {}), |
| 21 | + payload: { |
| 22 | + logger: { |
| 23 | + warn: () => {}, |
| 24 | + }, |
| 25 | + }, |
| 26 | + } as unknown as MinimalReq |
| 27 | +} |
| 28 | + |
| 29 | +describe('getRequestOrigin', () => { |
| 30 | + describe('when config.serverURL is set', () => { |
| 31 | + it('should return serverURL regardless of Host header', () => { |
| 32 | + const req = makeReq('https://ignored.com/api/forgot-password', 'attacker.com') |
| 33 | + const result = getRequestOrigin({ |
| 34 | + config: { serverURL: 'https://myapp.com', cors: '*', csrf: [] }, |
| 35 | + req, |
| 36 | + }) |
| 37 | + expect(result).toBe('https://myapp.com') |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + describe('when config.serverURL is not set', () => { |
| 42 | + describe('CORS allowlist validation', () => { |
| 43 | + it('should return origin when Host header matches a CORS string array entry', () => { |
| 44 | + const req = makeReq('https://myapp.com/api/forgot-password') |
| 45 | + const result = getRequestOrigin({ |
| 46 | + config: { serverURL: '', cors: ['https://myapp.com', 'https://other.com'], csrf: [] }, |
| 47 | + req, |
| 48 | + }) |
| 49 | + expect(result).toBe('https://myapp.com') |
| 50 | + }) |
| 51 | + |
| 52 | + it('should return origin when Host header matches a CORSConfig origins entry', () => { |
| 53 | + const req = makeReq('https://myapp.com/api/verify') |
| 54 | + const result = getRequestOrigin({ |
| 55 | + config: { |
| 56 | + serverURL: '', |
| 57 | + cors: { headers: [], origins: ['https://myapp.com'] }, |
| 58 | + csrf: [], |
| 59 | + }, |
| 60 | + req, |
| 61 | + }) |
| 62 | + expect(result).toBe('https://myapp.com') |
| 63 | + }) |
| 64 | + |
| 65 | + it('should return empty string when Host header is forged and not in CORS allowlist', () => { |
| 66 | + const req = makeReq('https://myapp.com/api/forgot-password', 'attacker.com') |
| 67 | + const result = getRequestOrigin({ |
| 68 | + config: { serverURL: '', cors: ['https://myapp.com'], csrf: [] }, |
| 69 | + req, |
| 70 | + }) |
| 71 | + expect(result).toBe('') |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + describe('CSRF allowlist validation', () => { |
| 76 | + it('should return origin when Host header matches a CSRF entry', () => { |
| 77 | + const req = makeReq('https://myapp.com/api/verify') |
| 78 | + const result = getRequestOrigin({ |
| 79 | + config: { serverURL: '', cors: [], csrf: ['https://myapp.com'] }, |
| 80 | + req, |
| 81 | + }) |
| 82 | + expect(result).toBe('https://myapp.com') |
| 83 | + }) |
| 84 | + |
| 85 | + it('should return origin when Host header is in CSRF but not in CORS', () => { |
| 86 | + const req = makeReq('https://myapp.com/api/verify') |
| 87 | + const result = getRequestOrigin({ |
| 88 | + config: { |
| 89 | + serverURL: '', |
| 90 | + cors: ['https://other.com'], |
| 91 | + csrf: ['https://myapp.com'], |
| 92 | + }, |
| 93 | + req, |
| 94 | + }) |
| 95 | + expect(result).toBe('https://myapp.com') |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + describe('malformed or missing Host header', () => { |
| 100 | + it('should return empty string when req.url is not a valid URL', () => { |
| 101 | + const req = makeReq('not-a-url') |
| 102 | + const result = getRequestOrigin({ |
| 103 | + config: { serverURL: '', cors: [], csrf: [] }, |
| 104 | + req, |
| 105 | + }) |
| 106 | + expect(result).toBe('') |
| 107 | + }) |
| 108 | + |
| 109 | + it('should return empty string when Host header is absent', () => { |
| 110 | + const req = makeReq('https://myapp.com/api/forgot-password', '') |
| 111 | + const result = getRequestOrigin({ |
| 112 | + config: { serverURL: '', cors: ['https://myapp.com'], csrf: [] }, |
| 113 | + req, |
| 114 | + }) |
| 115 | + expect(result).toBe('') |
| 116 | + }) |
| 117 | + }) |
| 118 | + }) |
| 119 | +}) |
0 commit comments