From 44ea2150fefa496475687d0c0dd92f83defccbab Mon Sep 17 00:00:00 2001 From: Raman Arora <raman@basistech.com> Date: Wed, 25 Mar 2020 11:42:16 -0400 Subject: [PATCH] 6172: Validation of correlation attribute value not happening correctly --- bindings/java/ivy.xml | 1 + bindings/java/nbproject/project.xml | 2 +- .../datamodel/CommunicationsManager.java | 41 ++--------- .../datamodel/CommunicationsUtils.java | 69 +++++++++++++++++++ 4 files changed, 75 insertions(+), 38 deletions(-) create mode 100644 bindings/java/src/org/sleuthkit/datamodel/CommunicationsUtils.java diff --git a/bindings/java/ivy.xml b/bindings/java/ivy.xml index 14073535f..af9f85d09 100644 --- a/bindings/java/ivy.xml +++ b/bindings/java/ivy.xml @@ -4,6 +4,7 @@ <dependency org="joda-time" name="joda-time" rev="2.4" /> <dependency org="com.google.guava" name="guava" rev="19.0"/> <dependency org="org.apache.commons" name="commons-lang3" rev="3.0"/> + <dependency org="commons-validator" name="commons-validator" rev="1.6"/> <dependency org="com.google.code.gson" name="gson" rev="2.8.5"/> diff --git a/bindings/java/nbproject/project.xml b/bindings/java/nbproject/project.xml index ccbcee3fd..58ab53b9b 100755 --- a/bindings/java/nbproject/project.xml +++ b/bindings/java/nbproject/project.xml @@ -114,7 +114,7 @@ <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/4"> <compilation-unit> <package-root>src</package-root> - <classpath mode="compile">lib;lib/diffutils-1.2.1.jar;lib/junit-4.8.2.jar;lib/postgresql-9.4-1201.jdbc41.jar;lib/c3p0-0.9.5.jar;lib/mchange-commons-java-0.2.9.jar;lib/c3p0-0.9.5-sources.jar;lib/c3p0-0.9.5-javadoc.jar;lib/joda-time-2.4.jar;lib/commons-lang3-3.0.jar;lib/guava-19.0.jar;lib/SparseBitSet-1.1.jar;lib/gson-2.8.5.jar</classpath> + <classpath mode="compile">lib;lib/diffutils-1.2.1.jar;lib/junit-4.8.2.jar;lib/postgresql-9.4-1201.jdbc41.jar;lib/c3p0-0.9.5.jar;lib/mchange-commons-java-0.2.9.jar;lib/c3p0-0.9.5-sources.jar;lib/c3p0-0.9.5-javadoc.jar;lib/joda-time-2.4.jar;lib/commons-lang3-3.0.jar;lib/guava-19.0.jar;lib/SparseBitSet-1.1.jar;lib/gson-2.8.5.jar;lib/commons-validator-1.6.jar</classpath> <built-to>build</built-to> <source-level>1.8</source-level> </compilation-unit> diff --git a/bindings/java/src/org/sleuthkit/datamodel/CommunicationsManager.java b/bindings/java/src/org/sleuthkit/datamodel/CommunicationsManager.java index e4bdea274..8790b4bca 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/CommunicationsManager.java +++ b/bindings/java/src/org/sleuthkit/datamodel/CommunicationsManager.java @@ -1315,52 +1315,19 @@ int getAccountTypeId(Account.Type accountType) { * * @return The normalized account id. */ - private String normalizeAccountID(Account.Type accountType, String accountUniqueID) { + private String normalizeAccountID(Account.Type accountType, String accountUniqueID) throws TskCoreException { String normailzeAccountID = accountUniqueID; if (accountType.equals(Account.Type.PHONE)) { - normailzeAccountID = normalizePhoneNum(accountUniqueID); + normailzeAccountID = CommunicationsUtils.normalizePhoneNum(accountUniqueID); } else if (accountType.equals(Account.Type.EMAIL)) { - normailzeAccountID = normalizeEmailAddress(accountUniqueID); + normailzeAccountID = CommunicationsUtils.normalizeEmailAddress(accountUniqueID); } return normailzeAccountID; } - /** - * Normalize the phone number by removing all non numeric characters, except - * for leading +. - * - * @param phoneNum The phone number to normalize - * - * @return The normalized phone number. - */ - private String normalizePhoneNum(String phoneNum) { - String normailzedPhoneNum = phoneNum.replaceAll("\\D", ""); - - if (phoneNum.startsWith("+")) { - normailzedPhoneNum = "+" + normailzedPhoneNum; - } - - if (normailzedPhoneNum.isEmpty()) { - normailzedPhoneNum = phoneNum; - } - - return normailzedPhoneNum; - } - - /** - * Normalize the given email address by converting it to lowercase. - * - * @param emailAddress The email address tot normalize - * - * @return The normalized email address. - */ - private String normalizeEmailAddress(String emailAddress) { - String normailzedEmailAddr = emailAddress.toLowerCase(); - - return normailzedEmailAddr; - } + /** * Builds the SQL for the given CommunicationsFilter. diff --git a/bindings/java/src/org/sleuthkit/datamodel/CommunicationsUtils.java b/bindings/java/src/org/sleuthkit/datamodel/CommunicationsUtils.java new file mode 100644 index 000000000..c25eb2cf9 --- /dev/null +++ b/bindings/java/src/org/sleuthkit/datamodel/CommunicationsUtils.java @@ -0,0 +1,69 @@ +/* + * Sleuth Kit Data Model + * + * Copyright 2020 Basis Technology Corp. + * Contact: carrier <at> sleuthkit <dot> org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.sleuthkit.datamodel; + +import org.apache.commons.validator.routines.EmailValidator; + +/** + * Provides general utility methods related to communications artifacts. + * + */ +public final class CommunicationsUtils { + + + /** + * Checks if the given string may be a phone number. + * Normalize the phone number by removing all non numeric characters, except + * for leading +. + * + * @param phoneNum The string to check and normalize. + * + * @return The normalized phone number. + * + * @throws TskCoreException If the given string is not a valid phone number. + * + */ + public static String normalizePhoneNum(String phoneNum) throws TskCoreException { + if (phoneNum.matches("\\+?[0-9()\\-\\s]+")) { + return phoneNum.replaceAll("[^0-9\\+]", ""); + } else { + throw new TskCoreException(String.format("Input string is not a valid phone number: %s", phoneNum)); + } + } + + /** + * 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. + * + * @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)) { + return emailAddress.toLowerCase(); + } else { + throw new TskCoreException(String.format("Input string is not a valid email address: %s", emailAddress)); + } + } + +} -- GitLab