Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! lib: refactor to avoid unsafe regex primordials
  • Loading branch information
aduh95 committed Jun 18, 2022
commit ef6fc5378438d5987133c8c3fa44ec3d93dc8886
6 changes: 3 additions & 3 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const {
RangeError,
ReflectApply,
RegExpPrototypeExec,
RegExpPrototypeSymbolMatch,
SafeArrayIterator,
SafeMap,
SafeWeakMap,
Expand Down Expand Up @@ -438,8 +437,9 @@ function getMessage(key, args, self) {
return ReflectApply(msg, self, args);
}

// eslint-disable-next-line node-core/avoid-prototype-pollution
const expectedLength = RegExpPrototypeSymbolMatch(/%[dfijoOs]/g, msg).length;
const regex = /%[dfijoOs]/g;
let expectedLength = 0;
while (RegExpPrototypeExec(regex, msg) !== null) expectedLength++;
assert(
expectedLength === args.length,
`Code: ${key}; The provided arguments length (${args.length}) does not ` +
Expand Down
12 changes: 7 additions & 5 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ const {
ReflectApply,
RegExp,
RegExpPrototypeExec,
RegExpPrototypeSymbolMatch,
RegExpPrototypeSymbolReplace,
RegExpPrototypeSymbolSplit,
SafeSet,
Expand Down Expand Up @@ -1641,13 +1640,16 @@ function _memory(cmd) {
// I need to know "depth."
// Because I can not tell the difference between a } that
// closes an object literal and a } that closes a function
const countMatches = (regex, str) => {
let count = 0;
while (RegExpPrototypeExec(regex, str) !== null) count++;
return count;
};

// Going down is { and ( e.g. function() {
// going up is } and )
// eslint-disable-next-line node-core/avoid-prototype-pollution
const dw = RegExpPrototypeSymbolMatch(/[{(]/g, cmd);
// eslint-disable-next-line node-core/avoid-prototype-pollution
const up = RegExpPrototypeSymbolMatch(/[})]/g, cmd);
const dw = countMatches(/[{(]/g, cmd);
const up = countMatches(/[})]/g, cmd);
let depth = dw.length - up.length;

if (depth) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-errors-systemerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ assert.throws(
() => { new SystemError(); },
{
name: 'TypeError',
message: 'String.prototype.match called on null or undefined'
message: "Cannot read properties of undefined (reading 'syscall')",
}
);

Expand Down