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: convertProtocols() error handling #23606

Closed
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,8 @@ E('ERR_NO_ICU',
'%s is not supported on Node.js compiled without ICU', TypeError);
E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported', Error);
E('ERR_OUT_OF_RANGE',
(name, range, value) => {
let msg = `The value of "${name}" is out of range.`;
(name, range, value, msg = null) => {
msg = msg ? msg : `The value of "${name}" is out of range.`;
if (range !== undefined) msg += ` It must be ${range}.`;
msg += ` Received ${value}`;
return msg;
Expand Down
9 changes: 8 additions & 1 deletion lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

'use strict';

const { ERR_TLS_CERT_ALTNAME_INVALID } = require('internal/errors').codes;
const {
ERR_TLS_CERT_ALTNAME_INVALID,
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const internalUtil = require('internal/util');
const internalTLS = require('internal/tls');
internalUtil.assertCrypto();
Expand Down Expand Up @@ -60,6 +63,10 @@ function convertProtocols(protocols) {
const lens = new Array(protocols.length);
const buff = Buffer.allocUnsafe(protocols.reduce((p, c, i) => {
var len = Buffer.byteLength(c);
if (len > 255) {
throw new ERR_OUT_OF_RANGE('', '<= 255', len, 'The byte length of the ' +
`protocol at index ${i} exceeds the maximum length.`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When looking at this signature it looks like a mistake that the first argument is an empty string.

I suggest to use the following instead:

ERR_OUT_OF_RANGE(str, range, input, replaceDefaultBoolean)

Using booleans as arguments is not great either but that way the first argument is at least used properly and the reader will hopefully be less surprised (as I would be). We might also use a different more expressive value than a boolean but that could be more error prone.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've refactored as per your suggestion. Check it out.

}
lens[i] = len;
return p + 1 + len;
}, 0));
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-tls-basic-validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,16 @@ common.expectsError(
assert(out.ALPNProtocols.equals(Buffer.from(expectView)));
}
}

{
const protocols = [(new String('a')).repeat(500)];
const out = {};
common.expectsError(
() => tls.convertALPNProtocols(protocols, out),
{
code: 'ERR_OUT_OF_RANGE',
message: 'The byte length of the protocol at index 0 exceeds the ' +
'maximum length. It must be <= 255. Received 500'
}
);
}