Skip to content

Commit 8b57b5e

Browse files
committed
tls: make Server.getTicketKeys throw instead of assert assert is for invariants and print a message about the condition being a bug in Node.js if false. throw ERR_INVALID_ARG_VALUE for publicly facing validation
1 parent e1ab0d1 commit 8b57b5e

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

lib/internal/tls/wrap.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,8 +1531,9 @@ Server.prototype.getTicketKeys = function getTicketKeys() {
15311531

15321532
Server.prototype.setTicketKeys = function setTicketKeys(keys) {
15331533
validateBuffer(keys);
1534-
assert(keys.byteLength === 48,
1535-
'Session ticket keys must be a 48-byte buffer');
1534+
if (keys.byteLength !== 48) {
1535+
throw new ERR_INVALID_ARG_VALUE('keys', keys.byteLength, 'must be exactly 48 bytes');
1536+
}
15361537
this._sharedCreds.context.setTicketKeys(keys);
15371538
};
15381539

test/parallel/test-tls-ticket-invalid-arg.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,22 @@ const server = new tls.Server();
1313
.forEach((arg) =>
1414
assert.throws(
1515
() => server.setTicketKeys(arg),
16-
{ code: 'ERR_INVALID_ARG_TYPE' }
16+
{
17+
name: 'TypeError',
18+
code: 'ERR_INVALID_ARG_TYPE',
19+
message: 'The "buffer" argument must be an instance of Buffer, TypedArray, or DataView.'
20+
+ common.invalidArgTypeHelper(arg),
21+
}
1722
));
1823

1924
[new Uint8Array(1), Buffer.from([1]), new DataView(new ArrayBuffer(2))].forEach(
2025
(arg) =>
21-
assert.throws(() => {
22-
server.setTicketKeys(arg);
23-
}, /Session ticket keys must be a 48-byte buffer/)
26+
assert.throws(
27+
() => server.setTicketKeys(arg),
28+
{
29+
name: 'TypeError',
30+
code: 'ERR_INVALID_ARG_VALUE',
31+
message: `The argument 'keys' must be exactly 48 bytes. Received ${arg.byteLength}`,
32+
}
33+
)
2434
);

0 commit comments

Comments
 (0)