|
|
|
@ -1,7 +1,7 @@ |
|
|
|
import { OAuthError, OAuthErrorCode } from '@modelcontextprotocol/server'; |
|
|
|
import { lookup as dnsLookup } from 'node:dns'; |
|
|
|
import { BlockList, isIP, LookupFunction } from 'node:net'; |
|
|
|
import { Agent, fetch as undiciFetch, Response } from 'undici'; |
|
|
|
import { Agent, fetch as undiciFetch } from 'undici'; |
|
|
|
|
|
|
|
const BLOCKED_IP_RANGES = new BlockList(); |
|
|
|
|
|
|
|
@ -12,15 +12,28 @@ BLOCKED_IP_RANGES.addSubnet('100.64.0.0', 10, 'ipv4'); // Carrier-grade NAT |
|
|
|
BLOCKED_IP_RANGES.addSubnet('127.0.0.0', 8, 'ipv4'); // Loopback
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('169.254.0.0', 16, 'ipv4'); // Link-local & cloud metadata (169.254.169.254)
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('172.16.0.0', 12, 'ipv4'); // RFC 1918 private
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('192.0.0.0', 24, 'ipv4'); // IETF protocol assignments
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('192.0.2.0', 24, 'ipv4'); // Documentation
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('192.88.99.0', 24, 'ipv4'); // 6to4 relay anycast
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('192.168.0.0', 16, 'ipv4'); // RFC 1918 private
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('198.18.0.0', 15, 'ipv4'); // Benchmarking
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('198.51.100.0', 24, 'ipv4'); // Documentation
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('203.0.113.0', 24, 'ipv4'); // Documentation
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('224.0.0.0', 4, 'ipv4'); // Multicast
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('240.0.0.0', 4, 'ipv4'); |
|
|
|
BLOCKED_IP_RANGES.addSubnet('240.0.0.0', 4, 'ipv4'); // Reserved
|
|
|
|
|
|
|
|
// IPv6 reserved / private / loopback ranges
|
|
|
|
BLOCKED_IP_RANGES.addAddress('::', 'ipv6'); // Unspecified
|
|
|
|
BLOCKED_IP_RANGES.addAddress('::1', 'ipv6'); // Loopback
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('::', 96, 'ipv6'); // Unspecified, loopback & IPv4-compatible
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('::ffff:0:0:0', 96, 'ipv6'); // IPv4-translated
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('64:ff9b::', 96, 'ipv6'); // NAT64
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('64:ff9b:1::', 48, 'ipv6'); // Local-use NAT64
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('100::', 64, 'ipv6'); // Discard-only
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('2001::', 23, 'ipv6'); // IETF protocol assignments & Teredo
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('2001:db8::', 32, 'ipv6'); // Documentation
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('2002::', 16, 'ipv6'); // 6to4
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('fc00::', 7, 'ipv6'); // Unique local (ULA)
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('fe80::', 10, 'ipv6'); // Link-local
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('fec0::', 10, 'ipv6'); // Site-local
|
|
|
|
BLOCKED_IP_RANGES.addSubnet('ff00::', 8, 'ipv6'); // Multicast
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -76,13 +89,12 @@ export function assertSafeExternalUrl(rawUrl: string): URL { |
|
|
|
*/ |
|
|
|
export async function fetchJsonWithSsrfGuard<T>( |
|
|
|
rawUrl: string, |
|
|
|
maxBytes = 64 * 1024 |
|
|
|
maxBytes = 5 * 1024 |
|
|
|
): Promise<T> { |
|
|
|
let response: Response; |
|
|
|
const url = assertSafeExternalUrl(rawUrl); |
|
|
|
|
|
|
|
try { |
|
|
|
response = await undiciFetch(url, { |
|
|
|
const response = await undiciFetch(url, { |
|
|
|
dispatcher: ssrfSafeAgent, |
|
|
|
headers: { |
|
|
|
Accept: 'application/json' |
|
|
|
@ -90,26 +102,13 @@ export async function fetchJsonWithSsrfGuard<T>( |
|
|
|
redirect: 'error', |
|
|
|
signal: AbortSignal.timeout(5000) |
|
|
|
}); |
|
|
|
} catch (error) { |
|
|
|
if (error instanceof OAuthError) { |
|
|
|
throw error; |
|
|
|
} |
|
|
|
|
|
|
|
if (error instanceof Error && error.cause instanceof OAuthError) { |
|
|
|
throw error.cause; |
|
|
|
} |
|
|
|
|
|
|
|
throw new OAuthError( |
|
|
|
OAuthErrorCode.InvalidClientMetadata, |
|
|
|
`Failed to fetch JSON from ${url.toString()}` |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
if (!response.ok) { |
|
|
|
if (response.status !== 200) { |
|
|
|
await response.body?.cancel(); |
|
|
|
|
|
|
|
throw new OAuthError( |
|
|
|
OAuthErrorCode.InvalidClientMetadata, |
|
|
|
`Failed to fetch JSON from ${url.toString()}: ${response.status} ${response.statusText}` |
|
|
|
`Failed to fetch JSON from ${url.toString()}: ${response.status}` |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
@ -121,7 +120,6 @@ export async function fetchJsonWithSsrfGuard<T>( |
|
|
|
totalBytes += (chunk as Uint8Array).byteLength; |
|
|
|
|
|
|
|
if (totalBytes > maxBytes) { |
|
|
|
await response.body.cancel(); |
|
|
|
throw new OAuthError( |
|
|
|
OAuthErrorCode.InvalidClientMetadata, |
|
|
|
`Response from ${url.toString()} exceeds maximum allowed size of ${maxBytes} bytes` |
|
|
|
@ -132,12 +130,19 @@ export async function fetchJsonWithSsrfGuard<T>( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
return JSON.parse(Buffer.concat(chunks).toString('utf8')) as T; |
|
|
|
} catch { |
|
|
|
} catch (error) { |
|
|
|
if (error instanceof OAuthError) { |
|
|
|
throw error; |
|
|
|
} |
|
|
|
|
|
|
|
if (error instanceof Error && error.cause instanceof OAuthError) { |
|
|
|
throw error.cause; |
|
|
|
} |
|
|
|
|
|
|
|
throw new OAuthError( |
|
|
|
OAuthErrorCode.InvalidClientMetadata, |
|
|
|
'Invalid JSON response' |
|
|
|
`Failed to fetch JSON from ${url.toString()}` |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|