]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/logging/Scrubber.java
Add --scrub-log flag to remove possibly sensitive information from the log
[signal-cli] / src / main / java / org / asamk / signal / logging / Scrubber.java
1 /*
2 * Copyright (C) 2014 Open Whisper Systems
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.asamk.signal.logging;
19
20 import java.util.Arrays;
21 import java.util.HashSet;
22 import java.util.Locale;
23 import java.util.Set;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 /**
28 * Scrub data for possibly sensitive information.
29 */
30 public final class Scrubber {
31
32 private Scrubber() {
33 }
34
35 /**
36 * The middle group will be censored.
37 * Supposedly, the shortest international phone numbers in use contain seven digits.
38 * Handles URL encoded +, %2B
39 */
40 private static final Pattern E164_PATTERN = Pattern.compile("(\\+|%2B)(\\d{5,13})(\\d{2})");
41 private static final String E164_CENSOR = "*************";
42
43 /**
44 * The second group will be censored.
45 */
46 private static final Pattern CRUDE_EMAIL_PATTERN = Pattern.compile("\\b([^\\s/])([^\\s/]*@[^\\s]+)");
47 private static final String EMAIL_CENSOR = "...@...";
48
49 /**
50 * The middle group will be censored.
51 */
52 private static final Pattern UUID_PATTERN = Pattern.compile(
53 "(JOB::)?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{10})([0-9a-f]{2})",
54 Pattern.CASE_INSENSITIVE);
55 private static final String UUID_CENSOR = "********-****-****-****-**********";
56
57 /**
58 * The entire string is censored.
59 */
60 private static final Pattern IPV4_PATTERN = Pattern.compile("\\b"
61 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
62 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
63 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
64 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
65 + "\\b");
66 private static final String IPV4_CENSOR = "...ipv4...";
67
68 /**
69 * The domain name except for TLD will be censored.
70 */
71 private static final Pattern DOMAIN_PATTERN = Pattern.compile("([a-z0-9]+\\.)+([a-z0-9\\-]*[a-z\\-][a-z0-9\\-]*)",
72 Pattern.CASE_INSENSITIVE);
73 private static final String DOMAIN_CENSOR = "***.";
74 private static final Set<String> TOP_100_TLDS = new HashSet<>(Arrays.asList("com",
75 "net",
76 "org",
77 "jp",
78 "de",
79 "uk",
80 "fr",
81 "br",
82 "it",
83 "ru",
84 "es",
85 "me",
86 "gov",
87 "pl",
88 "ca",
89 "au",
90 "cn",
91 "co",
92 "in",
93 "nl",
94 "edu",
95 "info",
96 "eu",
97 "ch",
98 "id",
99 "at",
100 "kr",
101 "cz",
102 "mx",
103 "be",
104 "tv",
105 "se",
106 "tr",
107 "tw",
108 "al",
109 "ua",
110 "ir",
111 "vn",
112 "cl",
113 "sk",
114 "ly",
115 "cc",
116 "to",
117 "no",
118 "fi",
119 "us",
120 "pt",
121 "dk",
122 "ar",
123 "hu",
124 "tk",
125 "gr",
126 "il",
127 "news",
128 "ro",
129 "my",
130 "biz",
131 "ie",
132 "za",
133 "nz",
134 "sg",
135 "ee",
136 "th",
137 "io",
138 "xyz",
139 "pe",
140 "bg",
141 "hk",
142 "lt",
143 "link",
144 "ph",
145 "club",
146 "si",
147 "site",
148 "mobi",
149 "by",
150 "cat",
151 "wiki",
152 "la",
153 "ga",
154 "xxx",
155 "cf",
156 "hr",
157 "ng",
158 "jobs",
159 "online",
160 "kz",
161 "ug",
162 "gq",
163 "ae",
164 "is",
165 "lv",
166 "pro",
167 "fm",
168 "tips",
169 "ms",
170 "sa",
171 "app"));
172
173 public static CharSequence scrub(CharSequence in) {
174
175 in = scrubE164(in);
176 in = scrubEmail(in);
177 in = scrubUuids(in);
178 in = scrubDomains(in);
179 in = scrubIpv4(in);
180
181 return in;
182 }
183
184 private static CharSequence scrubE164(CharSequence in) {
185 return scrub(in,
186 E164_PATTERN,
187 (matcher, output) -> output.append(matcher.group(1))
188 .append(E164_CENSOR, 0, matcher.group(2).length())
189 .append(matcher.group(3)));
190 }
191
192 private static CharSequence scrubEmail(CharSequence in) {
193 return scrub(in,
194 CRUDE_EMAIL_PATTERN,
195 (matcher, output) -> output.append(matcher.group(1)).append(EMAIL_CENSOR));
196 }
197
198 private static CharSequence scrubUuids(CharSequence in) {
199 return scrub(in, UUID_PATTERN, (matcher, output) -> {
200 if (matcher.group(1) != null && !matcher.group(1).isEmpty()) {
201 output.append(matcher.group(1)).append(matcher.group(2)).append(matcher.group(3));
202 } else {
203 output.append(UUID_CENSOR).append(matcher.group(3));
204 }
205 });
206 }
207
208 private static CharSequence scrubDomains(CharSequence in) {
209 return scrub(in, DOMAIN_PATTERN, (matcher, output) -> {
210 String match = matcher.group(0);
211 if (matcher.groupCount() == 2
212 && TOP_100_TLDS.contains(matcher.group(2).toLowerCase(Locale.US))
213 && !match.endsWith("whispersystems.org")
214 && !match.endsWith("signal.org")) {
215 output.append(DOMAIN_CENSOR).append(matcher.group(2));
216 } else {
217 output.append(match);
218 }
219 });
220 }
221
222 private static CharSequence scrubIpv4(CharSequence in) {
223 return scrub(in, IPV4_PATTERN, (matcher, output) -> output.append(IPV4_CENSOR));
224 }
225
226 private static CharSequence scrub(
227 CharSequence in, Pattern pattern, ProcessMatch processMatch
228 ) {
229 final StringBuilder output = new StringBuilder(in.length());
230 final Matcher matcher = pattern.matcher(in);
231
232 int lastEndingPos = 0;
233
234 while (matcher.find()) {
235 output.append(in, lastEndingPos, matcher.start());
236
237 processMatch.scrubMatch(matcher, output);
238
239 lastEndingPos = matcher.end();
240 }
241
242 if (lastEndingPos == 0) {
243 // there were no matches, save copying all the data
244 return in;
245 } else {
246 output.append(in, lastEndingPos, in.length());
247
248 return output;
249 }
250 }
251
252 private interface ProcessMatch {
253
254 void scrubMatch(Matcher matcher, StringBuilder output);
255 }
256 }