-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonalDataCheckService.java
More file actions
262 lines (238 loc) · 9.68 KB
/
Copy pathPersonalDataCheckService.java
File metadata and controls
262 lines (238 loc) · 9.68 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package org.eclipse.foodity.elasticsearch.service;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
public class PersonalDataCheckService {
private static final Pattern EMAIL_PATTERN = Pattern
.compile("\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}\\b");
private static final Pattern PHONE_CANDIDATE_PATTERN = Pattern
.compile("(?<![\\p{L}\\p{N}])(?:\\+?[0-9][0-9\\s()./-]{8,}[0-9])(?![\\p{L}\\p{N}])");
private static final Pattern DATE_LIKE_PATTERN = Pattern
.compile("^\\d{4}[-/]\\d{1,2}[-/]\\d{1,2}$|^\\d{1,2}[-/]\\d{1,2}[-/]\\d{2,4}$");
private static final Pattern DATE_TIME_LIKE_PATTERN = Pattern.compile(
"^\\d{4}[-/]\\d{1,2}[-/]\\d{1,2}(?:\\s+\\d{1,2}(?::\\d{2})?)?$|^\\d{1,2}[-/]\\d{1,2}[-/]\\d{2,4}(?:\\s+\\d{1,2}(?::\\d{2})?)?$");
private static final Pattern COORDINATE_TUPLE_PATTERN = Pattern
.compile("^[+\\-]?\\d{1,3}\\.\\d+(?:[\\s,;]+[+\\-]?\\d{1,3}\\.\\d+)+$");
private static final Pattern ID_LIKE_NUMERIC_HYPHEN_PATTERN = Pattern.compile("^\\d{7,}-\\d{3,}$");
private static final Pattern ID_LIKE_NUMERIC_SLASH_PATTERN = Pattern.compile("^\\d{3,}(?:/\\d+)+$");
private static final Pattern ID_LIKE_NUMERIC_SLASH_PATH_PATTERN = Pattern.compile("^(?:\\d{3,}/){2,}\\d{1,}$");
private static final Pattern DECIMAL_NUMBER_PATTERN = Pattern.compile("^[+\\-]?\\d+\\.\\d+$");
private static final Pattern LEADING_ZEROS_IDENTIFIER_PATTERN = Pattern.compile("^0{3,}\\d+$");
private static final PhoneNumberUtil PHONE_NUMBER_UTIL = PhoneNumberUtil.getInstance();
private static final Set<String> EUROPEAN_REGIONS = Set.of(
"AT", "BE", "BG", "CH", "CY", "CZ", "DE", "DK", "EE", "ES", "FI", "FR", "GB", "GR", "HR", "HU",
"IE", "IS", "IT", "LI", "LT", "LU", "LV", "MT", "NL", "NO", "PL", "PT", "RO", "SE", "SI", "SK");
private static final String KEYWORDS_FILE = "/keywords.txt";
private final List<String> keywords = new ArrayList<>();
private final List<Pattern> keywordPatterns = new ArrayList<>();
public void validateForUpload(MultipartFile file, boolean keywordWarningsConfirmed) {
try (InputStream inputStream = file.getInputStream()) {
validateForUpload(inputStream, keywordWarningsConfirmed);
} catch (IOException e) {
throw new FileProcessingException("Unable to read file for personal data check.", e);
}
}
public boolean checkForPersonalData(MultipartFile file) {
validateForUpload(file, false);
return false;
}
public boolean checkForPersonalData(InputStream inputStream) {
validateForUpload(inputStream, false);
return false;
}
public void validateForUpload(InputStream inputStream, boolean keywordWarningsConfirmed) {
PersonalDataScanResult scanResult = scanForPersonalData(inputStream);
if (scanResult.hasHardPersonalData) {
throw new PersonalDataException(
"Personal data detected (email/phone). Please remove all personal data before uploading this file.");
}
if (!keywordWarningsConfirmed && !scanResult.suspectedKeywords.isEmpty()) {
throw new PersonalDataWarningConfirmationRequiredException(
"Potential personal data detected based on suspicious keywords. Please confirm to continue.",
new ArrayList<>(scanResult.suspectedKeywords));
}
}
public void validateHardPersonalDataOnly(InputStream inputStream) {
PersonalDataScanResult scanResult = scanForPersonalData(inputStream);
if (scanResult.hasHardPersonalData) {
throw new PersonalDataException(
"Personal data detected (email/phone). Please remove all personal data before uploading this file.");
}
}
private PersonalDataScanResult scanForPersonalData(InputStream inputStream) {
boolean hasHardPersonalData = false;
Set<String> suspectedKeywords = new LinkedHashSet<>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
boolean telephonePatternFound = containsLikelyPhoneNumber(line);
boolean emailPatternFound = EMAIL_PATTERN.matcher(line).find();
if (telephonePatternFound || emailPatternFound) {
hasHardPersonalData = true;
}
suspectedKeywords.addAll(findSuspiciousKeywords(line));
if (hasHardPersonalData) {
break;
}
}
} catch (IOException e) {
throw new FileProcessingException("Unable to read stream for personal data check.", e);
}
return new PersonalDataScanResult(hasHardPersonalData, suspectedKeywords);
}
private boolean containsLikelyPhoneNumber(String line) {
Matcher matcher = PHONE_CANDIDATE_PATTERN.matcher(line);
while (matcher.find()) {
String candidate = matcher.group();
if (isLikelyPhoneCandidate(candidate)) {
return true;
}
}
return false;
}
private boolean isLikelyPhoneCandidate(String candidate) {
if (candidate == null) {
return false;
}
String trimmed = candidate.trim();
if (DATE_LIKE_PATTERN.matcher(trimmed).matches()) {
return false;
}
if (DATE_TIME_LIKE_PATTERN.matcher(trimmed).matches()) {
return false;
}
if (COORDINATE_TUPLE_PATTERN.matcher(trimmed).matches()) {
return false;
}
if (ID_LIKE_NUMERIC_HYPHEN_PATTERN.matcher(trimmed).matches()) {
return false;
}
if (ID_LIKE_NUMERIC_SLASH_PATTERN.matcher(trimmed).matches()) {
return false;
}
if (ID_LIKE_NUMERIC_SLASH_PATH_PATTERN.matcher(trimmed).matches()) {
return false;
}
if (DECIMAL_NUMBER_PATTERN.matcher(trimmed).matches()) {
return false;
}
if (LEADING_ZEROS_IDENTIFIER_PATTERN.matcher(trimmed).matches()) {
return false;
}
String normalized = normalizeToInternationalPhone(trimmed);
if (normalized == null) {
return false;
}
String digitsOnly = normalized.replaceAll("\\D", "");
if (digitsOnly.length() < 10 || digitsOnly.length() > 15) {
return false;
}
try {
PhoneNumber phoneNumber = PHONE_NUMBER_UTIL.parse(normalized, "ZZ");
if (!PHONE_NUMBER_UTIL.isValidNumber(phoneNumber)) {
return false;
}
String regionCode = PHONE_NUMBER_UTIL.getRegionCodeForNumber(phoneNumber);
if (regionCode == null || !EUROPEAN_REGIONS.contains(regionCode)) {
return false;
}
PhoneNumberUtil.PhoneNumberType type = PHONE_NUMBER_UTIL.getNumberType(phoneNumber);
return type == PhoneNumberUtil.PhoneNumberType.FIXED_LINE
|| type == PhoneNumberUtil.PhoneNumberType.MOBILE
|| type == PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE;
} catch (NumberParseException e) {
return false;
}
}
private String normalizeToInternationalPhone(String candidate) {
if (candidate == null) {
return null;
}
String compact = candidate.replaceAll("[\\s()./-]", "");
if (compact.startsWith("00")) {
compact = "+" + compact.substring(2);
}
if (!compact.startsWith("+")) {
return null;
}
if (!compact.matches("^\\+[0-9]{8,15}$")) {
return null;
}
return compact;
}
private Set<String> findSuspiciousKeywords(String line) {
Set<String> foundKeywords = new LinkedHashSet<>();
String normalizedLine = normalizeForComparison(line);
List<String> loadedKeywords = getKeywords();
for (int i = 0; i < loadedKeywords.size(); i++) {
Pattern keywordPattern = keywordPatterns.get(i);
Matcher matcher = keywordPattern.matcher(normalizedLine);
if (matcher.find()) {
foundKeywords.add(loadedKeywords.get(i));
}
}
return foundKeywords;
}
protected List<String> getKeywords() {
if (keywords.isEmpty()) {
try (InputStream inputStream = getClass().getResourceAsStream(KEYWORDS_FILE)) {
if (inputStream == null) {
throw new FileProcessingException("Unable to load keywords file for personal data check.");
}
List<String> loadedKeywords = new BufferedReader(new InputStreamReader(inputStream)).lines()
.map(String::trim)
.filter(keyword -> !keyword.isEmpty())
.map(this::normalizeForComparison)
.distinct()
.collect(Collectors.toList());
keywords.addAll(loadedKeywords);
keywordPatterns.addAll(
loadedKeywords.stream().map(this::buildKeywordPattern).collect(Collectors.toList()));
} catch (IOException e) {
throw new FileProcessingException("Unable to load keywords file for personal data check.", e);
}
}
return keywords;
}
private Pattern buildKeywordPattern(String keyword) {
String escapedKeyword = Pattern.quote(keyword);
String regex = "(?<![\\p{L}\\p{N}])" + escapedKeyword + "(?![\\p{L}\\p{N}])";
return Pattern.compile(regex);
}
private String normalizeForComparison(String value) {
if (value == null) {
return "";
}
String normalized = Normalizer.normalize(value, Normalizer.Form.NFD)
.replaceAll("\\p{M}+", "")
.toLowerCase(Locale.ROOT)
.trim();
return normalized;
}
private static class PersonalDataScanResult {
private final boolean hasHardPersonalData;
private final Set<String> suspectedKeywords;
private PersonalDataScanResult(boolean hasHardPersonalData, Set<String> suspectedKeywords) {
this.hasHardPersonalData = hasHardPersonalData;
this.suspectedKeywords = suspectedKeywords;
}
}
}