-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathorigin-check.middleware.spec.ts
More file actions
173 lines (136 loc) · 5.07 KB
/
origin-check.middleware.spec.ts
File metadata and controls
173 lines (136 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { originCheckMiddleware } from './origin-check.middleware';
// Mock isTrustedOrigin (async version)
jest.mock('./auth.server', () => ({
isTrustedOrigin: async (origin: string) => {
const staticOrigins = [
'http://localhost:3000',
'http://localhost:3002',
'https://app.trycomp.ai',
'https://portal.trycomp.ai',
];
if (staticOrigins.includes(origin)) return true;
try {
const url = new URL(origin);
return (
url.hostname.endsWith('.trycomp.ai') ||
url.hostname.endsWith('.staging.trycomp.ai') ||
url.hostname.endsWith('.trust.inc') ||
url.hostname === 'trust.inc'
);
} catch {
return false;
}
},
}));
function createMockReq(
method: string,
path: string,
origin?: string,
): Record<string, unknown> {
return {
method,
path,
headers: origin ? { origin } : {},
};
}
/** Flush the microtask queue so async middleware completes. */
const flushPromises = () => new Promise((resolve) => setImmediate(resolve));
function createMockRes(): Record<string, unknown> & { statusCode?: number; body?: unknown } {
const res: Record<string, unknown> & { statusCode?: number; body?: unknown } = {};
res.status = jest.fn().mockImplementation((code: number) => {
res.statusCode = code;
return res;
});
res.json = jest.fn().mockImplementation((body: unknown) => {
res.body = body;
return res;
});
return res;
}
describe('originCheckMiddleware', () => {
it('should allow GET requests regardless of origin', () => {
const req = createMockReq('GET', '/v1/controls', 'http://evil.com');
const res = createMockRes();
const next = jest.fn();
originCheckMiddleware(req as any, res as any, next);
expect(next).toHaveBeenCalled();
});
it('should allow HEAD requests regardless of origin', () => {
const req = createMockReq('HEAD', '/v1/health', 'http://evil.com');
const res = createMockRes();
const next = jest.fn();
originCheckMiddleware(req as any, res as any, next);
expect(next).toHaveBeenCalled();
});
it('should allow OPTIONS requests regardless of origin', () => {
const req = createMockReq('OPTIONS', '/v1/controls', 'http://evil.com');
const res = createMockRes();
const next = jest.fn();
originCheckMiddleware(req as any, res as any, next);
expect(next).toHaveBeenCalled();
});
it('should allow POST from trusted origin', async () => {
const req = createMockReq('POST', '/v1/organization/api-keys', 'http://localhost:3000');
const res = createMockRes();
const next = jest.fn();
originCheckMiddleware(req as any, res as any, next);
await flushPromises();
expect(next).toHaveBeenCalled();
});
it('should block POST from untrusted origin', async () => {
const req = createMockReq('POST', '/v1/organization/transfer-ownership', 'http://evil.com');
const res = createMockRes();
const next = jest.fn();
originCheckMiddleware(req as any, res as any, next);
await flushPromises();
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(403);
});
it('should block DELETE from untrusted origin', async () => {
const req = createMockReq('DELETE', '/v1/organization', 'http://evil.com');
const res = createMockRes();
const next = jest.fn();
originCheckMiddleware(req as any, res as any, next);
await flushPromises();
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(403);
});
it('should block PATCH from untrusted origin', async () => {
const req = createMockReq('PATCH', '/v1/members/123/role', 'http://evil.com');
const res = createMockRes();
const next = jest.fn();
originCheckMiddleware(req as any, res as any, next);
await flushPromises();
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(403);
});
it('should allow POST without Origin header (API key / service token)', () => {
const req = createMockReq('POST', '/v1/controls', undefined);
const res = createMockRes();
const next = jest.fn();
originCheckMiddleware(req as any, res as any, next);
expect(next).toHaveBeenCalled();
});
it('should allow POST to /api/auth routes (better-auth exempt)', () => {
const req = createMockReq('POST', '/api/auth/sign-in', 'http://evil.com');
const res = createMockRes();
const next = jest.fn();
originCheckMiddleware(req as any, res as any, next);
expect(next).toHaveBeenCalled();
});
it('should allow POST to health check', () => {
const req = createMockReq('POST', '/v1/health', 'http://evil.com');
const res = createMockRes();
const next = jest.fn();
originCheckMiddleware(req as any, res as any, next);
expect(next).toHaveBeenCalled();
});
it('should allow production origins', async () => {
const req = createMockReq('POST', '/v1/organization/api-keys', 'https://app.trycomp.ai');
const res = createMockRes();
const next = jest.fn();
originCheckMiddleware(req as any, res as any, next);
await flushPromises();
expect(next).toHaveBeenCalled();
});
});