Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(api): proxyUrl for residential/custom proxy egress on the API block#5867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6361b31
feat(api): add proxyUrl for residential/custom proxy egress on the AP…
mzxchandra 3d3b9e7
docs(api): document the Proxy URL advanced field and steer proxy cred…
waleedlatif1 d9b9440
fix(api): reject loopback/private proxy hosts unconditionally, closin…
waleedlatif1 ec633dc
chore(api): tighten proxy-path inline comments
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -5,6 +5,8 @@ import type { LookupFunction } from 'net' | ||
| import { createLogger } from '@sim/logger' | ||
| import { toError } from '@sim/utils/errors' | ||
| import { omit } from '@sim/utils/object' | ||
| import { HttpProxyAgent } from 'http-proxy-agent' | ||
| import { HttpsProxyAgent } from 'https-proxy-agent' | ||
| import * as ipaddr from 'ipaddr.js' | ||
| import { Agent, type RequestInit as UndiciRequestInit, fetch as undiciFetch } from 'undici' | ||
| import { isHosted, isPrivateDatabaseHostsAllowed } from '@/lib/core/config/env-flags' | ||
| @@ -148,6 +150,71 @@ export async function validateUrlWithDNS( | ||
| } | ||
| } | ||
| /** | ||
| * Result of validating a user-supplied HTTP proxy URL. | ||
| */ | ||
| export interface ProxyValidationResult { | ||
| isValid: boolean | ||
| /** Proxy URL with hostname rewritten to the resolved IP (creds/port preserved) to pin the proxy connection. */ | ||
| pinnedProxyUrl?: string | ||
| error?: string | ||
| } | ||
| /** | ||
| * Validates a user-supplied HTTP proxy URL and returns an IP-pinned form. | ||
| * | ||
| * When a request routes through a proxy, the TCP connection targets the proxy | ||
| * host (the proxy resolves the destination), so target-IP pinning no longer | ||
| * governs egress and the proxy URL becomes the SSRF surface. This function: | ||
| * 1. Enforces the `http:` scheme (raw TCP to the proxy, no TLS-to-proxy SNI to | ||
| * reconcile, so the host can be safely rewritten to an IP). | ||
| * 2. Resolves the proxy host's DNS and blocks private/reserved/loopback IPs via | ||
| * {@link validateUrlWithDNS}. | ||
| * 3. Pins the connection by rewriting the hostname to the resolved IP while | ||
| * preserving credentials/port, closing the DNS-rebinding (TOCTOU) window. | ||
| * | ||
| * @param proxyUrl - The proxy URL (e.g. `http://user:pass@host:port`) | ||
| */ | ||
| export async function validateAndPinProxyUrl( | ||
| proxyUrl: string | null | undefined | ||
| ): Promise<ProxyValidationResult> { | ||
| if (!proxyUrl || typeof proxyUrl !== 'string') { | ||
| return { isValid: false, error: 'proxyUrl must be a string' } | ||
| } | ||
| let parsed: URL | ||
| try { | ||
| parsed = new URL(proxyUrl) | ||
| } catch { | ||
| return { isValid: false, error: 'proxyUrl must be a valid URL' } | ||
| } | ||
| if (parsed.protocol !== 'http:') { | ||
| return { | ||
| isValid: false, | ||
| error: 'proxyUrl must use http:// (https/socks proxies are not supported)', | ||
| } | ||
| } | ||
| const validation = await validateUrlWithDNS(proxyUrl, 'proxyUrl', { allowHttp: true }) | ||
| if (!validation.isValid) { | ||
| return { isValid: false, error: validation.error } | ||
| } | ||
| const resolvedIP = validation.resolvedIP! | ||
| // validateUrlWithDNS permits loopback for self-hosted dev targets; a proxy governs | ||
| // egress, so loopback/private proxy hosts stay blocked unconditionally. | ||
| if (isPrivateOrReservedIP(resolvedIP)) { | ||
| return { isValid: false, error: 'proxyUrl resolves to a blocked IP address' } | ||
| } | ||
| // Bracket IPv6 literals: assigning an unbracketed IPv6 address to URL.hostname | ||
| // is a no-op, which would leave the DNS hostname in place and reopen rebinding. | ||
| parsed.hostname = resolvedIP.includes(':') ? `[${resolvedIP}]` : resolvedIP | ||
| return { isValid: true, pinnedProxyUrl: parsed.toString() } | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| /** | ||
| * Validates a database hostname by resolving DNS and checking the resolved IP | ||
| * against private/reserved ranges to prevent SSRF via database connections. | ||
| @@ -343,6 +410,12 @@ export interface SecureFetchOptions { | ||
| signal?: AbortSignal | ||
| /** Drop the Authorization header when following a redirect, so it is not sent to the redirect target's origin. */ | ||
| stripAuthOnRedirect?: boolean | ||
| /** | ||
| * Pre-validated, IP-pinned `http://` proxy URL (see {@link validateAndPinProxyUrl}). | ||
| * When set, the connection routes through this proxy and target-IP pinning is | ||
| * bypassed (the proxy resolves the target). | ||
| */ | ||
| proxyUrl?: string | ||
| } | ||
| export class SecureFetchHeaders { | ||
| @@ -678,11 +751,17 @@ export async function secureFetchWithPinnedIP( | ||
| const defaultPort = isHttps ? 443 : 80 | ||
| const port = parsed.port ? Number.parseInt(parsed.port, 10) : defaultPort | ||
| const lookup = createPinnedLookup(resolvedIP) | ||
| const agentOptions: http.AgentOptions = { lookup } | ||
| const agent = isHttps ? new https.Agent(agentOptions) : new http.Agent(agentOptions) | ||
| let agent: http.Agent | ||
| if (options.proxyUrl) { | ||
| // Proxy connection is already IP-pinned by validateAndPinProxyUrl; target-IP | ||
| // pinning is intentionally bypassed (the proxy resolves the target). https | ||
| // targets tunnel via CONNECT, http targets use absolute-URI forwarding. | ||
| agent = isHttps ? new HttpsProxyAgent(options.proxyUrl) : new HttpProxyAgent(options.proxyUrl) | ||
| } else { | ||
| const lookup = createPinnedLookup(resolvedIP) | ||
| const agentOptions: http.AgentOptions = { lookup } | ||
| agent = isHttps ? new https.Agent(agentOptions) : new http.Agent(agentOptions) | ||
| } | ||
| const { 'accept-encoding': _, ...sanitizedHeaders } = options.headers ?? {} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.