-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtunnel.test.js
More file actions
368 lines (318 loc) · 13.4 KB
/
Copy pathtunnel.test.js
File metadata and controls
368 lines (318 loc) · 13.4 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* Tunnel Proxy Tests
*/
import { describe, it, before, after } from 'node:test';
import assert from 'node:assert';
import { WebSocket } from 'ws';
import {
startTestServer,
stopTestServer,
createTestPod,
getBaseUrl,
getPodToken
} from './helpers.js';
describe('Tunnel Proxy', () => {
let wsUrl, baseUrl;
before(async () => {
await startTestServer({ tunnel: true });
await createTestPod('tunneler');
baseUrl = getBaseUrl();
wsUrl = baseUrl.replace('http', 'ws') + '/.tunnel';
});
after(async () => {
await stopTestServer();
});
function connectTunnel() {
const token = getPodToken('tunneler');
return new WebSocket(wsUrl, {
headers: { 'Authorization': `Bearer ${token}` }
});
}
function waitMsg(ws, type, timeout = 5000) {
return new Promise((resolve, reject) => {
function handler(data) {
const msg = JSON.parse(data.toString());
if (msg.type === type) {
clearTimeout(timer);
ws.removeListener('message', handler);
ws.removeListener('close', onClose);
resolve(msg);
}
}
function onClose() {
clearTimeout(timer);
ws.removeListener('message', handler);
reject(new Error(`WebSocket closed while waiting for "${type}"`));
}
const timer = setTimeout(() => {
ws.removeListener('message', handler);
ws.removeListener('close', onClose);
reject(new Error(`Timeout waiting for "${type}"`));
}, timeout);
ws.on('message', handler);
ws.on('close', onClose);
});
}
describe('Registration', () => {
it('should reject unauthenticated connections', async () => {
const ws = new WebSocket(wsUrl);
const msg = await waitMsg(ws, 'error');
assert.ok(msg.message.includes('Authentication'));
ws.close();
});
it('should register a tunnel name', async () => {
const ws = connectTunnel();
await new Promise(r => ws.on('open', r));
ws.send(JSON.stringify({ type: 'register', name: 'myapp' }));
const msg = await waitMsg(ws, 'registered');
assert.strictEqual(msg.name, 'myapp');
assert.strictEqual(msg.url, '/tunnel/myapp/');
ws.close();
await new Promise(r => setTimeout(r, 50));
});
it('should reject invalid tunnel names', async () => {
const ws = connectTunnel();
await new Promise(r => ws.on('open', r));
ws.send(JSON.stringify({ type: 'register', name: '...' }));
const msg = await waitMsg(ws, 'error');
assert.ok(msg.message.includes('Invalid'));
ws.close();
await new Promise(r => setTimeout(r, 50));
});
});
describe('HTTP Proxying', () => {
it('should proxy GET requests through the tunnel', async () => {
const ws = connectTunnel();
await new Promise(r => ws.on('open', r));
// Register tunnel
ws.send(JSON.stringify({ type: 'register', name: 'testapp' }));
await waitMsg(ws, 'registered');
// Listen for tunnel requests and respond
ws.on('message', (data) => {
const msg = JSON.parse(data.toString());
if (msg.type === 'request') {
ws.send(JSON.stringify({
type: 'response',
id: msg.id,
status: 200,
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ hello: 'world', path: msg.path })
}));
}
});
// Make HTTP request through the tunnel
const res = await fetch(`${baseUrl}/tunnel/testapp/api/hello`);
assert.strictEqual(res.status, 200);
const body = await res.json();
assert.strictEqual(body.hello, 'world');
assert.strictEqual(body.path, '/api/hello');
ws.close();
await new Promise(r => setTimeout(r, 50));
});
it('should proxy POST requests with body', async () => {
const ws = connectTunnel();
await new Promise(r => ws.on('open', r));
ws.send(JSON.stringify({ type: 'register', name: 'postapp' }));
await waitMsg(ws, 'registered');
ws.on('message', (data) => {
const msg = JSON.parse(data.toString());
if (msg.type === 'request') {
assert.strictEqual(msg.method, 'POST');
ws.send(JSON.stringify({
type: 'response',
id: msg.id,
status: 201,
headers: { 'content-type': 'text/plain' },
body: 'Created'
}));
}
});
const res = await fetch(`${baseUrl}/tunnel/postapp/items`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'test' })
});
assert.strictEqual(res.status, 201);
assert.strictEqual(await res.text(), 'Created');
ws.close();
await new Promise(r => setTimeout(r, 50));
});
it('should return 502 for unregistered tunnel', async () => {
const res = await fetch(`${baseUrl}/tunnel/nonexistent/path`);
assert.strictEqual(res.status, 502);
});
it('should forward custom headers', async () => {
const ws = connectTunnel();
await new Promise(r => ws.on('open', r));
ws.send(JSON.stringify({ type: 'register', name: 'headerapp' }));
await waitMsg(ws, 'registered');
let receivedHeaders;
ws.on('message', (data) => {
const msg = JSON.parse(data.toString());
if (msg.type === 'request') {
receivedHeaders = msg.headers;
ws.send(JSON.stringify({
type: 'response',
id: msg.id,
status: 200,
headers: { 'x-custom-response': 'from-tunnel' },
body: 'ok'
}));
}
});
const res = await fetch(`${baseUrl}/tunnel/headerapp/`, {
headers: { 'X-Custom-Request': 'to-tunnel' }
});
assert.strictEqual(res.status, 200);
assert.strictEqual(res.headers.get('x-custom-response'), 'from-tunnel');
assert.strictEqual(receivedHeaders['x-custom-request'], 'to-tunnel');
ws.close();
await new Promise(r => setTimeout(r, 50));
});
});
describe('Credential passthrough (#530)', () => {
// Register a tunnel, echo each request's received headers back in
// the response body, and attach the given response headers — lets
// tests assert both directions of the credential flow.
function echoTunnel(ws, name, { passthrough, responseHeaders = {} } = {}) {
ws.send(JSON.stringify({ type: 'register', name, ...(passthrough !== undefined && { passthrough }) }));
ws.on('message', (data) => {
const msg = JSON.parse(data.toString());
if (msg.type === 'request') {
ws.send(JSON.stringify({
type: 'response',
id: msg.id,
status: 200,
headers: { 'content-type': 'application/json', ...responseHeaders },
body: JSON.stringify({ receivedHeaders: msg.headers })
}));
}
});
return waitMsg(ws, 'registered');
}
it('default registration strips credentials both ways (the security default)', async () => {
const ws = connectTunnel();
await new Promise(r => ws.on('open', r));
const ack = await echoTunnel(ws, 'noauth', {
responseHeaders: { 'set-cookie': 'leak=1; Path=/' }
});
assert.strictEqual(ack.passthrough, false,
'ack must state passthrough is off');
const res = await fetch(`${baseUrl}/tunnel/noauth/`, {
headers: { Cookie: 'session=secret', Authorization: 'Bearer visitor-token' }
});
assert.strictEqual(res.status, 200);
const { receivedHeaders } = await res.json();
assert.strictEqual(receivedHeaders.cookie, undefined,
'cookie must NOT reach the tunnel client by default');
assert.strictEqual(receivedHeaders.authorization, undefined,
'authorization must NOT reach the tunnel client by default');
// Feature-detect here too: on some undici versions get('set-cookie')
// is null even when Set-Cookie headers ARE present (only readable
// via getSetCookie), which would make a null assertion vacuous and
// could mask a regression in the default strip mode.
if (typeof res.headers.getSetCookie === 'function') {
assert.deepStrictEqual(res.headers.getSetCookie(), [],
'set-cookie from the tunnel client must NOT reach the visitor by default');
} else {
assert.strictEqual(res.headers.get('set-cookie'), null,
'set-cookie from the tunnel client must NOT reach the visitor by default');
}
ws.close();
await new Promise(r => setTimeout(r, 50));
});
it('passthrough registration forwards Cookie/Authorization in and Set-Cookie out', async () => {
const ws = connectTunnel();
await new Promise(r => ws.on('open', r));
const ack = await echoTunnel(ws, 'authapp', {
passthrough: true,
// Array value: JSON serialization preserves it and Fastify
// emits one Set-Cookie header per entry.
responseHeaders: { 'set-cookie': ['sess=abc; Path=/', 'csrf=xyz; Path=/'] }
});
assert.strictEqual(ack.passthrough, true, 'ack must confirm passthrough');
const res = await fetch(`${baseUrl}/tunnel/authapp/`, {
headers: { Cookie: 'session=secret', Authorization: 'Bearer visitor-token' }
});
assert.strictEqual(res.status, 200);
const { receivedHeaders } = await res.json();
assert.strictEqual(receivedHeaders.cookie, 'session=secret',
'cookie must reach the tunnel client with passthrough');
assert.strictEqual(receivedHeaders.authorization, 'Bearer visitor-token',
'authorization must reach the tunnel client with passthrough');
// Headers#getSetCookie() (Node ≥18.15) is the supported way to
// read multiple Set-Cookie values; get('set-cookie') behaviour
// varies by undici version and may expose only one value. Feature-
// detect: assert the full pair on the real API, degrade to a
// single-cookie assertion on older runtimes (engines floor bump
// tracked in #541).
const hasGetSetCookie = typeof res.headers.getSetCookie === 'function';
const setCookie = hasGetSetCookie
? res.headers.getSetCookie().join('; ')
: (res.headers.get('set-cookie') || '');
assert.ok(setCookie.includes('sess=abc'), `sess cookie must survive; got: ${setCookie}`);
if (hasGetSetCookie) {
assert.ok(setCookie.includes('csrf=xyz'), `csrf cookie must survive; got: ${setCookie}`);
}
ws.close();
await new Promise(r => setTimeout(r, 50));
});
it('passthrough strips the relay\'s own IdP cookies but keeps the service cookies (#530 session-hijack guard)', async () => {
const ws = connectTunnel();
await new Promise(r => ws.on('open', r));
await echoTunnel(ws, 'mixedcookies', { passthrough: true });
// Visitor carries BOTH a cookie for the tunnelled app AND the
// relay's own oidc session cookies (path=/, so a real browser
// would attach them on /tunnel/... too). The relay cookies must
// never reach the tunnel client; the app cookie must.
const res = await fetch(`${baseUrl}/tunnel/mixedcookies/`, {
headers: {
// `appsid` deliberately avoids the `_session` substring so the
// assertions below can test cookie *values* without collision.
Cookie: 'appsid=keep; _session=relay-secret; _session.sig=relay-sig; _interaction=flow',
},
});
assert.strictEqual(res.status, 200);
const { receivedHeaders } = await res.json();
const fwd = receivedHeaders.cookie || '';
assert.ok(fwd.includes('appsid=keep'),
`tunnelled-service cookie must survive; got: ${fwd}`);
assert.ok(!fwd.includes('relay-secret') && !fwd.includes('relay-sig'),
`relay _session cookies must NOT be forwarded; got: ${fwd}`);
assert.ok(!fwd.includes('_interaction'),
`relay _interaction cookies must NOT be forwarded; got: ${fwd}`);
ws.close();
await new Promise(r => setTimeout(r, 50));
});
it('passthrough never forwards Proxy-Authorization (relay-directed credential)', async () => {
const ws = connectTunnel();
await new Promise(r => ws.on('open', r));
await echoTunnel(ws, 'proxyauth', { passthrough: true });
const res = await fetch(`${baseUrl}/tunnel/proxyauth/`, {
headers: { 'Proxy-Authorization': 'Basic cmVsYXk6c2VjcmV0' }
});
assert.strictEqual(res.status, 200);
const { receivedHeaders } = await res.json();
assert.strictEqual(receivedHeaders['proxy-authorization'], undefined,
'proxy-authorization is addressed to the relay and must never be forwarded');
ws.close();
await new Promise(r => setTimeout(r, 50));
});
it('non-boolean passthrough values do not enable forwarding (strict === true)', async () => {
const ws = connectTunnel();
await new Promise(r => ws.on('open', r));
// "true" (string) must not opt a tunnel into credential forwarding.
const ack = await echoTunnel(ws, 'stringy', { passthrough: 'true' });
assert.strictEqual(ack.passthrough, false,
'string "true" must not enable passthrough');
const res = await fetch(`${baseUrl}/tunnel/stringy/`, {
headers: { Cookie: 'session=secret' }
});
const { receivedHeaders } = await res.json();
assert.strictEqual(receivedHeaders.cookie, undefined,
'credentials stay stripped for non-boolean opt-in values');
ws.close();
await new Promise(r => setTimeout(r, 50));
});
});
});