Skip to content

Commit 821eb4f

Browse files
committed
Rust: Add sensitive data library.
1 parent c77bf2b commit 821eb4f

File tree

6 files changed

+330
-19
lines changed

6 files changed

+330
-19
lines changed

config/identical-files.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@
247247
"javascript/ql/lib/semmle/javascript/security/internal/SensitiveDataHeuristics.qll",
248248
"python/ql/lib/semmle/python/security/internal/SensitiveDataHeuristics.qll",
249249
"ruby/ql/lib/codeql/ruby/security/internal/SensitiveDataHeuristics.qll",
250-
"swift/ql/lib/codeql/swift/security/internal/SensitiveDataHeuristics.qll"
250+
"swift/ql/lib/codeql/swift/security/internal/SensitiveDataHeuristics.qll",
251+
"rust/ql/lib/codeql/rust/security/internal/SensitiveDataHeuristics.qll"
251252
],
252253
"IncompleteUrlSubstringSanitization": [
253254
"javascript/ql/src/Security/CWE-020/IncompleteUrlSubstringSanitization.qll",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Provides classes and predicates for identifying sensitive data.
3+
*
4+
* 'Sensitive' data is anything that should not be sent in unencrypted form. This library tries to
5+
* guess where sensitive data may either be stored in a variable or produced by a method.
6+
*/
7+
8+
import rust
9+
private import internal.SensitiveDataHeuristics
10+
private import codeql.rust.dataflow.DataFlow
11+
12+
/**
13+
* A data flow node that might contain sensitive data.
14+
*/
15+
cached
16+
abstract class SensitiveData extends DataFlow::Node {
17+
/**
18+
* Gets a classification of the kind of sensitive data this expression might contain.
19+
*/
20+
cached
21+
abstract SensitiveDataClassification getClassification();
22+
}
23+
24+
/**
25+
* A function that might produce sensitive data.
26+
*/
27+
private class SensitiveDataFunction extends Function {
28+
SensitiveDataClassification classification;
29+
30+
SensitiveDataFunction() {
31+
HeuristicNames::nameIndicatesSensitiveData(this.getName().getText(), classification)
32+
}
33+
34+
SensitiveDataClassification getClassification() { result = classification }
35+
}
36+
37+
/**
38+
* A function call that might produce sensitive data.
39+
*/
40+
private class SensitiveDataCall extends SensitiveData {
41+
SensitiveDataClassification classification;
42+
43+
SensitiveDataCall() {
44+
classification =
45+
this.asExpr()
46+
.getAstNode()
47+
.(CallExprBase)
48+
.getStaticTarget()
49+
.(SensitiveDataFunction)
50+
.getClassification()
51+
}
52+
53+
override SensitiveDataClassification getClassification() { result = classification }
54+
}
55+
56+
/**
57+
* A variable that might contain sensitive data.
58+
*/
59+
private class SensitiveDataVariable extends Variable {
60+
SensitiveDataClassification classification;
61+
62+
SensitiveDataVariable() {
63+
HeuristicNames::nameIndicatesSensitiveData(this.getName(), classification)
64+
}
65+
66+
SensitiveDataClassification getClassification() { result = classification }
67+
}
68+
69+
/**
70+
* A variable access that might produce sensitive data.
71+
*/
72+
private class SensitiveVariableAccess extends SensitiveData {
73+
SensitiveDataClassification classification;
74+
75+
SensitiveVariableAccess() {
76+
classification =
77+
this.asExpr()
78+
.getAstNode()
79+
.(VariableAccess)
80+
.getVariable()
81+
.(SensitiveDataVariable)
82+
.getClassification()
83+
}
84+
85+
override SensitiveDataClassification getClassification() { result = classification }
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/**
2+
* INTERNAL: Do not use.
3+
*
4+
* Provides classes and predicates for identifying strings that may indicate the presence of sensitive data.
5+
* Such that we can share this logic across our CodeQL analysis of different languages.
6+
*
7+
* 'Sensitive' data in general is anything that should not be sent around in unencrypted form.
8+
*/
9+
10+
/**
11+
* A classification of different kinds of sensitive data:
12+
*
13+
* - secret: generic secret or trusted data;
14+
* - id: a user name or other account information;
15+
* - password: a password or authorization key;
16+
* - certificate: a certificate.
17+
* - private: private data such as credit card numbers
18+
*
19+
* While classifications are represented as strings, this should not be relied upon.
20+
* Instead, use the predicates in `SensitiveDataClassification::` to work with
21+
* classifications.
22+
*/
23+
class SensitiveDataClassification extends string {
24+
SensitiveDataClassification() { this in ["secret", "id", "password", "certificate", "private"] }
25+
}
26+
27+
/**
28+
* Provides predicates to select the different kinds of sensitive data we support.
29+
*/
30+
module SensitiveDataClassification {
31+
/** Gets the classification for secret or trusted data. */
32+
SensitiveDataClassification secret() { result = "secret" }
33+
34+
/** Gets the classification for user names or other account information. */
35+
SensitiveDataClassification id() { result = "id" }
36+
37+
/** Gets the classification for passwords or authorization keys. */
38+
SensitiveDataClassification password() { result = "password" }
39+
40+
/** Gets the classification for certificates. */
41+
SensitiveDataClassification certificate() { result = "certificate" }
42+
43+
/** Gets the classification for private data. */
44+
SensitiveDataClassification private() { result = "private" }
45+
}
46+
47+
/**
48+
* INTERNAL: Do not use.
49+
*
50+
* Provides heuristics for identifying names related to sensitive information.
51+
*/
52+
module HeuristicNames {
53+
/**
54+
* Gets a regular expression that identifies strings that may indicate the presence of secret
55+
* or trusted data.
56+
*/
57+
string maybeSecret() { result = "(?is).*((?<!is|is_)secret|(?<!un|un_|is|is_)trusted).*" }
58+
59+
/**
60+
* Gets a regular expression that identifies strings that may indicate the presence of
61+
* user names or other account information.
62+
*/
63+
string maybeAccountInfo() {
64+
result = "(?is).*acc(ou)?nt.*" or
65+
result = "(?is).*(puid|username|userid|session(id|key)).*" or
66+
result = "(?s).*([uU]|^|_|[a-z](?=U))([uU][iI][dD]).*"
67+
}
68+
69+
/**
70+
* Gets a regular expression that identifies strings that may indicate the presence of
71+
* a password or an authorization key.
72+
*/
73+
string maybePassword() {
74+
result = "(?is).*pass(wd|word|code|phrase)(?!.*question).*" or
75+
result = "(?is).*(auth(entication|ori[sz]ation)?)key.*"
76+
}
77+
78+
/**
79+
* Gets a regular expression that identifies strings that may indicate the presence of
80+
* a certificate.
81+
*/
82+
string maybeCertificate() { result = "(?is).*(cert)(?!.*(format|name|ification)).*" }
83+
84+
/**
85+
* Gets a regular expression that identifies strings that may indicate the presence of
86+
* private data.
87+
*/
88+
string maybePrivate() {
89+
result =
90+
"(?is).*(" +
91+
// Inspired by the list on https://cwe.mitre.org/data/definitions/359.html
92+
// Government identifiers, such as Social Security Numbers
93+
"social.?security|employer.?identification|national.?insurance|resident.?id|" +
94+
"passport.?(num|no)|([_-]|\\b)ssn([_-]|\\b)|" +
95+
// Contact information, such as home addresses
96+
"post.?code|zip.?code|home.?addr|" +
97+
// and telephone numbers
98+
"(mob(ile)?|home).?(num|no|tel|phone)|(tel|fax|phone).?(num|no)|telephone|" +
99+
"emergency.?contact|" +
100+
// Geographic location - where the user is (or was)
101+
"latitude|longitude|nationality|" +
102+
// Financial data - such as credit card numbers, salary, bank accounts, and debts
103+
"(credit|debit|bank|visa).?(card|num|no|acc(ou)?nt)|acc(ou)?nt.?(no|num|credit)|" +
104+
"salary|billing|credit.?(rating|score)|([_-]|\\b)ccn([_-]|\\b)|" +
105+
// Communications - e-mail addresses, private e-mail messages, SMS text messages, chat logs, etc.
106+
// "e(mail|_mail)|" + // this seems too noisy
107+
// Health - medical conditions, insurance status, prescription records
108+
"birth.?da(te|y)|da(te|y).?(of.?)?birth|" +
109+
"medical|(health|care).?plan|healthkit|appointment|prescription|" +
110+
"blood.?(type|alcohol|glucose|pressure)|heart.?(rate|rhythm)|body.?(mass|fat)|" +
111+
"menstrua|pregnan|insulin|inhaler|" +
112+
// Relationships - work and family
113+
"employ(er|ee)|spouse|maiden.?name" +
114+
// ---
115+
").*"
116+
}
117+
118+
/**
119+
* Gets a regular expression that identifies strings that may indicate the presence
120+
* of sensitive data, with `classification` describing the kind of sensitive data involved.
121+
*/
122+
string maybeSensitiveRegexp(SensitiveDataClassification classification) {
123+
result = maybeSecret() and classification = SensitiveDataClassification::secret()
124+
or
125+
result = maybeAccountInfo() and classification = SensitiveDataClassification::id()
126+
or
127+
result = maybePassword() and classification = SensitiveDataClassification::password()
128+
or
129+
result = maybeCertificate() and
130+
classification = SensitiveDataClassification::certificate()
131+
or
132+
result = maybePrivate() and
133+
classification = SensitiveDataClassification::private()
134+
}
135+
136+
/**
137+
* Gets a regular expression that identifies strings that may indicate the presence of data
138+
* that is hashed or encrypted, and hence rendered non-sensitive, or contains special characters
139+
* suggesting nouns within the string do not represent the meaning of the whole string (e.g. a URL or a SQL query).
140+
*
141+
* We also filter out common words like `certain` and `concert`, since otherwise these could
142+
* be matched by the certificate regular expressions. Same for `accountable` (account), or
143+
* `secretarial` (secret).
144+
*/
145+
string notSensitiveRegexp() {
146+
result =
147+
"(?is).*([^\\w$.-]|redact|censor|obfuscate|hash|md5|sha|random|((?<!un)(en))?(crypt|(?<!pass)code)|certain|concert|secretar|accountant|accountab).*"
148+
}
149+
150+
/**
151+
* Holds if `name` may indicate the presence of sensitive data, and `name` does not indicate that
152+
* the data is in fact non-sensitive (for example since it is hashed or encrypted).
153+
*
154+
* That is, one of the regexps from `maybeSensitiveRegexp` matches `name` (with the given
155+
* classification), and none of the regexps from `notSensitiveRegexp` matches `name`.
156+
*/
157+
bindingset[name]
158+
predicate nameIndicatesSensitiveData(string name) {
159+
exists(string combinedRegexp |
160+
// Combine all the maybe-sensitive regexps into one using non-capturing groups and |.
161+
combinedRegexp =
162+
"(?:" + strictconcat(string r | r = maybeSensitiveRegexp(_) | r, ")|(?:") + ")"
163+
|
164+
name.regexpMatch(combinedRegexp)
165+
) and
166+
not name.regexpMatch(notSensitiveRegexp())
167+
}
168+
169+
/**
170+
* Holds if `name` may indicate the presence of sensitive data, and
171+
* `name` does not indicate that the data is in fact non-sensitive (for example since
172+
* it is hashed or encrypted). `classification` describes the kind of sensitive data
173+
* involved.
174+
*
175+
* That is, one of the regexps from `maybeSensitiveRegexp` matches `name` (with the
176+
* given classification), and none of the regexps from `notSensitiveRegexp` matches
177+
* `name`.
178+
*
179+
* When the set of names is large, it's worth using `nameIndicatesSensitiveData/1` as a first
180+
* pass, since that combines all the regexps into one, and should be faster. Then call this
181+
* predicate to get the classification(s).
182+
*/
183+
bindingset[name]
184+
predicate nameIndicatesSensitiveData(string name, SensitiveDataClassification classification) {
185+
name.regexpMatch(maybeSensitiveRegexp(classification)) and
186+
not name.regexpMatch(notSensitiveRegexp())
187+
}
188+
}

rust/ql/test/library-tests/sensitivedata/SensitiveData.expected

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import rust
2+
import codeql.rust.dataflow.DataFlow
3+
import codeql.rust.dataflow.TaintTracking
4+
import codeql.rust.security.SensitiveData
5+
import utils.test.InlineExpectationsTest
6+
7+
/**
8+
* Configuration for flow from any sensitive data source to an argument of the function `sink`.
9+
*/
10+
module SensitiveDataConfig implements DataFlow::ConfigSig {
11+
predicate isSource(DataFlow::Node source) { source instanceof SensitiveData }
12+
13+
predicate isSink(DataFlow::Node sink) {
14+
any(CallExpr call | call.getFunction().(PathExpr).getResolvedPath() = "crate::test::sink")
15+
.getArgList()
16+
.getAnArg() = sink.asExpr().getExpr()
17+
}
18+
}
19+
20+
module SensitiveDataFlow = TaintTracking::Global<SensitiveDataConfig>;
21+
22+
module SensitiveDataTest implements TestSig {
23+
string getARelevantTag() { result = "sensitive" }
24+
25+
predicate hasActualResult(Location location, string element, string tag, string value) {
26+
exists(DataFlow::Node source, DataFlow::Node sink |
27+
SensitiveDataFlow::flow(source, sink) and
28+
location = sink.getLocation() and
29+
element = sink.toString() and
30+
tag = "sensitive" and
31+
value = source.(SensitiveData).getClassification()
32+
)
33+
}
34+
}
35+
36+
import MakeTest<SensitiveDataTest>

rust/ql/test/library-tests/sensitivedata/test.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ fn test_passwords(
2727
ms: &MyStruct
2828
) {
2929
// passwords
30-
sink(password); // $ MISSING: sensitive=password
31-
sink(passwd); // $ MISSING: sensitive=password
32-
sink(my_password); // $ MISSING: sensitive=password
33-
sink(password_str); // $ MISSING: sensitive=password
30+
sink(password); // $ sensitive=password
31+
sink(passwd); // $ sensitive=password
32+
sink(my_password); // $ sensitive=password
33+
sink(password_str); // $ sensitive=password
3434
sink(pass_phrase); // $ MISSING: sensitive=password
3535
sink(auth_key); // $ MISSING: sensitive=password
36-
sink(authenticationkey); // $ MISSING: sensitive=password
37-
sink(authKey); // $ MISSING: sensitive=password
36+
sink(authenticationkey); // $ sensitive=password
37+
sink(authKey); // $ sensitive=password
3838

3939
sink(ms); // $ MISSING: sensitive=password
4040
sink(ms.password.as_str()); // $ MISSING: sensitive=password
4141

42-
sink(get_password()); // $ MISSING: sensitive=password
42+
sink(get_password()); // $ sensitive=password
4343
let password2 = get_string();
44-
sink(password2); // $ MISSING: sensitive=password
44+
sink(password2); // $ sensitive=password
4545

4646
// not passwords
4747
sink(harmless);
@@ -69,25 +69,25 @@ fn test_credentials(
6969
ms: &MyStruct
7070
) {
7171
// credentials
72-
sink(account_key); // $ MISSING: sensitive=secret
73-
sink(accnt_key); // $ MISSING: sensitive=secret
72+
sink(account_key); // $ sensitive=id
73+
sink(accnt_key); // $ sensitive=id
7474
sink(license_key); // $ MISSING: sensitive=secret
75-
sink(secret_key); // $ MISSING: sensitive=secret
75+
sink(secret_key); // $ sensitive=secret
7676

77-
sink(ms.get_certificate()); // $ MISSING: sensitive=certificate
77+
sink(ms.get_certificate()); // $ sensitive=certificate
7878

79-
sink(generate_secret_key()); // $ MISSING: sensitive=secret
79+
sink(generate_secret_key()); // $ sensitive=secret
8080
sink(get_secure_key()); // $ MISSING: sensitive=secret
8181
sink(get_private_key()); // $ MISSING: sensitive=secret
82-
sink(get_secret_token()); // $ MISSING: sensitive=secret
82+
sink(get_secret_token()); // $ sensitive=secret
8383

8484
// not credentials
8585
sink(is_secret);
86-
sink(num_accounts);
87-
sink(uid);
86+
sink(num_accounts); // $ SPURIOUS: sensitive=id
87+
sink(uid); // $ SPURIOUS: sensitive=id
8888

89-
sink(ms.get_certificate_url());
90-
sink(ms.get_certificate_file());
89+
sink(ms.get_certificate_url()); // $ SPURIOUS: sensitive=certificate
90+
sink(ms.get_certificate_file()); // $ SPURIOUS: sensitive=certificate
9191

9292
sink(get_public_key());
9393
sink(get_next_token());

0 commit comments

Comments
 (0)