Skip to content

Commit de57b2b

Browse files
authored
QL: add ql/rexexp-pattern
1 parent da6a915 commit de57b2b

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @name RegexpInsteadOfPattern
3+
* @description The `matches` builtin predicate takes a special pattern format as an input, not a regular expression.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id ql/rexexp-pattern
7+
* @precision medium
8+
*/
9+
10+
import ql
11+
12+
/**
13+
* Gets a regular expression pattern that matches the syntax of likely regular expressions.
14+
*/
15+
private string getALikelyRegExpPattern() {
16+
result = "/.*/[gimuy]{1,5}" or // pattern with at least one flag: /foo/i
17+
result = "/\\^.*/[gimuy]{0,5}" or // pattern with anchor: /^foo/
18+
result = "/.*\\$/[gimuy]{0,5}" or // pattern with anchor: /foo$/
19+
result = "\\^.*\\$" or // pattern body with anchors: ^foo$
20+
result = ".*(?<!\\\\)\\\\[dDwWsSB].*" or // contains a builtin character class: \s
21+
result = ".*(?<!\\\\)\\\\[\\[\\]()*+?{}|^$.].*" or // contains an escaped meta-character: \(
22+
result = ".*\\[\\^?[\\p{Alnum}\\p{Blank}_-]+\\][*+].*" // contains a quantified custom character class: [^a-zA-Z123]+
23+
}
24+
25+
predicate isMatchesCall(MemberCall c) { c.getMemberName() = "matches" }
26+
27+
from Call c, String arg
28+
where
29+
isMatchesCall(c) and
30+
c.getArgument(0) = arg and
31+
arg.getValue().regexpMatch(getALikelyRegExpPattern())
32+
select c,
33+
"Argument \"$@\" looks like a reguar expression, but will be interpreted as a SQL 'LIKE' pattern.",
34+
arg, arg.getValue()

0 commit comments

Comments
 (0)