Skip to content

Commit de8a59e

Browse files
committed
Add regex-dot-wildcard-instead-of-literal.ql
1 parent e068e21 commit de8a59e

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

codeql-custom-queries-java/not-working-queries/regex-misleading-character-class.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java
66

7+
// TODO: Consider using `semmle.code.java.regex.RegexTreeView` library, see existing queries
8+
79
// TODO: These are already declared in `regex-wrong-alphabetic-range.ql`, reduce code duplication
810
abstract class RegexMethod extends Method {
911
abstract int regexParamIndex();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Finds a single non-escaped `.` in a Regex pattern without any qualifier.
3+
* In that case the `.` is treated as 'any character' instead of being matched literally.
4+
*
5+
* Consider this pattern:
6+
* ```
7+
* \d{4}.\d{2}.\d{2}
8+
* ```
9+
* The intention might have been to match dates such as `2024.01.01`, but it also matches
10+
* malformed dates such as `2024&01(01`.\
11+
* The pattern should have escaped the `.` as `\.` instead.
12+
*
13+
* @kind problem
14+
*/
15+
16+
import java
17+
// Uses alias `re` to avoid conflicting declarations
18+
import semmle.code.java.regex.RegexTreeView as re
19+
20+
// Note: This does not match all Regex patterns, see
21+
// https://github.com/github/codeql/blob/codeql-cli/v2.15.5/java/ql/lib/semmle/code/java/regex/RegexFlowConfigs.qll#L161-L162
22+
from re::RegExpDot dot
23+
where not dot.getParent() instanceof re::RegExpQuantifier
24+
select dot, "This `.` should probably be escaped to match it literally"

codeql-custom-queries-java/queries/likely-bugs/regex-wrong-alphabetic-range.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
import java
88

9+
// TODO: Consider using `semmle.code.java.regex.RegexTreeView` library, see existing queries;
10+
// though this implementation here is simpler and more efficient?
11+
// TODO: Might overlap with standard CodeQL `java/overly-large-range` query (https://github.com/github/codeql/blob/codeql-cli/v2.15.5/java/ql/src/Security/CWE/CWE-020/OverlyLargeRange.ql)
12+
913
abstract class RegexMethod extends Method {
1014
abstract int regexParamIndex();
1115
}

0 commit comments

Comments
 (0)