Skip to content

Commit 3170cb4

Browse files
Andre Jodat-DanbraniMylesBorins
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‎

Lines changed: 4 additions & 3 deletions
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-
letmsg=`The value of "${name}" is out of range.`;
837+
(str,range,input,replaceDefaultBoolean=false)=>{
838+
letmsg=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
returnmsg;
842843
},RangeError);
843844
E('ERR_REQUIRE_ESM','Must use import to load ES Module: %s',Error);

‎lib/tls.js‎

Lines changed: 8 additions & 1 deletion
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
constinternalUtil=require('internal/util');
2629
constinternalTLS=require('internal/tls');
2730
internalUtil.assertCrypto();
@@ -59,6 +62,10 @@ function convertProtocols(protocols){
5962
constlens=newArray(protocols.length);
6063
constbuff=Buffer.allocUnsafe(protocols.reduce((p,c,i)=>{
6164
varlen=Buffer.byteLength(c);
65+
if(len>255){
66+
thrownewERR_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
returnp+1+len;
6471
},0));

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

Lines changed: 13 additions & 0 deletions
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+
constprotocols=[(newString('a')).repeat(500)];
121+
constout={};
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)