|
1 | 1 | import dns from 'dns/promises' |
2 | 2 | import http from 'http' |
3 | 3 | import https from 'https' |
| 4 | +import type { LookupFunction } from 'net' |
4 | 5 | import { createLogger } from '@sim/logger' |
5 | 6 | import * as ipaddr from 'ipaddr.js' |
6 | 7 |
|
@@ -907,26 +908,28 @@ export async function secureFetchWithPinnedIP( |
907 | 908 | const isIPv6 = resolvedIP.includes(':') |
908 | 909 | const family = isIPv6 ? 6 : 4 |
909 | 910 |
|
910 | | - const agentOptions = { |
911 | | - lookup: ( |
912 | | - _hostname: string, |
913 | | - _options: unknown, |
914 | | - callback: (err: NodeJS.ErrnoException | null, address: string, family: number) => void |
915 | | - ) => { |
| 911 | + const lookup: LookupFunction = (_hostname, options, callback) => { |
| 912 | + if (options.all) { |
| 913 | + callback(null, [{ address: resolvedIP, family }]) |
| 914 | + } else { |
916 | 915 | callback(null, resolvedIP, family) |
917 | | - }, |
| 916 | + } |
918 | 917 | } |
919 | 918 |
|
920 | | - const agent = isHttps |
921 | | - ? new https.Agent(agentOptions as https.AgentOptions) |
922 | | - : new http.Agent(agentOptions as http.AgentOptions) |
| 919 | + const agentOptions: http.AgentOptions = { lookup } |
| 920 | + |
| 921 | + const agent = isHttps ? new https.Agent(agentOptions) : new http.Agent(agentOptions) |
| 922 | + |
| 923 | + // Remove accept-encoding since Node.js http/https doesn't auto-decompress |
| 924 | + // Headers are lowercase due to Web Headers API normalization in executeToolRequest |
| 925 | + const { 'accept-encoding': _, ...sanitizedHeaders } = options.headers ?? {} |
923 | 926 |
|
924 | 927 | const requestOptions: http.RequestOptions = { |
925 | 928 | hostname: parsed.hostname, |
926 | 929 | port, |
927 | 930 | path: parsed.pathname + parsed.search, |
928 | 931 | method: options.method || 'GET', |
929 | | - headers: options.headers || {}, |
| 932 | + headers: sanitizedHeaders, |
930 | 933 | agent, |
931 | 934 | timeout: options.timeout || 30000, |
932 | 935 | } |
|
0 commit comments