|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import http from 'node:http' |
| 5 | +import type { AddressInfo } from 'node:net' |
| 6 | +import { gzipSync } from 'node:zlib' |
| 7 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 8 | + |
| 9 | +vi.mock('@sim/security/dns', () => ({ |
| 10 | + resolveHostAddresses: vi.fn(), |
| 11 | + preferIpv4: (addresses: string[]) => addresses[0], |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/core/config/env-flags', () => ({ |
| 15 | + isHosted: false, |
| 16 | + getEgressAllowedHosts: () => undefined, |
| 17 | + getEgressAllowedIpRanges: () => undefined, |
| 18 | + isLegacyPrivateDatabaseAccessAllowed: () => false, |
| 19 | + getProxyUrl: () => undefined, |
| 20 | +})) |
| 21 | + |
| 22 | +import { |
| 23 | + createPinnedConnectionPool, |
| 24 | + secureFetchWithPinnedIP, |
| 25 | +} from '@/lib/core/security/input-validation.server' |
| 26 | + |
| 27 | +const servers: http.Server[] = [] |
| 28 | + |
| 29 | +afterEach(() => { |
| 30 | + for (const server of servers.splice(0)) { |
| 31 | + server.closeAllConnections() |
| 32 | + server.close() |
| 33 | + } |
| 34 | +}) |
| 35 | + |
| 36 | +/** Starts a loopback server that counts the TCP connections it accepts. */ |
| 37 | +async function startServer(handler: http.RequestListener) { |
| 38 | + const server = http.createServer(handler) |
| 39 | + servers.push(server) |
| 40 | + let connections = 0 |
| 41 | + server.on('connection', () => connections++) |
| 42 | + await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve)) |
| 43 | + return { |
| 44 | + origin: `http://127.0.0.1:${(server.address() as AddressInfo).port}`, |
| 45 | + connections: () => connections, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +describe('secureFetchWithPinnedIP connection reuse', () => { |
| 50 | + it('reuses one pinned connection across requests that share a pool', async () => { |
| 51 | + const server = await startServer((_req, res) => res.end('ok')) |
| 52 | + const pool = createPinnedConnectionPool() |
| 53 | + try { |
| 54 | + for (let index = 0; index < 3; index++) { |
| 55 | + const response = await secureFetchWithPinnedIP(server.origin, '127.0.0.1', { |
| 56 | + profile: 'configuredEndpoint', |
| 57 | + connectionPool: pool, |
| 58 | + }) |
| 59 | + await expect(response.text()).resolves.toBe('ok') |
| 60 | + } |
| 61 | + expect(server.connections()).toBe(1) |
| 62 | + } finally { |
| 63 | + pool.destroy() |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + it('opens a fresh connection per request without a pool', async () => { |
| 68 | + const server = await startServer((_req, res) => res.end('ok')) |
| 69 | + for (let index = 0; index < 2; index++) { |
| 70 | + const response = await secureFetchWithPinnedIP(server.origin, '127.0.0.1', { |
| 71 | + profile: 'configuredEndpoint', |
| 72 | + }) |
| 73 | + await response.text() |
| 74 | + } |
| 75 | + expect(server.connections()).toBe(2) |
| 76 | + }) |
| 77 | + |
| 78 | + it('never shares an agent between different pinned addresses', () => { |
| 79 | + const pool = createPinnedConnectionPool() |
| 80 | + try { |
| 81 | + const first = pool.agent(true, 'api.example.com', 443, '203.0.113.1') |
| 82 | + expect(pool.agent(true, 'api.example.com', 443, '203.0.113.1')).toBe(first) |
| 83 | + expect(pool.agent(true, 'api.example.com', 443, '203.0.113.2')).not.toBe(first) |
| 84 | + expect(pool.agent(true, 'other.example.com', 443, '203.0.113.1')).not.toBe(first) |
| 85 | + } finally { |
| 86 | + pool.destroy() |
| 87 | + } |
| 88 | + }) |
| 89 | +}) |
| 90 | + |
| 91 | +describe('secureFetchWithPinnedIP compressed responses', () => { |
| 92 | + it('asks for compression only when requested and returns the decoded body', async () => { |
| 93 | + const encodings: (string | undefined)[] = [] |
| 94 | + const server = await startServer((req, res) => { |
| 95 | + encodings.push(req.headers['accept-encoding']) |
| 96 | + if (req.headers['accept-encoding']?.includes('gzip')) { |
| 97 | + res.writeHead(200, { 'Content-Encoding': 'gzip' }) |
| 98 | + res.end(gzipSync(Buffer.from('{"ok":true}'))) |
| 99 | + } else { |
| 100 | + res.end('{"ok":true}') |
| 101 | + } |
| 102 | + }) |
| 103 | + const compressed = await secureFetchWithPinnedIP(server.origin, '127.0.0.1', { |
| 104 | + profile: 'configuredEndpoint', |
| 105 | + acceptCompressed: true, |
| 106 | + }) |
| 107 | + await expect(compressed.json()).resolves.toEqual({ ok: true }) |
| 108 | + const plain = await secureFetchWithPinnedIP(server.origin, '127.0.0.1', { |
| 109 | + profile: 'configuredEndpoint', |
| 110 | + }) |
| 111 | + await expect(plain.json()).resolves.toEqual({ ok: true }) |
| 112 | + expect(encodings).toEqual(['gzip, deflate, br', undefined]) |
| 113 | + }) |
| 114 | +}) |
| 115 | + |
| 116 | +describe('secureFetchWithPinnedIP address pinning', () => { |
| 117 | + it.each([ |
| 118 | + ['a fresh agent', undefined], |
| 119 | + ['a pooled agent', createPinnedConnectionPool()], |
| 120 | + ])('pins the address on the request itself with %s', async (_label, pool) => { |
| 121 | + const server = await startServer((_req, res) => res.end('ok')) |
| 122 | + const request = vi.spyOn(http, 'request') |
| 123 | + try { |
| 124 | + const response = await secureFetchWithPinnedIP( |
| 125 | + server.origin.replace('127.0.0.1', 'localhost'), |
| 126 | + '127.0.0.1', |
| 127 | + { |
| 128 | + profile: 'configuredEndpoint', |
| 129 | + connectionPool: pool, |
| 130 | + } |
| 131 | + ) |
| 132 | + await response.text() |
| 133 | + const options = request.mock.calls[0]?.[0] as http.RequestOptions |
| 134 | + const lookup = options.lookup as unknown as ( |
| 135 | + hostname: string, |
| 136 | + options: object, |
| 137 | + callback: (error: Error | null, address: string, family: number) => void |
| 138 | + ) => void |
| 139 | + const resolved = await new Promise<string>((resolve) => |
| 140 | + lookup('localhost', {}, (_error, address) => resolve(address)) |
| 141 | + ) |
| 142 | + expect(resolved).toBe('127.0.0.1') |
| 143 | + } finally { |
| 144 | + request.mockRestore() |
| 145 | + pool?.destroy() |
| 146 | + } |
| 147 | + }) |
| 148 | +}) |
0 commit comments