Skip to content

Commit fd42dce

Browse files
committed
[tests] https test pass, fix #511. Exposed the rejectUnauthorized flag
1 parent a2b1f0a commit fd42dce

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

lib/http-proxy.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ proxy.createProxyServer = proxy.createServer = function createProxyServer(option
3030
* ssl : <object to be passed to https.createServer()>
3131
* ws : <true/false, if you want to proxy websockets>
3232
* xfwd : <true/false, adds x-forward headers>
33+
* secure : <true/false, verify SSL certificate>
3334
* }
3435
*
3536
* NOTE: `options.ws` and `options.ssl` are optional.

lib/http-proxy/common.js

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
3737
extend(outgoing.headers, options.headers);
3838
}
3939

40+
if (options[forward || 'target'].protocol == 'https:') {
41+
outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
42+
}
43+
44+
4045
outgoing.agent = options.agent || false;
4146
outgoing.path = req.url;
4247

test/lib-https-proxy-test.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Object.defineProperty(gen, 'port', {
1717
});
1818

1919
describe('lib/http-proxy.js', function() {
20-
describe('#createProxyServer using HTTPS', function() {
20+
describe('HTTPS #createProxyServer', function() {
2121
describe('HTTPS to HTTP', function () {
2222
it('should proxy the request en send back the response', function (done) {
2323
var ports = { source: gen.port, proxy: gen.port };
@@ -79,6 +79,8 @@ describe('lib/http-proxy.js', function() {
7979

8080
var proxy = httpProxy.createProxyServer({
8181
target: 'https://127.0.0.1:' + ports.source,
82+
// Allow to use SSL self signed
83+
secure: false
8284
}).listen(ports.proxy);
8385

8486
http.request({
@@ -100,5 +102,33 @@ describe('lib/http-proxy.js', function() {
100102
}).end();
101103
})
102104
})
105+
describe('HTTPS not allow SSL self signed', function () {
106+
it('should fail with error', function (done) {
107+
var ports = { source: gen.port, proxy: gen.port };
108+
var source = https.createServer({
109+
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
110+
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
111+
}).listen(ports.source);
112+
113+
var proxy = httpProxy.createProxyServer({
114+
target: 'https://127.0.0.1:' + ports.source,
115+
secure: true
116+
});
117+
118+
proxy.listen(ports.proxy);
119+
120+
proxy.on('error', function (err, req, res) {
121+
expect(err).to.be.an(Error);
122+
expect(err.toString()).to.be('Error: DEPTH_ZERO_SELF_SIGNED_CERT')
123+
done();
124+
})
125+
126+
http.request({
127+
hostname: '127.0.0.1',
128+
port: ports.proxy,
129+
method: 'GET'
130+
}).end();
131+
})
132+
})
103133
});
104134
});

0 commit comments

Comments
 (0)