Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/typed-rest-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[typed-rest-client](https://github.com/microsoft/typed-rest-client) is the HTTP stack used in many Azure DevOps–style tooling. This package provides `createProxyRestClient`, which subclasses `HttpClient` / `RestClient` so **HTTPS** uses `ProxyHeadersAgent` instead of tunnel-agent, preserving custom CONNECT headers.

HTTP URLs still use the base client behavior (no custom CONNECT path for plain HTTP in this integration).
`createProxyRestClient` only supports HTTPS. Plain `http://` URLs and HTTPS-to-HTTP redirects throw, because HTTP would bypass the proxy.

## Getting Started

Expand Down
19 changes: 14 additions & 5 deletions lib/typed-rest-client-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import { getProxyHeaders } from './core/proxy-headers-store.js';

const require = createRequire(import.meta.url);

function rejectHttpUrl(parsedUrl) {
if (parsedUrl?.protocol === 'http:') {
throw new TypeError(
'javascript-proxy-headers/typed-rest-client only supports HTTPS; HTTP would bypass the proxy',
);
}
}

/**
* Copy CONNECT headers onto a RestClient result (or rejected error).
* typed-rest-client v2/v3 call `processResponse`, which builds a fresh object.
Expand Down Expand Up @@ -62,10 +70,8 @@ function createProxyHeadersHttpClientClass(HttpClient) {
}

_getAgent(parsedUrl) {
if (parsedUrl.protocol === 'https:') {
return this.proxyAgent;
}
return super._getAgent(parsedUrl);
rejectHttpUrl(parsedUrl);
return this.proxyAgent;
}

request(verb, requestUrl, data, headers) {
Expand All @@ -85,7 +91,7 @@ function createProxyHeadersHttpClientClass(HttpClient) {
}

/**
* RestClient with ProxyHeadersAgent for HTTPS.
* RestClient with ProxyHeadersAgent for HTTPS. Plain HTTP is rejected.
*
* @param {import('typed-rest-client/RestClient').RestClient} RestClient
* @param {ReturnType<createProxyHeadersHttpClientClass>} ProxyHeadersHttpClient
Expand All @@ -101,6 +107,9 @@ function createProxyHeadersRestClientClass(RestClient, ProxyHeadersHttpClient) {
* @param {object} proxyOpts
*/
constructor(userAgent, baseUrl, handlers, requestOptions, proxyOpts) {
if (baseUrl) {
rejectHttpUrl(new URL(baseUrl));
}
super(userAgent, baseUrl, handlers, requestOptions ? { ...requestOptions, proxy: undefined } : undefined);
this.client = new ProxyHeadersHttpClient(
userAgent,
Expand Down
94 changes: 94 additions & 0 deletions test/test_connect_security.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,3 +764,97 @@ test('got refuses HTTPS to HTTP redirects that would bypass the proxy', async ()
cleanup();
}
});

test('typed-rest-client rejects plain HTTP so it cannot bypass the proxy', async () => {
const httpHits = [];
const httpOrigin = http.createServer((req, res) => {
httpHits.push(req.url);
res.end('{"via":"http"}');
});
await listen(httpOrigin);
const httpPort = httpOrigin.address().port;

const proxyHits = [];
const proxy = net.createServer((sock) => {
sock.on('data', (d) => proxyHits.push(d.toString('utf8').split('\r\n')[0]));
sock.on('error', () => {});
});
await listen(proxy);
const proxyPort = proxy.address().port;

try {
const client = createProxyRestClient({
userAgent: 'javascript-proxy-headers-test',
proxy: `http://127.0.0.1:${proxyPort}`,
});
await assert.rejects(
() => client.get(`http://127.0.0.1:${httpPort}/plain`),
/only supports HTTPS/,
);
assert.equal(httpHits.length, 0);
assert.equal(proxyHits.length, 0);
} finally {
httpOrigin.close();
proxy.close();
}
});

test('typed-rest-client refuses HTTPS to HTTP redirects that would bypass the proxy', async () => {
const { cert, key, cleanup } = makeSelfSignedCert();
const httpHits = [];
const httpOrigin = http.createServer((req, res) => {
httpHits.push(req.url);
res.end('{"via":"http-direct"}');
});
await listen(httpOrigin);
const httpPort = httpOrigin.address().port;

const proxy = net.createServer((sock) => {
let buf = Buffer.alloc(0);
let handed = false;
sock.on('data', (d) => {
if (handed) return;
buf = Buffer.concat([buf, d]);
if (buf.indexOf('\r\n\r\n') === -1) return;
handed = true;
sock.write('HTTP/1.1 200 Connection Established\r\nX-ProxyMesh-IP: 198.51.100.9\r\n\r\n');
const tlsSock = new tls.TLSSocket(sock, { isServer: true, cert, key });
tlsSock.on('secure', () => {
let httpBuf = Buffer.alloc(0);
tlsSock.on('data', (chunk) => {
httpBuf = Buffer.concat([httpBuf, chunk]);
if (httpBuf.indexOf('\r\n\r\n') === -1) return;
const location = `http://127.0.0.1:${httpPort}/leaked`;
tlsSock.end(
'HTTP/1.1 302 Found\r\n' +
`Location: ${location}\r\n` +
'Content-Length: 0\r\nConnection: close\r\n\r\n',
);
});
});
});
sock.on('error', () => {});
});
await listen(proxy);
const proxyPort = proxy.address().port;

try {
const client = createProxyRestClient({
userAgent: 'javascript-proxy-headers-test',
proxy: `http://127.0.0.1:${proxyPort}`,
requestOptions: { allowRedirectDowngrade: true },
});
client.proxyAgent.tlsOptions = { rejectUnauthorized: false };

await assert.rejects(
() => client.get('https://127.0.0.1/start'),
/HTTP would bypass the proxy/,
);
await delay(50);
assert.equal(httpHits.length, 0);
} finally {
httpOrigin.close();
proxy.close();
cleanup();
}
});