Skip to content
Snippets Groups Projects
Unverified Commit 5a2b5f3c authored by Richard Cordovano's avatar Richard Cordovano Committed by GitHub
Browse files

Merge pull request #1870 from raman-bt/6219-validation-exceptions

6219: TskCoreException thrown from normalizePhoneNum.
parents a5b44006 bd45f258
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
}
......@@ -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));
}
}
......
......@@ -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());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment