Skip to content

Commit c234c1b

Browse files
Matías HernándezAndroid Build Coastguard Worker
authored andcommitted
Forbid granting access to NLSes with too-long component names
This makes the limitation, which was previously only checked on the Settings UI, enforced everywhere. Fixes: 260570119 Fixes: 286043036 Test: atest + manually (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:6fcdbd0c6efc67b014b8e1b43c5ec233f912ee8b) Merged-In: I4c25d80978cb37a8fa1531f5045259d25ac64692 Change-Id: I4c25d80978cb37a8fa1531f5045259d25ac64692
1 parent e6171cf commit c234c1b

5 files changed

Lines changed: 47 additions & 3 deletions

File tree

core/java/android/app/NotificationManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,12 @@ public class NotificationManager {
571571
*/
572572
public static final int BUBBLE_PREFERENCE_SELECTED = 2;
573573

574+
/**
575+
* Maximum length of the component name of a registered NotificationListenerService.
576+
* @hide
577+
*/
578+
public static int MAX_SERVICE_COMPONENT_NAME_LENGTH = 500;
579+
574580
@UnsupportedAppUsage
575581
private static INotificationManager sService;
576582

packages/SettingsLib/src/com/android/settingslib/RestrictedSwitchPreference.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ public String getPackageName() {
243243
return mHelper != null ? mHelper.packageName : null;
244244
}
245245

246-
public void updateState(@NonNull String packageName, int uid, boolean isEnabled) {
246+
/** Updates enabled state based on associated package. */
247+
public void updateState(
248+
@NonNull String packageName, int uid, boolean isEnableAllowed, boolean isEnabled) {
247249
mHelper.updatePackageDetails(packageName, uid);
248250
if (mAppOpsManager == null) {
249251
mAppOpsManager = getContext().getSystemService(AppOpsManager.class);
@@ -254,7 +256,9 @@ public void updateState(@NonNull String packageName, int uid, boolean isEnabled)
254256
final boolean ecmEnabled = getContext().getResources().getBoolean(
255257
com.android.internal.R.bool.config_enhancedConfirmationModeEnabled);
256258
final boolean appOpsAllowed = !ecmEnabled || mode == AppOpsManager.MODE_ALLOWED;
257-
if (isEnabled) {
259+
if (!isEnableAllowed && !isEnabled) {
260+
setEnabled(false);
261+
} else if (isEnabled) {
258262
setEnabled(true);
259263
} else if (appOpsAllowed && isDisabledByAppOps()) {
260264
setEnabled(true);

services/core/java/com/android/server/notification/NotificationManagerService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5484,6 +5484,11 @@ public void setNotificationListenerAccessGrantedForUser(ComponentName listener,
54845484
boolean granted, boolean userSet) {
54855485
Objects.requireNonNull(listener);
54865486
checkNotificationListenerAccess();
5487+
if (granted && listener.flattenToString().length()
5488+
> NotificationManager.MAX_SERVICE_COMPONENT_NAME_LENGTH) {
5489+
throw new IllegalArgumentException(
5490+
"Component name too long: " + listener.flattenToString());
5491+
}
54875492
if (!userSet && isNotificationListenerAccessUserSet(listener)) {
54885493
// Don't override user's choice
54895494
return;

services/core/java/com/android/server/vr/VrManagerService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,11 @@ private void grantNotificationListenerAccess(String pkg, int userId) {
10491049

10501050
for (ComponentName c : possibleServices) {
10511051
if (Objects.equals(c.getPackageName(), pkg)) {
1052-
nm.setNotificationListenerAccessGrantedForUser(c, userId, true);
1052+
try {
1053+
nm.setNotificationListenerAccessGrantedForUser(c, userId, true);
1054+
} catch (Exception e) {
1055+
Slog.w(TAG, "Could not grant NLS access to package " + pkg, e);
1056+
}
10531057
}
10541058
}
10551059
}

services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import static junit.framework.Assert.assertTrue;
8585
import static junit.framework.Assert.fail;
8686

87+
import static org.junit.Assert.assertThrows;
8788
import static org.mockito.ArgumentMatchers.isNull;
8889
import static org.mockito.Matchers.anyBoolean;
8990
import static org.mockito.Matchers.anyLong;
@@ -3847,6 +3848,30 @@ public void testSetListenerAccessForUser() throws Exception {
38473848
any(), anyInt(), anyBoolean(), anyBoolean(), anyBoolean());
38483849
}
38493850

3851+
@Test
3852+
public void testSetListenerAccessForUser_grantWithNameTooLong_throws() {
3853+
UserHandle user = UserHandle.of(mContext.getUserId() + 10);
3854+
ComponentName c = new ComponentName("com.example.package",
3855+
com.google.common.base.Strings.repeat("Blah", 150));
3856+
3857+
assertThrows(IllegalArgumentException.class,
3858+
() -> mBinderService.setNotificationListenerAccessGrantedForUser(
3859+
c, user.getIdentifier(), /* enabled= */ true, true));
3860+
}
3861+
3862+
@Test
3863+
public void testSetListenerAccessForUser_revokeWithNameTooLong_okay() throws Exception {
3864+
UserHandle user = UserHandle.of(mContext.getUserId() + 10);
3865+
ComponentName c = new ComponentName("com.example.package",
3866+
com.google.common.base.Strings.repeat("Blah", 150));
3867+
3868+
mBinderService.setNotificationListenerAccessGrantedForUser(
3869+
c, user.getIdentifier(), /* enabled= */ false, true);
3870+
3871+
verify(mListeners).setPackageOrComponentEnabled(
3872+
c.flattenToString(), user.getIdentifier(), true, /* enabled= */ false, true);
3873+
}
3874+
38503875
@Test
38513876
public void testSetAssistantAccessForUser() throws Exception {
38523877
UserInfo ui = new UserInfo();

0 commit comments

Comments
 (0)