Skip to content

Commit 2e295e4

Browse files
committed
filter out potential misparses from rb/suspicious-regexp-range
1 parent a343cea commit 2e295e4

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

ruby/ql/src/queries/security/cwe-020/SuspiciousRegexpRange.ql

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313

1414
import codeql.ruby.security.SuspiciousRegexpRangeQuery
1515

16+
RegExpCharacterClass potentialMisparsedCharClass() {
17+
// some escapes, e.g. [\000-\037] are currently misparsed.
18+
result.getAChild().(RegExpNormalChar).getValue() = "\\"
19+
or
20+
// nested char classes are currently misparsed
21+
result.getAChild().(RegExpNormalChar).getValue() = "["
22+
}
23+
1624
from RegExpCharacterRange range, string reason
17-
where problem(range, reason)
25+
where
26+
problem(range, reason) and
27+
not range.getParent() = potentialMisparsedCharClass()
1828
select range, "Suspicious character range that " + reason + "."

ruby/ql/test/query-tests/security/cwe-020/SuspiciousRegexpRange/suspicous_regexp_range.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
printable = /[!-~]/ # OK - used to select most printable ASCII characters
1010

11-
codePoints = /[^\x21-\x7E]|[[\](){}<>/%]/g # OK
11+
codePoints = /[^\x21-\x7E]|[\[\](){}<>\/%]/ # OK
1212

13-
NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g # OK
13+
NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/ # OK
1414

1515
smallOverlap = /[0-9a-fA-f]/ # NOT OK
1616

@@ -25,3 +25,7 @@
2525
overlapsWithClass1 = /[0-9\d]/ # NOT OK
2626

2727
overlapsWithClass2 = /[\w,.-?:*+]/ # NOT OK
28+
29+
escapes = /[\000-\037\047\134\177-\377]/n # OK - they are escapes
30+
31+
nested = /[a-z&&[^a-c]]/ # OK

0 commit comments

Comments
 (0)