-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix proxy headers leak errconnect on strict proxies #792
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
Closed
joelwizard
wants to merge
6
commits into
actions:main
from
joelwizard:joelwizard/fix-proxy-headers-errconnect
+221
−6
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1203fab
Do not pass proxy headers to the proxy agent
joelwizard ba6bdbd
Add proxy tests
joelwizard 7a13797
Correct the comment to point to the right issue
joelwizard d4973cc
Update __tests__/proxy-policy.test.ts
joelwizard aef0e9d
Make the tests both proxy cased and redo the import
joelwizard f88bc1e
Rename the test more appropriately
joelwizard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /** | ||
| * Tests that the proxyPolicy does NOT leak application-level request headers | ||
| * into the HTTP CONNECT tunnel handshake. | ||
| * | ||
| * Background: HttpsProxyAgent's `headers` constructor option specifies headers | ||
| * to send in the CONNECT request to the proxy server. When Azure SDK request | ||
| * headers (Content-Type, x-ms-version, x-ms-blob-type, etc.) are passed here, | ||
| * strict corporate proxies (Fortinet, Zscaler) reject the tunnel — causing | ||
| * ECONNRESET. See: https://github.com/actions/upload-artifact/issues/747 | ||
| */ | ||
| import {describe, test, expect, beforeEach, afterEach} from '@jest/globals' | ||
|
|
||
| import { | ||
| createPipelineRequest, | ||
| type PipelineRequest, | ||
| type SendRequest | ||
| } from '@typespec/ts-http-runtime' | ||
| import {proxyPolicy} from '@typespec/ts-http-runtime/internal/policies' | ||
| import {HttpsProxyAgent} from 'https-proxy-agent' | ||
| import {HttpProxyAgent} from 'http-proxy-agent' | ||
|
|
||
| describe('proxyPolicy', () => { | ||
| const PROXY_URL = 'http://corporate-proxy.example.com:3128' | ||
|
|
||
| let savedHttpsProxy: string | undefined | ||
| let savedHttpProxy: string | undefined | ||
| let savedNoProxy: string | undefined | ||
|
|
||
| beforeEach(() => { | ||
| // Save and set proxy env vars | ||
| savedHttpsProxy = process.env['HTTPS_PROXY'] | ||
| savedHttpProxy = process.env['HTTP_PROXY'] | ||
| savedNoProxy = process.env['NO_PROXY'] | ||
|
|
||
| process.env['HTTPS_PROXY'] = PROXY_URL | ||
| process.env['HTTP_PROXY'] = PROXY_URL | ||
| delete process.env['NO_PROXY'] | ||
| }) | ||
joelwizard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| afterEach(() => { | ||
| // Restore original env | ||
| if (savedHttpsProxy !== undefined) { | ||
| process.env['HTTPS_PROXY'] = savedHttpsProxy | ||
| } else { | ||
| delete process.env['HTTPS_PROXY'] | ||
| } | ||
| if (savedHttpProxy !== undefined) { | ||
| process.env['HTTP_PROXY'] = savedHttpProxy | ||
| } else { | ||
| delete process.env['HTTP_PROXY'] | ||
| } | ||
| if (savedNoProxy !== undefined) { | ||
| process.env['NO_PROXY'] = savedNoProxy | ||
| } else { | ||
| delete process.env['NO_PROXY'] | ||
| } | ||
| }) | ||
|
|
||
| /** | ||
| * A mock "next" handler that captures the request after the proxy policy | ||
| * has set the agent, so we can inspect it. | ||
| */ | ||
| function createCapturingNext(): SendRequest & { | ||
| capturedRequest: PipelineRequest | undefined | ||
| } { | ||
| const fn = async (request: PipelineRequest) => { | ||
| fn.capturedRequest = request | ||
| return { | ||
| status: 200, | ||
| headers: createPipelineRequest({url: ''}).headers, | ||
| request | ||
| } | ||
| } | ||
| fn.capturedRequest = undefined as PipelineRequest | undefined | ||
| return fn | ||
| } | ||
|
|
||
| test('does not leak application headers into HttpsProxyAgent CONNECT request', async () => { | ||
| const policy = proxyPolicy() | ||
| const next = createCapturingNext() | ||
|
|
||
| // Simulate an Azure Blob Storage upload request with typical SDK headers | ||
| const request = createPipelineRequest({ | ||
| url: 'https://productionresultssa0.blob.core.windows.net/artifacts/upload' | ||
| }) | ||
| request.headers.set('Content-Type', 'application/octet-stream') | ||
| request.headers.set('x-ms-version', '2024-11-04') | ||
| request.headers.set('x-ms-blob-type', 'BlockBlob') | ||
| request.headers.set( | ||
| 'x-ms-client-request-id', | ||
| '00000000-0000-0000-0000-000000000000' | ||
| ) | ||
|
|
||
| await policy.sendRequest(request, next) | ||
|
|
||
| // The policy should have assigned an HttpsProxyAgent | ||
| const agent = next.capturedRequest?.agent | ||
| expect(agent).toBeDefined() | ||
| expect(agent).toBeInstanceOf(HttpsProxyAgent) | ||
|
|
||
| // CRITICAL: The agent's proxyHeaders must NOT contain application headers. | ||
| // If this fails, application headers are being leaked into the CONNECT | ||
| // request, which breaks strict corporate proxies. | ||
| const proxyAgent = agent as HttpsProxyAgent<string> | ||
| const proxyHeaders = | ||
| typeof proxyAgent.proxyHeaders === 'function' | ||
| ? proxyAgent.proxyHeaders() | ||
| : proxyAgent.proxyHeaders | ||
|
|
||
| expect(proxyHeaders).toBeDefined() | ||
|
|
||
| // None of the Azure SDK application headers should appear | ||
| const headerObj = proxyHeaders as Record<string, string> | ||
| expect(headerObj['content-type']).toBeUndefined() | ||
| expect(headerObj['Content-Type']).toBeUndefined() | ||
| expect(headerObj['x-ms-version']).toBeUndefined() | ||
| expect(headerObj['x-ms-blob-type']).toBeUndefined() | ||
| expect(headerObj['x-ms-client-request-id']).toBeUndefined() | ||
|
|
||
| // proxyHeaders should be empty (no application headers leaked) | ||
| expect(Object.keys(headerObj).length).toBe(0) | ||
| }) | ||
|
|
||
| test('does not leak application headers into HttpProxyAgent CONNECT request', async () => { | ||
| const policy = proxyPolicy() | ||
| const next = createCapturingNext() | ||
|
|
||
| // Simulate an insecure (HTTP) request with application headers | ||
| const request = createPipelineRequest({ | ||
| url: 'http://example.com/api/upload', | ||
| allowInsecureConnection: true | ||
| }) | ||
joelwizard marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| request.headers.set('Content-Type', 'application/json') | ||
| request.headers.set('Authorization', 'Bearer some-token') | ||
|
|
||
| await policy.sendRequest(request, next) | ||
|
|
||
| const agent = next.capturedRequest?.agent | ||
| expect(agent).toBeDefined() | ||
| expect(agent).toBeInstanceOf(HttpProxyAgent) | ||
| }) | ||
|
|
||
| test('still routes HTTPS requests through the proxy', async () => { | ||
| const policy = proxyPolicy() | ||
| const next = createCapturingNext() | ||
|
|
||
| const request = createPipelineRequest({ | ||
| url: 'https://results-receiver.actions.githubusercontent.com/twirp/test' | ||
| }) | ||
|
|
||
| await policy.sendRequest(request, next) | ||
|
|
||
| const agent = next.capturedRequest?.agent | ||
| expect(agent).toBeDefined() | ||
| expect(agent).toBeInstanceOf(HttpsProxyAgent) | ||
|
|
||
| // Verify the proxy URL is correct | ||
| const proxyAgent = agent as HttpsProxyAgent<string> | ||
| expect(proxyAgent.proxy.href).toBe(`${PROXY_URL}/`) | ||
| }) | ||
|
|
||
| test('bypasses proxy for no_proxy hosts', async () => { | ||
| // Use customNoProxyList since globalNoProxyList is only loaded once. | ||
| // Patterns starting with "." match subdomains (e.g. ".example.com" | ||
| // matches "api.example.com"), bare names match the host exactly. | ||
| const policy = proxyPolicy(undefined, { | ||
| customNoProxyList: ['.blob.core.windows.net', 'exact-host.test'] | ||
| }) | ||
| const next = createCapturingNext() | ||
|
|
||
| // This host matches ".blob.core.windows.net" via subdomain matching | ||
| const request = createPipelineRequest({ | ||
| url: 'https://productionresultssa0.blob.core.windows.net/artifacts/upload' | ||
| }) | ||
|
|
||
| await policy.sendRequest(request, next) | ||
|
|
||
| // Agent should not be set for a bypassed host | ||
| expect(next.capturedRequest?.agent).toBeUndefined() | ||
| }) | ||
|
|
||
| test('does not override a custom agent already set on the request', async () => { | ||
| const policy = proxyPolicy() | ||
| const next = createCapturingNext() | ||
|
|
||
| const customAgent = new HttpsProxyAgent('http://custom-proxy:9999') | ||
| const request = createPipelineRequest({ | ||
| url: 'https://blob.core.windows.net/test' | ||
| }) | ||
| request.agent = customAgent | ||
|
|
||
| await policy.sendRequest(request, next) | ||
|
|
||
| // The policy should not overwrite the pre-existing agent | ||
| expect(next.capturedRequest?.agent).toBe(customAgent) | ||
| }) | ||
| }) | ||
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.
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.