Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit db352fb

Browse files
committedSep 13, 2017
tls: re-allow falsey option values
5723c4c was an unintentional breaking change in that it changed the behaviour of `tls.createSecureContext()` to throw on false-y input rather than ignoring it. This breaks real-world applications like `npm`. This restores the previous behaviour. PR-URL: nodejs#15131 Ref: nodejs#15053 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Alexey Orlenko <[email protected]> Reviewed-By: MichaëZasso <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent 83f797b commit db352fb

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed
 

‎lib/_tls_common.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ exports.createSecureContext = function createSecureContext(options, context) {
8080
// NOTE: It's important to add CA before the cert to be able to load
8181
// cert's issuer in C++ code.
8282
var ca = options.ca;
83-
if (ca !== undefined) {
83+
if (ca) {
8484
if (Array.isArray(ca)) {
8585
for (i = 0; i < ca.length; ++i) {
8686
val = ca[i];
@@ -96,7 +96,7 @@ exports.createSecureContext = function createSecureContext(options, context) {
9696
}
9797

9898
var cert = options.cert;
99-
if (cert !== undefined) {
99+
if (cert) {
100100
if (Array.isArray(cert)) {
101101
for (i = 0; i < cert.length; ++i) {
102102
val = cert[i];
@@ -115,7 +115,7 @@ exports.createSecureContext = function createSecureContext(options, context) {
115115
// which leads to the crash later on.
116116
var key = options.key;
117117
var passphrase = options.passphrase;
118-
if (key !== undefined) {
118+
if (key) {
119119
if (Array.isArray(key)) {
120120
for (i = 0; i < key.length; ++i) {
121121
val = key[i];

‎test/parallel/test-tls-options-boolean-check.js

+41-23
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,9 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
6464
[false, [certStr, certStr2]],
6565
[[{ pem: keyBuff }], false],
6666
[[{ pem: keyBuff }, { pem: keyBuff }], false]
67-
].map((params) => {
67+
].map(([key, cert]) => {
6868
assert.doesNotThrow(() => {
69-
tls.createServer({
70-
key: params[0],
71-
cert: params[1]
72-
});
69+
tls.createServer({ key, cert });
7370
});
7471
});
7572

@@ -100,16 +97,13 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
10097
[[keyStr, keyStr2], [true, false], invalidCertRE],
10198
[[keyStr, keyStr2], true, invalidCertRE],
10299
[true, [certBuff, certBuff2], invalidKeyRE]
103-
].map((params) => {
100+
].map(([key, cert, message]) => {
104101
assert.throws(() => {
105-
tls.createServer({
106-
key: params[0],
107-
cert: params[1]
108-
});
102+
tls.createServer({ key, cert });
109103
}, common.expectsError({
110104
code: 'ERR_INVALID_ARG_TYPE',
111105
type: TypeError,
112-
message: params[2]
106+
message
113107
}));
114108
});
115109

@@ -123,13 +117,9 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
123117
[keyBuff, certBuff, caArrBuff],
124118
[keyBuff, certBuff, caArrDataView],
125119
[keyBuff, certBuff, false],
126-
].map((params) => {
120+
].map(([key, cert, ca]) => {
127121
assert.doesNotThrow(() => {
128-
tls.createServer({
129-
key: params[0],
130-
cert: params[1],
131-
ca: params[2]
132-
});
122+
tls.createServer({ key, cert, ca });
133123
});
134124
});
135125

@@ -141,16 +131,44 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
141131
[keyBuff, certBuff, 1],
142132
[keyBuff, certBuff, true],
143133
[keyBuff, certBuff, [caCert, true]]
144-
].map((params) => {
134+
].map(([key, cert, ca]) => {
145135
assert.throws(() => {
146-
tls.createServer({
147-
key: params[0],
148-
cert: params[1],
149-
ca: params[2]
150-
});
136+
tls.createServer({ key, cert, ca });
151137
}, common.expectsError({
152138
code: 'ERR_INVALID_ARG_TYPE',
153139
type: TypeError,
154140
message: /^The "ca" argument must be one of type string, Buffer, TypedArray, or DataView$/
155141
}));
156142
});
143+
144+
// Checks to ensure tls.createServer throws an error for CA assignment
145+
// Format ['key', 'cert', 'ca']
146+
[
147+
[keyBuff, certBuff, true],
148+
[keyBuff, certBuff, {}],
149+
[keyBuff, certBuff, 1],
150+
[keyBuff, certBuff, true],
151+
[keyBuff, certBuff, [caCert, true]]
152+
].map(([key, cert, ca]) => {
153+
assert.throws(() => {
154+
tls.createServer({ key, cert, ca });
155+
}, common.expectsError({
156+
code: 'ERR_INVALID_ARG_TYPE',
157+
type: TypeError,
158+
message: /^The "ca" argument must be one of type string, Buffer, TypedArray, or DataView$/
159+
}));
160+
});
161+
162+
// Checks to ensure tls.createSecureContext works with false-y input
163+
// Format ['key', 'cert', 'ca']
164+
[
165+
[null, null, null],
166+
[false, false, false],
167+
[undefined, undefined, undefined],
168+
['', '', ''],
169+
[0, 0, 0]
170+
].map(([key, cert, ca]) => {
171+
assert.doesNotThrow(() => {
172+
tls.createSecureContext({ key, cert, ca });
173+
});
174+
});

0 commit comments

Comments
 (0)
Please sign in to comment.