tls: remove prototype primordials · nodejs/node@ab9adfc · GitHub
Skip to content

Commit ab9adfc

Browse files
aduh95anonrig
andcommitted
tls: remove prototype primordials
Co-authored-by: Yagiz Nizipli <yagiz@nizipli.com> PR-URL: #53699 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 6237172 commit ab9adfc

5 files changed

Lines changed: 68 additions & 96 deletions

File tree

doc/contributing/primordials.md

Lines changed: 1 addition & 0 deletions

lib/_tls_common.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
'use strict';
2323

2424
const {
25-
ArrayPrototypePush,
2625
JSONParse,
27-
RegExpPrototypeSymbolReplace,
2826
} = primordials;
2927

3028
const tls = require('tls');
@@ -133,21 +131,21 @@ function translatePeerCertificate(c) {
133131
c.infoAccess = { __proto__: null };
134132

135133
// XXX: More key validation?
136-
RegExpPrototypeSymbolReplace(/([^\n:]*):([^\n]*)(?:\n|$)/g, info,
137-
(all, key, val) => {
138-
if (val.charCodeAt(0) === 0x22) {
139-
// The translatePeerCertificate function is only
140-
// used on internally created legacy certificate
141-
// objects, and any value that contains a quote
142-
// will always be a valid JSON string literal,
143-
// so this should never throw.
144-
val = JSONParse(val);
145-
}
146-
if (key in c.infoAccess)
147-
ArrayPrototypePush(c.infoAccess[key], val);
148-
else
149-
c.infoAccess[key] = [val];
150-
});
134+
info.replace(/([^\n:]*):([^\n]*)(?:\n|$)/g,
135+
(all, key, val) => {
136+
if (val.charCodeAt(0) === 0x22) {
137+
// The translatePeerCertificate function is only
138+
// used on internally created legacy certificate
139+
// objects, and any value that contains a quote
140+
// will always be a valid JSON string literal,
141+
// so this should never throw.
142+
val = JSONParse(val);
143+
}
144+
if (key in c.infoAccess)
145+
c.infoAccess[key].push(val);
146+
else
147+
c.infoAccess[key] = [val];
148+
});
151149
}
152150
return c;
153151
}

lib/_tls_wrap.js

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,11 @@
2222
'use strict';
2323

2424
const {
25-
ArrayPrototypeForEach,
26-
ArrayPrototypeJoin,
27-
ArrayPrototypePush,
28-
FunctionPrototype,
2925
ObjectAssign,
3026
ObjectDefineProperty,
3127
ObjectSetPrototypeOf,
3228
ReflectApply,
3329
RegExp,
34-
RegExpPrototypeExec,
35-
RegExpPrototypeSymbolReplace,
36-
StringPrototypeReplaceAll,
37-
StringPrototypeSlice,
3830
Symbol,
3931
SymbolFor,
4032
} = primordials;
@@ -119,7 +111,7 @@ const kPskIdentityHint = Symbol('pskidentityhint');
119111
const kPendingSession = Symbol('pendingSession');
120112
const kIsVerified = Symbol('verified');
121113

122-
const noop = FunctionPrototype;
114+
const noop = () => {};
123115

124116
let ipServernameWarned = false;
125117
let tlsTracingWarned = false;
@@ -475,8 +467,7 @@ function onerror(err) {
475467
owner.destroy(err);
476468
} else if (owner._tlsOptions?.isServer &&
477469
owner._rejectUnauthorized &&
478-
RegExpPrototypeExec(/peer did not return a certificate/,
479-
err.message) !== null) {
470+
/peer did not return a certificate/.test(err.message)) {
480471
// Ignore server's authorization errors
481472
owner.destroy();
482473
} else {
@@ -1162,7 +1153,7 @@ function makeSocketMethodProxy(name) {
11621153
};
11631154
}
11641155

1165-
ArrayPrototypeForEach([
1156+
[
11661157
'getCipher',
11671158
'getSharedSigalgs',
11681159
'getEphemeralKeyInfo',
@@ -1173,7 +1164,7 @@ ArrayPrototypeForEach([
11731164
'getTLSTicket',
11741165
'isSessionReused',
11751166
'enableTrace',
1176-
], (method) => {
1167+
].forEach((method) => {
11771168
TLSSocket.prototype[method] = makeSocketMethodProxy(method);
11781169
});
11791170

@@ -1470,10 +1461,10 @@ Server.prototype.setSecureContext = function(options) {
14701461
if (options.sessionIdContext) {
14711462
this.sessionIdContext = options.sessionIdContext;
14721463
} else {
1473-
this.sessionIdContext = StringPrototypeSlice(
1474-
crypto.createHash('sha1')
1475-
.update(ArrayPrototypeJoin(process.argv, ' '))
1476-
.digest('hex'), 0, 32);
1464+
this.sessionIdContext = crypto.createHash('sha1')
1465+
.update(process.argv.join(' '))
1466+
.digest('hex')
1467+
.slice(0, 32);
14771468
}
14781469

14791470
if (options.sessionTimeout)
@@ -1568,10 +1559,10 @@ Server.prototype.setOptions = deprecate(function(options) {
15681559
if (options.sessionIdContext) {
15691560
this.sessionIdContext = options.sessionIdContext;
15701561
} else {
1571-
this.sessionIdContext = StringPrototypeSlice(
1572-
crypto.createHash('sha1')
1573-
.update(ArrayPrototypeJoin(process.argv, ' '))
1574-
.digest('hex'), 0, 32);
1562+
this.sessionIdContext = crypto.createHash('sha1')
1563+
.update(process.argv.join(' '))
1564+
.digest('hex')
1565+
.slice(0, 32);
15751566
}
15761567
if (options.pskCallback) this[kPskCallback] = options.pskCallback;
15771568
if (options.pskIdentityHint) this[kPskIdentityHint] = options.pskIdentityHint;
@@ -1588,14 +1579,15 @@ Server.prototype.addContext = function(servername, context) {
15881579
throw new ERR_TLS_REQUIRED_SERVER_NAME();
15891580
}
15901581

1591-
const re = new RegExp('^' + StringPrototypeReplaceAll(
1592-
RegExpPrototypeSymbolReplace(/([.^$+?\-\\[\]{}])/g, servername, '\\$1'),
1593-
'*', '[^.]*',
1594-
) + '$');
1582+
const re = new RegExp(`^${
1583+
servername
1584+
.replace(/([.^$+?\-\\[\]{}])/g, '\\$1')
1585+
.replaceAll('*', '[^.]*')
1586+
}$`);
15951587

15961588
const secureContext =
15971589
context instanceof common.SecureContext ? context : tls.createSecureContext(context);
1598-
ArrayPrototypePush(this._contexts, [re, secureContext.context]);
1590+
this._contexts.push([re, secureContext.context]);
15991591
};
16001592

16011593
Server.prototype[EE.captureRejectionSymbol] = function(
@@ -1616,7 +1608,7 @@ function SNICallback(servername, callback) {
16161608

16171609
for (let i = contexts.length - 1; i >= 0; --i) {
16181610
const elem = contexts[i];
1619-
if (RegExpPrototypeExec(elem[0], servername) !== null) {
1611+
if (elem[0].test(servername)) {
16201612
callback(null, elem[1]);
16211613
return;
16221614
}

lib/eslint.config_partial.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,16 +488,18 @@ export default [
488488
{
489489
files: [
490490
'lib/_http_*.js',
491+
'lib/_tls_*.js',
491492
'lib/http.js',
492493
'lib/http2.js',
493494
'lib/internal/http.js',
494495
'lib/internal/http2/*.js',
496+
'lib/tls.js',
495497
],
496498
rules: {
497499
'no-restricted-syntax': [
498500
...noRestrictedSyntax,
499501
{
500-
selector: 'VariableDeclarator:has(.init[name="primordials"]) Identifier[name=/Prototype/]:not([name=/^(Object|Reflect)(Get|Set)PrototypeOf$/])',
502+
selector: 'VariableDeclarator:has(.init[name="primordials"]) Identifier[name=/Prototype[A-Z]/]:not([name=/^(Object|Reflect)(Get|Set)PrototypeOf$/])',
501503
message: 'We do not use prototype primordials in this file',
502504
},
503505
],

lib/tls.js

Lines changed: 30 additions & 51 deletions

0 commit comments

Comments
 (0)