diff --git a/bindings/java/src/org/sleuthkit/datamodel/CommunicationsUtils.java b/bindings/java/src/org/sleuthkit/datamodel/CommunicationsUtils.java index 348613758ac4f927cb3737b4a7f667c87dd83951..0e603bef0efa88bc6751bea243b9da5616f3480f 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/CommunicationsUtils.java +++ b/bindings/java/src/org/sleuthkit/datamodel/CommunicationsUtils.java @@ -18,6 +18,7 @@ */ package org.sleuthkit.datamodel; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.validator.routines.EmailValidator; /** @@ -33,11 +34,10 @@ private CommunicationsUtils() { } /** - * Checks if the given string may be a phone number. - * Normalize the phone number by removing all non numeric characters, except - * for leading +. + * Normalize the given phone number by removing all non numeric characters, + * except for a leading +. * - * @param phoneNum The string to check and normalize. + * @param phoneNum The string to normalize. * * @return The normalized phone number. * @@ -45,7 +45,7 @@ private CommunicationsUtils() { * */ public static String normalizePhoneNum(String phoneNum) throws TskCoreException { - if (phoneNum.matches("\\+?[0-9()\\-\\s]+")) { + if (isValidPhoneNumber(phoneNum)) { return phoneNum.replaceAll("[^0-9\\+]", ""); } else { throw new TskCoreException(String.format("Input string is not a valid phone number: %s", phoneNum)); @@ -53,22 +53,69 @@ public static String normalizePhoneNum(String phoneNum) throws TskCoreException } /** - * Checks if the given string is a valid email address. * Normalizes the given email address by converting it to lowercase. * - * @param emailAddress The string to be checked and normalized. + * @param emailAddress The email address string to be normalized. * * @return The normalized email address. * @throws TskCoreException If the given string is not a valid email address. */ public static String normalizeEmailAddress(String emailAddress) throws TskCoreException { - EmailValidator validator = EmailValidator.getInstance(true, true); - if (validator.isValid(emailAddress)) { + if (isValidEmailAddress(emailAddress)) { return emailAddress.toLowerCase(); } else { throw new TskCoreException(String.format("Input string is not a valid email address: %s", emailAddress)); } } + /** + * Checks if the given accountId is a valid id for + * the specified account type. + * + * @param accountType Account type. + * @param accountUniqueID Id to check. + * + * @return True, if the id is a valid id for the given account type, False otherwise. + */ + public static boolean isValidAccountId(Account.Type accountType, String accountUniqueID) { + if (accountType == Account.Type.PHONE) { + return isValidPhoneNumber(accountUniqueID); + } + if (accountType == Account.Type.EMAIL) { + return isValidPhoneNumber(accountUniqueID); + } + + return !StringUtils.isEmpty(accountUniqueID); + } + + /** + * Checks if the given string is a valid phone number. + * + * @param phoneNum Phone number string to check. + * + * @return True if the given string is a valid phone number, false otherwise. + */ + public static boolean isValidPhoneNumber(String phoneNum) { + if (!StringUtils.isEmpty(phoneNum)) { + return phoneNum.matches("\\+?[0-9()\\-\\s]+"); + } + return false; + } + + /** + * Checks if the given string is a valid email address. + * + * @param emailAddress String to check. + * + * @return True if the given string is a valid email address, false otherwise. + */ + public static boolean isValidEmailAddress(String emailAddress) { + if (!StringUtils.isEmpty(emailAddress)) { + EmailValidator validator = EmailValidator.getInstance(true, true); + return validator.isValid(emailAddress); + } + + return false; + } } diff --git a/bindings/java/src/org/sleuthkit/datamodel/blackboardutils/CommunicationArtifactsHelper.java b/bindings/java/src/org/sleuthkit/datamodel/blackboardutils/CommunicationArtifactsHelper.java index 578bb6c544a97c789d46dc22836834b95ef206da..e63275e0a348f3498c052f4cbeab0f204c387db1 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/blackboardutils/CommunicationArtifactsHelper.java +++ b/bindings/java/src/org/sleuthkit/datamodel/blackboardutils/CommunicationArtifactsHelper.java @@ -33,6 +33,7 @@ import org.sleuthkit.datamodel.Blackboard.BlackboardException; import org.sleuthkit.datamodel.BlackboardArtifact; import org.sleuthkit.datamodel.BlackboardAttribute; +import org.sleuthkit.datamodel.CommunicationsUtils; import org.sleuthkit.datamodel.Content; import org.sleuthkit.datamodel.DataSource; import org.sleuthkit.datamodel.Relationship; @@ -311,7 +312,7 @@ public BlackboardArtifact addContact(String contactName, /** * Creates a contact's account instance of specified account type, if the - * account id is not null/empty. + * account id is not null/empty and is a valid account id for the account type. * * Also creates a CONTACT relationship between the self account and the new * contact account. @@ -322,7 +323,7 @@ private void createContactMethodAccountAndRelationship(Account.Type accountType, // Find/Create an account instance for each of the contact method // Create a relationship between selfAccount and contactAccount - if (!StringUtils.isEmpty(accountUniqueID)) { + if (CommunicationsUtils.isValidAccountId(accountType, accountUniqueID)) { AccountFileInstance contactAccountInstance = createAccountInstance(accountType, accountUniqueID); // Create a relationship between self account and the contact account @@ -506,7 +507,7 @@ public BlackboardArtifact addMessage(String messageType, // set sender attribute and create sender account AccountFileInstance senderAccountInstance = null; - if (!StringUtils.isEmpty(senderId)) { + if (CommunicationsUtils.isValidAccountId(moduleAccountsType, senderId)) { senderAccountInstance = createAccountInstance(moduleAccountsType, senderId); } @@ -515,7 +516,7 @@ public BlackboardArtifact addMessage(String messageType, String recipientsStr = ""; if (!isEffectivelyEmpty(recipientIdsList)) { for (String recipient : recipientIdsList) { - if (!StringUtils.isEmpty(recipient)) { + if (CommunicationsUtils.isValidAccountId(moduleAccountsType, recipient)) { recipientAccountsList.add(createAccountInstance(moduleAccountsType, recipient)); } } @@ -741,7 +742,7 @@ public BlackboardArtifact addCalllog(CommunicationDirection direction, addCommDirectionIfKnown(direction, attributes); AccountFileInstance callerAccountInstance = null; - if (!StringUtils.isEmpty(callerId)) { + if (CommunicationsUtils.isValidAccountId(moduleAccountsType, callerId)) { callerAccountInstance = createAccountInstance(moduleAccountsType, callerId); } @@ -751,7 +752,7 @@ public BlackboardArtifact addCalllog(CommunicationDirection direction, if (!isEffectivelyEmpty(calleeIdsList)) { calleesStr = addressListToString(calleeIdsList); for (String callee : calleeIdsList) { - if (!StringUtils.isEmpty(callee)) { + if (CommunicationsUtils.isValidAccountId(moduleAccountsType, callee)) { recipientAccountsList.add(createAccountInstance(moduleAccountsType, callee)); } } diff --git a/bindings/java/src/org/sleuthkit/datamodel/blackboardutils/WebBrowserArtifactsHelper.java b/bindings/java/src/org/sleuthkit/datamodel/blackboardutils/WebBrowserArtifactsHelper.java index 1a4cb5f2cb808720b6fcefaac49efa1a59fdbd2b..f3b7814666bf318c24a74dfe37b2650c7388a49a 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/blackboardutils/WebBrowserArtifactsHelper.java +++ b/bindings/java/src/org/sleuthkit/datamodel/blackboardutils/WebBrowserArtifactsHelper.java @@ -30,6 +30,7 @@ import org.sleuthkit.datamodel.BlackboardArtifact; import org.sleuthkit.datamodel.BlackboardAttribute; import org.sleuthkit.datamodel.CommunicationsManager; +import org.sleuthkit.datamodel.CommunicationsUtils; import org.sleuthkit.datamodel.Content; import org.sleuthkit.datamodel.SleuthkitCase; import org.sleuthkit.datamodel.TskCoreException; @@ -299,11 +300,11 @@ public BlackboardArtifact addWebFormAddress(String personName, String email, Collection<BlackboardAttribute> attributes = new ArrayList<>(); CommunicationsManager commManager = this.getSleuthkitCase().getCommunicationsManager(); - if(StringUtils.isNotBlank(email)) { + if (CommunicationsUtils.isValidEmailAddress(email)) { commManager.createAccountFileInstance(Account.Type.EMAIL, email, this.getModuleName(), this.getContent()); } - if(StringUtils.isNotBlank(phoneNumber)) { + if(CommunicationsUtils.isValidPhoneNumber(phoneNumber)) { commManager.createAccountFileInstance(Account.Type.PHONE, phoneNumber, this.getModuleName(), this.getContent()); }