From 0edf1e3bc009acc84ce58c7d68fc66c6915c15bb Mon Sep 17 00:00:00 2001 From: Grace Jia Date: Fri, 4 Oct 2019 14:29:19 -0700 Subject: Implemented piiHandle to avoid importing it from Telecomm package. Test: atest com.android.providers.blockednumber.BlockedNumberProviderTest Bug: 140579775 Change-Id: I934f2ddb5e5db4f979c9a14a0a8635e927ee5b8f --- .../blockednumber/BlockedNumberProvider.java | 2 +- src/com/android/providers/blockednumber/Utils.java | 100 +++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) (limited to 'src/com/android') diff --git a/src/com/android/providers/blockednumber/BlockedNumberProvider.java b/src/com/android/providers/blockednumber/BlockedNumberProvider.java index 2e029d8..4d6a284 100644 --- a/src/com/android/providers/blockednumber/BlockedNumberProvider.java +++ b/src/com/android/providers/blockednumber/BlockedNumberProvider.java @@ -15,7 +15,7 @@ */ package com.android.providers.blockednumber; -import static android.telecom.Log.piiHandle; +import static com.android.providers.blockednumber.Utils.piiHandle; import android.Manifest; import android.annotation.NonNull; diff --git a/src/com/android/providers/blockednumber/Utils.java b/src/com/android/providers/blockednumber/Utils.java index 6aaf178..66f0fcb 100644 --- a/src/com/android/providers/blockednumber/Utils.java +++ b/src/com/android/providers/blockednumber/Utils.java @@ -15,21 +15,33 @@ */ package com.android.providers.blockednumber; +import static android.util.Log.isLoggable; + import android.annotation.NonNull; import android.annotation.Nullable; import android.content.Context; import android.location.Country; import android.location.CountryDetector; +import android.net.Uri; +import android.os.Build; +import android.telecom.PhoneAccount; import android.telephony.PhoneNumberUtils; import android.text.TextUtils; import java.util.Locale; public class Utils { + /** + * When generating a bug report, include the last X dialable digits when logging phone numbers. + */ + private static final int NUM_DIALABLE_DIGITS_TO_LOG = Build.IS_USER ? 0 : 2; + private Utils() { } public static final int MIN_INDEX_LEN = 8; + public static String TAG = "BlockedNumberProvider"; + public static boolean VERBOSE = isLoggable(TAG, android.util.Log.VERBOSE); /** * @return The current country code. @@ -74,4 +86,92 @@ public class Utils { public static @Nullable String wrapSelectionWithParens(@Nullable String selection) { return TextUtils.isEmpty(selection) ? null : "(" + selection + ")"; } + + /** + * Generates an obfuscated string for a calling handle in {@link Uri} format, or a raw phone + * phone number in {@link String} format. + * @param pii The information to obfuscate. + * @return The obfuscated string. + */ + public static String piiHandle(Object pii) { + if (pii == null || VERBOSE) { + return String.valueOf(pii); + } + + StringBuilder sb = new StringBuilder(); + if (pii instanceof Uri) { + Uri uri = (Uri) pii; + String scheme = uri.getScheme(); + + if (!TextUtils.isEmpty(scheme)) { + sb.append(scheme).append(":"); + } + + String textToObfuscate = uri.getSchemeSpecificPart(); + if (PhoneAccount.SCHEME_TEL.equals(scheme)) { + obfuscatePhoneNumber(sb, textToObfuscate); + } else if (PhoneAccount.SCHEME_SIP.equals(scheme)) { + for (int i = 0; i < textToObfuscate.length(); i++) { + char c = textToObfuscate.charAt(i); + if (c != '@' && c != '.') { + c = '*'; + } + sb.append(c); + } + } else { + sb.append(pii(pii)); + } + } else if (pii instanceof String) { + String number = (String) pii; + obfuscatePhoneNumber(sb, number); + } + + return sb.toString(); + } + + /** + * Obfuscates a phone number, allowing NUM_DIALABLE_DIGITS_TO_LOG digits to be exposed for the + * phone number. + * @param sb String buffer to write obfuscated number to. + * @param phoneNumber The number to obfuscate. + */ + private static void obfuscatePhoneNumber(StringBuilder sb, String phoneNumber) { + int numDigitsToObfuscate = getDialableCount(phoneNumber) + - NUM_DIALABLE_DIGITS_TO_LOG; + for (int i = 0; i < phoneNumber.length(); i++) { + char c = phoneNumber.charAt(i); + boolean isDialable = PhoneNumberUtils.isDialable(c); + if (isDialable) { + numDigitsToObfuscate--; + } + sb.append(isDialable && numDigitsToObfuscate >= 0 ? "*" : c); + } + } + + /** + * Redact personally identifiable information for production users. + * If we are running in verbose mode, return the original string, + * and return "***" otherwise. + */ + public static String pii(Object pii) { + if (pii == null || VERBOSE) { + return String.valueOf(pii); + } + return "***"; + } + + /** + * Determines the number of dialable characters in a string. + * @param toCount The string to count dialable characters in. + * @return The count of dialable characters. + */ + private static int getDialableCount(String toCount) { + int numDialable = 0; + for (char c : toCount.toCharArray()) { + if (PhoneNumberUtils.isDialable(c)) { + numDialable++; + } + } + return numDialable; + } } -- cgit v1.2.3