Skip to content

Commit 262d4b2

Browse files
committed
https: disallow boolean types for key and cert options
When using https.createServer, passing boolean values for `key` and `cert` properties in the options object parameter doesn't throw an error an could lead to potential issues if they're accidentally passed. This PR aims to throw a reasonable error if a boolean was passed to either of those properties. Fixes: #12802
1 parent b5556e4 commit 262d4b2

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

lib/https.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ function Server(opts, requestListener) {
4040
}
4141
opts = util._extend({}, opts);
4242

43+
if (opts && typeof opts.key === 'boolean')
44+
throw new Error('"options.key" must not be a boolean');
45+
46+
if (opts && typeof opts.cert === 'boolean')
47+
throw new Error('"options.cert" must not be a boolean');
48+
4349
if (process.features.tls_npn && !opts.NPNProtocols) {
4450
opts.NPNProtocols = ['http/1.1', 'http/1.0'];
4551
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
'use strict';
23+
const common = require('../common');
24+
25+
if (!common.hasCrypto)
26+
common.skip('missing crypto');
27+
28+
const assert = require('assert');
29+
const https = require('https');
30+
const fs = require('fs');
31+
32+
assert.doesNotThrow(() =>
33+
https.createServer({
34+
key: fs.readFileSync(`${common.fixturesDir}/keys/agent1-key.pem`),
35+
cert: fs.readFileSync(`${common.fixturesDir}/keys/agent1-cert.pem`)
36+
}));
37+
38+
assert.throws(() =>
39+
https.createServer({
40+
key: true,
41+
cert: fs.readFileSync(`${common.fixturesDir}/keys/agent1-cert.pem`)
42+
}), /"options\.key" must not be a boolean/);
43+
44+
assert.throws(() =>
45+
https.createServer({
46+
key: false,
47+
cert: fs.readFileSync(`${common.fixturesDir}/keys/agent1-cert.pem`)
48+
}), /"options\.key" must not be a boolean/);
49+
50+
assert.throws(() =>
51+
https.createServer({
52+
key: fs.readFileSync(`${common.fixturesDir}/keys/agent1-key.pem`),
53+
cert: true
54+
}), /"options\.cert" must not be a boolean/);
55+
56+
assert.throws(() =>
57+
https.createServer({
58+
key: fs.readFileSync(`${common.fixturesDir}/keys/agent1-key.pem`),
59+
cert: false
60+
}), /"options\.cert" must not be a boolean/);
61+
62+
assert.throws(() =>
63+
https.createServer({
64+
key: false,
65+
cert: false
66+
}), /"options\.key" must not be a boolean/);
67+
68+
assert.throws(() =>
69+
https.createServer({
70+
key: true,
71+
cert: true
72+
}), /"options\.key" must not be a boolean/);
73+
74+
assert.throws(() =>
75+
https.createServer({
76+
key: true,
77+
cert: false
78+
}), /"options\.key" must not be a boolean/);
79+
80+
assert.throws(() =>
81+
https.createServer({
82+
key: false,
83+
cert: true
84+
}), /"options\.key" must not be a boolean/);

0 commit comments

Comments
 (0)