From e0bff5eb1826b477655570e3157ed0d21467aa12 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C3=ADas=20Hern=C3=A1ndez?= <matiashe@google.com>
Date: Mon, 3 Jun 2024 12:34:59 +0200
Subject: [PATCH] Reject zen rule icon if its resource name is too long

Fixes: 341691431
Test: atest ZenModeHelperTest
Flag: NONE
Change-Id: I93ac73bc3981b49be60d967eb2366a0067f6c852
---
 .../server/notification/ZenModeHelper.java      | 10 +++++++++-
 .../server/notification/ZenModeHelperTest.java  | 17 +++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java
index ae29e1bba8d5..454bd20c59e2 100644
--- a/services/core/java/com/android/server/notification/ZenModeHelper.java
+++ b/services/core/java/com/android/server/notification/ZenModeHelper.java
@@ -149,6 +149,8 @@ public class ZenModeHelper {
 
     private static final String IMPLICIT_RULE_ID_PREFIX = "implicit_"; // + pkg_name
 
+    private static final int MAX_ICON_RESOURCE_NAME_LENGTH = 1000;
+
     /**
      * Send new activation AutomaticZenRule statuses to apps with a min target SDK version
      */
@@ -2645,7 +2647,13 @@ public class ZenModeHelper {
         requireNonNull(packageName);
         try {
             final Resources res = mPm.getResourcesForApplication(packageName);
-            return res.getResourceName(resId);
+            String resourceName = res.getResourceName(resId);
+            if (resourceName != null && resourceName.length() > MAX_ICON_RESOURCE_NAME_LENGTH) {
+                Slog.e(TAG, "Resource name for ID=" + resId + " in package " + packageName
+                        + " is too long (" + resourceName.length() + "); ignoring it");
+                return null;
+            }
+            return resourceName;
         } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
             Slog.e(TAG, "Resource name for ID=" + resId + " not found in package " + packageName
                     + ". Resource IDs may change when the application is upgraded, and the system"
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
index e4188b04cc43..72ace84b855b 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
@@ -6168,6 +6168,23 @@ public class ZenModeHelperTest extends UiServiceTestCase {
                 .isEqualTo(previousManualZenPolicy);
     }
 
+    @Test
+    @EnableFlags(Flags.FLAG_MODES_API)
+    public void addRule_iconIdWithResourceNameTooLong_ignoresIcon() {
+        int resourceId = 999;
+        String veryLongResourceName = "com.android.server.notification:drawable/"
+                + "omg_this_is_one_long_resource_name".repeat(100);
+        when(mResources.getResourceName(resourceId)).thenReturn(veryLongResourceName);
+        when(mResources.getIdentifier(veryLongResourceName, null, null)).thenReturn(resourceId);
+
+        String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(),
+                new AutomaticZenRule.Builder("Rule", CONDITION_ID).setIconResId(resourceId).build(),
+                UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID);
+        AutomaticZenRule storedRule = mZenModeHelper.getAutomaticZenRule(ruleId);
+
+        assertThat(storedRule.getIconResId()).isEqualTo(0);
+    }
+
     private static void addZenRule(ZenModeConfig config, String id, String ownerPkg, int zenMode,
             @Nullable ZenPolicy zenPolicy) {
         ZenRule rule = new ZenRule();
-- 
GitLab