Skip to content

Commit cd33e4d

Browse files
committed
Make string interpolation sanitizer reusable
1 parent b6ce37b commit cd33e4d

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/** Provides commonly used dataflow sanitizers */
2+
3+
private import codeql.ruby.AST
4+
private import codeql.ruby.DataFlow
5+
6+
/**
7+
* A sanitizer for flow into a string interpolation component,
8+
* provided that component does not form a prefix of the string.
9+
*
10+
* This is useful for URLs and paths, where the fixed prefix prevents the user from controlling the target.
11+
*/
12+
class PrefixedStringInterpolation extends DataFlow::Node {
13+
PrefixedStringInterpolation() {
14+
exists(StringlikeLiteral str, int n | str.getComponent(n) = this.asExpr().getExpr() and n > 0)
15+
}
16+
}

ruby/ql/lib/codeql/ruby/security/UrlRedirectCustomizations.qll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private import codeql.ruby.DataFlow
99
private import codeql.ruby.Concepts
1010
private import codeql.ruby.dataflow.RemoteFlowSources
1111
private import codeql.ruby.dataflow.BarrierGuards
12+
private import codeql.ruby.dataflow.Sanitizers
1213

1314
/**
1415
* Provides default sources, sinks and sanitizers for detecting
@@ -103,11 +104,7 @@ module UrlRedirect {
103104
*
104105
* We currently don't catch these cases.
105106
*/
106-
class StringInterpolationAsSanitizer extends Sanitizer {
107-
StringInterpolationAsSanitizer() {
108-
exists(StringlikeLiteral str, int n | str.getComponent(n) = this.asExpr().getExpr() and n > 0)
109-
}
110-
}
107+
class StringInterpolationAsSanitizer extends PrefixedStringInterpolation, Sanitizer { }
111108

112109
/**
113110
* These methods return a new `ActionController::Parameters` or a `Hash` containing a subset of

0 commit comments

Comments
 (0)