The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
Honor Node all-address lookup callbacks in provider transport (#712)
Max Baluev5a5aca0a4352
diff --git a/backend/src/lib/providerUrl.test.ts b/backend/src/lib/providerUrl.test.tsindex 3b90c1db3..ebfd2896e 100644--- a/backend/src/lib/providerUrl.test.ts+++ b/backend/src/lib/providerUrl.test.ts@@ -1,5 +1,21 @@-import { describe, expect, it } from 'vitest'-import { isBlockedAddress } from './providerUrl.js'+import { afterEach, describe, expect, it, vi } from 'vitest'+import { EventEmitter } from 'node:events'+import { createServer, type IncomingMessage } from 'node:http'+import { request as httpsRequest } from 'node:https'+import { lookup as dnsLookup } from 'node:dns/promises'+import type { LookupAddress, LookupAllOptions } from 'node:dns'+import { getDefaultAutoSelectFamily, type LookupFunction } from 'node:net'+import { env } from '../config/env.js'+import { isBlockedAddress, providerFetch, readBodyCapped } from './providerUrl.js'++vi.mock('node:dns/promises', () => ({ lookup: vi.fn() }))+vi.mock('node:https', () => ({ request: vi.fn() }))+vi.mock('../config/env.js', () => ({ env: { PROVIDER_ALLOW_INSECURE_URLS: false } }))++afterEach(() => {+ vi.resetAllMocks()+ env.PROVIDER_ALLOW_INSECURE_URLS = false+})describe('isBlockedAddress', () => {it('judges every spelling of an IPv4-carrying IPv6 address as that IPv4', () => {@@ -29,3 +45,86 @@ describe('isBlockedAddress', () => {expect(isBlockedAddress('8.8.8.8')).toBe(false)})})+++describe('providerFetch DNS callback', () => {+ // Select the all-address overload used by the guarded connection lookup.+ const lookupAll: (hostname: string, options: LookupAllOptions) => Promise<LookupAddress[]> = dnsLookup+ // Drive the exact hook installed on HTTPS requests, without dialing fixture addresses.+ async function connect(all: boolean, host = 'provider.example') {+ let resolved: { address: unknown; family?: number } | undefined+ vi.mocked(httpsRequest).mockImplementation(((+ options: { hostname: string; lookup: LookupFunction },+ onResponse: (response: IncomingMessage) => void,+ ) => {+ const request = Object.assign(new EventEmitter(), {+ write: vi.fn(), destroy: vi.fn(),+ end: () => options.lookup(options.hostname, { all }, (error, address, family) => {+ if (error) request.emit('error', error)+ else {+ resolved = { address, family }+ onResponse({ statusCode: 200, headers: {} } as IncomingMessage)+ }+ }),+ })+ return request+ }) as unknown as typeof httpsRequest)+ await providerFetch(`https://${host}`)+ return resolved+ }++ it('returns every validated address for all:true and the scalar shape otherwise', async () => {+ const entries = [{ address: '1.1.1.1', family: 4 }, { address: '2606:4700:4700::1111', family: 6 }]+ for (const all of [true, false]) {+ vi.mocked(lookupAll).mockResolvedValue(entries)+ expect(await connect(all)).toEqual(all+ ? { address: entries, family: undefined }+ : { address: entries[0]!.address, family: 4 })+ }+ vi.mocked(dnsLookup).mockClear()+ for (const [host, address, family] of [['1.1.1.1', '1.1.1.1', 4], ['[2606:4700:4700::1111]', '2606:4700:4700::1111', 6]] as const) {+ for (const all of [true, false]) expect(await connect(all, host)).toEqual(all+ ? { address: [{ address, family }], family: undefined }+ : { address, family })+ }+ expect(dnsLookup).not.toHaveBeenCalled()+ })++ it('refuses the entire answer set when any address is blocked, in both callback modes', async () => {+ for (const all of [true, false]) {+ for (const entries of [+ [{ address: '1.1.1.1', family: 4 }, { address: '127.0.0.1', family: 4 }],+ [{ address: '2606:4700:4700::1111', family: 6 }, { address: '::ffff:a9fe:a9fe', family: 6 }],+ [],+ ]) {+ vi.mocked(lookupAll).mockResolvedValue(entries)+ await expect(connect(all)).rejects.toMatchObject({ code: 'BLOCKED_ADDRESS' })+ }+ for (const host of ['127.0.0.1', '[::1]']) {+ await expect(connect(all, host)).rejects.toMatchObject({ code: 'BLOCKED_ADDRESS' })+ }+ vi.mocked(dnsLookup).mockRejectedValue(new Error('fixture resolution failed'))+ await expect(connect(all)).rejects.toMatchObject({ code: 'DNS_FAILED' })+ }+ })++ it('works with a real Node socket using default family autoselection', async () => {+ expect(getDefaultAutoSelectFamily()).toBe(true)+ // The stock development flag is only for this isolated loopback HTTP fixture.+ env.PROVIDER_ALLOW_INSECURE_URLS = true+ vi.mocked(lookupAll).mockResolvedValue([{ address: '127.0.0.1', family: 4 }])+ const server = createServer((_request, response) => response.end('original provider response'))+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))+ try {+ const address = server.address()+ if (!address || typeof address === 'string') throw new Error('fixture did not bind TCP')+ const response = await providerFetch(`http://provider.example:${address.port}`)+ expect(response.status).toBe(200)+ expect(await readBodyCapped(response)).toBe('original provider response')+ expect(dnsLookup).toHaveBeenCalledWith('provider.example', { all: true, verbatim: true })+ } finally {+ server.closeAllConnections()+ await new Promise<void>((resolve) => server.close(() => resolve()))+ }+ })+})diff --git a/backend/src/lib/providerUrl.ts b/backend/src/lib/providerUrl.tsindex 7c62b71a2..61e106163 100644--- a/backend/src/lib/providerUrl.ts+++ b/backend/src/lib/providerUrl.ts@@ -16,7 +16,7 @@*/import { request as httpRequest, type IncomingMessage } from 'node:http'import { request as httpsRequest } from 'node:https'-import { isIP } from 'node:net'+import { isIP, type LookupFunction } from 'node:net'import { lookup as dnsLookup } from 'node:dns/promises'import { env } from '../config/env.js'@@ -292,22 +292,20 @@ export function providerFetch(target: string, init: ProviderRequestInit = {}): P})}-/** Matches Node's `net.LookupFunction` callback: `address` is required, so errors pass an empty one. */-type LookupCallback = (err: NodeJS.ErrnoException | null, address: string, family: number) => void-/*** `dns.lookup`-shaped hook that refuses blocked addresses.** Node calls this with the hostname at connect time; whatever it returns is what the socket dials.* Refusing here means a rebinding attack has nothing to rebind to.*/-function guardedLookup(hostname: string, options: unknown, callback: LookupCallback): void {+const guardedLookup: LookupFunction = (hostname, options, callback) => {if (isIP(hostname)) {if (refuseAddress(hostname)) {callback(new ProviderUrlError(REASONS.BLOCKED_ADDRESS, 'BLOCKED_ADDRESS') as NodeJS.ErrnoException, '', 0)return}- callback(null, hostname, isIP(hostname))+ if (options.all) callback(null, [{ address: hostname, family: isIP(hostname) }])+ else callback(null, hostname, isIP(hostname))return}dnsLookup(hostname, { all: true, verbatim: true })@@ -320,7 +318,9 @@ function guardedLookup(hostname: string, options: unknown, callback: LookupCallbcallback(new ProviderUrlError(REASONS.BLOCKED_ADDRESS, 'BLOCKED_ADDRESS') as NodeJS.ErrnoException, '', 0)return}- callback(null, usable.address, usable.family)+ // Node's family autoselection asks for all addresses, not the scalar lookup result.+ if (options.all) callback(null, entries)+ else callback(null, usable.address, usable.family)}).catch(() => callback(new ProviderUrlError(REASONS.DNS_FAILED, 'DNS_FAILED') as NodeJS.ErrnoException, '', 0))}