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

tls: re-allow falsey option values #15131

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ exports.createSecureContext = function createSecureContext(options, context) {
// NOTE: It's important to add CA before the cert to be able to load
// cert's issuer in C++ code.
var ca = options.ca;
if (ca !== undefined) {
if (ca) {
if (Array.isArray(ca)) {
for (i = 0; i < ca.length; ++i) {
val = ca[i];
Expand All @@ -96,7 +96,7 @@ exports.createSecureContext = function createSecureContext(options, context) {
}

var cert = options.cert;
if (cert !== undefined) {
if (cert) {
if (Array.isArray(cert)) {
for (i = 0; i < cert.length; ++i) {
val = cert[i];
Expand All @@ -115,7 +115,7 @@ exports.createSecureContext = function createSecureContext(options, context) {
// which leads to the crash later on.
var key = options.key;
var passphrase = options.passphrase;
if (key !== undefined) {
if (key) {
if (Array.isArray(key)) {
for (i = 0; i < key.length; ++i) {
val = key[i];
Expand Down
64 changes: 41 additions & 23 deletions test/parallel/test-tls-options-boolean-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
[false, [certStr, certStr2]],
[[{ pem: keyBuff }], false],
[[{ pem: keyBuff }, { pem: keyBuff }], false]
].map((params) => {
].map(([key, cert]) => {
assert.doesNotThrow(() => {
tls.createServer({
key: params[0],
cert: params[1]
});
tls.createServer({ key, cert });
});
});

Expand Down Expand Up @@ -100,16 +97,13 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
[[keyStr, keyStr2], [true, false], invalidCertRE],
[[keyStr, keyStr2], true, invalidCertRE],
[true, [certBuff, certBuff2], invalidKeyRE]
].map((params) => {
].map(([key, cert, message]) => {
assert.throws(() => {
tls.createServer({
key: params[0],
cert: params[1]
});
tls.createServer({ key, cert });
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: params[2]
message
}));
});

Expand All @@ -123,13 +117,9 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
[keyBuff, certBuff, caArrBuff],
[keyBuff, certBuff, caArrDataView],
[keyBuff, certBuff, false],
].map((params) => {
].map(([key, cert, ca]) => {
assert.doesNotThrow(() => {
tls.createServer({
key: params[0],
cert: params[1],
ca: params[2]
});
tls.createServer({ key, cert, ca });
});
});

Expand All @@ -141,16 +131,44 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
[keyBuff, certBuff, 1],
[keyBuff, certBuff, true],
[keyBuff, certBuff, [caCert, true]]
].map((params) => {
].map(([key, cert, ca]) => {
assert.throws(() => {
tls.createServer({
key: params[0],
cert: params[1],
ca: params[2]
});
tls.createServer({ key, cert, ca });
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "ca" argument must be one of type string, Buffer, TypedArray, or DataView$/
}));
});

// Checks to ensure tls.createServer throws an error for CA assignment
// Format ['key', 'cert', 'ca']
[
[keyBuff, certBuff, true],
[keyBuff, certBuff, {}],
[keyBuff, certBuff, 1],
[keyBuff, certBuff, true],
[keyBuff, certBuff, [caCert, true]]
].map(([key, cert, ca]) => {
assert.throws(() => {
tls.createServer({ key, cert, ca });
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "ca" argument must be one of type string, Buffer, TypedArray, or DataView$/
}));
});

// Checks to ensure tls.createSecureContext works with false-y input
// Format ['key', 'cert', 'ca']
[
[null, null, null],
[false, false, false],
[undefined, undefined, undefined],
['', '', ''],
[0, 0, 0]
].map(([key, cert, ca]) => {
assert.doesNotThrow(() => {
tls.createSecureContext({ key, cert, ca });
});
});