Skip to content

Commit d92430b

Browse files
committed
JS: Fix FP from char class
1 parent 9e41166 commit d92430b

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

javascript/ql/src/Security/CWE-178/CaseSensitiveMiddlewarePath.ql

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ string invertCase(string s) {
2020
if s.regexpMatch(".*[a-z].*") then result = s.toUpperCase() else result = s.toLowerCase()
2121
}
2222

23+
RegExpCharacterClass getEnclosingClass(RegExpTerm term) {
24+
term = result.getAChild()
25+
or
26+
term = result.getAChild().(RegExpRange).getAChild()
27+
}
28+
2329
/**
2430
* Holds if `term` distinguishes between upper and lower case letters, assuming the `i` flag is not present.
2531
*/
@@ -28,7 +34,7 @@ predicate isCaseSensitiveRegExp(RegExpTerm term) {
2834
exists(RegExpConstant const |
2935
const = term.getAChild*() and
3036
const.getValue().regexpMatch(".*[a-zA-Z].*") and
31-
not const.getParent().(RegExpCharacterClass).getAChild().(RegExpConstant).getValue() =
37+
not getEnclosingClass(const).getAChild().(RegExpConstant).getValue() =
3238
invertCase(const.getValue()) and
3339
not const.getParent*() instanceof RegExpNegativeLookahead and
3440
not const.getParent*() instanceof RegExpNegativeLookbehind
@@ -59,8 +65,11 @@ string getExampleString(RegExpTerm term) {
5965
}
6066

6167
string getCaseSensitiveBypassExample(RegExpTerm term) {
62-
result = invertCase(getExampleString(term)) and
63-
result != ""
68+
exists(string example |
69+
example = getExampleString(term) and
70+
result = invertCase(example) and
71+
result != example // getting an example string is approximate; ensure we got a proper case-change example
72+
)
6473
}
6574

6675
/**
@@ -83,7 +92,7 @@ predicate isCaseSensitiveMiddleware(
8392
isCaseSensitiveRegExp(regexp.getRoot()) and
8493
exists(string flags |
8594
flags = regexp.getFlags() and
86-
not flags.matches("%i%")
95+
not RegExp::isIgnoreCase(flags)
8796
)
8897
)
8998
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const express = require('express');
2+
const app = express();
3+
4+
app.get(/\/[a-zA-Z]+/, (req, res, next) => { // OK - regexp term is case insensitive
5+
next();
6+
});
7+
8+
app.get('/foo', (req, res) => {
9+
});

0 commit comments

Comments
 (0)