Skip to content

Commit def62e8

Browse files
authored
Merge pull request github#5718 from hvitved/csharp/hardcoded-cred-remove-cp
C#: Remove CP from `HardcodedCredentials::getCredentialSink`
2 parents 1ed11b2 + 15e4b7f commit def62e8

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

csharp/ql/src/semmle/code/csharp/security/dataflow/HardcodedCredentials.qll

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,24 @@ module HardcodedCredentials {
111111
}
112112

113113
/**
114-
* Gets a regular expression for matching names of locations (variables, parameters, keys) that
115-
* indicate the value being held is a credential.
114+
* An assignable whose name indicates that the value being held is a credential.
116115
*/
117-
private string getACredentialRegex() {
118-
result = "(?i).*pass(wd|word|code|phrase)(?!.*question).*" or
119-
result = "(?i).*(puid|username|userid).*" or
120-
result = "(?i).*(cert)(?!.*(format|name)).*"
116+
private class CredentialVar extends Assignable {
117+
pragma[noinline]
118+
CredentialVar() {
119+
exists(string name | name = this.getName() |
120+
name.regexpMatch("(?i).*pass(wd|word|code|phrase)(?!.*question).*")
121+
or
122+
name.regexpMatch("(?i).*(puid|username|userid).*")
123+
or
124+
name.regexpMatch("(?i).*(cert)(?!.*(format|name)).*")
125+
)
126+
}
127+
}
128+
129+
private class CredentialVariableAccess extends VariableAccess {
130+
pragma[noinline]
131+
CredentialVariableAccess() { this.getTarget() instanceof CredentialVar }
121132
}
122133

123134
/**
@@ -128,11 +139,11 @@ module HardcodedCredentials {
128139
) {
129140
// An argument to a library call that looks like a credential
130141
// "...flows to the [Username] parameter in [call to method CreateUser]"
131-
exists(Call call |
142+
exists(Call call, CredentialVar param |
132143
supplementaryElement = call and
133144
description = "the $@ parameter in $@" and
134-
sink = call.getArgumentForName(sinkName) and
135-
sinkName.regexpMatch(getACredentialRegex()) and
145+
sink = call.getArgumentForParameter(param) and
146+
sinkName = param.getName() and
136147
call.getTarget().fromLibrary()
137148
)
138149
or
@@ -144,22 +155,20 @@ module HardcodedCredentials {
144155
description = "the $@ in $@" and
145156
sink = call.getArgument(0) and
146157
sinkName = "setter call argument" and
147-
p.getName().regexpMatch(getACredentialRegex()) and
158+
p instanceof CredentialVar and
148159
p.fromLibrary()
149160
)
150161
or
151162
// Sink compared to password variable
152163
// "...flows to [] which is compared against [access of UserName]"
153-
exists(ComparisonTest ct, VariableAccess credentialAccess, string varName |
164+
exists(ComparisonTest ct, CredentialVariableAccess credentialAccess |
154165
sinkName = sink.toString() and
155166
supplementaryElement = credentialAccess and
156167
description = "$@ which is compared against $@" and
157168
ct.getAnArgument() = credentialAccess and
158169
ct.getAnArgument() = sink and
159170
ct.getComparisonKind().isEquality() and
160-
not sink = credentialAccess and
161-
varName = credentialAccess.getTarget().getName() and
162-
varName.regexpMatch(getACredentialRegex())
171+
not sink = credentialAccess
163172
)
164173
}
165174

0 commit comments

Comments
 (0)