https: isolate Agent TLS policy keys

Encode TLS option types and values in HTTPS Agent keys so distinct trust
and identity policies cannot share sockets or cached sessions. Assign
stable identities to opaque SecureContext and callback objects.

Refs: https://hackerone.com/reports/3946081

Assisted-by: codex:gpt-5.6-sol
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
Co-authored-by: Filip Skokan <panva.ip@gmail.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
Backport-PR-URL: https://github.com/nodejs-private/node-private/pull/1041
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
PR-URL: https://github.com/nodejs-private/node-private/pull/1036
CVE-ID: CVE-2026-123877
This commit is contained in:
authored and Antoine du Hamel committed 2026-11-07 13:22:38 +01:00
1 parent 62ae278a23
commit fa61009dc1
3 files changed
+383 -21

No files matched your search

+106 -16
View File
@@ -22,6 +22,7 @@
'use strict';
const {
ArrayBufferIsView,
ArrayIsArray,
ArrayPrototypeIndexOf,
ArrayPrototypePush,
@@ -35,6 +36,8 @@ const {
ObjectSetPrototypeOf,
ReflectApply,
ReflectConstruct,
SafeWeakMap,
String,
Symbol,
SymbolAsyncDispose,
} = primordials;
@@ -51,6 +54,8 @@ assertCrypto();
const tls = require('tls');
const kPerRequestCheckServerIdentity = Symbol('per-request checkServerIdentity');
let perRequestCheckServerIdentityIndex = 0;
const tlsPolicyObjectIds = new SafeWeakMap();
let tlsPolicyObjectId = 0;
const {
kProxyConfig,
checkShouldUseProxy,
@@ -510,16 +515,65 @@ Agent.prototype.keepSocketAlive = function keepSocketAlive(socket) {
return FunctionPrototypeCall(HttpAgent.prototype.keepSocketAlive, this, socket);
};
function getPfxAgentKey(pfx, passphrase) {
if (!ArrayIsArray(pfx))
return pfx;
// Encode values with their type and length so distinct TLS policies cannot
// collapse to the same Agent key. Use stable identities for opaque objects.
function getTlsOptionKey(value) {
if (value === undefined)
return '';
if (value === null)
return 'n';
let key = '';
for (let i = 0; i < pfx.length; i++) {
const value = pfx[i];
const raw = value?.buf || value;
const pass = value?.passphrase || passphrase;
key += `:${raw}:${pass}`;
if (ArrayIsArray(value)) {
let key = `a${value.length}`;
for (let i = 0; i < value.length; i++) {
const itemKey = getTlsOptionKey(value[i]);
key += `:${itemKey.length}:${itemKey}`;
}
return key;
}
if (ArrayBufferIsView(value)) {
const bytes = Buffer.from(value.buffer, value.byteOffset, value.byteLength);
return `v${bytes.byteLength}:${bytes.toString('base64')}`;
}
const type = typeof value;
if (type === 'object' || type === 'function') {
let id = tlsPolicyObjectIds.get(value);
if (id === undefined) {
id = ++tlsPolicyObjectId;
tlsPolicyObjectIds.set(value, id);
}
return `o${id}`;
}
const string = String(value);
return `${type[0]}${string.length}:${string}`;
}
function getKeyMaterialAgentKey(value, passphrase, property,
useTruthyProperties) {
if (!ArrayIsArray(value)) {
return `${getTlsOptionKey(value)}:${getTlsOptionKey(passphrase)}`;
}
let key = `a${value.length}`;
for (let i = 0; i < value.length; i++) {
const item = value[i];
let raw = item;
let pass = passphrase;
if (item !== null && typeof item === 'object' &&
!ArrayBufferIsView(item)) {
if (useTruthyProperties) {
raw = item[property] || item;
pass = item.passphrase || passphrase;
} else {
raw = item[property] !== undefined ? item[property] : item;
pass = item.passphrase !== undefined ? item.passphrase : passphrase;
}
}
const itemKey = `${getTlsOptionKey(raw)}:${getTlsOptionKey(pass)}`;
key += `:${itemKey.length}:${itemKey}`;
}
return key;
}
@@ -539,11 +593,11 @@ Agent.prototype.getName = function getName(options = kEmptyObject) {
name += ':';
if (options.ca)
name += options.ca;
name += getTlsOptionKey(options.ca);
name += ':';
if (options.cert)
name += options.cert;
name += getTlsOptionKey(options.cert);
name += ':';
if (options.clientCertEngine)
@@ -554,12 +608,16 @@ Agent.prototype.getName = function getName(options = kEmptyObject) {
name += options.ciphers;
name += ':';
if (options.key)
name += options.key;
if (options.key) {
name += getKeyMaterialAgentKey(
options.key, options.passphrase, 'pem', false);
}
name += ':';
if (options.pfx)
name += getPfxAgentKey(options.pfx, options.passphrase);
if (options.pfx) {
name += getKeyMaterialAgentKey(
options.pfx, options.passphrase, 'buf', true);
}
name += ':';
if (options.rejectUnauthorized !== undefined)
@@ -583,7 +641,7 @@ Agent.prototype.getName = function getName(options = kEmptyObject) {
name += ':';
if (options.crl)
name += options.crl;
name += getTlsOptionKey(options.crl);
name += ':';
if (options.honorCipherOrder !== undefined)
@@ -617,6 +675,38 @@ Agent.prototype.getName = function getName(options = kEmptyObject) {
if (options.privateKeyEngine)
name += options.privateKeyEngine;
name += ':';
if (options.secureContext)
name += getTlsOptionKey(options.secureContext);
name += ':';
if (options.allowPartialTrustChain !== undefined)
name += getTlsOptionKey(options.allowPartialTrustChain);
name += ':';
if (options.ALPNProtocols)
name += getTlsOptionKey(options.ALPNProtocols);
name += ':';
if (options.certificateCompression)
name += getTlsOptionKey(options.certificateCompression);
name += ':';
if (options.minDHSize !== undefined)
name += getTlsOptionKey(options.minDHSize);
name += ':';
if (options.pskCallback)
name += getTlsOptionKey(options.pskCallback);
name += ':';
if (options.requestOCSP !== undefined)
name += getTlsOptionKey(options.requestOCSP);
name += ':';
if (options.session)
name += getTlsOptionKey(options.session);
if (options[kPerRequestCheckServerIdentity])
name += `:${options[kPerRequestCheckServerIdentity]}`;