Skip to content

Commit 2ad28ab

Browse files
committed
add library inputs as a source to poly-redos
1 parent 158ea26 commit 2ad28ab

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

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

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

2733
/**
2834
* A data flow sink node for polynomial regular expression denial-of-service vulnerabilities.
@@ -53,6 +59,15 @@ module PolynomialReDoS {
5359
*/
5460
class RemoteFlowSourceAsSource extends Source, RemoteFlowSource { }
5561

62+
import codeql.ruby.frameworks.core.Gem::Gem as Gem
63+
64+
/** A library input, considered as a flow source. */
65+
class LibraryInputAsSource extends Source {
66+
LibraryInputAsSource() { this = Gem::getALibraryInput() }
67+
68+
override string describe() { result = "library input" }
69+
}
70+
5671
/**
5772
* Gets the AST of a regular expression object that can flow to `node`.
5873
*/
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ edges
2323
| PolynomialReDoS.rb:29:9:29:18 | ...[...] : | PolynomialReDoS.rb:30:5:30:5 | b |
2424
| PolynomialReDoS.rb:31:9:31:14 | call to params : | PolynomialReDoS.rb:31:9:31:18 | ...[...] : |
2525
| PolynomialReDoS.rb:31:9:31:18 | ...[...] : | PolynomialReDoS.rb:32:5:32:5 | c |
26+
| lib/index.rb:2:11:2:11 | x : | lib/index.rb:4:13:4:13 | x |
2627
nodes
2728
| PolynomialReDoS.rb:4:12:4:17 | call to params : | semmle.label | call to params : |
2829
| PolynomialReDoS.rb:4:12:4:24 | ...[...] : | semmle.label | ...[...] : |
@@ -52,6 +53,8 @@ nodes
5253
| PolynomialReDoS.rb:32:5:32:5 | c | semmle.label | c |
5354
| PolynomialReDoS.rb:42:10:42:13 | name | semmle.label | name |
5455
| PolynomialReDoS.rb:47:10:47:13 | name | semmle.label | name |
56+
| lib/index.rb:2:11:2:11 | x : | semmle.label | x : |
57+
| lib/index.rb:4:13:4:13 | x | semmle.label | x |
5558
subpaths
5659
#select
5760
| 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 |
@@ -74,3 +77,4 @@ subpaths
7477
| PolynomialReDoS.rb:32:5:32:20 | call to sub! | PolynomialReDoS.rb:31:9:31:14 | call to params : | PolynomialReDoS.rb:32:5:32:5 | c | 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:31:9:31:14 | call to params | user-provided value |
7578
| PolynomialReDoS.rb:42:5:45:7 | case ... | PolynomialReDoS.rb:4:12:4:17 | call to params : | PolynomialReDoS.rb:42:10:42:13 | 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 |
7679
| 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 |
80+
| 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 |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Foo
2+
def bar(x)
3+
# Run the /a+$/ regex on the input x.
4+
match = x.match(/a+$/)
5+
end
6+
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)