Skip to content

Commit 2f404df

Browse files
authored
Merge pull request github#10782 from erik-krogh/rbPoly
Ruby: add library input as a source for `rb/polynomial-redos`
2 parents 26d5fb2 + 634087b commit 2f404df

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

ruby/ql/lib/codeql/ruby/frameworks/core/Gem.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,13 @@ module Gem {
8585

8686
/** Gets a parameter from an exported method, which is an input to this gem. */
8787
DataFlow::ParameterNode getAnInputParameter() {
88-
exists(MethodBase method | method = getAPublicModule().getAMethod() |
89-
result.getParameter() = method.getAParameter() and
88+
exists(MethodBase method |
89+
method = getAPublicModule().getAMethod() and
90+
result.getParameter() = method.getAParameter()
91+
|
9092
method.isPublic()
93+
or
94+
method.isProtected()
9195
)
9296
}
9397
}

ruby/ql/lib/codeql/ruby/security/regexp/PolynomialReDoSCustomizations.qll

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ module PolynomialReDoS {
2424
/**
2525
* A data flow source node for polynomial regular expression denial-of-service vulnerabilities.
2626
*/
27-
abstract class Source extends DataFlow::Node { }
27+
abstract class Source extends DataFlow::Node {
28+
/**
29+
* Gets a string that describes the source.
30+
* For use in the alert message.
31+
*/
32+
string describe() { result = "user-provided value" }
33+
}
2834

2935
/**
3036
* A data flow sink node for polynomial regular expression denial-of-service vulnerabilities.
@@ -55,6 +61,15 @@ module PolynomialReDoS {
5561
*/
5662
class RemoteFlowSourceAsSource extends Source, RemoteFlowSource { }
5763

64+
private import codeql.ruby.frameworks.core.Gem::Gem as Gem
65+
66+
/** A library input, considered as a flow source. */
67+
class LibraryInputAsSource extends Source {
68+
LibraryInputAsSource() { this = Gem::getALibraryInput() }
69+
70+
override string describe() { result = "library input" }
71+
}
72+
5873
/**
5974
* A regexp match against a superlinear backtracking term, seen as a sink for
6075
* polynomial regular expression denial-of-service vulnerabilities.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `rb/polynomial-redos` query now considers the entrypoints of the API of a gem as sources.

ruby/ql/src/queries/security/cwe-1333/PolynomialReDoS.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ where
2727
select sinkNode.getHighlight(), source, sink,
2828
"This $@ that depends on a $@ may run slow on strings " + regexp.getPrefixMessage() +
2929
"with many repetitions of '" + regexp.getPumpString() + "'.", regexp, "regular expression",
30-
source.getNode(), "user-provided value"
30+
source.getNode(), source.getNode().(PolynomialReDoS::Source).describe()

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ edges
3434
| PolynomialReDoS.rb:70:12:70:24 | ...[...] : | PolynomialReDoS.rb:73:32:73:35 | name : |
3535
| PolynomialReDoS.rb:73:32:73:35 | name : | PolynomialReDoS.rb:76:35:76:39 | input : |
3636
| PolynomialReDoS.rb:76:35:76:39 | input : | PolynomialReDoS.rb:77:5:77:9 | input |
37+
| lib/index.rb:2:11:2:11 | x : | lib/index.rb:4:13:4:13 | x |
38+
| lib/index.rb:8:13:8:13 | x : | lib/index.rb:9:15:9:15 | x |
3739
nodes
3840
| PolynomialReDoS.rb:4:12:4:17 | call to params : | semmle.label | call to params : |
3941
| PolynomialReDoS.rb:4:12:4:24 | ...[...] : | semmle.label | ...[...] : |
@@ -76,6 +78,10 @@ nodes
7678
| PolynomialReDoS.rb:73:32:73:35 | name : | semmle.label | name : |
7779
| PolynomialReDoS.rb:76:35:76:39 | input : | semmle.label | input : |
7880
| PolynomialReDoS.rb:77:5:77:9 | input | semmle.label | input |
81+
| lib/index.rb:2:11:2:11 | x : | semmle.label | x : |
82+
| lib/index.rb:4:13:4:13 | x | semmle.label | x |
83+
| lib/index.rb:8:13:8:13 | x : | semmle.label | x : |
84+
| lib/index.rb:9:15:9:15 | x | semmle.label | x |
7985
subpaths
8086
#select
8187
| 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 |
@@ -101,3 +107,5 @@ subpaths
101107
| 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 |
102108
| 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 |
103109
| 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 |
110+
| lib/index.rb:4:13:4:26 | call to match | lib/index.rb:2:11:2:11 | x : | lib/index.rb:4:13:4:13 | x | This $@ that depends on a $@ may run slow on strings with many repetitions of 'a'. | lib/index.rb:4:22:4:23 | a+ | regular expression | lib/index.rb:2:11:2:11 | x | library input |
111+
| lib/index.rb:9:15:9:28 | call to match | lib/index.rb:8:13:8:13 | x : | lib/index.rb:9:15:9:15 | x | This $@ that depends on a $@ may run slow on strings with many repetitions of 'a'. | lib/index.rb:9:24:9:25 | a+ | regular expression | lib/index.rb:8:13:8:13 | x | library input |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Foo
2+
def bar(x)
3+
# Run the /a+$/ regex on the input x.
4+
match = x.match(/a+$/)
5+
end
6+
7+
protected
8+
def baz(x)
9+
match = x.match(/a+$/)
10+
end
11+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Gem::Specification.new do |s|
2+
s.name = 'poly-redos'
3+
s.require_path = "lib"
4+
end
5+

0 commit comments

Comments
 (0)