Skip to content

test: add ignoreUpstreamProxyCertificate tests #583

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

Merged
merged 4 commits into from
Apr 4, 2025
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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "proxy-chain",
"version": "2.5.8",
"version": "2.5.9",
"description": "Node.js implementation of a proxy server (think Squid) with support for SSL, authentication, upstream proxy chaining, and protocol tunneling.",
"main": "dist/index.js",
"keywords": [
Expand Down
95 changes: 95 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,101 @@ it('supports pre-response CONNECT payload', (done) => {
});
});

describe('supports ignoreUpstreamProxyCertificate', () => {
const serverOptions = {
key: sslKey,
cert: sslCrt,
};

const responseMessage = 'Hello World!';

it('fails on upstream error', async () => {
const target = https.createServer(serverOptions, (_req, res) => {
res.write(responseMessage);
res.end();
});

await util.promisify(target.listen.bind(target))(0);

const proxyServer = new ProxyChain.Server({
port: 6666,
prepareRequestFunction: () => {
return {
upstreamProxyUrl: `https://localhost:${target.address().port}`,
};
},
});

let proxyServerError = false;
proxyServer.on('requestFailed', () => {
// requestFailed will be called if we pass an invalid proxy url
proxyServerError = true;
});

await proxyServer.listen();

/**
* request is sent with rejectUnauthorized: true
* so when the SSL certificate is not trusted (self-signed, expired, invalid), client will reject the connection
*/
const response = await requestPromised({
proxy: 'http://localhost:6666',
url: 'http://httpbin.org/ip',
});

expect(proxyServerError).to.be.equal(false);

expect(response.statusCode).to.be.equal(599);

proxyServer.close();
target.close();
});

it('bypass upstream error', async () => {
const target = https.createServer(serverOptions, (_req, res) => {
res.write(responseMessage);
res.end();
});

await util.promisify(target.listen.bind(target))(0);

const proxyServer = new ProxyChain.Server({
port: 6666,
prepareRequestFunction: () => {
return {
ignoreUpstreamProxyCertificate: true,
upstreamProxyUrl: `https://localhost:${target.address().port}`,
};
},
});

let proxyServerError = false;
proxyServer.on('requestFailed', () => {
// requestFailed will be called if we pass an invalid proxy url
proxyServerError = true;
});

await proxyServer.listen();

/**
* request is sent with rejectUnauthorized: false
* so when the SSL certificate is not trusted (self-signed, expired, invalid), client won't reject the connection
*/
const response = await requestPromised({
proxy: 'http://localhost:6666',
url: 'http://httpbin.org/ip',
});

expect(proxyServerError).to.be.equal(false);

expect(response.statusCode).to.be.equal(200);
expect(response.body).to.be.equal(responseMessage);

proxyServer.close();
target.close();
});
});

// Run all combinations of test parameters
const useSslVariants = [
false,
Expand Down
Loading