-
Notifications
You must be signed in to change notification settings - Fork 278
/
Redaction.java
217 lines (203 loc) · 5.9 KB
/
Redaction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package datadog.trace.bootstrap.debugger.util;
import datadog.trace.api.Config;
import datadog.trace.util.ClassNameTrie;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
public class Redaction {
// Need to be a unique instance (new String) for reference equality (==) and
// avoid internalization (intern) by the JVM because it's a string constant
public static final String REDACTED_VALUE = new String("redacted".toCharArray());
private static final Pattern COMMA_PATTERN = Pattern.compile(",");
private static final List<String> PREDEFINED_KEYWORDS =
Arrays.asList(
"2fa",
"accesstoken",
"aiohttpsession",
"apikey",
"appkey",
"apisecret",
"apisignature",
"applicationkey",
"auth",
"authorization",
"authtoken",
"ccnumber",
"certificatepin",
"cipher",
"clientid",
"clientsecret",
"connectionstring",
"connectsid",
"cookie",
"credentials",
"creditcard",
"csrf",
"csrftoken",
"cvv",
"databaseurl",
"dburl",
"encryptionkey",
"encryptionkeyid",
"env",
"geolocation",
"gpgkey",
"ipaddress",
"jti",
"jwt",
"licensekey",
"masterkey",
"mysqlpwd",
"nonce",
"oauth",
"oauthtoken",
"otp",
"passhash",
"passwd",
"password",
"passwordb",
"pemfile",
"pgpkey",
"phpsessid",
"pin",
"pincode",
"pkcs8",
"privatekey",
"publickey",
"pwd",
"recaptchakey",
"refreshtoken",
"routingnumber",
"salt",
"secret",
"secretkey",
"secrettoken",
"securityanswer",
"securitycode",
"securityquestion",
"serviceaccountcredentials",
"session",
"sessionid",
"sessionkey",
"setcookie",
"signature",
"signaturekey",
"sshkey",
"ssn",
"symfony",
"token",
"transactionid",
"twiliotoken",
"usersession",
"voterid",
"xapikey",
"xauthtoken",
"xcsrftoken",
"xforwardedfor",
"xrealip",
"xsrf",
"xsrftoken");
private static final Set<String> KEYWORDS = ConcurrentHashMap.newKeySet();
private static ClassNameTrie typeTrie = ClassNameTrie.Builder.EMPTY_TRIE;
private static List<String> redactedClasses;
private static List<String> redactedPackages;
static {
/*
* based on sentry list: https://github.com/getsentry/sentry-python/blob/fefb454287b771ac31db4e30fa459d9be2f977b8/sentry_sdk/scrubber.py#L17-L58
*/
KEYWORDS.addAll(PREDEFINED_KEYWORDS);
}
public static void addUserDefinedKeywords(Config config) {
String redactedIdentifiers = config.getDebuggerRedactedIdentifiers();
if (redactedIdentifiers == null) {
return;
}
String[] identifiers = COMMA_PATTERN.split(redactedIdentifiers);
for (String identifier : identifiers) {
KEYWORDS.add(normalize(identifier));
}
}
public static void addUserDefinedTypes(Config config) {
String redactedTypes = config.getDebuggerRedactedTypes();
if (redactedTypes == null) {
return;
}
List<String> packages = null;
List<String> classes = null;
ClassNameTrie.Builder builder = new ClassNameTrie.Builder();
String[] types = COMMA_PATTERN.split(redactedTypes);
for (String type : types) {
builder.put(type, 1);
if (type.endsWith("*")) {
if (packages == null) {
packages = new ArrayList<>();
}
type =
type.endsWith(".*")
? type.substring(0, type.length() - 2)
: type.substring(0, type.length() - 1);
packages.add(type);
} else {
if (classes == null) {
classes = new ArrayList<>();
}
classes.add(type);
}
}
typeTrie = builder.buildTrie();
redactedPackages = packages;
redactedClasses = classes;
}
public static boolean isRedactedKeyword(String name) {
if (name == null) {
return false;
}
name = normalize(name);
return KEYWORDS.contains(name);
}
public static boolean isRedactedType(String className) {
if (className == null) {
return false;
}
return typeTrie.apply(className) > 0;
}
public static List<String> getRedactedPackages() {
return redactedPackages != null ? redactedPackages : Collections.emptyList();
}
public static List<String> getRedactedClasses() {
return redactedClasses != null ? redactedClasses : Collections.emptyList();
}
public static void clearUserDefinedTypes() {
typeTrie = ClassNameTrie.Builder.EMPTY_TRIE;
}
public static void resetUserDefinedKeywords() {
KEYWORDS.clear();
KEYWORDS.addAll(PREDEFINED_KEYWORDS);
}
private static String normalize(String name) {
StringBuilder sb = null;
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
boolean isUpper = Character.isUpperCase(c);
boolean isRemovable = isRemovableChar(c);
if (isUpper || isRemovable || sb != null) {
if (sb == null) {
sb = new StringBuilder(name.substring(0, i));
}
if (isUpper) {
sb.append(Character.toLowerCase(c));
} else if (!isRemovable) {
sb.append(c);
}
}
}
return sb != null ? sb.toString() : name;
}
private static boolean isRemovableChar(char c) {
return c == '_' || c == '-' || c == '$' || c == '@';
}
}