Skip to content

Commit 930a71c

Browse files
ShogunPandaaduh95
authored andcommitted
https: encode all TLS fields in agent names
`https.Agent#getName()` joined several TLS options with `:` without encoding them, so a value containing `:` could shift into the next field and produce the same name as a different set of options. A `DataView` passed as `dhparam` was also reduced to its type name. Encode the remaining string, boolean, number, buffer and array valued options, including `sigalgs`, with `getTlsOptionKey()`, which prefixes the type and the length, like the options that were already encoded. Assisted-By: OpenAI GPT-5.6 Sol <openai/gpt-5.6-sol> Signed-off-by: Paolo Insogna <paolo@cowtech.it> PR-URL: #67641 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 7363c8e commit 930a71c

3 files changed

Lines changed: 69 additions & 30 deletions

File tree

‎lib/https.js‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const {
3030
ArrayPrototypeSplice,
3131
ArrayPrototypeUnshift,
3232
FunctionPrototypeCall,
33-
JSONStringify,
3433
NumberParseInt,
3534
ObjectAssign,
3635
ObjectSetPrototypeOf,
@@ -606,7 +605,7 @@ Agent.prototype.getName = function getName(options = kEmptyObject) {
606605

607606
name += ':';
608607
if (options.ciphers)
609-
name += options.ciphers;
608+
name += getTlsOptionKey(options.ciphers);
610609

611610
name += ':';
612611
if (options.key) {
@@ -622,51 +621,51 @@ Agent.prototype.getName = function getName(options = kEmptyObject) {
622621

623622
name += ':';
624623
if (options.rejectUnauthorized !== undefined)
625-
name += options.rejectUnauthorized;
624+
name += getTlsOptionKey(options.rejectUnauthorized);
626625

627626
name += ':';
628627
if (options.servername && options.servername !== options.host)
629-
name += options.servername;
628+
name += getTlsOptionKey(options.servername);
630629

631630
name += ':';
632631
if (options.minVersion)
633-
name += options.minVersion;
632+
name += getTlsOptionKey(options.minVersion);
634633

635634
name += ':';
636635
if (options.maxVersion)
637-
name += options.maxVersion;
636+
name += getTlsOptionKey(options.maxVersion);
638637

639638
name += ':';
640639
if (options.secureProtocol)
641-
name += options.secureProtocol;
640+
name += getTlsOptionKey(options.secureProtocol);
642641

643642
name += ':';
644643
if (options.crl)
645644
name += getTlsOptionKey(options.crl);
646645

647646
name += ':';
648647
if (options.honorCipherOrder !== undefined)
649-
name += options.honorCipherOrder;
648+
name += getTlsOptionKey(options.honorCipherOrder);
650649

651650
name += ':';
652651
if (options.ecdhCurve)
653-
name += options.ecdhCurve;
652+
name += getTlsOptionKey(options.ecdhCurve);
654653

655654
name += ':';
656655
if (options.dhparam)
657-
name += options.dhparam;
656+
name += getTlsOptionKey(options.dhparam);
658657

659658
name += ':';
660659
if (options.secureOptions !== undefined)
661-
name += options.secureOptions;
660+
name += getTlsOptionKey(options.secureOptions);
662661

663662
name += ':';
664663
if (options.sessionIdContext)
665-
name += options.sessionIdContext;
664+
name += getTlsOptionKey(options.sessionIdContext);
666665

667666
name += ':';
668667
if (options.sigalgs)
669-
name += JSONStringify(options.sigalgs);
668+
name += getTlsOptionKey(options.sigalgs);
670669

671670
name += ':';
672671
if (options.secureContext)

‎test/parallel/test-https-agent-additional-options.js‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,29 @@ const updatedValues = new Map([
5656
['sessionIdContext', 'sessionIdContext'],
5757
]);
5858

59-
let value;
59+
// Every variation must be pooled under its own name, and repeating a
60+
// variation must reuse the name it had.
61+
const requested = new Set();
62+
let expectedNames = 1;
6063
function variations(iter, port, cb) {
6164
return common.mustCall((res) => {
6265
res.resume();
6366
https.globalAgent.once('free', common.mustCall(() => {
64-
// Verify that the most recent connection is in the freeSockets pool.
67+
// Verify that the connections are in the freeSockets pool, one name for
68+
// each distinct set of options.
6569
const keys = Object.keys(https.globalAgent.freeSockets);
66-
if (value) {
67-
assert.ok(
68-
keys.some((val) => val.startsWith(value.toString() + ':') ||
69-
val.endsWith(':' + value.toString()) ||
70-
val.includes(':' + value.toString() + ':')),
71-
`missing value: ${value.toString()} in ${keys}`
72-
);
73-
}
70+
assert.strictEqual(keys.length, expectedNames, `unexpected ${keys}`);
7471
const next = iter.next();
7572

7673
if (next.done) {
7774
https.globalAgent.destroy();
7875
server.close();
7976
} else {
80-
// Save `value` for check the next time.
8177
const [key, val] = next.value;
82-
value = val;
78+
if (!requested.has(key)) {
79+
requested.add(key);
80+
expectedNames++;
81+
}
8382
https.get({ ...getBaseOptions(port), [key]: val },
8483
variations(iter, port, cb));
8584
}
@@ -90,5 +89,7 @@ function variations(iter, port, cb) {
9089
server.listen(0, common.mustCall(() => {
9190
const port = server.address().port;
9291
https.globalAgent.keepAlive = true;
93-
https.get(getBaseOptions(port), variations(updatedValues.entries(), port));
92+
const all = [...updatedValues.entries()];
93+
const entries = [...all, all[0]];
94+
https.get(getBaseOptions(port), variations(entries.values(), port));
9495
}));

‎test/parallel/test-https-agent-getname.js‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ const options = {
4747

4848
assert.strictEqual(
4949
agent.getName(options),
50-
'0.0.0.0:443:192.168.1.1:s2:ca:s4:cert:ciphers:s3:key::' +
51-
's3:pfx::false:localhost:::secureProtocol:' +
52-
'a3:7:v1:Yw==:7:v1:cg==:7:v1:bA==:false:ecdhCurve:dhparam:0:' +
53-
'sessionIdContext:"sigalgs":::::::::'
50+
'0.0.0.0:443:192.168.1.1:s2:ca:s4:cert:s7:ciphers:s3:key::' +
51+
's3:pfx::b5:false:s9:localhost:::s14:secureProtocol:' +
52+
'a3:7:v1:Yw==:7:v1:cg==:7:v1:bA==:b5:false:s9:ecdhCurve:s7:dhparam:' +
53+
'n1:0:s16:sessionIdContext:s7:sigalgs:::::::::'
5454
);
5555

5656
{
@@ -156,4 +156,43 @@ assert.strictEqual(
156156
agent.getName({ ...baseOptions, session: Buffer.from('one') }),
157157
agent.getName({ ...baseOptions, session: Buffer.from('two') })
158158
);
159+
160+
// Values containing the field delimiter must not shift across fields.
161+
const collisions = [
162+
[{ servername: 'a:b' }, { servername: 'a', minVersion: 'b:' }],
163+
[{ minVersion: 'a:b' }, { minVersion: 'a', maxVersion: 'b:' }],
164+
[{ maxVersion: 'a:b' }, { maxVersion: 'a', secureProtocol: 'b:' }],
165+
[{ rejectUnauthorized: 'false:a' },
166+
{ rejectUnauthorized: 'false', servername: 'a:' }],
167+
[{ honorCipherOrder: 'a:b' }, { honorCipherOrder: 'a', ecdhCurve: 'b:' }],
168+
[{ ecdhCurve: 'a:b' }, { ecdhCurve: 'a', dhparam: 'b:' }],
169+
[{ dhparam: 'a:b' }, { dhparam: 'a', secureOptions: 'b:' }],
170+
[{ secureOptions: '1:a' }, { secureOptions: 1, sessionIdContext: 'a:' }],
171+
[{ sessionIdContext: 'a:b' }, { sessionIdContext: 'a', sigalgs: 'b:' }],
172+
[{ sigalgs: ['a:b'] }, { sigalgs: ['a', 'b'] }],
173+
[{ servername: 'a', rejectUnauthorized: false, ecdhCurve: 'P-256:P-384' },
174+
{ rejectUnauthorized: 'false:a', honorCipherOrder: 'P-256',
175+
ecdhCurve: 'P-384' }],
176+
];
177+
for (const [first, second] of collisions) {
178+
assert.notStrictEqual(
179+
agent.getName({ ...baseOptions, ...first }),
180+
agent.getName({ ...baseOptions, ...second }),
181+
`${JSON.stringify(first)} collides with ${JSON.stringify(second)}`
182+
);
183+
}
184+
185+
// The type of a value is part of its key.
186+
assert.notStrictEqual(
187+
agent.getName({ ...baseOptions, rejectUnauthorized: false }),
188+
agent.getName({ ...baseOptions, rejectUnauthorized: 'false' })
189+
);
190+
191+
assert.notStrictEqual(
192+
agent.getName({ ...baseOptions, dhparam: new DataView(new ArrayBuffer(2)) }),
193+
agent.getName({
194+
...baseOptions,
195+
dhparam: new DataView(new Uint8Array([1, 2]).buffer),
196+
})
197+
);
159198
}

0 commit comments

Comments
 (0)