From 24f8fcfbc0f4a2c7c8cf19b43951cccb763807d5 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 1 Dec 2026 18:14:24 +0000 Subject: [PATCH] Reject HTTP URLs in createProxyRestClient Co-authored-by: ProxyMesh AI --- docs/typed-rest-client.md | 2 +- lib/typed-rest-client-proxy.js | 19 +++++-- test/test_connect_security.js | 94 ++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 6 deletions(-) diff --git a/docs/typed-rest-client.md b/docs/typed-rest-client.md index f7cd3a9..0d3d8c7 100644 --- a/docs/typed-rest-client.md +++ b/docs/typed-rest-client.md @@ -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 diff --git a/lib/typed-rest-client-proxy.js b/lib/typed-rest-client-proxy.js index fbf9095..188506d 100644 --- a/lib/typed-rest-client-proxy.js +++ b/lib/typed-rest-client-proxy.js @@ -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. @@ -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) { @@ -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} ProxyHeadersHttpClient @@ -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, diff --git a/test/test_connect_security.js b/test/test_connect_security.js index 8651e3e..560c803 100644 --- a/test/test_connect_security.js +++ b/test/test_connect_security.js @@ -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(); + } +});