From 59262de257cb0e68792309eecf01aea8aa891984 Mon Sep 17 00:00:00 2001 From: Roberto Bianchi Date: Mon, 30 Nov 2026 16:32:36 +0100 Subject: [PATCH] tls: avoid mutable push in checkServerIdentity checkServerIdentity() and its alt name splitter collected the certificate's DNS names and IP addresses with .push() on private arrays, which looks up Array.prototype.push at call time. Replacing that method from user code changed which names the hostname check saw. Use indexed assignments instead. Add a test case for an escaped alt name that follows an unescaped one, a path no existing test reached. Assisted-by: Claude Code Signed-off-by: Roberto Bianchi --- lib/tls.js | 8 ++++---- test/parallel/test-tls-check-server-identity.js | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/tls.js b/lib/tls.js index 13171847c7e8..efc60cad4d0c 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -438,7 +438,7 @@ function splitEscapedAltNames(altNames) { } else if (nextSep !== -1) { // There is a separator and no quote before it. currentToken += altNames.substring(offset, nextSep); - result.push(currentToken); + result[result.length] = currentToken; currentToken = ''; offset = nextSep + 2; } else { @@ -446,7 +446,7 @@ function splitEscapedAltNames(altNames) { offset = altNames.length; } } - result.push(currentToken); + result[result.length] = currentToken; return result; } @@ -493,9 +493,9 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) { altNames.split(', '); splitAltNames.forEach((name) => { if (name.startsWith('DNS:')) { - dnsNames.push(name.slice(4)); + dnsNames[dnsNames.length] = name.slice(4); } else if (name.startsWith('IP Address:')) { - ips.push(canonicalizeIP(name.slice(11))); + ips[ips.length] = canonicalizeIP(name.slice(11)); } }); } diff --git a/test/parallel/test-tls-check-server-identity.js b/test/parallel/test-tls-check-server-identity.js index 78ba9caf584e..79826decaf94 100644 --- a/test/parallel/test-tls-check-server-identity.js +++ b/test/parallel/test-tls-check-server-identity.js @@ -345,6 +345,13 @@ const tests = [ subject: {} } }, + // Escaped DNS name after an unescaped one + { + host: 'a.b.a.com', cert: { + subjectaltname: 'DNS:a.b.a.com, DNS:"b.a.com"', + subject: {} + } + }, { host: 'bxa.a.com', cert: {