Skip to content

Commit ed2ec1a

Browse files
committed
Ruby: Reduce size of isLocalSourceNode
1 parent 355c1f5 commit ed2ec1a

File tree

3 files changed

+31
-131
lines changed

3 files changed

+31
-131
lines changed

ruby/ql/lib/codeql/ruby/dataflow/internal/DataFlowPrivate.qll

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ private class Argument extends CfgNodes::ExprCfgNode {
207207
cached
208208
private module Cached {
209209
private import TaintTrackingPrivate as TaintTrackingPrivate
210+
private import codeql.ruby.typetracking.TypeTrackerSpecific as TypeTrackerSpecific
210211

211212
cached
212213
newtype TNode =
@@ -332,21 +333,22 @@ private module Cached {
332333

333334
cached
334335
predicate isLocalSourceNode(Node n) {
335-
not n instanceof SynthHashSplatParameterNode and
336-
(
337-
n instanceof ParameterNode
338-
or
339-
n instanceof PostUpdateNodes::ExprPostUpdateNode
340-
or
341-
// Nodes that can't be reached from another entry definition or expression.
342-
not reachedFromExprOrEntrySsaDef(n)
343-
or
344-
// Ensure all entry SSA definitions are local sources -- for parameters, this
345-
// is needed by type tracking. Note that when the parameter has a default value,
346-
// it will be reachable from an expression (the default value) and therefore
347-
// won't be caught by the rule above.
348-
entrySsaDefinition(n)
349-
)
336+
n instanceof ParameterNode and
337+
not n instanceof SynthHashSplatParameterNode
338+
or
339+
// Expressions that can't be reached from another entry definition or expression
340+
n instanceof ExprNode and
341+
not reachedFromExprOrEntrySsaDef(n)
342+
or
343+
// Ensure all entry SSA definitions are local sources -- for parameters, this
344+
// is needed by type tracking
345+
entrySsaDefinition(n)
346+
or
347+
// Needed for flow out in type tracking
348+
n instanceof SynthReturnNode
349+
or
350+
// Needed for stores in type tracking
351+
TypeTrackerSpecific::basicStoreStep(_, n, _)
350352
}
351353

352354
cached

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,10 @@ predicate allBackslashesEscaped(DataFlow::Node node) {
104104
allBackslashesEscaped(node.getAPredecessor())
105105
or
106106
// general data flow from a (destructive) [g]sub!
107-
exists(DataFlow::PostUpdateNode post, StringSubstitutionCall sub |
107+
exists(StringSubstitutionCall sub |
108108
sub.isDestructive() and
109109
allBackslashesEscaped(sub) and
110-
post.getPreUpdateNode() = sub.getReceiver() and
111-
post.getASuccessor() = node
110+
node.(DataFlow::PostUpdateNode).getPreUpdateNode() = sub.getReceiver()
112111
)
113112
}
114113

@@ -125,19 +124,18 @@ predicate removesFirstOccurrence(StringSubstitutionCall sub, string str) {
125124
* call.
126125
*/
127126
DataFlow::CallNode getAMethodCall(StringSubstitutionCall call) {
128-
exists(DataFlow::Node receiver |
129-
receiver = result.getReceiver() and
130-
(
131-
// for a non-destructive string substitution, is there flow from it to the
132-
// receiver of another method call?
133-
not call.isDestructive() and call.(DataFlow::LocalSourceNode).flowsTo(receiver)
134-
or
135-
// for a destructive string substitution, is there flow from its
136-
// post-update receiver to the receiver of another method call?
137-
call.isDestructive() and
138-
exists(DataFlow::PostUpdateNode post | post.getPreUpdateNode() = call.getReceiver() |
139-
post.(DataFlow::LocalSourceNode).flowsTo(receiver)
140-
)
127+
exists(DataFlow::Node receiver | receiver = result.getReceiver() |
128+
// for a non-destructive string substitution, is there flow from it to the
129+
// receiver of another method call?
130+
not call.isDestructive() and
131+
DataFlow::localFlow(call, receiver)
132+
or
133+
// for a destructive string substitution, is there flow from its
134+
// post-update receiver to the receiver of another method call?
135+
call.isDestructive() and
136+
exists(DataFlow::PostUpdateNode post |
137+
post.getPreUpdateNode() = call.getReceiver() and
138+
DataFlow::localFlowStep+(post, receiver)
141139
)
142140
)
143141
}

0 commit comments

Comments
 (0)