|
| 1 | +import { originCheckMiddleware } from './origin-check.middleware'; |
| 2 | + |
| 3 | +// Mock getTrustedOrigins |
| 4 | +jest.mock('./auth.server', () => ({ |
| 5 | + getTrustedOrigins: () => [ |
| 6 | + 'http://localhost:3000', |
| 7 | + 'http://localhost:3002', |
| 8 | + 'https://app.trycomp.ai', |
| 9 | + 'https://portal.trycomp.ai', |
| 10 | + ], |
| 11 | +})); |
| 12 | + |
| 13 | +function createMockReq( |
| 14 | + method: string, |
| 15 | + path: string, |
| 16 | + origin?: string, |
| 17 | +): Record<string, unknown> { |
| 18 | + return { |
| 19 | + method, |
| 20 | + path, |
| 21 | + headers: origin ? { origin } : {}, |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +function createMockRes(): Record<string, unknown> & { statusCode?: number; body?: unknown } { |
| 26 | + const res: Record<string, unknown> & { statusCode?: number; body?: unknown } = {}; |
| 27 | + res.status = jest.fn().mockImplementation((code: number) => { |
| 28 | + res.statusCode = code; |
| 29 | + return res; |
| 30 | + }); |
| 31 | + res.json = jest.fn().mockImplementation((body: unknown) => { |
| 32 | + res.body = body; |
| 33 | + return res; |
| 34 | + }); |
| 35 | + return res; |
| 36 | +} |
| 37 | + |
| 38 | +describe('originCheckMiddleware', () => { |
| 39 | + it('should allow GET requests regardless of origin', () => { |
| 40 | + const req = createMockReq('GET', '/v1/controls', 'http://evil.com'); |
| 41 | + const res = createMockRes(); |
| 42 | + const next = jest.fn(); |
| 43 | + |
| 44 | + originCheckMiddleware(req as any, res as any, next); |
| 45 | + |
| 46 | + expect(next).toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should allow HEAD requests regardless of origin', () => { |
| 50 | + const req = createMockReq('HEAD', '/v1/health', 'http://evil.com'); |
| 51 | + const res = createMockRes(); |
| 52 | + const next = jest.fn(); |
| 53 | + |
| 54 | + originCheckMiddleware(req as any, res as any, next); |
| 55 | + |
| 56 | + expect(next).toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should allow OPTIONS requests regardless of origin', () => { |
| 60 | + const req = createMockReq('OPTIONS', '/v1/controls', 'http://evil.com'); |
| 61 | + const res = createMockRes(); |
| 62 | + const next = jest.fn(); |
| 63 | + |
| 64 | + originCheckMiddleware(req as any, res as any, next); |
| 65 | + |
| 66 | + expect(next).toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should allow POST from trusted origin', () => { |
| 70 | + const req = createMockReq('POST', '/v1/organization/api-keys', 'http://localhost:3000'); |
| 71 | + const res = createMockRes(); |
| 72 | + const next = jest.fn(); |
| 73 | + |
| 74 | + originCheckMiddleware(req as any, res as any, next); |
| 75 | + |
| 76 | + expect(next).toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should block POST from untrusted origin', () => { |
| 80 | + const req = createMockReq('POST', '/v1/organization/transfer-ownership', 'http://evil.com'); |
| 81 | + const res = createMockRes(); |
| 82 | + const next = jest.fn(); |
| 83 | + |
| 84 | + originCheckMiddleware(req as any, res as any, next); |
| 85 | + |
| 86 | + expect(next).not.toHaveBeenCalled(); |
| 87 | + expect(res.status).toHaveBeenCalledWith(403); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should block DELETE from untrusted origin', () => { |
| 91 | + const req = createMockReq('DELETE', '/v1/organization', 'http://evil.com'); |
| 92 | + const res = createMockRes(); |
| 93 | + const next = jest.fn(); |
| 94 | + |
| 95 | + originCheckMiddleware(req as any, res as any, next); |
| 96 | + |
| 97 | + expect(next).not.toHaveBeenCalled(); |
| 98 | + expect(res.status).toHaveBeenCalledWith(403); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should block PATCH from untrusted origin', () => { |
| 102 | + const req = createMockReq('PATCH', '/v1/members/123/role', 'http://evil.com'); |
| 103 | + const res = createMockRes(); |
| 104 | + const next = jest.fn(); |
| 105 | + |
| 106 | + originCheckMiddleware(req as any, res as any, next); |
| 107 | + |
| 108 | + expect(next).not.toHaveBeenCalled(); |
| 109 | + expect(res.status).toHaveBeenCalledWith(403); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should allow POST without Origin header (API key / service token)', () => { |
| 113 | + const req = createMockReq('POST', '/v1/controls', undefined); |
| 114 | + const res = createMockRes(); |
| 115 | + const next = jest.fn(); |
| 116 | + |
| 117 | + originCheckMiddleware(req as any, res as any, next); |
| 118 | + |
| 119 | + expect(next).toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should allow POST to /api/auth routes (better-auth exempt)', () => { |
| 123 | + const req = createMockReq('POST', '/api/auth/sign-in', 'http://evil.com'); |
| 124 | + const res = createMockRes(); |
| 125 | + const next = jest.fn(); |
| 126 | + |
| 127 | + originCheckMiddleware(req as any, res as any, next); |
| 128 | + |
| 129 | + expect(next).toHaveBeenCalled(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should allow POST to health check', () => { |
| 133 | + const req = createMockReq('POST', '/v1/health', 'http://evil.com'); |
| 134 | + const res = createMockRes(); |
| 135 | + const next = jest.fn(); |
| 136 | + |
| 137 | + originCheckMiddleware(req as any, res as any, next); |
| 138 | + |
| 139 | + expect(next).toHaveBeenCalled(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should allow production origins', () => { |
| 143 | + const req = createMockReq('POST', '/v1/organization/api-keys', 'https://app.trycomp.ai'); |
| 144 | + const res = createMockRes(); |
| 145 | + const next = jest.fn(); |
| 146 | + |
| 147 | + originCheckMiddleware(req as any, res as any, next); |
| 148 | + |
| 149 | + expect(next).toHaveBeenCalled(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments