Skip to content

Commit f2384a6

Browse files
committed
Ruby: Share more code with JS
1 parent 025e34d commit f2384a6

File tree

5 files changed

+47
-48
lines changed

5 files changed

+47
-48
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,46 @@ private predicate isCommonWordMatcher(RegExpTerm t) {
157157
.getValue() = "w"
158158
)
159159
}
160+
161+
/**
162+
* Holds if `replace` has a pattern argument containing a regular expression
163+
* `dangerous` which matches a dangerous string beginning with `prefix`, in an
164+
* attempt to avoid a vulnerability of kind `kind`.
165+
*/
166+
predicate isResult(
167+
StringSubstitutionCall replace, EmptyReplaceRegExpTerm dangerous, string prefix, string kind
168+
) {
169+
exists(EmptyReplaceRegExpTerm regexp |
170+
replace = regexp.getCall() and
171+
dangerous.getRootTerm() = regexp and
172+
// skip leading optional elements
173+
not dangerous.isNullable() and
174+
// only warn about the longest match
175+
prefix = max(string m | matchesDangerousPrefix(dangerous, m, kind) | m order by m.length(), m) and
176+
// only warn once per kind
177+
not exists(EmptyReplaceRegExpTerm other |
178+
other = dangerous.getAChild+() or other = dangerous.getPredecessor+()
179+
|
180+
matchesDangerousPrefix(other, _, kind) and
181+
not other.isNullable()
182+
) and
183+
// avoid anchored terms
184+
not exists(RegExpAnchor a | regexp = a.getRootTerm()) and
185+
// Don't flag replace operations that are called repeatedly in a loop, as they can actually work correctly.
186+
not replace.flowsTo(replace.getReceiver+())
187+
)
188+
}
189+
190+
/**
191+
* Holds if `replace` has a pattern argument containing a regular expression
192+
* `dangerous` which matches a dangerous string beginning with `prefix`. `msg`
193+
* is the alert we report.
194+
*/
195+
query predicate problems(
196+
StringSubstitutionCall replace, string msg, EmptyReplaceRegExpTerm dangerous, string prefix
197+
) {
198+
exists(string kind |
199+
isResult(replace, dangerous, prefix, kind) and
200+
msg = "This string may still contain $@, which may cause a " + kind + " vulnerability."
201+
)
202+
}

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Provides language-specific predicates for reasoning about improper multi-character sanitization.
33
*/
44

5-
private import codeql.ruby.frameworks.core.String
5+
import codeql.ruby.frameworks.core.String
66
import codeql.ruby.regexp.RegExpTreeView
77
import codeql.ruby.security.performance.ReDoSUtil as ReDoSUtil
88

ruby/ql/src/queries/security/cwe-116/IncompleteMultiCharacterSanitization.ql

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,4 @@
1313
* external/cwe/cwe-116
1414
*/
1515

16-
import codeql.ruby.DataFlow
17-
import codeql.ruby.security.IncompleteMultiCharacterSanitizationQuery as Query
18-
import codeql.ruby.regexp.RegExpTreeView
19-
20-
from DataFlow::Node replace, RegExpTerm dangerous, string prefix, string kind
21-
where Query::problems(replace, dangerous, prefix, kind)
22-
select replace, "This string may still contain $@, which may cause a " + kind + " vulnerability.",
23-
dangerous, prefix
16+
import codeql.ruby.security.IncompleteMultiCharacterSanitization

ruby/ql/test/query-tests/security/cwe-116/IncompleteMultiCharacterSanitization/IncompleteMultiCharacterSanitization.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import ruby
66
import codeql.ruby.regexp.RegExpTreeView as RETV
77
import codeql.ruby.DataFlow
8-
import codeql.ruby.security.IncompleteMultiCharacterSanitizationQuery as Query
8+
import codeql.ruby.security.IncompleteMultiCharacterSanitization as Query
99
import TestUtilities.InlineExpectationsTest
1010

1111
class Test extends InlineExpectationsTest {
@@ -25,7 +25,7 @@ predicate hasResult(Location location, string element, string value) {
2525
element = replace.toString() and
2626
value = shortKind(kind)
2727
|
28-
Query::problems(replace, dangerous, prefix, kind)
28+
Query::isResult(replace, dangerous, prefix, kind)
2929
)
3030
}
3131

0 commit comments

Comments
 (0)