@open-mercato/web-research-browser 0.9.1-develop.7574.1.837288eec1 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -7
- package/src/__tests__/egressProxy.test.ts +121 -33
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/web-research-browser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -38,16 +38,16 @@
|
|
|
38
38
|
"@open-mercato/web-research": "^0.6.0"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@open-mercato/web-research-serp": "0.
|
|
41
|
+
"@open-mercato/web-research-serp": "0.10.0",
|
|
42
42
|
"playwright": "^1.40.0",
|
|
43
|
-
"zod": "^4.
|
|
43
|
+
"zod": "^4.6.6"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@open-mercato/web-research": "0.
|
|
46
|
+
"@open-mercato/web-research": "0.10.0",
|
|
47
47
|
"@types/jest": "^30.0.0",
|
|
48
48
|
"@types/node": "^26.0.1",
|
|
49
49
|
"esbuild": "^0.28.1",
|
|
50
|
-
"glob": "^
|
|
50
|
+
"glob": "^14.0.6",
|
|
51
51
|
"jest": "^30.4.2",
|
|
52
52
|
"ts-jest": "^29.4.11",
|
|
53
53
|
"typescript": "^6.0.3"
|
|
@@ -59,6 +59,5 @@
|
|
|
59
59
|
"type": "git",
|
|
60
60
|
"url": "https://github.com/open-mercato/open-mercato",
|
|
61
61
|
"directory": "packages/web-research-browser"
|
|
62
|
-
}
|
|
63
|
-
"stableVersion": "0.9.0"
|
|
62
|
+
}
|
|
64
63
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createServer as createHttpServer, request as sendHttp, type IncomingHttpHeaders, type Server } from 'node:http'
|
|
2
|
-
import { connect as netConnect, createServer as createTcpServer, type AddressInfo, type Socket } from 'node:net'
|
|
2
|
+
import { connect as netConnect, createServer as createTcpServer, isIP, type AddressInfo, type Socket } from 'node:net'
|
|
3
3
|
import type { LookupFn } from '@open-mercato/web-research'
|
|
4
4
|
import { pinnedConnect, startEgressProxy, type EgressDialTarget, type EgressProxy } from '../egressProxy'
|
|
5
5
|
|
|
@@ -8,6 +8,7 @@ const RESET_IP = '93.184.216.35'
|
|
|
8
8
|
const DEAD_IP = '93.184.216.36'
|
|
9
9
|
const MALFORMED_IP = '93.184.216.37'
|
|
10
10
|
const PUBLIC_IPV6 = '2606:2800:220:1:248:1893:25c8:1946'
|
|
11
|
+
const PUBLIC_6TO4 = '2002:808:808::1'
|
|
11
12
|
|
|
12
13
|
type Recorded = { host: string | undefined; url: string | undefined; headers: IncomingHttpHeaders }
|
|
13
14
|
|
|
@@ -15,6 +16,7 @@ type Harness = {
|
|
|
15
16
|
proxy: EgressProxy
|
|
16
17
|
proxyPort: number
|
|
17
18
|
dials: EgressDialTarget[]
|
|
19
|
+
upstreamSockets: Socket[]
|
|
18
20
|
privateConnections: () => number
|
|
19
21
|
publicRequests: Recorded[]
|
|
20
22
|
publicSockets: Set<Socket>
|
|
@@ -30,6 +32,13 @@ async function listen(server: Server | ReturnType<typeof createTcpServer>): Prom
|
|
|
30
32
|
return (server.address() as AddressInfo).port
|
|
31
33
|
}
|
|
32
34
|
|
|
35
|
+
async function waitUntil(condition: () => boolean, timeoutMs = 5_000): Promise<void> {
|
|
36
|
+
const deadline = Date.now() + timeoutMs
|
|
37
|
+
while (!condition() && Date.now() < deadline) {
|
|
38
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 1))
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
33
42
|
function closeAll(): Promise<void[]> {
|
|
34
43
|
const closing = [...servers, ...tcpServers].map(
|
|
35
44
|
(server) =>
|
|
@@ -101,6 +110,8 @@ async function buildHarness(): Promise<Harness> {
|
|
|
101
110
|
'redirect.test': PUBLIC_IP,
|
|
102
111
|
'private.test': '10.0.0.5',
|
|
103
112
|
'metadata.test': '169.254.169.254',
|
|
113
|
+
'local-translation.test': '64:ff9b:1::a00:1',
|
|
114
|
+
'private-6to4.test': '2002:a00:1::1',
|
|
104
115
|
'reset.test': RESET_IP,
|
|
105
116
|
'dead.test': DEAD_IP,
|
|
106
117
|
'malformed.test': MALFORMED_IP,
|
|
@@ -110,16 +121,17 @@ async function buildHarness(): Promise<Harness> {
|
|
|
110
121
|
if (hostname === 'flip.test') {
|
|
111
122
|
const address = flipAnswers.shift()
|
|
112
123
|
if (!address) throw new Error('no answer')
|
|
113
|
-
return [{ address, family:
|
|
124
|
+
return [{ address, family: isIP(address) }]
|
|
114
125
|
}
|
|
115
126
|
const address = table[hostname]
|
|
116
127
|
if (!address) throw new Error(`ENOTFOUND ${hostname}`)
|
|
117
|
-
return [{ address, family:
|
|
128
|
+
return [{ address, family: isIP(address) }]
|
|
118
129
|
}
|
|
119
130
|
|
|
120
131
|
const dials: EgressDialTarget[] = []
|
|
132
|
+
const upstreamSockets: Socket[] = []
|
|
121
133
|
const portFor = (addresses: readonly string[]): number => {
|
|
122
|
-
if (addresses.length === 1 &&
|
|
134
|
+
if (addresses.length === 1 && [PUBLIC_IP, PUBLIC_IPV6, PUBLIC_6TO4].includes(addresses[0])) return publicPort
|
|
123
135
|
if (addresses.length === 1 && addresses[0] === RESET_IP) return resetPort
|
|
124
136
|
if (addresses.length === 1 && addresses[0] === DEAD_IP) return deadPort
|
|
125
137
|
if (addresses.length === 1 && addresses[0] === MALFORMED_IP) return malformedPort
|
|
@@ -129,13 +141,16 @@ async function buildHarness(): Promise<Harness> {
|
|
|
129
141
|
lookup,
|
|
130
142
|
connect: (target) => {
|
|
131
143
|
dials.push(target)
|
|
132
|
-
|
|
144
|
+
const upstream = netConnect({ host: '127.0.0.1', port: portFor(target.addresses) })
|
|
145
|
+
upstreamSockets.push(upstream)
|
|
146
|
+
return upstream
|
|
133
147
|
},
|
|
134
148
|
})
|
|
135
149
|
return {
|
|
136
150
|
proxy,
|
|
137
151
|
proxyPort: Number(new URL(proxy.url).port),
|
|
138
152
|
dials,
|
|
153
|
+
upstreamSockets,
|
|
139
154
|
privateConnections: () => privateHits,
|
|
140
155
|
publicRequests,
|
|
141
156
|
publicSockets,
|
|
@@ -229,6 +244,17 @@ describe('browser egress proxy (#7218)', () => {
|
|
|
229
244
|
expect(harness.dials).toEqual([])
|
|
230
245
|
})
|
|
231
246
|
|
|
247
|
+
it.each([
|
|
248
|
+
'[64:ff9b:1::a00:1]',
|
|
249
|
+
'[2002:a00:1::1]',
|
|
250
|
+
'local-translation.test',
|
|
251
|
+
'private-6to4.test',
|
|
252
|
+
])('refuses CONNECT and HTTP egress to %s before dialling', async (host) => {
|
|
253
|
+
expect(await connectStatus(`${host}:443`)).toBe(403)
|
|
254
|
+
expect((await proxiedRequest(`http://${host}/secret`)).status).toBe(403)
|
|
255
|
+
expect(harness.dials).toEqual([])
|
|
256
|
+
})
|
|
257
|
+
|
|
232
258
|
it.each(['a@b:1', 'public.test', 'public.test:0', 'public.test:70000', '[::1:443', 'public.test:443/x'])(
|
|
233
259
|
'answers the malformed authority %s with 400',
|
|
234
260
|
async (authority) => {
|
|
@@ -254,11 +280,11 @@ describe('browser egress proxy (#7218)', () => {
|
|
|
254
280
|
expect(harness.dials[0]?.port).toBe(80)
|
|
255
281
|
})
|
|
256
282
|
|
|
257
|
-
it('dials
|
|
258
|
-
const { status, socket } = await openConnect(`[${
|
|
283
|
+
it.each([PUBLIC_IPV6, PUBLIC_6TO4])('dials public IPv6 %s through the pinned dial', async (address) => {
|
|
284
|
+
const { status, socket } = await openConnect(`[${address}]:443`)
|
|
259
285
|
socket.destroy()
|
|
260
286
|
expect(status).toBe(200)
|
|
261
|
-
expect(harness.dials).toEqual([{ host:
|
|
287
|
+
expect(harness.dials).toEqual([{ host: address, port: 443, addresses: [address] }])
|
|
262
288
|
})
|
|
263
289
|
|
|
264
290
|
it('re-resolves every connection, so a host that turns private is refused', async () => {
|
|
@@ -368,11 +394,14 @@ describe('browser egress proxy (#7218)', () => {
|
|
|
368
394
|
expect(tunnel.status).toBe(200)
|
|
369
395
|
tunnel.socket.on('error', () => undefined)
|
|
370
396
|
const clientClosed = new Promise<void>((resolve) => tunnel.socket.once('close', () => resolve()))
|
|
397
|
+
await waitUntil(() => harness.publicSockets.size === 1)
|
|
398
|
+
expect(harness.publicSockets.size).toBe(1)
|
|
371
399
|
|
|
372
400
|
await harness.proxy.close()
|
|
401
|
+
expect(harness.upstreamSockets.map((socket) => socket.destroyed)).toEqual([true])
|
|
373
402
|
await clientClosed
|
|
374
403
|
|
|
375
|
-
await
|
|
404
|
+
await waitUntil(() => harness.publicSockets.size === 0)
|
|
376
405
|
expect(harness.publicSockets.size).toBe(0)
|
|
377
406
|
await expect(
|
|
378
407
|
new Promise((resolve, reject) => {
|
|
@@ -387,6 +416,59 @@ describe('browser egress proxy (#7218)', () => {
|
|
|
387
416
|
})
|
|
388
417
|
})
|
|
389
418
|
|
|
419
|
+
const REQUEST_FORMS = [
|
|
420
|
+
['CONNECT', 'CONNECT vetting.test:443 HTTP/1.1\r\nHost: vetting.test:443\r\n\r\n'],
|
|
421
|
+
['absolute-form', 'GET http://vetting.test/ HTTP/1.1\r\nHost: vetting.test\r\n\r\n'],
|
|
422
|
+
]
|
|
423
|
+
|
|
424
|
+
const CLIENT_DEPARTURES: Array<[string, (client: Socket) => void]> = [
|
|
425
|
+
['disconnect', (client) => client.destroy()],
|
|
426
|
+
['reset', (client) => client.resetAndDestroy()],
|
|
427
|
+
]
|
|
428
|
+
|
|
429
|
+
type LookupRecords = Awaited<ReturnType<LookupFn>>
|
|
430
|
+
|
|
431
|
+
type DeferredLookupProxy = {
|
|
432
|
+
proxy: EgressProxy
|
|
433
|
+
dials: EgressDialTarget[]
|
|
434
|
+
started: Promise<void>
|
|
435
|
+
answer: (records: LookupRecords) => void
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function proxyRoundTrip(proxy: EgressProxy): Promise<void> {
|
|
439
|
+
return new Promise((resolve, reject) => {
|
|
440
|
+
const barrier = netConnect({ host: '127.0.0.1', port: Number(new URL(proxy.url).port) })
|
|
441
|
+
barrier.once('error', reject)
|
|
442
|
+
barrier.once('close', () => reject(new Error('[internal] barrier closed without a response')))
|
|
443
|
+
barrier.once('data', () => {
|
|
444
|
+
barrier.destroy()
|
|
445
|
+
resolve()
|
|
446
|
+
})
|
|
447
|
+
barrier.write('GET /barrier HTTP/1.1\r\nHost: barrier\r\nConnection: close\r\n\r\n')
|
|
448
|
+
})
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
async function startProxyWithDeferredLookup(): Promise<DeferredLookupProxy> {
|
|
452
|
+
const dials: EgressDialTarget[] = []
|
|
453
|
+
let resolveLookup: (records: LookupRecords) => void = () => undefined
|
|
454
|
+
let lookupStarted: () => void = () => undefined
|
|
455
|
+
const started = new Promise<void>((resolve) => {
|
|
456
|
+
lookupStarted = resolve
|
|
457
|
+
})
|
|
458
|
+
const proxy = await startEgressProxy({
|
|
459
|
+
lookup: () =>
|
|
460
|
+
new Promise((resolve) => {
|
|
461
|
+
resolveLookup = resolve
|
|
462
|
+
lookupStarted()
|
|
463
|
+
}),
|
|
464
|
+
connect: (dialTarget) => {
|
|
465
|
+
dials.push(dialTarget)
|
|
466
|
+
return netConnect({ host: '127.0.0.1', port: 9 })
|
|
467
|
+
},
|
|
468
|
+
})
|
|
469
|
+
return { proxy, dials, started, answer: (records) => resolveLookup(records) }
|
|
470
|
+
}
|
|
471
|
+
|
|
390
472
|
describe('pinned dial and vetting limits (#7218)', () => {
|
|
391
473
|
it('connects the default dial to the vetted address without resolving the hostname', async () => {
|
|
392
474
|
const target = createTcpServer((socket) => socket.end())
|
|
@@ -443,38 +525,44 @@ describe('pinned dial and vetting limits (#7218)', () => {
|
|
|
443
525
|
}
|
|
444
526
|
})
|
|
445
527
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
})
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
}
|
|
528
|
+
describe.each(CLIENT_DEPARTURES)('when the client leaves by %s', (_departure, leave) => {
|
|
529
|
+
it.each(REQUEST_FORMS)('does not dial for a client that left during vetting (%s)', async (_form, request) => {
|
|
530
|
+
const { proxy, dials, started, answer } = await startProxyWithDeferredLookup()
|
|
531
|
+
try {
|
|
532
|
+
const client = netConnect({ host: '127.0.0.1', port: Number(new URL(proxy.url).port) })
|
|
533
|
+
client.on('error', () => undefined)
|
|
534
|
+
client.write(request)
|
|
535
|
+
await started
|
|
536
|
+
const clientClosed = new Promise<void>((resolve) => client.once('close', () => resolve()))
|
|
537
|
+
leave(client)
|
|
538
|
+
await clientClosed
|
|
539
|
+
await proxyRoundTrip(proxy)
|
|
540
|
+
answer([{ address: PUBLIC_IP, family: 4 }])
|
|
541
|
+
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
542
|
+
|
|
543
|
+
expect(dials).toEqual([])
|
|
544
|
+
} finally {
|
|
545
|
+
await proxy.close()
|
|
546
|
+
}
|
|
463
547
|
})
|
|
548
|
+
})
|
|
549
|
+
|
|
550
|
+
it.each(REQUEST_FORMS)('does not dial a request still being vetted when the proxy closes (%s)', async (_form, request) => {
|
|
551
|
+
const { proxy, dials, started, answer } = await startProxyWithDeferredLookup()
|
|
552
|
+
const client = netConnect({ host: '127.0.0.1', port: Number(new URL(proxy.url).port) })
|
|
553
|
+
client.on('error', () => undefined)
|
|
464
554
|
try {
|
|
465
|
-
|
|
466
|
-
client.on('error', () => undefined)
|
|
467
|
-
client.write('CONNECT leaving.test:443 HTTP/1.1\r\nHost: leaving.test:443\r\n\r\n')
|
|
555
|
+
client.write(request)
|
|
468
556
|
await started
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
await clientClosed
|
|
472
|
-
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
557
|
+
|
|
558
|
+
const closing = proxy.close()
|
|
473
559
|
answer([{ address: PUBLIC_IP, family: 4 }])
|
|
560
|
+
await closing
|
|
474
561
|
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
475
562
|
|
|
476
563
|
expect(dials).toEqual([])
|
|
477
564
|
} finally {
|
|
565
|
+
client.destroy()
|
|
478
566
|
await proxy.close()
|
|
479
567
|
}
|
|
480
568
|
})
|