Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 35607f3

Browse files
committed
tls, https: validate server certificate by default
This commit changes the default value of the rejectUnauthorized option from false to true. What that means is that tls.connect(), https.get() and https.request() will reject invalid server certificates from now on, including self-signed certificates. There is an escape hatch: if you set the NODE_TLS_REJECT_UNAUTHORIZED environment variable to the literal string "0", node.js reverts to its old behavior. Fixes #3949.
1 parent 4c171a5 commit 35607f3

38 files changed

+131
-24
lines changed

doc/api/https.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ The following options from [tls.connect()][] can also be specified. However, a
119119
- `rejectUnauthorized`: If `true`, the server certificate is verified against
120120
the list of supplied CAs. An `'error'` event is emitted if verification
121121
fails. Verification happens at the connection level, *before* the HTTP
122-
request is sent. Default `false`.
122+
request is sent. Default `true`.
123123

124124
In order to specify these options, use a custom `Agent`.
125125

doc/api/tls.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ Creates a new client connection to the given `port` and `host` (old API) or
240240

241241
- `rejectUnauthorized`: If `true`, the server certificate is verified against
242242
the list of supplied CAs. An `'error'` event is emitted if verification
243-
fails. Default: `false`.
243+
fails. Default: `true`.
244244

245245
- `NPNProtocols`: An array of string or `Buffer` containing supported NPN
246246
protocols. `Buffer` should have following format: `0x05hello0x05world`,

lib/https.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
var tls = require('tls');
2323
var http = require('http');
24+
var util = require('util');
2425
var url = require('url');
2526
var inherits = require('util').inherits;
2627

@@ -97,11 +98,25 @@ exports.request = function(options, cb) {
9798
throw new Error('Protocol:' + options.protocol + ' not supported.');
9899
}
99100

100-
if (options.agent === undefined) {
101-
options.agent = globalAgent;
101+
options = util._extend({
102+
createConnection: createConnection,
103+
defaultPort: 443
104+
}, options);
105+
106+
if (typeof options.agent === 'undefined') {
107+
if (typeof options.ca === 'undefined' &&
108+
typeof options.cert === 'undefined' &&
109+
typeof options.ciphers === 'undefined' &&
110+
typeof options.key === 'undefined' &&
111+
typeof options.passphrase === 'undefined' &&
112+
typeof options.pfx === 'undefined' &&
113+
typeof options.rejectUnauthorized === 'undefined') {
114+
options.agent = globalAgent;
115+
} else {
116+
options.agent = new Agent(options);
117+
}
102118
}
103-
options.createConnection = createConnection;
104-
options.defaultPort = options.defaultPort || 443;
119+
105120
return new http.ClientRequest(options, cb);
106121
};
107122

lib/tls.js

+5
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,11 @@ exports.connect = function(/* [port, host], options, cb */) {
12721272
var options = args[0];
12731273
var cb = args[1];
12741274

1275+
var defaults = {
1276+
rejectUnauthorized: '0' !== process.env.NODE_TLS_REJECT_UNAUTHORIZED
1277+
};
1278+
options = util._extend(defaults, options || {});
1279+
12751280
var socket = options.socket ? options.socket : new net.Stream();
12761281

12771282
var sslcontext = crypto.createCredentials(options);

test/fixtures/GH-892-request.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
// Called by test/simple/test-regress-GH-892.js
22+
// Called by test/pummel/test-regress-GH-892.js
23+
24+
// disable strict server certificate validation by the client
25+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
2326

2427
var https = require('https');
2528
var fs = require('fs');

test/pummel/test-https-large-response.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
23-
22+
// disable strict server certificate validation by the client
23+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
2424

2525
var common = require('../common');
2626
var assert = require('assert');

test/pummel/test-tls-throttle.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
23-
24-
2522
// Server sends a large string. Client counts bytes and pauses every few
2623
// seconds. Makes sure that pause and resume work properly.
24+
25+
// disable strict server certificate validation by the client
26+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
27+
2728
var common = require('../common');
2829
var assert = require('assert');
2930
var tls = require('tls');

test/simple/test-http-host-headers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
23-
22+
// disable strict server certificate validation by the client
23+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
2424

2525
var http = require('http'),
2626
https = require('https'),

test/simple/test-http-url.parse-https.request.js

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// disable strict server certificate validation by the client
23+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
24+
2225
var common = require('../common');
2326
var assert = require('assert');
2427
var https = require('https');

test/simple/test-https-agent.js

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ if (!process.versions.openssl) {
2727
process.exit(0);
2828
}
2929

30+
// disable strict server certificate validation by the client
31+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
32+
3033
var common = require('../common');
3134
var assert = require('assert');
3235
var https = require('https');

test/simple/test-https-client-get-url.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ if (!process.versions.openssl) {
2424
process.exit(0);
2525
}
2626

27+
// disable strict server certificate validation by the client
28+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
29+
2730
var common = require('../common');
2831
var assert = require('assert');
2932
var https = require('https');

test/simple/test-https-client-reject.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,21 @@ var server = https.createServer(options, function(req, res) {
4747

4848
function unauthorized() {
4949
var req = https.request({
50-
port: common.PORT
50+
port: common.PORT,
51+
rejectUnauthorized: false
5152
}, function(res) {
5253
assert(!req.socket.authorized);
5354
rejectUnauthorized();
5455
});
5556
req.on('error', function(err) {
56-
assert(false);
57+
throw err;
5758
});
5859
req.end();
5960
}
6061

6162
function rejectUnauthorized() {
6263
var options = {
63-
port: common.PORT,
64-
rejectUnauthorized: true
64+
port: common.PORT
6565
};
6666
options.agent = new https.Agent(options);
6767
var req = https.request(options, function(res) {
@@ -76,7 +76,6 @@ function rejectUnauthorized() {
7676
function authorized() {
7777
var options = {
7878
port: common.PORT,
79-
rejectUnauthorized: true,
8079
ca: [fs.readFileSync(path.join(common.fixturesDir, 'test_cert.pem'))]
8180
};
8281
options.agent = new https.Agent(options);

test/simple/test-https-drain.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ if (!process.versions.openssl) {
2424
process.exit(0);
2525
}
2626

27+
// disable strict server certificate validation by the client
28+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
29+
2730
var common = require('../common');
2831
var assert = require('assert');
2932
var https = require('https');

test/simple/test-https-eof-for-eom.js

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ if (!process.versions.openssl) {
3434
process.exit(0);
3535
}
3636

37+
// disable strict server certificate validation by the client
38+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
39+
3740
var common = require('../common');
3841
var assert = require('assert');
3942
var tls = require('tls');

test/simple/test-https-localaddress.js

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// disable strict server certificate validation by the client
23+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
24+
2225
var common = require('../common');
2326
var https = require('https'),
2427
fs = require('fs'),

test/simple/test-https-pfx.js

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// disable strict server certificate validation by the client
23+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
24+
2225
var common = require('../common');
2326
var assert = require('assert');
2427
var https = require('https');

test/simple/test-https-socket-options.js

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ if (!process.versions.openssl) {
2727
process.exit(0);
2828
}
2929

30+
// disable strict server certificate validation by the client
31+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
32+
3033
var common = require('../common');
3134
var assert = require('assert');
3235

test/simple/test-https-strict.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ if (!process.versions.openssl) {
2424
process.exit(0);
2525
}
2626

27+
// disable strict server certificate validation by the client
28+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
29+
2730
var common = require('../common');
2831
var assert = require('assert');
2932

test/simple/test-https-timeout.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ if (!process.versions.openssl) {
2424
process.exit(0);
2525
}
2626

27+
// disable strict server certificate validation by the client
28+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
29+
2730
var common = require('../common');
2831
var assert = require('assert');
2932
var fs = require('fs');

test/simple/test-regress-GH-1531.js

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ if (!process.versions.openssl) {
2727
var https = require('https');
2828
var assert = require('assert');
2929
var fs = require('fs');
30+
// disable strict server certificate validation by the client
31+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
32+
3033
var common = require('../common');
3134

3235
var options = {

test/simple/test-tls-client-reject.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ var server = tls.createServer(options, function(socket) {
4848
});
4949

5050
function unauthorized() {
51-
var socket = tls.connect(common.PORT, function() {
51+
var socket = tls.connect({
52+
port: common.PORT,
53+
rejectUnauthorized: false
54+
}, function() {
5255
assert(!socket.authorized);
5356
socket.end();
5457
rejectUnauthorized();
@@ -60,9 +63,7 @@ function unauthorized() {
6063
}
6164

6265
function rejectUnauthorized() {
63-
var socket = tls.connect(common.PORT, {
64-
rejectUnauthorized: true
65-
}, function() {
66+
var socket = tls.connect(common.PORT, function() {
6667
assert(false);
6768
});
6869
socket.on('error', function(err) {
@@ -74,7 +75,6 @@ function rejectUnauthorized() {
7475

7576
function authorized() {
7677
var socket = tls.connect(common.PORT, {
77-
rejectUnauthorized: true,
7878
ca: [fs.readFileSync(path.join(common.fixturesDir, 'test_cert.pem'))]
7979
}, function() {
8080
assert(socket.authorized);

test/simple/test-tls-client-resume.js

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ if (!process.versions.openssl) {
2828
process.exit(0);
2929
}
3030

31+
// disable strict server certificate validation by the client
32+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
33+
3134
var common = require('../common');
3235
var assert = require('assert');
3336
var tls = require('tls');

test/simple/test-tls-client-verify.js

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ var testCases =
5959
];
6060

6161

62+
// disable strict server certificate validation by the client
63+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
64+
6265
var common = require('../common');
6366
var assert = require('assert');
6467
var fs = require('fs');

test/simple/test-tls-connect-given-socket.js

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// disable strict server certificate validation by the client
23+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
24+
2225
var common = require('../common');
2326
var assert = require('assert');
2427
var tls = require('tls');

test/simple/test-tls-connect-simple.js

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// disable strict server certificate validation by the client
23+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
24+
2225
var common = require('../common');
2326
var assert = require('assert');
2427
var tls = require('tls');

test/simple/test-tls-getcipher.js

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// disable strict server certificate validation by the client
23+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
24+
2225
var common = require('../common');
2326
var assert = require('assert');
2427
var tls = require('tls');

test/simple/test-tls-honorcipherorder.js

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// disable strict server certificate validation by the client
23+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
24+
2225
var common = require('../common');
2326
var assert = require('assert');
2427
var tls = require('tls');

test/simple/test-tls-npn-server-client.js

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ if (!process.features.tls_npn) {
2525
process.exit(0);
2626
}
2727

28+
// disable strict server certificate validation by the client
29+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
30+
2831
var common = require('../common'),
2932
assert = require('assert'),
3033
fs = require('fs'),

test/simple/test-tls-over-http-tunnel.js

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ if (!process.versions.openssl) {
2727
process.exit(0);
2828
}
2929

30+
// disable strict server certificate validation by the client
31+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
32+
3033
var common = require('../common');
3134
var assert = require('assert');
3235

test/simple/test-tls-passphrase.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ if (!process.versions.openssl) {
2424
process.exit(0);
2525
}
2626

27+
// disable strict server certificate validation by the client
28+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
29+
2730
var common = require('../common');
2831
var assert = require('assert');
2932
var tls = require('tls');

test/simple/test-tls-pause-close.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ if (!process.versions.openssl) {
2424
process.exit(0);
2525
}
2626

27+
// disable strict server certificate validation by the client
28+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
29+
2730
var common = require('../common');
2831
var assert = require('assert');
2932
var tls = require('tls');

0 commit comments

Comments
 (0)