Skip to content

Commit 9d7d6c2

Browse files
hvitvedaibaars
authored andcommitted
Review comments
1 parent 77c47bc commit 9d7d6c2

File tree

4 files changed

+44
-30
lines changed

4 files changed

+44
-30
lines changed

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

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,25 @@ private DataFlowCallable viableLibraryCallable(DataFlowCall call) {
294294
)
295295
}
296296

297-
/** Holds if there is a call like `receiver.extend(M)` */
297+
/** Holds if there is a call like `receiver.extend(M)`. */
298298
pragma[nomagic]
299-
private predicate flowsToExtendCall(DataFlow::LocalSourceNode receiver, Module m) {
299+
private predicate extendCall(DataFlow::ExprNode receiver, Module m) {
300300
exists(DataFlow::CallNode extendCall |
301301
extendCall.getMethodName() = "extend" and
302302
exists(DataFlow::LocalSourceNode sourceNode | sourceNode.flowsTo(extendCall.getArgument(0)) |
303303
selfInModule(sourceNode.(SsaSelfDefinitionNode).getVariable(), m) or
304304
m = resolveConstantReadAccess(sourceNode.asExpr().getExpr())
305305
) and
306-
receiver.flowsTo(extendCall.getReceiver())
306+
receiver = extendCall.getReceiver()
307307
)
308308
}
309309

310310
/** Holds if there is a call like `M.extend(N)` */
311311
pragma[nomagic]
312312
private predicate extendCallModule(Module m, Module n) {
313-
exists(DataFlow::LocalSourceNode receiver | flowsToExtendCall(receiver, n) |
313+
exists(DataFlow::LocalSourceNode receiver, DataFlow::ExprNode e |
314+
receiver.flowsTo(e) and extendCall(e, n)
315+
|
314316
selfInModule(receiver.(SsaSelfDefinitionNode).getVariable(), m) or
315317
m = resolveConstantReadAccess(receiver.asExpr().getExpr())
316318
)
@@ -373,14 +375,7 @@ private module Cached {
373375
// ```
374376
exists(DataFlow::Node receiver |
375377
methodCall(call, receiver, method) and
376-
(
377-
receiver = trackSingletonMethodOnInstance(result, method)
378-
or
379-
exists(Module m |
380-
flowsToExtendCall(any(DataFlow::LocalSourceNode n | n.flowsTo(receiver)), m) and
381-
result = lookupMethod(m, pragma[only_bind_into](method))
382-
)
383-
)
378+
receiver = trackSingletonMethodOnInstance(result, method)
384379
)
385380
or
386381
// singleton method defined on a module
@@ -394,14 +389,7 @@ private module Cached {
394389
// ```
395390
exists(DataFlow::Node sourceNode, Module m |
396391
flowsToMethodCall(call, sourceNode, method) and
397-
(
398-
singletonMethodOnModule(result, method, m)
399-
or
400-
exists(Module other |
401-
extendCallModule(m, other) and
402-
result = lookupMethod(other, pragma[only_bind_into](method))
403-
)
404-
)
392+
singletonMethodOnModule(result, method, m)
405393
|
406394
// ```rb
407395
// def C.singleton; end # <- result
@@ -717,6 +705,12 @@ private predicate flowsToSingletonMethodObject(
717705
* class << c
718706
* def m6; end # not included
719707
* end
708+
*
709+
* module M
710+
* def instance; end # included in `N` via `extend` call below
711+
* end
712+
* N.extend(M)
713+
* N.instance
720714
* ```
721715
*/
722716
pragma[nomagic]
@@ -727,6 +721,11 @@ private predicate singletonMethodOnModule(MethodBase method, string name, Module
727721
)
728722
or
729723
flowsToSingletonMethodObject(trackModuleAccess(m), method, name)
724+
or
725+
exists(Module other |
726+
extendCallModule(m, other) and
727+
method = lookupMethod(other, name)
728+
)
730729
}
731730

732731
/**
@@ -753,6 +752,11 @@ private predicate singletonMethodOnModule(MethodBase method, string name, Module
753752
* class << c
754753
* def m6; end # included
755754
* end
755+
*
756+
* module M
757+
* def instance; end # included in `c` via `extend` call below
758+
* end
759+
* c.extend(M)
756760
* ```
757761
*/
758762
pragma[nomagic]
@@ -761,6 +765,12 @@ predicate singletonMethodOnInstance(MethodBase method, string name, Expr object)
761765
not selfInModule(object.(SelfVariableReadAccess).getVariable(), _) and
762766
// cannot use `trackModuleAccess` because of negative recursion
763767
not exists(resolveConstantReadAccess(object))
768+
or
769+
exists(DataFlow::ExprNode receiver, Module other |
770+
extendCall(receiver, other) and
771+
object = receiver.getExprNode().getExpr() and
772+
method = lookupMethod(other, name)
773+
)
764774
}
765775

766776
/**

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ private module Cached {
8585
op.getAnOperand() = nodeFrom.asExpr() and
8686
not op.getExpr() =
8787
any(Expr e |
88+
// included in normal data-flow
8889
e instanceof AssignExpr or
90+
e instanceof BinaryLogicalOperation or
91+
// has flow summary
8992
e instanceof SplatExpr
9093
)
9194
)

ruby/ql/lib/codeql/ruby/frameworks/stdlib/Pathname.qll

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module Pathname {
2626
*
2727
* Every `PathnameInstance` is considered to be a `FileNameSource`.
2828
*/
29-
class PathnameInstance extends FileNameSource, DataFlow::Node {
29+
class PathnameInstance extends FileNameSource {
3030
PathnameInstance() { any(PathnameConfiguration c).hasFlowTo(this) }
3131
}
3232

@@ -44,14 +44,15 @@ module Pathname {
4444
override predicate isSink(DataFlow::Node sink) { any() }
4545

4646
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
47-
exists(DataFlow::CallNode c | node2 = c |
48-
c.getReceiver() = node1 and
49-
c.getMethodName() =
50-
[
51-
"+", "/", "basename", "cleanpath", "expand_path", "join", "realpath",
52-
"relative_path_from", "sub", "sub_ext", "to_path"
53-
]
54-
)
47+
node2 =
48+
any(DataFlow::CallNode c |
49+
c.getReceiver() = node1 and
50+
c.getMethodName() =
51+
[
52+
"+", "/", "basename", "cleanpath", "expand_path", "join", "realpath",
53+
"relative_path_from", "sub", "sub_ext", "to_path"
54+
]
55+
)
5556
}
5657
}
5758

ruby/ql/test/library-tests/modules/callgraph.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ getTarget
186186
| calls.rb:488:1:488:31 | call to singleton | calls.rb:481:5:483:7 | singleton |
187187
| calls.rb:494:1:494:32 | call to singleton | calls.rb:481:5:483:7 | singleton |
188188
| calls.rb:501:1:501:32 | call to singleton | calls.rb:481:5:483:7 | singleton |
189-
| calls.rb:504:1:504:13 | call to singleton | calls.rb:481:5:483:7 | singleton |
190189
| calls.rb:507:1:507:13 | call to singleton | calls.rb:481:5:483:7 | singleton |
191190
| calls.rb:516:5:516:35 | call to include | calls.rb:108:5:110:7 | include |
192191
| calls.rb:519:9:519:35 | call to puts | calls.rb:102:5:102:30 | puts |
@@ -311,6 +310,7 @@ unresolvedCall
311310
| calls.rb:485:5:485:15 | call to extend |
312311
| calls.rb:491:5:491:32 | call to extend |
313312
| calls.rb:499:1:499:51 | call to extend |
313+
| calls.rb:504:1:504:13 | call to singleton |
314314
| calls.rb:505:1:505:32 | call to extend |
315315
| calls.rb:510:5:512:7 | call to protected |
316316
| calls.rb:511:9:511:42 | call to puts |

0 commit comments

Comments
 (0)