Skip to content

Commit 3170cb4

Browse files
Andre Jodat-DanbraniMylesBorins
Andre Jodat-Danbrani
authored andcommitted
tls: throw if protocol too long
The convertProtocols() function now throws a range error when the byte length of a protocol is too long to fit in a Buffer. Also added a test case in test/parallel/test-tls-basic-validations.js to cover this. PR-URL: #23606 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 15d05bb commit 3170cb4

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

lib/internal/errors.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,11 @@ E('ERR_NO_ICU',
834834
'%s is not supported on Node.js compiled without ICU', TypeError);
835835
E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported', Error);
836836
E('ERR_OUT_OF_RANGE',
837-
(name, range, value) => {
838-
let msg = `The value of "${name}" is out of range.`;
837+
(str, range, input, replaceDefaultBoolean = false) => {
838+
let msg = replaceDefaultBoolean ? str :
839+
`The value of "${str}" is out of range.`;
839840
if (range !== undefined) msg += ` It must be ${range}.`;
840-
msg += ` Received ${value}`;
841+
msg += ` Received ${input}`;
841842
return msg;
842843
}, RangeError);
843844
E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error);

lib/tls.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222
'use strict';
2323

24-
const { ERR_TLS_CERT_ALTNAME_INVALID } = require('internal/errors').codes;
24+
const {
25+
ERR_TLS_CERT_ALTNAME_INVALID,
26+
ERR_OUT_OF_RANGE
27+
} = require('internal/errors').codes;
2528
const internalUtil = require('internal/util');
2629
const internalTLS = require('internal/tls');
2730
internalUtil.assertCrypto();
@@ -59,6 +62,10 @@ function convertProtocols(protocols) {
5962
const lens = new Array(protocols.length);
6063
const buff = Buffer.allocUnsafe(protocols.reduce((p, c, i) => {
6164
var len = Buffer.byteLength(c);
65+
if (len > 255) {
66+
throw new ERR_OUT_OF_RANGE('The byte length of the protocol at index ' +
67+
`${i} exceeds the maximum length.`, '<= 255', len, true);
68+
}
6269
lens[i] = len;
6370
return p + 1 + len;
6471
}, 0));

test/parallel/test-tls-basic-validations.js

+13
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,16 @@ common.expectsError(
115115
tls.convertNPNProtocols(buffer, out);
116116
assert(out.NPNProtocols.equals(Buffer.from('abcd')));
117117
}
118+
119+
{
120+
const protocols = [(new String('a')).repeat(500)];
121+
const out = {};
122+
common.expectsError(
123+
() => tls.convertALPNProtocols(protocols, out),
124+
{
125+
code: 'ERR_OUT_OF_RANGE',
126+
message: 'The byte length of the protocol at index 0 exceeds the ' +
127+
'maximum length. It must be <= 255. Received 500'
128+
}
129+
);
130+
}

0 commit comments

Comments
 (0)