Skip to content

Commit 116873c

Browse files
authored
Merge pull request github#16314 from github/nickrolfe/rb-sensitive
Ruby: do fewer regexp matches in SensitiveActions
2 parents 290b0fc + 8f2e51f commit 116873c

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

ruby/ql/lib/codeql/ruby/security/SensitiveActions.qll

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,37 @@ abstract class SensitiveNode extends DataFlow::Node {
2828
}
2929

3030
/** A method call that might produce sensitive data. */
31-
class SensitiveCall extends SensitiveNode instanceof DataFlow::CallNode {
31+
abstract class SensitiveCall extends SensitiveNode { }
32+
33+
private class SensitiveDataMethodNameCall extends SensitiveCall instanceof DataFlow::CallNode {
3234
SensitiveDataClassification classification;
3335

34-
SensitiveCall() {
36+
SensitiveDataMethodNameCall() {
3537
classification = this.getMethodName().(SensitiveDataMethodName).getClassification()
36-
or
37-
// This is particularly to pick up methods with an argument like "password", which
38-
// may indicate a lookup.
39-
exists(string s | super.getArgument(_).asExpr().getConstantValue().isStringlikeValue(s) |
40-
nameIndicatesSensitiveData(s, classification)
41-
)
4238
}
4339

4440
override string describe() { result = "a call to " + super.getMethodName() }
4541

4642
override SensitiveDataClassification getClassification() { result = classification }
4743
}
4844

45+
private class SensitiveArgumentCall extends SensitiveCall instanceof DataFlow::CallNode {
46+
string argName;
47+
48+
SensitiveArgumentCall() {
49+
// This is particularly to pick up methods with an argument like "password", which may indicate
50+
// a lookup.
51+
super.getArgument(_).asExpr().getConstantValue().isStringlikeValue(argName) and
52+
nameIndicatesSensitiveData(argName)
53+
}
54+
55+
override string describe() { result = "a call to " + super.getMethodName() }
56+
57+
override SensitiveDataClassification getClassification() {
58+
nameIndicatesSensitiveData(argName, result)
59+
}
60+
}
61+
4962
/** An access to a variable or hash value that might contain sensitive data. */
5063
abstract class SensitiveVariableAccess extends SensitiveNode {
5164
string name;
@@ -93,7 +106,7 @@ private string unprefixedVariableName(string name) { result = name.regexpReplace
93106

94107
/** A write to a variable or property that might contain sensitive data. */
95108
private class BasicSensitiveWrite extends SensitiveWrite {
96-
SensitiveDataClassification classification;
109+
string unprefixedName;
97110

98111
BasicSensitiveWrite() {
99112
exists(string name |
@@ -111,23 +124,29 @@ private class BasicSensitiveWrite extends SensitiveWrite {
111124
*/
112125

113126
writesProperty(this, name) and
114-
nameIndicatesSensitiveData(unprefixedVariableName(name), classification)
127+
unprefixedName = unprefixedVariableName(name) and
128+
nameIndicatesSensitiveData(unprefixedName)
115129
)
116130
}
117131

118132
/** Gets a classification of the kind of sensitive data the write might handle. */
119-
SensitiveDataClassification getClassification() { result = classification }
133+
SensitiveDataClassification getClassification() {
134+
nameIndicatesSensitiveData(unprefixedName, result)
135+
}
120136
}
121137

122138
/** An access to a variable or hash value that might contain sensitive data. */
123139
private class BasicSensitiveVariableAccess extends SensitiveVariableAccess {
124-
SensitiveDataClassification classification;
140+
string unprefixedName;
125141

126142
BasicSensitiveVariableAccess() {
127-
nameIndicatesSensitiveData(unprefixedVariableName(name), classification)
143+
unprefixedName = unprefixedVariableName(name) and
144+
nameIndicatesSensitiveData(unprefixedName)
128145
}
129146

130-
override SensitiveDataClassification getClassification() { result = classification }
147+
override SensitiveDataClassification getClassification() {
148+
nameIndicatesSensitiveData(unprefixedName, result)
149+
}
131150
}
132151

133152
/** A method name that suggests it may be sensitive. */
@@ -143,11 +162,11 @@ abstract class SensitiveDataMethodName extends SensitiveMethodName {
143162

144163
/** A method name that might return sensitive credential data. */
145164
class CredentialsMethodName extends SensitiveDataMethodName {
146-
SensitiveDataClassification classification;
147-
148-
CredentialsMethodName() { nameIndicatesSensitiveData(this, classification) }
165+
CredentialsMethodName() { nameIndicatesSensitiveData(this) }
149166

150-
override SensitiveDataClassification getClassification() { result = classification }
167+
override SensitiveDataClassification getClassification() {
168+
nameIndicatesSensitiveData(this, result)
169+
}
151170
}
152171

153172
/**

0 commit comments

Comments
 (0)