Skip to content

Commit 4ca6ac7

Browse files
committed
lib: avoid unsafe String methods that depend on RegExp prototype methods
1 parent ce658bd commit 4ca6ac7

File tree

23 files changed

+120
-111
lines changed

23 files changed

+120
-111
lines changed

lib/_tls_common.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const {
2727
ArrayPrototypeJoin,
2828
ArrayPrototypePush,
2929
ObjectCreate,
30-
StringPrototypeReplace,
30+
RegExpPrototypeSymbolReplace,
3131
StringPrototypeSplit,
3232
StringPrototypeStartsWith,
3333
} = primordials;
@@ -394,13 +394,15 @@ exports.translatePeerCertificate = function translatePeerCertificate(c) {
394394
c.infoAccess = ObjectCreate(null);
395395

396396
// XXX: More key validation?
397-
StringPrototypeReplace(info, /([^\n:]*):([^\n]*)(?:\n|$)/g,
398-
(all, key, val) => {
399-
if (key in c.infoAccess)
400-
ArrayPrototypePush(c.infoAccess[key], val);
401-
else
402-
c.infoAccess[key] = [val];
403-
});
397+
RegExpPrototypeSymbolReplace(
398+
/([^\n:]*):([^\n]*)(?:\n|$)/g,
399+
info,
400+
(all, key, val) => {
401+
if (key in c.infoAccess)
402+
ArrayPrototypePush(c.infoAccess[key], val);
403+
else
404+
c.infoAccess[key] = [val];
405+
});
404406
}
405407
return c;
406408
};

lib/_tls_wrap.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ const {
3131
ObjectSetPrototypeOf,
3232
ReflectApply,
3333
RegExp,
34+
RegExpPrototypeSymbolReplace,
3435
RegExpPrototypeTest,
35-
StringPrototypeReplace,
36+
StringPrototypeReplaceAll,
3637
StringPrototypeSlice,
3738
Symbol,
3839
SymbolFor,
@@ -1433,9 +1434,10 @@ Server.prototype.addContext = function(servername, context) {
14331434
throw new ERR_TLS_REQUIRED_SERVER_NAME();
14341435
}
14351436

1436-
const re = new RegExp('^' + StringPrototypeReplace(
1437-
StringPrototypeReplace(servername, /([.^$+?\-\\[\]{}])/g, '\\$1'),
1438-
/\*/g, '[^.]*'
1437+
const re = new RegExp('^' + StringPrototypeReplaceAll(
1438+
RegExpPrototypeSymbolReplace(/([.^$+?\-\\[\]{}])/g, servername, '\\$1'),
1439+
'*',
1440+
'[^.]*',
14391441
) + '$');
14401442
ArrayPrototypePush(this._contexts,
14411443
[re, tls.createSecureContext(context).context]);

lib/assert.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ const {
3535
ObjectKeys,
3636
ObjectPrototypeIsPrototypeOf,
3737
ReflectApply,
38+
RegExpPrototypeSymbolReplace,
3839
RegExpPrototypeTest,
3940
SafeMap,
4041
String,
4142
StringPrototypeCharCodeAt,
4243
StringPrototypeIncludes,
4344
StringPrototypeIndexOf,
44-
StringPrototypeReplace,
45+
StringPrototypeReplaceAll,
4546
StringPrototypeSlice,
4647
StringPrototypeSplit,
4748
StringPrototypeStartsWith,
@@ -271,9 +272,10 @@ function parseCode(code, offset) {
271272

272273
return [
273274
node.node.start,
274-
StringPrototypeReplace(StringPrototypeSlice(code,
275-
node.node.start, node.node.end),
276-
escapeSequencesRegExp, escapeFn)
275+
RegExpPrototypeSymbolReplace(
276+
escapeSequencesRegExp,
277+
StringPrototypeSlice(code, node.node.start, node.node.end),
278+
escapeFn)
277279
];
278280
}
279281

@@ -345,7 +347,7 @@ function getErrMessage(message, fn) {
345347
// Always normalize indentation, otherwise the message could look weird.
346348
if (StringPrototypeIncludes(message, '\n')) {
347349
if (EOL === '\r\n') {
348-
message = StringPrototypeReplace(message, /\r\n/g, '\n');
350+
message = StringPrototypeReplaceAll(message, '\r\n', '\n');
349351
}
350352
const frames = StringPrototypeSplit(message, '\n');
351353
message = ArrayPrototypeShift(frames);

lib/buffer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ const {
3636
ObjectDefineProperties,
3737
ObjectDefineProperty,
3838
ObjectSetPrototypeOf,
39+
RegExpPrototypeSymbolReplace,
3940
StringPrototypeCharCodeAt,
40-
StringPrototypeReplace,
4141
StringPrototypeSlice,
4242
StringPrototypeToLowerCase,
4343
StringPrototypeTrim,
@@ -812,8 +812,8 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
812812
const max = INSPECT_MAX_BYTES;
813813
const actualMax = MathMin(max, this.length);
814814
const remaining = this.length - max;
815-
let str = StringPrototypeTrim(StringPrototypeReplace(
816-
this.hexSlice(0, actualMax), /(.{2})/g, '$1 '));
815+
let str = StringPrototypeTrim(RegExpPrototypeSymbolReplace(
816+
/(.{2})/g, this.hexSlice(0, actualMax), '$1 '));
817817
if (remaining > 0)
818818
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
819819
// Inspect special properties as well, if possible.

lib/internal/console/constructor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const {
2525
StringPrototypeIncludes,
2626
StringPrototypePadStart,
2727
StringPrototypeRepeat,
28-
StringPrototypeReplace,
28+
StringPrototypeReplaceAll,
2929
StringPrototypeSlice,
3030
StringPrototypeSplit,
3131
Symbol,
@@ -266,7 +266,7 @@ ObjectDefineProperties(Console.prototype, {
266266

267267
if (groupIndent.length !== 0) {
268268
if (StringPrototypeIncludes(string, '\n')) {
269-
string = StringPrototypeReplace(string, /\n/g, `\n${groupIndent}`);
269+
string = StringPrototypeReplaceAll(string, '\n', `\n${groupIndent}`);
270270
}
271271
string = groupIndent + string;
272272
}

lib/internal/dns/utils.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const {
88
ArrayPrototypePush,
99
FunctionPrototypeBind,
1010
NumberParseInt,
11-
StringPrototypeMatch,
12-
StringPrototypeReplace,
11+
RegExpPrototypeExec,
12+
RegExpPrototypeSymbolReplace,
1313
} = primordials;
1414

1515
const errors = require('internal/errors');
@@ -79,21 +79,22 @@ class Resolver {
7979
if (ipVersion !== 0)
8080
return ArrayPrototypePush(newSet, [ipVersion, serv, IANA_DNS_PORT]);
8181

82-
const match = StringPrototypeMatch(serv, IPv6RE);
82+
const match = RegExpPrototypeExec(IPv6RE, serv);
8383

8484
// Check for an IPv6 in brackets.
8585
if (match) {
8686
ipVersion = isIP(match[1]);
8787

8888
if (ipVersion !== 0) {
8989
const port = NumberParseInt(
90-
StringPrototypeReplace(serv, addrSplitRE, '$2')) || IANA_DNS_PORT;
90+
RegExpPrototypeSymbolReplace(addrSplitRE, serv, '$2')
91+
) || IANA_DNS_PORT;
9192
return ArrayPrototypePush(newSet, [ipVersion, match[1], port]);
9293
}
9394
}
9495

9596
// addr::port
96-
const addrSplitMatch = StringPrototypeMatch(serv, addrSplitRE);
97+
const addrSplitMatch = RegExpPrototypeExec(addrSplitRE, serv);
9798

9899
if (addrSplitMatch) {
99100
const hostIP = addrSplitMatch[1];

lib/internal/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ const {
3434
ObjectKeys,
3535
RangeError,
3636
ReflectApply,
37+
RegExpPrototypeExec,
3738
RegExpPrototypeTest,
3839
SafeMap,
3940
SafeWeakMap,
4041
String,
4142
StringPrototypeEndsWith,
4243
StringPrototypeIncludes,
43-
StringPrototypeMatch,
4444
StringPrototypeSlice,
4545
StringPrototypeSplit,
4646
StringPrototypeStartsWith,
@@ -381,7 +381,7 @@ function getMessage(key, args, self) {
381381
}
382382

383383
const expectedLength =
384-
(StringPrototypeMatch(msg, /%[dfijoOs]/g) || []).length;
384+
(RegExpPrototypeExec(/%[dfijoOs]/g, msg) || []).length;
385385
assert(
386386
expectedLength === args.length,
387387
`Code: ${key}; The provided arguments length (${args.length}) does not ` +

lib/internal/fs/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const {
1818
ReflectOwnKeys,
1919
StringPrototypeEndsWith,
2020
StringPrototypeIncludes,
21-
StringPrototypeReplace,
21+
StringPrototypeReplaceAll,
2222
Symbol,
2323
TypedArrayPrototypeIncludes,
2424
} = primordials;
@@ -369,7 +369,7 @@ function preprocessSymlinkDestination(path, type, linkPath) {
369369
return pathModule.toNamespacedPath(path);
370370
}
371371
// Windows symlinks don't tolerate forward slashes.
372-
return StringPrototypeReplace(path, /\//g, '\\');
372+
return StringPrototypeReplaceAll(path, '/', '\\');
373373
}
374374

375375
// Constructor for file stats.

lib/internal/main/print_help.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ const {
88
MathMax,
99
ObjectKeys,
1010
RegExp,
11+
RegExpPrototypeSymbolReplace,
1112
StringPrototypeTrimLeft,
1213
StringPrototypeRepeat,
13-
StringPrototypeReplace,
1414
SafeMap,
1515
} = primordials;
1616

@@ -72,14 +72,14 @@ const envVars = new SafeMap(ArrayPrototypeConcat([
7272

7373

7474
function indent(text, depth) {
75-
return StringPrototypeReplace(text, /^/gm, StringPrototypeRepeat(' ', depth));
75+
return RegExpPrototypeSymbolReplace(/^/gm, text, StringPrototypeRepeat(' ', depth));
7676
}
7777

7878
function fold(text, width) {
79-
return StringPrototypeReplace(text,
80-
new RegExp(`([^\n]{0,${width}})( |$)`, 'g'),
81-
(_, newLine, end) =>
82-
newLine + (end === ' ' ? '\n' : ''));
79+
return RegExpPrototypeSymbolReplace(
80+
new RegExp(`([^\n]{0,${width}})( |$)`, 'g'),
81+
text,
82+
(_, newLine, end) => newLine + (end === ' ' ? '\n' : ''));
8383
}
8484

8585
function getArgDescription(type) {

lib/internal/modules/cjs/loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const {
4646
ObjectSetPrototypeOf,
4747
ReflectApply,
4848
ReflectSet,
49+
RegExpPrototypeExec,
4950
RegExpPrototypeTest,
5051
SafeMap,
5152
SafeWeakMap,
@@ -55,7 +56,6 @@ const {
5556
StringPrototypeEndsWith,
5657
StringPrototypeLastIndexOf,
5758
StringPrototypeIndexOf,
58-
StringPrototypeMatch,
5959
StringPrototypeSlice,
6060
StringPrototypeSplit,
6161
StringPrototypeStartsWith,
@@ -463,7 +463,7 @@ const EXPORTS_PATTERN = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/;
463463
function resolveExports(nmPath, request) {
464464
// The implementation's behavior is meant to mirror resolution in ESM.
465465
const [, name, expansion = ''] =
466-
StringPrototypeMatch(request, EXPORTS_PATTERN) || [];
466+
RegExpPrototypeExec(EXPORTS_PATTERN, request) || [];
467467
if (!name)
468468
return;
469469
const pkgPath = path.resolve(nmPath, name);

0 commit comments

Comments
 (0)