Skip to content

Commit b837c56

Browse files
committed
Refactor RootApi and GrapeApiClass constructors for improved readability; add getHelperSelf method to retrieve self parameter in helpers block.
1 parent 1bf6101 commit b837c56

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

ruby/ql/lib/codeql/ruby/frameworks/Grape.qll

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ module Grape {
2525
* In other words, it does not subclass any other Grape API class in source code.
2626
*/
2727
class RootApi extends GrapeApiClass {
28-
RootApi() {
29-
not this = any(GrapeApiClass parent).getAnImmediateDescendent()
30-
}
28+
RootApi() { not this = any(GrapeApiClass parent).getAnImmediateDescendent() }
3129
}
3230

3331
/**
@@ -44,9 +42,7 @@ module Grape {
4442
* ```
4543
*/
4644
class GrapeApiClass extends DataFlow::ClassNode {
47-
GrapeApiClass() {
48-
this = grapeApiBaseClass().getADescendentModule()
49-
}
45+
GrapeApiClass() { this = grapeApiBaseClass().getADescendentModule() }
5046

5147
/**
5248
* Gets a `GrapeEndpoint` defined in this class.
@@ -63,6 +59,20 @@ module Grape {
6359
// is invoked with an instance as the `self`.
6460
result = this.getModuleLevelSelf()
6561
}
62+
63+
/**
64+
* Gets the `self` parameter belonging to a method defined within a
65+
* `helpers` block in this API class.
66+
*
67+
* These methods become available in endpoint contexts through Grape's DSL.
68+
*/
69+
DataFlow::SelfParameterNode getHelperSelf() {
70+
exists(DataFlow::CallNode helpersCall |
71+
helpersCall = this.getAModuleLevelCall("helpers") and
72+
result.getSelfVariable().getDeclaringScope().getOuterScope+() =
73+
helpersCall.getBlock().asExpr().getExpr()
74+
)
75+
}
6676
}
6777

6878
private DataFlow::ConstRef grapeApiBaseClass() {
@@ -122,17 +132,12 @@ module Grape {
122132
*/
123133
private class GrapeParamsCall extends ParamsCallImpl {
124134
GrapeParamsCall() {
125-
// Params calls within endpoint blocks
126-
exists(GrapeApiClass api |
127-
this.getMethodName() = "params" and
128-
this.getParent+() = api.getADeclaration()
129-
)
130-
or
131-
// Params calls within helper methods (defined in helpers blocks)
132-
exists(GrapeApiClass api, DataFlow::CallNode helpersCall |
133-
helpersCall = api.getAModuleLevelCall("helpers") and
134-
this.getMethodName() = "params" and
135-
this.getParent+() = helpersCall.getBlock().asExpr().getExpr()
135+
exists(API::Node n | this = n.getAMethodCall("params").asExpr().getExpr() |
136+
// Params calls within endpoint blocks
137+
n = grapeApiInstance()
138+
or
139+
// Params calls within helper methods (defined in helpers blocks)
140+
n = any(GrapeApiClass c).getHelperSelf().track()
136141
)
137142
}
138143
}

0 commit comments

Comments
 (0)