lib: avoid unsafe String methods that depend on RegExp prototype methods#36593
Closed
ExE-Boss wants to merge 1 commit intonodejs:mainfrom
Closed
lib: avoid unsafe String methods that depend on RegExp prototype methods#36593ExE-Boss wants to merge 1 commit intonodejs:mainfrom
ExE-Boss wants to merge 1 commit intonodejs:mainfrom
Conversation
aduh95
requested changes
Dec 21, 2020
| const re = new RegExp('^' + StringPrototypeReplace( | ||
| StringPrototypeReplace(servername, /([.^$+?\-\\[\]{}])/g, '\\$1'), | ||
| /\*/g, '[^.]*' | ||
| const re = new RegExp('^' + StringPrototypeReplaceAll( |
Contributor
There was a problem hiding this comment.
String.prototype.replaceAll is not available to primordials because it can be disabled by --no-harmony-string-replaceall
4ca6ac7 to
eeb3a9b
Compare
Contributor
|
Note that // User-land
RegExp.prototype[Symbol.replace] = () => 'foo';
String.prototype[Symbol.replace] = () => 'baz';
// Core
console.log(StringPrototypeReplace('ber', /e/, 'a')); // 'foo'
console.log(StringPrototypeReplace('ber', 'e', 'a')); // 'baz'
console.log(StringPrototypeReplaceAll('beer', 'e', 'a')); // 'baz'
console.log(RegExpPrototypeSymbolReplace(/e/, 'ber', 'a')); // 'bar'
console.log(RegExpPrototypeSymbolReplace(/e/g, 'beer', 'a')); // 'baar'It's hard to predict if having a RegEx is worth it performance wise, but it's something we should have in mind. |
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The string prototype methods that take
RegExparguments depend on the following properties not being mutated by user code:String.prototypemethodRegExp.prototypemethodString.prototype.matchRegExp.prototype[Symbol.match], which dependson
RegExp.prototype.execString.prototype.matchAllRegExp.prototype[Symbol.matchAll], which dependson
%RegExpStringIteratorPrototype%String.prototype.replaceRegExp.prototype[Symbol.replace]String.prototype.replaceAllRegExp.prototype[Symbol.replace]String.prototype.searchRegExp.prototype[Symbol.search]String.prototype.splitRegExp.prototype[Symbol.split]Note that the string‑taking overloads are safe‑ish.
Alternatively, it might be a good idea to create
SafeRegExp.Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passes