-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkers.test.ts
More file actions
254 lines (253 loc) · 7.74 KB
/
Copy pathworkers.test.ts
File metadata and controls
254 lines (253 loc) · 7.74 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { test, expect } from 'vitest';
import { build } from 'esbuild';
import { builtinModules } from 'node:module';
import {
Miniflare,
convertV4MiniflareOptions,
Response as MiniflareResponse,
} from 'miniflare';
import { createTestContext } from '../../packages/pgstencil/src/testing.ts';
import type { EmailMessage } from '../../packages/pgstencil/src/email.ts';
import {
allOAuthCredentials,
endpointPaths,
mockOAuthServer,
} from '../support/oauth-server.ts';
import { codeFrom } from './helpers.ts';
import type { AuthState } from '../../packages/auth/src/fetch.ts';
const origin = 'https://worker.example.test';
const bundle = build({
entryPoints: ['tests/support/worker.ts'],
bundle: true,
write: false,
format: 'esm',
platform: 'node',
conditions: ['workerd', 'worker'],
external: ['node:*', 'cloudflare:*'],
alias: Object.fromEntries(
builtinModules
.filter((name) => !name.startsWith('node:'))
.map((name) => [name, `node:${name}`]),
),
banner: {
js: "import { createRequire } from 'node:module'; const require = createRequire('/worker.js');",
},
});
type WorkerResponse = Awaited<ReturnType<Miniflare['dispatchFetch']>>;
const cookie = (response: WorkerResponse) =>
response.headers
.getSetCookie()
.map((v) => v.split(';')[0])
.join('; ');
const workerTest = test.extend<{ f: Awaited<ReturnType<typeof fixture>> }>({
f: async ({}, use) => {
const f = await fixture();
try {
await use(f);
} finally {
await f.close();
}
},
});
async function fixture() {
const context = await createTestContext();
const provider = await mockOAuthServer();
const bindings: Record<string, string> = {
APP_ORIGIN: origin,
AUTH_SECRET: 'workers-test-secret-at-least-32-characters',
};
for (const [name, credentials] of Object.entries(allOAuthCredentials)) {
bindings[`${name.toUpperCase()}_CLIENT_ID`] = credentials.clientId;
bindings[`${name.toUpperCase()}_CLIENT_SECRET`] = credentials.clientSecret;
}
const worker = new Miniflare(
convertV4MiniflareOptions({
modules: true,
script: (await bundle).outputFiles![0]!.text,
compatibilityDate: '2026-09-08',
compatibilityFlags: ['nodejs_compat'],
bindings,
hyperdrives: { HYPERDRIVE: context.database.url },
async outboundService(req) {
const url = new URL(req.url);
if (url.origin === 'https://inbox.test') {
await context.email.send((await req.json()) as EmailMessage);
return new MiniflareResponse('ok');
}
const path = endpointPaths[url.origin + url.pathname];
if (!path)
throw new Error(
`Unexpected outbound request: ${url.origin}${url.pathname}`,
);
const response = await fetch(provider.origin + path + url.search, {
method: req.method,
headers: Object.fromEntries(req.headers),
...(req.method === 'POST' ? { body: await req.text() } : {}),
});
return new MiniflareResponse(await response.arrayBuffer(), {
status: response.status,
headers: {
'content-type':
response.headers.get('content-type') ?? 'application/json',
},
});
},
}),
);
try {
await worker.ready;
} catch (error) {
await worker.dispose();
await provider.close();
await context.close();
throw error;
}
const get = (path: string, cookies = '') =>
worker.dispatchFetch(origin + path, {
headers: { cookie: cookies },
redirect: 'manual',
});
const post = (
path: string,
csrf: string,
cookies: string,
body: unknown = {},
requestOrigin = origin,
) =>
worker.dispatchFetch(origin + path, {
method: 'POST',
headers: {
origin: requestOrigin,
cookie: cookies,
'x-csrf-token': csrf,
'content-type': 'application/json',
},
body: JSON.stringify(body),
redirect: 'manual',
});
return {
...context,
worker,
provider,
get,
post,
async close() {
await worker.dispose();
await provider.close();
await context.close();
},
};
}
workerTest(
'real workerd + Postgres: email login, secure cookies, CSRF, exact 24-hour expiry',
async ({ f }) => {
const start = await f.get('/api/auth/session');
expect(start.status).toBe(200);
const state = (await start.json()) as AuthState;
const pending = cookie(start);
expect(
(
await f.post(
'/api/auth/email',
state.csrf,
pending,
{ email: 'worker@example.test' },
'https://attacker.test',
)
).status,
).toBe(403);
expect(
(
await f.post('/api/auth/email', state.csrf, pending, {
email: 'worker@example.test',
})
).status,
).toBe(200);
const email = await f.email.next();
const verified = await f.post('/api/auth/verify', state.csrf, pending, {
method: 'code',
value: codeFrom(email),
});
expect(verified.status).toBe(200);
expect(verified.headers.getSetCookie()[0]).toContain(
'HttpOnly; SameSite=Lax; Max-Age=86400; Expires=Thu, 02 Jan 2020 00:00:00 GMT; Secure',
);
const sessionCookie = cookie(verified);
const session = (await (
await f.get('/api/auth/session', sessionCookie)
).json()) as AuthState;
expect(session.session?.user.email).toBe('worker@example.test');
expect(
(await f.post('/api/auth/logout', 'forged', sessionCookie)).status,
).toBe(403);
await f.worker.dispatchFetch(origin + '/__test/time', {
method: 'POST',
body: '2020-01-01T23:00:00Z',
});
expect(
(
(await (
await f.get('/api/auth/session', sessionCookie)
).json()) as AuthState
).session,
).not.toBeNull();
await f.worker.dispatchFetch(origin + '/__test/time', {
method: 'POST',
body: '2020-01-02T00:00:00Z',
});
expect(
(
(await (
await f.get('/api/auth/session', sessionCookie)
).json()) as AuthState
).session,
).toBeNull();
},
);
workerTest.for(['google', 'apple', 'facebook'] as const)(
'real workerd: %s callback, browser binding and replay protection',
async (provider, { f }) => {
const start = await f.get('/api/auth/session');
const state = (await start.json()) as AuthState;
const started = await f.post(
`/api/auth/oauth/${provider}/start`,
state.csrf,
cookie(start),
);
expect(started.status).toBe(200);
const callback = f.provider.authorize(
provider,
new URL(((await started.json()) as { url: string }).url),
);
let path = callback.pathname + callback.search;
if (provider === 'apple') {
const relay = await f.worker.dispatchFetch(origin + callback.pathname, {
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
origin: 'https://appleid.apple.com',
},
body: callback.searchParams.toString(),
redirect: 'manual',
});
expect(relay.status).toBe(303);
expect(relay.headers.get('referrer-policy')).toBe('no-referrer');
expect(relay.headers.getSetCookie()).toEqual([]);
path = relay.headers.get('location')!;
}
const unbound = await f.get(path);
expect(unbound.headers.get('location')).toContain('/login?error=');
const result = await f.get(path, cookie(started));
expect(result.headers.get('location')).toBe('/profile');
expect(
(
(await (
await f.get('/api/auth/session', cookie(result))
).json()) as AuthState
).session?.user.email,
).toBe('oauth@example.test');
expect(
(await f.get(path, cookie(started))).headers.get('location'),
).toContain('/login?error=');
},
);