From b2dd7ecee9509873c965e9d41b959cce79559375 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 16 May 2025 15:56:26 -0700 Subject: [PATCH] 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. --- lib/internal/tls/wrap.js | 5 +++-- test/parallel/test-tls-ticket-invalid-arg.js | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/internal/tls/wrap.js b/lib/internal/tls/wrap.js index ceb770ab336646..536c188b2322aa 100644 --- a/lib/internal/tls/wrap.js +++ b/lib/internal/tls/wrap.js @@ -1530,8 +1530,9 @@ Server.prototype.getTicketKeys = function getTicketKeys() { Server.prototype.setTicketKeys = function setTicketKeys(keys) { validateBuffer(keys); - assert(keys.byteLength === 48, - 'Session ticket keys must be a 48-byte buffer'); + if (keys.byteLength !== 48) { + throw new ERR_INVALID_ARG_VALUE('keys', keys.byteLength, 'must be exactly 48 bytes'); + } this._sharedCreds.context.setTicketKeys(keys); }; diff --git a/test/parallel/test-tls-ticket-invalid-arg.js b/test/parallel/test-tls-ticket-invalid-arg.js index 55143cdca31e77..b436f518aa9025 100644 --- a/test/parallel/test-tls-ticket-invalid-arg.js +++ b/test/parallel/test-tls-ticket-invalid-arg.js @@ -13,12 +13,22 @@ const server = new tls.Server(); .forEach((arg) => assert.throws( () => server.setTicketKeys(arg), - { code: 'ERR_INVALID_ARG_TYPE' } + { + name: 'TypeError', + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "buffer" argument must be an instance of Buffer, TypedArray, or DataView.' + + common.invalidArgTypeHelper(arg), + } )); [new Uint8Array(1), Buffer.from([1]), new DataView(new ArrayBuffer(2))].forEach( (arg) => - assert.throws(() => { - server.setTicketKeys(arg); - }, /Session ticket keys must be a 48-byte buffer/) + assert.throws( + () => server.setTicketKeys(arg), + { + name: 'TypeError', + code: 'ERR_INVALID_ARG_VALUE', + message: `The argument 'keys' must be exactly 48 bytes. Received ${arg.byteLength}`, + } + ) );