Skip to content

Commit 2af5925

Browse files
committed
Ruby: improve coverage of GlobalID::Identification modelling
1 parent 7de5113 commit 2af5925

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,40 @@ module GlobalId {
7070
}
7171
}
7272

73-
// TODO: methods in this module are available to any class that includes it, not just ActiveRecord models
7473
/** `GlobalID::Identification` */
7574
module Identification {
75+
/** A `DataFlow::CallNode` against an instance of a class that includes the `GlobalID::Identification` module */
76+
private class IdentificationInstanceCall extends DataFlow::CallNode {
77+
IdentificationInstanceCall() {
78+
this =
79+
DataFlow::getConstant("GlobalID")
80+
.getConstant("Identification")
81+
.getADescendentModule()
82+
.getAnImmediateReference()
83+
.getAMethodCall(["new", "find"])
84+
.getAMethodCall()
85+
or
86+
this instanceof ActiveRecordInstanceMethodCall
87+
}
88+
}
89+
7690
/** A call to `GlobalID::Identification.to_global_id` */
77-
class ToGlobalIdCall extends ActiveRecordInstanceMethodCall {
91+
class ToGlobalIdCall extends IdentificationInstanceCall {
7892
ToGlobalIdCall() { this.getMethodName() = ["to_global_id", "to_gid"] }
7993
}
8094

8195
/** A call to `GlobalID::Identification.to_gid_param` */
82-
class ToGidParamCall extends ActiveRecordInstanceMethodCall {
96+
class ToGidParamCall extends DataFlow::CallNode {
8397
ToGidParamCall() { this.getMethodName() = "to_gid_param" }
8498
}
8599

86100
/** A call to `GlobalID::Identification.to_signed_global_id` */
87-
class ToSignedGlobalIdCall extends ActiveRecordInstanceMethodCall {
101+
class ToSignedGlobalIdCall extends DataFlow::CallNode {
88102
ToSignedGlobalIdCall() { this.getMethodName() = ["to_signed_global_id", "to_sgid"] }
89103
}
90104

91105
/** A call to `GlobalID::Identification.to_sgid_param` */
92-
class ToSgidParamCall extends ActiveRecordInstanceMethodCall {
106+
class ToSgidParamCall extends DataFlow::CallNode {
93107
ToSgidParamCall() { this.getMethodName() = "to_sgid_param" }
94108
}
95109
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
locateCalls
22
| globalid.rb:6:3:6:30 | call to locate |
33
| globalid.rb:11:3:11:30 | call to locate |
4+
| globalid.rb:70:3:70:30 | call to locate |
45
locateSignedCalls
56
| globalid.rb:16:3:16:38 | call to locate_signed |
67
| globalid.rb:21:3:21:38 | call to locate_signed |
8+
| globalid.rb:89:3:89:38 | call to locate_signed |
79
toGlobalIdCalls
810
| globalid.rb:5:9:5:33 | call to to_global_id |
911
| globalid.rb:10:9:10:27 | call to to_gid |
12+
| globalid.rb:56:9:56:16 | call to to_gid |
13+
| globalid.rb:62:9:62:22 | call to to_global_id |
1014
toGidParamCalls
1115
| globalid.rb:35:10:35:34 | call to to_gid_param |
16+
| globalid.rb:68:10:68:23 | call to to_gid_param |
1217
toSignedGlobalIdCalls
1318
| globalid.rb:15:10:15:41 | call to to_signed_global_id |
1419
| globalid.rb:20:10:20:29 | call to to_sgid |
20+
| globalid.rb:75:10:75:18 | call to to_sgid |
21+
| globalid.rb:81:10:81:30 | call to to_signed_global_id |
1522
toSgidParamCalls
1623
| globalid.rb:41:11:41:36 | call to to_sgid_param |
24+
| globalid.rb:87:11:87:25 | call to to_sgid_param |
1725
globalIdParseCalls
1826
| globalid.rb:36:9:36:27 | call to parse |
27+
| globalid.rb:69:9:69:27 | call to parse |
1928
globalIdFindCalls
2029
| globalid.rb:37:3:37:19 | call to find |
30+
| globalid.rb:57:3:57:19 | call to find |
31+
| globalid.rb:63:3:63:19 | call to find |
2132
signedGlobalIdParseCalls
2233
| globalid.rb:42:10:42:35 | call to parse |
34+
| globalid.rb:88:10:88:35 | call to parse |
2335
signedGlobalIdFindCalls
2436
| globalid.rb:43:3:43:26 | call to find |
37+
| globalid.rb:76:3:76:26 | call to find |
38+
| globalid.rb:82:3:82:26 | call to find |

ruby/ql/test/library-tests/frameworks/globalid/globalid.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,49 @@ def m8
4242
sgid = SignedGlobalID.parse sgidp
4343
SignedGlobalID.find sgid
4444
end
45+
46+
class Person
47+
include GlobalID::Identification
48+
49+
def self.find(id)
50+
# implementation goes here
51+
end
52+
end
53+
54+
def m9
55+
p = Person.find(1)
56+
gid = p.to_gid
57+
GlobalID.find gid
58+
end
59+
60+
def m10
61+
p = Person.find(1)
62+
gid = p.to_global_id
63+
GlobalID.find gid
64+
end
65+
66+
def m11
67+
p = Person.find(1)
68+
gidp = p.to_gid_param
69+
gid = GlobalID.parse gidp
70+
GlobalID::Locator.locate gid
71+
end
72+
73+
def m12
74+
p = Person.find(1)
75+
sgid = p.to_sgid
76+
SignedGlobalID.find sgid
77+
end
78+
79+
def m10
80+
p = Person.find(1)
81+
sgid = p.to_signed_global_id
82+
SignedGlobalID.find sgid
83+
end
84+
85+
def m11
86+
p = Person.find(1)
87+
sgidp = p.to_sgid_param
88+
sgid = SignedGlobalID.parse sgidp
89+
GlobalID::Locator.locate_signed sgid
90+
end

0 commit comments

Comments
 (0)