Skip to content

Commit 8363171

Browse files
committed
Ruby: Add MkModuleObject as API node for a module/class
1 parent 7332cec commit 8363171

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

ruby/ql/lib/codeql/ruby/ApiGraphs.qll

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,9 @@ module API {
601601
/** A use of an API member at the node `nd`. */
602602
MkUse(DataFlow::Node nd) { isUse(nd) } or
603603
/** A value that escapes into an external library at the node `nd` */
604-
MkDef(DataFlow::Node nd) { isDef(nd) }
604+
MkDef(DataFlow::Node nd) { isDef(nd) } or
605+
/** A module object seen as a use node. */
606+
MkModuleObject(DataFlow::ModuleNode mod)
605607

606608
private string resolveTopLevel(ConstantReadAccess read) {
607609
result = read.getModule().getQualifiedName() and
@@ -684,7 +686,14 @@ module API {
684686
* Holds if `ref` is a use of node `nd`.
685687
*/
686688
cached
687-
predicate use(TApiNode nd, DataFlow::Node ref) { nd = MkUse(ref) }
689+
predicate use(TApiNode nd, DataFlow::Node ref) {
690+
nd = MkUse(ref)
691+
or
692+
exists(DataFlow::ModuleNode mod |
693+
nd = MkModuleObject(mod) and
694+
ref = mod.getAnImmediateReference()
695+
)
696+
}
688697

689698
/**
690699
* Holds if `rhs` is a RHS of node `nd`.
@@ -802,6 +811,14 @@ module API {
802811
trackUseNode(use).flowsTo(call.getReceiver())
803812
}
804813

814+
/**
815+
* Holds if `superclass` is the superclass of `mod`.
816+
*/
817+
pragma[nomagic]
818+
private predicate superclassNode(DataFlow::ModuleNode mod, DataFlow::Node superclass) {
819+
superclass.asExpr().getExpr() = mod.getADeclaration().(ClassDeclaration).getSuperclassExpr()
820+
}
821+
805822
/**
806823
* Holds if there is an edge from `pred` to `succ` in the API graph that is labeled with `lbl`.
807824
*/
@@ -830,14 +847,11 @@ module API {
830847
defStep(lbl, trackDefNode(predNode), succNode)
831848
)
832849
or
833-
// `pred` is a use of class A
834-
// `succ` is a use of class B
835-
// there exists a class declaration B < A
836-
exists(ClassDeclaration c, DataFlow::Node a, DataFlow::Node b |
837-
use(pred, a) and
838-
use(succ, b) and
839-
b.asExpr().getExpr().(ConstantReadAccess).getAQualifiedName() = c.getAQualifiedName() and
840-
pragma[only_bind_into](c).getSuperclassExpr() = a.asExpr().getExpr() and
850+
exists(DataFlow::Node predNode, DataFlow::Node superclassNode, DataFlow::ModuleNode mod |
851+
use(pred, predNode) and
852+
trackUseNode(predNode).flowsTo(superclassNode) and
853+
superclassNode(mod, superclassNode) and
854+
succ = MkModuleObject(mod) and
841855
lbl = Label::subclass()
842856
)
843857
or

0 commit comments

Comments
 (0)