Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS: Enabled Regular Expression Unicode Sets #18055

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions javascript/extractor/src/com/semmle/jcorn/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ private Token readRegexp() {
String validFlags = "gim";
if (this.options.ecmaVersion() >= 6) validFlags = "gimuy";
if (this.options.ecmaVersion() >= 9) validFlags = "gimsuy";
if (this.options.ecmaVersion() >= 12) validFlags = "gimsuyv";
if (!mods.matches("^[" + validFlags + "]*$"))
this.raise(start, "Invalid regular expression flag");
if (mods.indexOf('u') >= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public static enum ECMAVersion {
ECMA2017(2017, 8),
ECMA2018(2018, 9),
ECMA2019(2019, 10),
ECMA2020(2020, 11);
ECMA2020(2020, 11),
ECMA2021(2021, 12);

private final int version;
public final int legacyVersion;
Expand Down Expand Up @@ -232,7 +233,7 @@ public Set<String> getPredefinedGlobals() {
private VirtualSourceRoot virtualSourceRoot;

public ExtractorConfig(boolean experimental) {
this.ecmaVersion = experimental ? ECMAVersion.ECMA2020 : ECMAVersion.ECMA2019;
this.ecmaVersion = experimental ? ECMAVersion.ECMA2021 : ECMAVersion.ECMA2019;
this.platform = Platform.AUTO;
this.jsx = true;
this.sourceType = SourceType.AUTO;
Expand Down
6 changes: 6 additions & 0 deletions javascript/ql/lib/semmle/javascript/Expr.qll
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@
/** Holds if this regular expression has an `s` flag. */
predicate isDotAll() { RegExp::isDotAll(this.getFlags()) }

// /** Holds if this regular expression has an `u` flag. */
predicate isUnicode() { RegExp::isUnicode(this.getFlags()) }

Check warning on line 485 in javascript/ql/lib/semmle/javascript/Expr.qll

View workflow job for this annotation

GitHub Actions / qldoc

Missing QLdoc for member-predicate Expr::RegExpLiteral::isUnicode/0

/** Holds if this regular expression has an `v` flag. */
predicate isUnicodeSets() { RegExp::isUnicodeSets(this.getFlags()) }

override string getAPrimaryQlClass() { result = "RegExpLiteral" }
}

Expand Down
12 changes: 12 additions & 0 deletions javascript/ql/lib/semmle/javascript/Regexp.qll
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,14 @@ module RegExp {
bindingset[flags]
predicate isDotAll(string flags) { flags.matches("%s%") }

/** Holds if `flags` includes the `u` flag. */
bindingset[flags]
predicate isUnicode(string flags) { flags.matches("%u%") }

/** Holds if `flags` includes the `v` flag. */
bindingset[flags]
predicate isUnicodeSets(string flags) { flags.matches("%v%") }

/** Holds if `flags` includes the `m` flag or is the unknown flag `?`. */
bindingset[flags]
predicate maybeMultiline(string flags) { flags = unknownFlag() or isMultiline(flags) }
Expand All @@ -1178,6 +1186,10 @@ module RegExp {
bindingset[flags]
predicate maybeDotAll(string flags) { flags = unknownFlag() or isDotAll(flags) }

/** Holds if `flags` includes the `s` flag or is the unknown flag `?`. */
bindingset[flags]
predicate maybeUnicodeSets(string flags) { flags = unknownFlag() or isUnicodeSets(flags) }

/** Holds if `term` and all of its disjuncts are anchored on both ends. */
predicate isFullyAnchoredTerm(RegExpTerm term) {
exists(RegExpSequence seq | term = seq |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import javascript
from RegExpLiteral literal, RegExpConstant wideConstant
where
wideConstant.getLiteral() = literal and
not literal.getFlags().matches("%u%") and
not (literal.isUnicode() or literal.isUnicodeSets()) and
wideConstant.getValue().length() > 1 and
(
wideConstant.getParent() instanceof RegExpCharacterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/[𒍀-𒍅]/u; // OK
/𒍀+/; // NOT OK
/𒍀+/u; // OK
/𒍀+/v; // OK
/(𒍀)+/; // OK
Loading