-
-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Use secureFetch and secureAxiosRequest for more URLs #5886
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
Changes from all commits
08c40f3
e63f6bd
32582c9
dca8052
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,25 +96,43 @@ export async function checkDenyList(url: string): Promise<void> { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Optional TLS options for secureAxiosRequest (e.g. custom CA for mutual TLS or private CAs). | ||
| */ | ||
| export interface SecureRequestAgentOptions { | ||
| ca?: string | string[] | Buffer | ||
| } | ||
|
|
||
| /** | ||
| * Makes a secure HTTP request that validates all URLs in redirect chains against the deny list | ||
| * @param config - Axios request configuration | ||
| * @param config - Axios request configuration (httpsAgent/httpAgent are ignored; use agentOptions for custom CA) | ||
| * @param maxRedirects - Maximum number of redirects to follow (default: 5) | ||
| * @param agentOptions - Optional TLS options (e.g. { ca } for custom CA PEM) | ||
| * @returns Promise<AxiosResponse> | ||
| * @throws Error if any URL in the redirect chain is denied | ||
| */ | ||
| export async function secureAxiosRequest(config: AxiosRequestConfig, maxRedirects: number = 5): Promise<AxiosResponse> { | ||
| export async function secureAxiosRequest( | ||
| config: AxiosRequestConfig, | ||
| maxRedirects: number = 5, | ||
| agentOptions?: SecureRequestAgentOptions | ||
| ): Promise<AxiosResponse> { | ||
| let currentUrl = config.url | ||
| if (!currentUrl) { | ||
| throw new Error('secureAxiosRequest: url is required') | ||
| } | ||
|
|
||
| let redirects = 0 | ||
| let currentConfig = { ...config, maxRedirects: 0, validateStatus: () => true } // Disable automatic redirects, accept all status codes | ||
| let currentConfig: AxiosRequestConfig = { | ||
| ...config, | ||
| maxRedirects: 0, | ||
| validateStatus: () => true, | ||
| httpsAgent: undefined, | ||
| httpAgent: undefined | ||
| } // Disable automatic redirects; agents set per-request below | ||
|
|
||
| while (redirects <= maxRedirects) { | ||
| const target = await resolveAndValidate(currentUrl) | ||
| const agent = createPinnedAgent(target) | ||
| const agent = createPinnedAgent(target, agentOptions) | ||
|
|
||
| currentConfig = { | ||
| ...currentConfig, | ||
|
|
@@ -168,17 +186,23 @@ export async function secureAxiosRequest(config: AxiosRequestConfig, maxRedirect | |
| * @param url - URL to fetch | ||
| * @param init - Fetch request options | ||
| * @param maxRedirects - Maximum number of redirects to follow (default: 5) | ||
| * @param agentOptions - Optional TLS options (e.g. { ca } for custom CA PEM) | ||
| * @returns Promise<Response> | ||
| * @throws Error if any URL in the redirect chain is denied | ||
| */ | ||
| export async function secureFetch(url: string, init?: RequestInit, maxRedirects: number = 5): Promise<Response> { | ||
| export async function secureFetch( | ||
| url: string, | ||
| init?: RequestInit, | ||
| maxRedirects: number = 5, | ||
| agentOptions?: SecureRequestAgentOptions | ||
| ): Promise<Response> { | ||
| let currentUrl = url | ||
| let redirectCount = 0 | ||
| let currentInit = { ...init, redirect: 'manual' as const } // Disable automatic redirects | ||
|
|
||
| while (redirectCount <= maxRedirects) { | ||
| const resolved = await resolveAndValidate(currentUrl) | ||
| const agent = createPinnedAgent(resolved) | ||
| const agent = createPinnedAgent(resolved, agentOptions) | ||
|
|
||
| const response = await fetch(currentUrl, { ...currentInit, agent: () => agent }) | ||
|
Comment on lines
203
to
207
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Remediation: Check if the redirect URL has a different origin than the current URL and, if so, strip sensitive headers from the request configuration.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may be better for another PR as it's not directly related to this change |
||
|
|
||
|
|
@@ -263,12 +287,13 @@ async function resolveAndValidate(url: string): Promise<ResolvedTarget> { | |
| } | ||
| } | ||
|
|
||
| function createPinnedAgent(target: ResolvedTarget): http.Agent | https.Agent { | ||
| function createPinnedAgent(target: ResolvedTarget, options?: { ca?: string | string[] | Buffer }): http.Agent | https.Agent { | ||
| const Agent = target.protocol === 'https' ? https.Agent : http.Agent | ||
|
|
||
| return new Agent({ | ||
| lookup: (_host, _opts, cb) => { | ||
| cb(null, target.ip, target.family) | ||
| } | ||
| }, | ||
| ...options | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.