Skip to content

Commit

Permalink
Add CAs for proxy connection (microsoft/vscode#235410)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmarti committed Dec 5, 2024
1 parent b71e934 commit 8e32b6f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ type FindProxyForURL = (req: http.ClientRequest, opts: http.RequestOptions, url:
export class PacProxyAgent extends Agent {
resolver: FindProxyForURL;
opts: PacProxyAgentOptions;
addCAs: (opts: PacProxyAgentOptions) => Promise<void>;
casAdded = false;
cache?: Readable;

constructor(resolver: FindProxyForURL, opts: PacProxyAgentOptions = {}) {
constructor(resolver: FindProxyForURL, opts: PacProxyAgentOptions = {}, addCAs: (opts: PacProxyAgentOptions) => Promise<void> = async () => {}) {
super(opts);
debug('Creating PacProxyAgent with options %o', opts);

this.resolver = resolver;
this.opts = { ...opts };
this.addCAs = addCAs;
this.cache = undefined;
}

Expand Down Expand Up @@ -96,6 +99,11 @@ export class PacProxyAgent extends Agent {
} else if (proxyURL.startsWith('http')) {
// Use an HTTP or HTTPS proxy
// http://dev.chromium.org/developers/design-documents/secure-web-proxy
if (!this.casAdded && proxyURL.startsWith('https')) {
debug('Adding CAs to proxy options');
this.casAdded = true;
await this.addCAs(this.opts);
}
if (secureEndpoint) {
agent = new HttpsProxyAgent2(proxyURL, this.opts);
} else {
Expand Down Expand Up @@ -235,7 +243,8 @@ class HttpsProxyAgent2<Uri extends string> extends HttpsProxyAgent<Uri> {

export function createPacProxyAgent(
resolver: FindProxyForURL,
opts?: PacProxyAgentOptions
opts?: PacProxyAgentOptions,
addCAs?: (opts: PacProxyAgentOptions) => Promise<void>,
): PacProxyAgent {
if (!opts) {
opts = {};
Expand All @@ -245,7 +254,7 @@ export function createPacProxyAgent(
throw new TypeError('a resolve function must be specified!');
}

return new PacProxyAgent(resolver, opts);
return new PacProxyAgent(resolver, opts, addCAs);
}
type PacProxyAgentOptions =
HttpProxyAgentOptions<''> &
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function createProxyResolver(params: ProxyAgentParams) {

const stackText = ''; // getLogLevel() === LogLevel.Trace ? '\n' + new Error('Error for stack trace').stack : '';

addCertificatesV1(params, flags.addCertificatesV1, opts, () => {
addCertificatesToOptionsV1(params, flags.addCertificatesV1, opts, () => {
if (!flags.useProxySettings) {
callback('DIRECT');
return;
Expand Down Expand Up @@ -395,7 +395,7 @@ export function createHttpPatch(params: ProxyAgentParams, originals: typeof http
originalAgent: (!useProxySettings || isLocalhost || config === 'fallback') ? originalAgent : undefined,
lookupProxyAuthorization: params.lookupProxyAuthorization,
// keepAlive: ((originalAgent || originals.globalAgent) as { keepAlive?: boolean }).keepAlive, // Skipping due to https://github.com/microsoft/vscode/issues/228872.
});
}, opts => new Promise<void>(resolve => addCertificatesToOptionsV1(params, params.addCertificatesV1(), opts, resolve)));
agent.protocol = isHttps ? 'https:' : 'http:';
options.agent = agent
if (isHttps) {
Expand Down Expand Up @@ -727,7 +727,7 @@ function getAgentOptions(systemCA: string[] | undefined, requestInit: RequestIni
return { allowH2, requestCA, proxyCA };
}

function addCertificatesV1(params: ProxyAgentParams, addCertificatesV1: boolean, opts: http.RequestOptions, callback: () => void) {
function addCertificatesToOptionsV1(params: ProxyAgentParams, addCertificatesV1: boolean, opts: http.RequestOptions | tls.ConnectionOptions, callback: () => void) {
if (addCertificatesV1) {
getOrLoadAdditionalCertificates(params)
.then(caCertificates => {
Expand Down

0 comments on commit 8e32b6f

Please sign in to comment.