55 */
66
77import swift
8+ import internal.SensitiveDataHeuristics
89
910private newtype TSensitiveDataType =
1011 TCredential ( ) or
@@ -29,7 +30,12 @@ class SensitiveCredential extends SensitiveDataType, TCredential {
2930 override string toString ( ) { result = "credential" }
3031
3132 override string getRegexp ( ) {
32- result = ".*(password|passwd|accountid|account.?key|accnt.?key|license.?key|trusted).*"
33+ exists ( SensitiveDataClassification classification |
34+ not classification = SensitiveDataClassification:: id ( ) and // not accurate enough
35+ result = HeuristicNames:: maybeSensitiveRegexp ( classification )
36+ )
37+ or
38+ result = "(?is).*(account|accnt|license).?(id|key).*"
3339 }
3440}
3541
@@ -41,7 +47,7 @@ class SensitivePrivateInfo extends SensitiveDataType, TPrivateInfo {
4147
4248 override string getRegexp ( ) {
4349 result =
44- ".*(" +
50+ "(?is) .*(" +
4551 // Inspired by the list on https://cwe.mitre.org/data/definitions/359.html
4652 // Government identifiers, such as Social Security Numbers
4753 "social.?security|national.?insurance|" +
@@ -52,7 +58,7 @@ class SensitivePrivateInfo extends SensitiveDataType, TPrivateInfo {
5258 // Geographic location - where the user is (or was)
5359 "latitude|longitude|" +
5460 // Financial data - such as credit card numbers, salary, bank accounts, and debts
55- "credit.?card|debit.?card|salary|bank.?account|" +
61+ "credit.?card|debit.?card|salary|bank.?account|acc(ou)?nt.?(no|num)| " +
5662 // Communications - e-mail addresses, private e-mail messages, SMS text messages, chat logs, etc.
5763 "email|" +
5864 // Health - medical conditions, insurance status, prescription records
@@ -69,15 +75,18 @@ class SensitivePrivateInfo extends SensitiveDataType, TPrivateInfo {
6975 * contain hashed or encrypted data, or are only a reference to data that is
7076 * actually stored elsewhere.
7177 */
72- private string regexpProbablySafe ( ) { result = ".*(hash|crypt|file|path|url|invalid).*" }
78+ private string regexpProbablySafe ( ) {
79+ result = HeuristicNames:: notSensitiveRegexp ( ) or
80+ result = "(?is).*(file|path|url|invalid).*"
81+ }
7382
7483/**
7584 * A `VarDecl` that might be used to contain sensitive data.
7685 */
7786private class SensitiveVarDecl extends VarDecl {
7887 SensitiveDataType sensitiveType ;
7988
80- SensitiveVarDecl ( ) { this .getName ( ) .toLowerCase ( ) . regexpMatch ( sensitiveType .getRegexp ( ) ) }
89+ SensitiveVarDecl ( ) { this .getName ( ) .regexpMatch ( sensitiveType .getRegexp ( ) ) }
8190
8291 predicate hasInfo ( string label , SensitiveDataType type ) {
8392 label = this .getName ( ) and
@@ -90,11 +99,15 @@ private class SensitiveVarDecl extends VarDecl {
9099 */
91100private class SensitiveFunction extends Function {
92101 SensitiveDataType sensitiveType ;
102+ string name ; // name of the function, not including the argument list.
93103
94- SensitiveFunction ( ) { this .getName ( ) .toLowerCase ( ) .regexpMatch ( sensitiveType .getRegexp ( ) ) }
104+ SensitiveFunction ( ) {
105+ name = this .getShortName ( ) and
106+ name .regexpMatch ( sensitiveType .getRegexp ( ) )
107+ }
95108
96109 predicate hasInfo ( string label , SensitiveDataType type ) {
97- label = this . getName ( ) and
110+ label = name and
98111 sensitiveType = type
99112 }
100113}
@@ -105,7 +118,7 @@ private class SensitiveFunction extends Function {
105118private class SensitiveArgument extends Argument {
106119 SensitiveDataType sensitiveType ;
107120
108- SensitiveArgument ( ) { this .getLabel ( ) .toLowerCase ( ) . regexpMatch ( sensitiveType .getRegexp ( ) ) }
121+ SensitiveArgument ( ) { this .getLabel ( ) .regexpMatch ( sensitiveType .getRegexp ( ) ) }
109122
110123 predicate hasInfo ( string label , SensitiveDataType type ) {
111124 label = this .getLabel ( ) and
@@ -138,7 +151,7 @@ class SensitiveExpr extends Expr {
138151 )
139152 ) and
140153 // do not mark as sensitive it if it is probably safe
141- not label .toLowerCase ( ) . regexpMatch ( regexpProbablySafe ( ) )
154+ not label .regexpMatch ( regexpProbablySafe ( ) )
142155 }
143156
144157 /**
@@ -156,7 +169,7 @@ class SensitiveExpr extends Expr {
156169 * A function that is likely used to encrypt or hash data.
157170 */
158171private class EncryptionFunction extends Function {
159- EncryptionFunction ( ) { this .getName ( ) .regexpMatch ( ".*(crypt|hash|encode|protect).*" ) }
172+ EncryptionFunction ( ) { this .getName ( ) .regexpMatch ( "(?is) .*(crypt|hash|encode|protect).*" ) }
160173}
161174
162175/**
0 commit comments