Skip to content

Commit 369f88b

Browse files
committed
Java: Fix for multiple parse mode flags.
1 parent 32c1088 commit 369f88b

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

java/ql/lib/semmle/code/java/regex/regex.qll

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,20 +472,27 @@ abstract class RegexString extends StringLiteral {
472472
)
473473
}
474474

475-
private predicate flagGroupStart(int start, int end, string c) {
475+
private predicate flagGroupStart(int start, int end) {
476476
this.isGroupStart(start) and
477477
this.getChar(start + 1) = "?" and
478-
end = start + 3 and
479-
c = this.getChar(start + 2) and
480-
c in ["i", "m", "s", "u", "x", "U"]
478+
this.getChar(start + 2) in ["i", "m", "s", "u", "x", "U"] and
479+
end = start + 2
480+
}
481+
482+
private predicate flagGroup(int start, int end, string c) {
483+
exists(int inStart, int inEnd |
484+
this.flagGroupStart(start, inStart) and
485+
this.groupContents(start, end, inStart, inEnd) and
486+
this.getChar([inStart .. inEnd - 1]) = c
487+
)
481488
}
482489

483490
/**
484491
* Gets the mode of this regular expression string if
485492
* it is defined by a prefix.
486493
*/
487494
string getModeFromPrefix() {
488-
exists(string c | this.flagGroupStart(_, _, c) |
495+
exists(string c | this.flagGroup(_, _, c) |
489496
c = "i" and result = "IGNORECASE"
490497
or
491498
c = "m" and result = "MULTILINE"
@@ -540,7 +547,7 @@ abstract class RegexString extends StringLiteral {
540547
private predicate groupStart(int start, int end) {
541548
this.nonCapturingGroupStart(start, end)
542549
or
543-
this.flagGroupStart(start, end, _)
550+
this.flagGroupStart(start, end)
544551
or
545552
this.namedGroupStart(start, end)
546553
or

java/ql/test/query-tests/security/CWE-730/ExpRedosTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class ExpRedosTest {
8787
"(?s)(.|\\n)*!", // $ hasExpRedos
8888

8989
// NOT GOOD; attack: "\n".repeat(100) + "."
90-
"(?is)(.|\\n)*!", // $ MISSING: hasExpRedos
90+
"(?is)(.|\\n)*!", // $ hasExpRedos
9191

9292
// GOOD
9393
"([\\w.]+)*",

0 commit comments

Comments
 (0)