From d40c037bccbae7537698968c1650a633710c5b6e Mon Sep 17 00:00:00 2001 From: Ben Reich Date: Wed, 9 Apr 2025 11:02:01 +1000 Subject: [SP 2025-09-01] Trim the application name to make it safe for presentation The application name is presented in the ConfirmFragment and as such we don't want to allow for any length. This follows a similar approach to PackageManager using the TextUtils.makeSafeForPresentation with a total available character length of 500. This removes the unused getCallingAppName from the DirectoryFragment as it was causing false positives from DirectoryFragment to avoid false positives when trying to find who calls the Shared function. On top of this, add some quotation marks around the app name to avoid the app name being a contination of the existing text in the dialog, e.g. 'This will let app name access current and future content storage in Alarms' will now be 'This will let "app name" access current and future content storage'. Bug: 397216537 Test: atest com.android.documentsui.picker.ApplicationNameTest Flag: EXEMPT bug fix Change-Id: Iad0d03de09b1e4ad953bd6bd46a619cfcc56d384 (cherry picked from commit c8ef2db3bb4645704384226976e59583b9e8d3d3) --- res/values/strings.xml | 2 +- src/com/android/documentsui/base/Shared.java | 12 ++- .../documentsui/dirlist/DirectoryFragment.java | 5 -- .../documentsui/dirlist/DocumentsAdapter.java | 1 - .../documentsui/dirlist/TestEnvironment.java | 5 -- .../documentsui/picker/ApplicationNameTest.kt | 91 ++++++++++++++++++++++ 6 files changed, 102 insertions(+), 14 deletions(-) create mode 100644 tests/unit/com/android/documentsui/picker/ApplicationNameTest.kt diff --git a/res/values/strings.xml b/res/values/strings.xml index c3f11bab4..4cd85344b 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -575,7 +575,7 @@ Allow %1$s to access files in %2$s? - This will let %1$s access current and future content stored in %2$s. + This will let "%1$s" access current and future content stored in %2$s. Can\u2019t use this folder diff --git a/src/com/android/documentsui/base/Shared.java b/src/com/android/documentsui/base/Shared.java index ac089999f..bb8a39393 100644 --- a/src/com/android/documentsui/base/Shared.java +++ b/src/com/android/documentsui/base/Shared.java @@ -16,6 +16,9 @@ package com.android.documentsui.base; +import static android.text.TextUtils.SAFE_STRING_FLAG_SINGLE_LINE; +import static android.text.TextUtils.SAFE_STRING_FLAG_TRIM; + import static com.android.documentsui.base.SharedMinimal.TAG; import static com.android.documentsui.ChangeIds.RESTRICT_STORAGE_ACCESS_FRAMEWORK; @@ -265,7 +268,7 @@ public final class Shared { * @return the calling app name or general anonymous name if not found */ @NonNull - public static String getCallingAppName(Activity activity) { + public static CharSequence getCallingAppName(Activity activity) { final String anonymous = activity.getString(R.string.anonymous_application); final String packageName = getCallingPackageName(activity); if (TextUtils.isEmpty(packageName)) { @@ -281,7 +284,12 @@ public final class Shared { } CharSequence result = pm.getApplicationLabel(ai); - return TextUtils.isEmpty(result) ? anonymous : result.toString(); + if (TextUtils.isEmpty(result)) { + return anonymous; + } + + return TextUtils.makeSafeForPresentation( + result.toString(), 500, 0, SAFE_STRING_FLAG_TRIM | SAFE_STRING_FLAG_SINGLE_LINE); } /** diff --git a/src/com/android/documentsui/dirlist/DirectoryFragment.java b/src/com/android/documentsui/dirlist/DirectoryFragment.java index 2911d04e9..ae728f616 100644 --- a/src/com/android/documentsui/dirlist/DirectoryFragment.java +++ b/src/com/android/documentsui/dirlist/DirectoryFragment.java @@ -1634,10 +1634,5 @@ public class DirectoryFragment extends Fragment implements SwipeRefreshLayout.On public ActionHandler getActionHandler() { return mActions; } - - @Override - public String getCallingAppName() { - return Shared.getCallingAppName(mActivity); - } } } diff --git a/src/com/android/documentsui/dirlist/DocumentsAdapter.java b/src/com/android/documentsui/dirlist/DocumentsAdapter.java index 41ce73c8c..b32c15335 100644 --- a/src/com/android/documentsui/dirlist/DocumentsAdapter.java +++ b/src/com/android/documentsui/dirlist/DocumentsAdapter.java @@ -90,7 +90,6 @@ public abstract class DocumentsAdapter extends RecyclerView.Adapter + + @Before + fun setUp() { + MockitoAnnotations.openMocks(this) + whenever(mockActivity.resources).thenReturn(resources) + whenever(mockActivity.packageManager).thenReturn(pm) + whenever(resources.getString(R.string.anonymous_application)).thenReturn(ANONYMOUS_PACKAGE) + whenever(mockActivity.callingPackage).thenReturn(PACKAGE_NAME) + } + + @Test + fun testNameIsSanitized() { + val info = ApplicationInfo() + whenever(pm.getApplicationInfo(PACKAGE_NAME, 0)).thenReturn(info) + + whenever(pm.getApplicationLabel(eq(info))).thenReturn(testData.first) + assertEquals(Shared.getCallingAppName(mockActivity), testData.second) + } +} -- cgit v1.2.3-59-g8ed1b