Skip to content

Commit 557b49c

Browse files
hmachvitved
authored andcommitted
Ruby: Add basic modeling for ViewComponent
1 parent 5b3a2b3 commit 557b49c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

ruby/ql/lib/codeql/ruby/Frameworks.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ private import codeql.ruby.frameworks.Yaml
3838
private import codeql.ruby.frameworks.Sequel
3939
private import codeql.ruby.frameworks.Ldap
4040
private import codeql.ruby.frameworks.Jwt
41+
private import codeql.ruby.frameworks.ViewComponent
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
private import codeql.ruby.AST
2+
private import codeql.ruby.DataFlow
3+
private import codeql.ruby.dataflow.SSA
4+
private import codeql.ruby.dataflow.internal.DataFlowPrivate as DataFlowPrivate
5+
6+
/**
7+
* Provides modeling for the `view_component` gem.
8+
*/
9+
module ViewComponent {
10+
/**
11+
* A subclass of `ViewComponent::Base`.
12+
*/
13+
class ComponentClass extends DataFlow::ClassNode {
14+
ComponentClass() {
15+
this = DataFlow::getConstant("ViewComponent").getConstant("Base").getADescendentModule()
16+
}
17+
18+
/**
19+
* Returns the template file for this component.
20+
*/
21+
ErbFile getTemplate() {
22+
result.getAbsolutePath() =
23+
this.getLocation().getFile().getAbsolutePath().replaceAll(".rb", ".html.erb")
24+
}
25+
}
26+
27+
/**
28+
* An additional jump step from a `ComponentClass` passed as an argument in a call to `render`
29+
* to the `self` variable in its corresponding template.
30+
*/
31+
private predicate jumpStep(DataFlow::Node node1, DataFlowPrivate::SsaSelfDefinitionNode node2) {
32+
exists(DataFlow::CallNode call, ComponentClass component |
33+
call.getMethodName() = "render" and
34+
call.getArgument(0) = node1 and
35+
component.trackInstance().getAValueReachableFromSource() = node1 and
36+
node2.getLocation().getFile() = component.getTemplate() and
37+
node2.getSelfScope() instanceof Toplevel and
38+
node2.getDefinitionExt() instanceof Ssa::SelfDefinition
39+
)
40+
}
41+
}

0 commit comments

Comments
 (0)