Skip to content

Commit 2fceee4

Browse files
committed
track regular expressions that gets compiled with Regexp.compile
1 parent acf28eb commit 2fceee4

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

ruby/ql/lib/codeql/ruby/regexp/internal/RegExpConfiguration.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ class RegExpConfiguration extends Configuration {
5353
DataFlow::Node nodeFrom, DataFlow::FlowState stateFrom, DataFlow::Node nodeTo,
5454
DataFlow::FlowState stateTo
5555
) {
56+
stateFrom = "string" and
57+
stateTo = "reg" and
58+
exists(DataFlow::CallNode call |
59+
call = API::getTopLevelMember("Regexp").getAMethodCall(["compile", "new"]) and
60+
nodeFrom = call.getArgument(0) and
61+
nodeTo = call
62+
)
63+
or
5664
stateFrom = stateTo and
5765
stateFrom = "string" and
5866
(

ruby/ql/test/query-tests/security/cwe-1333-polynomial-redos/PolynomialReDoS.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ edges
3030
| PolynomialReDoS.rb:58:37:58:40 | name : | PolynomialReDoS.rb:65:42:65:46 | input : |
3131
| PolynomialReDoS.rb:61:33:61:37 | input : | PolynomialReDoS.rb:62:5:62:9 | input |
3232
| PolynomialReDoS.rb:65:42:65:46 | input : | PolynomialReDoS.rb:66:5:66:9 | input |
33+
| PolynomialReDoS.rb:70:12:70:17 | call to params : | PolynomialReDoS.rb:70:12:70:24 | ...[...] : |
34+
| PolynomialReDoS.rb:70:12:70:24 | ...[...] : | PolynomialReDoS.rb:73:32:73:35 | name : |
35+
| PolynomialReDoS.rb:73:32:73:35 | name : | PolynomialReDoS.rb:76:35:76:39 | input : |
36+
| PolynomialReDoS.rb:76:35:76:39 | input : | PolynomialReDoS.rb:77:5:77:9 | input |
3337
nodes
3438
| PolynomialReDoS.rb:4:12:4:17 | call to params : | semmle.label | call to params : |
3539
| PolynomialReDoS.rb:4:12:4:24 | ...[...] : | semmle.label | ...[...] : |
@@ -67,6 +71,11 @@ nodes
6771
| PolynomialReDoS.rb:62:5:62:9 | input | semmle.label | input |
6872
| PolynomialReDoS.rb:65:42:65:46 | input : | semmle.label | input : |
6973
| PolynomialReDoS.rb:66:5:66:9 | input | semmle.label | input |
74+
| PolynomialReDoS.rb:70:12:70:17 | call to params : | semmle.label | call to params : |
75+
| PolynomialReDoS.rb:70:12:70:24 | ...[...] : | semmle.label | ...[...] : |
76+
| PolynomialReDoS.rb:73:32:73:35 | name : | semmle.label | name : |
77+
| PolynomialReDoS.rb:76:35:76:39 | input : | semmle.label | input : |
78+
| PolynomialReDoS.rb:77:5:77:9 | input | semmle.label | input |
7079
subpaths
7180
#select
7281
| PolynomialReDoS.rb:10:5:10:17 | ... =~ ... | PolynomialReDoS.rb:4:12:4:17 | call to params : | PolynomialReDoS.rb:10:5:10:8 | name | This $@ that depends on a $@ may run slow on strings with many repetitions of ' '. | PolynomialReDoS.rb:7:19:7:21 | \\s+ | regular expression | PolynomialReDoS.rb:4:12:4:17 | call to params | user-provided value |
@@ -91,3 +100,4 @@ subpaths
91100
| PolynomialReDoS.rb:47:5:50:7 | case ... | PolynomialReDoS.rb:4:12:4:17 | call to params : | PolynomialReDoS.rb:47:10:47:13 | name | This $@ that depends on a $@ may run slow on strings with many repetitions of ' '. | PolynomialReDoS.rb:48:14:48:16 | \\s+ | regular expression | PolynomialReDoS.rb:4:12:4:17 | call to params | user-provided value |
92101
| PolynomialReDoS.rb:62:5:62:22 | call to gsub | PolynomialReDoS.rb:54:12:54:17 | call to params : | PolynomialReDoS.rb:62:5:62:9 | input | This $@ that depends on a $@ may run slow on strings with many repetitions of ' '. | PolynomialReDoS.rb:56:31:56:33 | \\s+ | regular expression | PolynomialReDoS.rb:54:12:54:17 | call to params | user-provided value |
93102
| PolynomialReDoS.rb:66:5:66:34 | call to match? | PolynomialReDoS.rb:54:12:54:17 | call to params : | PolynomialReDoS.rb:66:5:66:9 | input | This $@ that depends on a $@ may run slow on strings with many repetitions of ' '. | PolynomialReDoS.rb:58:30:58:32 | \\s+ | regular expression | PolynomialReDoS.rb:54:12:54:17 | call to params | user-provided value |
103+
| PolynomialReDoS.rb:77:5:77:22 | call to gsub | PolynomialReDoS.rb:70:12:70:17 | call to params : | PolynomialReDoS.rb:77:5:77:9 | input | This $@ that depends on a $@ may run slow on strings with many repetitions of ' '. | PolynomialReDoS.rb:72:28:72:30 | \\s+ | regular expression | PolynomialReDoS.rb:70:12:70:17 | call to params | user-provided value |

ruby/ql/test/query-tests/security/cwe-1333-polynomial-redos/PolynomialReDoS.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,15 @@ def indirect_use_of_reg (reg, input)
6565
def as_string_indirect (reg_as_string, input)
6666
input.match? reg_as_string, '' # NOT GOOD
6767
end
68+
69+
def re_compile_indirect
70+
name = params[:name] # source
71+
72+
reg = Regexp.new '^\s+|\s+$'
73+
re_compile_indirect_2 reg, name
74+
end
75+
76+
def re_compile_indirect_2 (reg, input)
77+
input.gsub reg, '' # NOT GOOD
78+
end
6879
end

0 commit comments

Comments
 (0)