20 package org.sleuthkit.autopsy.centralrepository.datamodel;
22 import java.util.Arrays;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Optional;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.commons.validator.routines.DomainValidator;
29 import org.apache.commons.validator.routines.EmailValidator;
52 if (attributeType == null) {
59 String trimmedData = data.trim();
61 switch (attributeType.getId()) {
67 return normalizeEmail(trimmedData);
69 return normalizePhone(trimmedData);
88 if (defaultCorrelationType.getId() == attributeType.getId()) {
92 final String errorMessage = String.format(
93 "Validator function not found for attribute type: %s",
94 attributeType.getDisplayName());
112 if (typeOption.isPresent()) {
116 throw new CentralRepoException(String.format(
"Given attributeTypeId did not correspond to any known Attribute: %s", attributeTypeId));
123 private static String
normalizeMd5(String data)
throws CorrelationAttributeNormalizationException {
124 final String validMd5Regex =
"^[a-f0-9]{32}$";
125 final String dataLowered = data.toLowerCase();
126 if (dataLowered.matches(validMd5Regex)) {
129 throw new CorrelationAttributeNormalizationException(String.format(
"Data purporting to be an MD5 was found not to comform to expected format: %s", data));
137 private static String
normalizeDomain(String data)
throws CorrelationAttributeNormalizationException {
138 DomainValidator validator = DomainValidator.getInstance(
true);
139 if (validator.isValid(data)) {
142 final String validIpAddressRegex =
"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
143 if (data.matches(validIpAddressRegex)) {
146 throw new CorrelationAttributeNormalizationException(String.format(
"Data was expected to be a valid domain: %s", data));
160 static String normalizeEmail(String emailAddress)
throws CorrelationAttributeNormalizationException {
161 if (isValidEmailAddress(emailAddress)) {
162 return emailAddress.toLowerCase().trim();
164 throw new CorrelationAttributeNormalizationException(String.format(
"Data was expected to be a valid email address: %s", emailAddress));
177 static String normalizePhone(String phoneNumber)
throws CorrelationAttributeNormalizationException {
178 if (isValidPhoneNumber(phoneNumber)) {
179 String normalizedNumber = phoneNumber.replaceAll(
"\\s+",
"");
180 normalizedNumber = normalizedNumber.replaceAll(
"[\\-()]",
"");
184 throw new CorrelationAttributeNormalizationException(String.format(
"Phone number string %s is too short ", phoneNumber));
186 return normalizedNumber;
189 throw new CorrelationAttributeNormalizationException(String.format(
"Data was expected to be a valid phone number: %s", phoneNumber));
196 private static String
normalizeUsbId(String data)
throws CorrelationAttributeNormalizationException {
214 private static String
verifySsid(String data)
throws CorrelationAttributeNormalizationException {
215 if (data.length() <= 32) {
218 throw new CorrelationAttributeNormalizationException(
"Name provided was longer than the maximum valid SSID (32 characters). Name: " + data);
244 private static String
normalizeIccid(String data)
throws CorrelationAttributeNormalizationException {
245 final String validIccidRegex =
"^89[f0-9]{17,22}$";
246 final String iccidWithoutSeperators = data.toLowerCase().replaceAll(SEPERATORS_REGEX,
"");
247 if (iccidWithoutSeperators.matches(validIccidRegex)) {
248 return iccidWithoutSeperators;
250 throw new CorrelationAttributeNormalizationException(
"Data provided was not a valid ICCID. : " + data);
271 private static String
normalizeImsi(String data)
throws CorrelationAttributeNormalizationException {
272 final String validImsiRegex =
"^[0-9]{14,15}$";
273 final String imsiWithoutSeperators = data.replaceAll(SEPERATORS_REGEX,
"");
274 if (imsiWithoutSeperators.matches(validImsiRegex)) {
275 return imsiWithoutSeperators;
277 throw new CorrelationAttributeNormalizationException(
"Data provided was not a valid IMSI. : " + data);
295 private static String
normalizeMac(String data)
throws CorrelationAttributeNormalizationException {
296 final String validMacRegex =
"^([a-f0-9]{12}|[a-f0-9]{16})$";
297 final String macWithoutSeperators = data.toLowerCase().replaceAll(SEPERATORS_REGEX,
"");
298 if (macWithoutSeperators.matches(validMacRegex)) {
299 return macWithoutSeperators;
301 throw new CorrelationAttributeNormalizationException(
"Data provided was not a valid MAC address. : " + data);
324 private static String
normalizeImei(String data)
throws CorrelationAttributeNormalizationException {
325 final String validImeiRegex =
"^[0-9]{14,16}$";
326 final String imeiWithoutSeperators = data.replaceAll(SEPERATORS_REGEX,
"");
327 if (imeiWithoutSeperators.matches(validImeiRegex)) {
328 return imeiWithoutSeperators;
330 throw new CorrelationAttributeNormalizationException(
"Data provided was not a valid IMEI. : " + data);
352 static boolean isValidPhoneNumber(String phoneNumber) {
356 for (
int i = 0; i < phoneNumber.length(); i++) {
357 if ( !((i == 0 && phoneNumber.charAt(i) ==
'+')
358 || Character.isSpaceChar(phoneNumber.charAt(i))
359 || Character.isDigit(phoneNumber.charAt(i))
360 || PHONENUMBER_CHARS.contains(String.valueOf(phoneNumber.charAt(i))))) {
377 static boolean isValidEmailAddress(String emailAddress) {
378 if (!StringUtils.isEmpty(emailAddress)) {
379 EmailValidator validator = EmailValidator.getInstance(
true,
true);
380 return validator.isValid(emailAddress);
static final String SEPERATORS_REGEX
static final int EMAIL_TYPE_ID
static final int USBID_TYPE_ID
static String normalizeImsi(String data)
static String normalizeImei(String data)
static String extractDomain(String urlString)
static final int ICCID_TYPE_ID
static String normalize(int attributeTypeId, String data)
static List< CorrelationAttributeInstance.Type > getDefaultCorrelationTypes()
static String normalizeMd5(String data)
static final int IMEI_TYPE_ID
static final Set< String > PHONENUMBER_CHARS
static final int MIN_PHONENUMBER_LEN
static String normalizeMac(String data)
static String normalize(CorrelationAttributeInstance.Type attributeType, String data)
static String normalizeIccid(String data)
static final int DOMAIN_TYPE_ID
static String normalizeDomain(String data)
static final int PHONE_TYPE_ID
static final int MAC_TYPE_ID
static final int IMSI_TYPE_ID
static String verifySsid(String data)
CorrelationAttributeNormalizer()
static final int SSID_TYPE_ID
static String normalizeUsbId(String data)
static final int FILES_TYPE_ID