Skip to content
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

Protocol not parsed when setting proxy config from env vars #3070

Merged
merged 4 commits into from
Dec 3, 2020
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ These are the available config options for making requests. Only the `url` is re
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),

// `proxy` defines the hostname and port of the proxy server.
// `proxy` defines the hostname, port, and protocol of the proxy server.
// You can also define your proxy using the conventional `http_proxy` and
// `https_proxy` environment variables. If you are using environment variables
// for your proxy configuration, you can also define a `no_proxy` environment
Expand All @@ -432,7 +432,9 @@ These are the available config options for making requests. Only the `url` is re
// supplies credentials.
// This will set an `Proxy-Authorization` header, overwriting any existing
// `Proxy-Authorization` custom headers you have set using `headers`.
// If the proxy server uses HTTPS, then you must set the protocol to `https`.
proxy: {
chinesedfan marked this conversation as resolved.
Show resolved Hide resolved
protocol: 'https',
host: '127.0.0.1',
port: 9000,
auth: {
Expand Down
4 changes: 2 additions & 2 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ module.exports = function httpAdapter(config) {
});
}


if (shouldProxy) {
proxy = {
host: parsedProxyUrl.hostname,
port: parsedProxyUrl.port
port: parsedProxyUrl.port,
protocol: parsedProxyUrl.protocol
jonathanfoster marked this conversation as resolved.
Show resolved Hide resolved
};

if (parsedProxyUrl.auth) {
Expand Down
17 changes: 17 additions & 0 deletions test/unit/adapters/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-----BEGIN CERTIFICATE-----
MIICpDCCAYwCCQDbqELLwgbPdDANBgkqhkiG9w0BAQUFADAUMRIwEAYDVQQDDAls
b2NhbGhvc3QwHhcNMjAwNjI2MjIxMTQ3WhcNNDcxMTExMjIxMTQ3WjAUMRIwEAYD
VQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQD6
Ogt99/dZ0UgbCuVV1RZ9n28Ov3DzrJCkjperQoXomIq3Fr4RUI1a2rwe3mtl3UzE
1IVZVvWPGdEsEQHwXfAsP/jFGTwI3HDyOhcqzFQSKsjvqJWYkOOb+2r3SBrFlRZW
09k/3lC+hx2XtuuG68u4Xgn3AlUvm2vplgCN7eiYcGeNwVuf2eHdOqTRTqiYCZLi
T8GtdYMDXOrwsGZs/jUKd9U0ar/lqwMhmw07yzlVDM2MWM2tyq/asQ7Sf7vuoMFu
oAtDJ3E+bK1k/7SNhdyP4RonhyUCkWG+mzoKDS1qgXroTiQSDUksAvOCTcj8BNIT
ee+Lcn9FaTKNJiKiU9q/AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAFi5ZpaUj+mU
dsgOka+j2/njgNXux3cOjhm7z/N7LeTuDENAOrYa5b+j5JX/YM7RKHrkbXHsQbfs
GB3ufH6QhSiCd/AdsXp/TbCE/8gdq8ykkjwVP1bvBle9oPH7x1aO/WP/odsepYUv
o9aOZW4iNQVmwamU62ezglf3QD7HPeE4LnZueaFtuzRoC+aWT9v0MIeUPJLe3WDQ
FEySwUuthMDJEv92/TeK0YOiunmseCu2mvdiDj6E3C9xa5q2DWgl+msu7+bPgvYO
GuWaoNeQQGk7ebBO3Hk3IyaGx6Cbd8ty+YaZW7dUT+m7KCs1VkxdcDMjZJVWiJy4
4HcEcKboG4Y=
-----END CERTIFICATE-----
102 changes: 102 additions & 0 deletions test/unit/adapters/http.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var axios = require('../../../index');
var http = require('http');
var https = require('https');
var net = require('net');
var url = require('url');
var zlib = require('zlib');
Expand Down Expand Up @@ -489,6 +490,57 @@ describe('supports http with nodejs', function () {
});
});

it('should support HTTPS proxies', function (done) {
var options = {
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'cert.pem'))
};

server = https.createServer(options, function (req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.end('12345');
}).listen(4444, function () {
proxy = https.createServer(options, function (request, response) {
var parsed = url.parse(request.url);
var opts = {
host: parsed.hostname,
port: parsed.port,
path: parsed.path,
protocol: parsed.protocol,
rejectUnauthorized: false
};

https.get(opts, function (res) {
var body = '';
res.on('data', function (data) {
body += data;
});
res.on('end', function () {
response.setHeader('Content-Type', 'text/html; charset=UTF-8');
response.end(body + '6789');
});
});
}).listen(4000, function () {
axios.get('https://localhost:4444/', {
proxy: {
host: 'localhost',
port: 4000,
protocol: 'https'
},
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
}).then(function (res) {
assert.equal(res.data, '123456789', 'should pass through proxy');
done();
}).catch(function (err) {
assert.fail(err);
done()
});
});
});
});

it('should not pass through disabled proxy', function (done) {
// set the env variable
process.env.http_proxy = 'http://does-not-exists.example.com:4242/';
Expand Down Expand Up @@ -542,6 +594,56 @@ describe('supports http with nodejs', function () {
});
});

it('should support HTTPS proxy set via env var', function (done) {
var options = {
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'cert.pem'))
};

server = https.createServer(options, function (req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.end('12345');
}).listen(4444, function () {
proxy = https.createServer(options, function (request, response) {
var parsed = url.parse(request.url);
var opts = {
host: parsed.hostname,
port: parsed.port,
path: parsed.path,
protocol: parsed.protocol,
rejectUnauthorized: false
};

https.get(opts, function (res) {
var body = '';
res.on('data', function (data) {
body += data;
});
res.on('end', function () {
response.setHeader('Content-Type', 'text/html; charset=UTF-8');
response.end(body + '6789');
});
});
}).listen(4000, function () {
process.env.https_proxy = 'https://localhost:4000/';
chinesedfan marked this conversation as resolved.
Show resolved Hide resolved

axios.get('https://localhost:4444/', {
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
}).then(function (res) {
assert.equal(res.data, '123456789', 'should pass through proxy');
done();
}).catch(function (err) {
assert.fail(err);
done()
}).finally(function () {
process.env.https_proxy = ''
});
});
});
});

it('should not use proxy for domains in no_proxy', function (done) {
server = http.createServer(function (req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
Expand Down
27 changes: 27 additions & 0 deletions test/unit/adapters/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA+joLfff3WdFIGwrlVdUWfZ9vDr9w86yQpI6Xq0KF6JiKtxa+
EVCNWtq8Ht5rZd1MxNSFWVb1jxnRLBEB8F3wLD/4xRk8CNxw8joXKsxUEirI76iV
mJDjm/tq90gaxZUWVtPZP95Qvocdl7brhuvLuF4J9wJVL5tr6ZYAje3omHBnjcFb
n9nh3Tqk0U6omAmS4k/BrXWDA1zq8LBmbP41CnfVNGq/5asDIZsNO8s5VQzNjFjN
rcqv2rEO0n+77qDBbqALQydxPmytZP+0jYXcj+EaJ4clApFhvps6Cg0taoF66E4k
Eg1JLALzgk3I/ATSE3nvi3J/RWkyjSYiolPavwIDAQABAoIBAEbMi5ndwjfAlkVI
hPEPNKjgpnymwB/CEL7utY04akkQeBcrsSWXBBfT0exuBDczMVhzxTMs/pe5t0xf
l4vaGG18wDeMV0cukCqJMyrh21u0jVv5+DHNtQjaTz6eQSzsbQCuOkbu8SuncUEO
+X8YUnDc8rbYCyBIOnVCAvAlg201uW0G5G9NEwJOu6cAKMKkogdHqv+FRX96C5hm
gtbGEzpGV2vVClgMwMcX49ucluZvqLvit/yehNVd0VOtW/kuLup4R6q0abHRapDd
95rJAhPvar4mzP+UgJrGQ9hozqhizDthBjnsmGeMBUiBCkay7OXIZpvLoCpQkti1
WIWuikkCgYEA/oZqq71RT1nPuI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