Skip to content
Snippets Groups Projects
Commit 5b0c3efe authored by Raman Arora's avatar Raman Arora
Browse files

6219: TskCoreException thrown from normalizePhoneNum.

 Do not attempt to create an account when we do not have a valid phone number or email address.
parent 432494de
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
*/ */
package org.sleuthkit.datamodel; package org.sleuthkit.datamodel;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.EmailValidator; import org.apache.commons.validator.routines.EmailValidator;
/** /**
...@@ -33,11 +34,10 @@ private CommunicationsUtils() { ...@@ -33,11 +34,10 @@ private CommunicationsUtils() {
} }
/** /**
* Checks if the given string may be a phone number. * Normalize the given phone number by removing all non numeric characters,
* Normalize the phone number by removing all non numeric characters, except * except for a leading +.
* for leading +.
* *
* @param phoneNum The string to check and normalize. * @param phoneNum The string to normalize.
* *
* @return The normalized phone number. * @return The normalized phone number.
* *
...@@ -53,22 +53,69 @@ public static String normalizePhoneNum(String phoneNum) throws TskCoreException ...@@ -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. * 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. * @return The normalized email address.
* @throws TskCoreException If the given string is not a valid email address. * @throws TskCoreException If the given string is not a valid email address.
*/ */
public static String normalizeEmailAddress(String emailAddress) throws TskCoreException { public static String normalizeEmailAddress(String emailAddress) throws TskCoreException {
EmailValidator validator = EmailValidator.getInstance(true, true); if (isValidEmailAddress(emailAddress)) {
if (validator.isValid(emailAddress)) {
return emailAddress.toLowerCase(); return emailAddress.toLowerCase();
} else { } else {
throw new TskCoreException(String.format("Input string is not a valid email address: %s", emailAddress)); 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;
}
} }
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
import org.sleuthkit.datamodel.Blackboard.BlackboardException; import org.sleuthkit.datamodel.Blackboard.BlackboardException;
import org.sleuthkit.datamodel.BlackboardArtifact; import org.sleuthkit.datamodel.BlackboardArtifact;
import org.sleuthkit.datamodel.BlackboardAttribute; import org.sleuthkit.datamodel.BlackboardAttribute;
import org.sleuthkit.datamodel.CommunicationsUtils;
import org.sleuthkit.datamodel.Content; import org.sleuthkit.datamodel.Content;
import org.sleuthkit.datamodel.DataSource; import org.sleuthkit.datamodel.DataSource;
import org.sleuthkit.datamodel.Relationship; import org.sleuthkit.datamodel.Relationship;
...@@ -311,7 +312,7 @@ public BlackboardArtifact addContact(String contactName, ...@@ -311,7 +312,7 @@ public BlackboardArtifact addContact(String contactName,
/** /**
* Creates a contact's account instance of specified account type, if the * 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 * Also creates a CONTACT relationship between the self account and the new
* contact account. * contact account.
...@@ -322,7 +323,7 @@ private void createContactMethodAccountAndRelationship(Account.Type accountType, ...@@ -322,7 +323,7 @@ private void createContactMethodAccountAndRelationship(Account.Type accountType,
// Find/Create an account instance for each of the contact method // Find/Create an account instance for each of the contact method
// Create a relationship between selfAccount and contactAccount // Create a relationship between selfAccount and contactAccount
if (!StringUtils.isEmpty(accountUniqueID)) { if (CommunicationsUtils.isValidAccountId(accountType, accountUniqueID)) {
AccountFileInstance contactAccountInstance = createAccountInstance(accountType, accountUniqueID); AccountFileInstance contactAccountInstance = createAccountInstance(accountType, accountUniqueID);
// Create a relationship between self account and the contact account // Create a relationship between self account and the contact account
...@@ -506,7 +507,7 @@ public BlackboardArtifact addMessage(String messageType, ...@@ -506,7 +507,7 @@ public BlackboardArtifact addMessage(String messageType,
// set sender attribute and create sender account // set sender attribute and create sender account
AccountFileInstance senderAccountInstance = null; AccountFileInstance senderAccountInstance = null;
if (!StringUtils.isEmpty(senderId)) { if (CommunicationsUtils.isValidAccountId(moduleAccountsType, senderId)) {
senderAccountInstance = createAccountInstance(moduleAccountsType, senderId); senderAccountInstance = createAccountInstance(moduleAccountsType, senderId);
} }
...@@ -515,7 +516,7 @@ public BlackboardArtifact addMessage(String messageType, ...@@ -515,7 +516,7 @@ public BlackboardArtifact addMessage(String messageType,
String recipientsStr = ""; String recipientsStr = "";
if (!isEffectivelyEmpty(recipientIdsList)) { if (!isEffectivelyEmpty(recipientIdsList)) {
for (String recipient : recipientIdsList) { for (String recipient : recipientIdsList) {
if (!StringUtils.isEmpty(recipient)) { if (CommunicationsUtils.isValidAccountId(moduleAccountsType, recipient)) {
recipientAccountsList.add(createAccountInstance(moduleAccountsType, recipient)); recipientAccountsList.add(createAccountInstance(moduleAccountsType, recipient));
} }
} }
...@@ -741,7 +742,7 @@ public BlackboardArtifact addCalllog(CommunicationDirection direction, ...@@ -741,7 +742,7 @@ public BlackboardArtifact addCalllog(CommunicationDirection direction,
addCommDirectionIfKnown(direction, attributes); addCommDirectionIfKnown(direction, attributes);
AccountFileInstance callerAccountInstance = null; AccountFileInstance callerAccountInstance = null;
if (!StringUtils.isEmpty(callerId)) { if (CommunicationsUtils.isValidAccountId(moduleAccountsType, callerId)) {
callerAccountInstance = createAccountInstance(moduleAccountsType, callerId); callerAccountInstance = createAccountInstance(moduleAccountsType, callerId);
} }
...@@ -751,7 +752,7 @@ public BlackboardArtifact addCalllog(CommunicationDirection direction, ...@@ -751,7 +752,7 @@ public BlackboardArtifact addCalllog(CommunicationDirection direction,
if (!isEffectivelyEmpty(calleeIdsList)) { if (!isEffectivelyEmpty(calleeIdsList)) {
calleesStr = addressListToString(calleeIdsList); calleesStr = addressListToString(calleeIdsList);
for (String callee : calleeIdsList) { for (String callee : calleeIdsList) {
if (!StringUtils.isEmpty(callee)) { if (CommunicationsUtils.isValidAccountId(moduleAccountsType, callee)) {
recipientAccountsList.add(createAccountInstance(moduleAccountsType, callee)); recipientAccountsList.add(createAccountInstance(moduleAccountsType, callee));
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment