MASTG-TEST-0381: References to Insecure PendingIntent Creation
Overview¶
This test checks for references to PendingIntent creation APIs to identify potentially insecure implementations. A PendingIntent wraps an Intent that will be executed later on behalf of the app's identity and permissions, making it critical to configure them securely.
PendingIntent objects can be obtained through these API methods:
PendingIntent.getActivity()PendingIntent.getActivities()PendingIntent.getService()PendingIntent.getForegroundService()PendingIntent.getBroadcast()
The primary security concerns when using PendingIntent are:
-
Mutability: A mutable
PendingIntentallows the receiving app to modify the base intent's unfilled fields (action, data, categories, extras, etc.). This can enable malicious apps to redirect the intent to unintended components or inject malicious data. UsePendingIntent.FLAG_IMMUTABLEto prevent modification of the base intent. Note that prior to Android 12 (API level 31),PendingIntentobjects were mutable by default, while since Android 12 (API level 31), the mutability of eachPendingIntentobject must be specified using eitherFLAG_MUTABLEor theFLAG_IMMUTABLEflag. -
Implicit Intents: Using an implicit base intent (without explicitly specifying the target component class) can allow malicious apps to intercept the
PendingIntentand redirect its execution. An intent should usesetClass(),setClassName(), orsetComponent()to specify the target component explicitly.
For more details on PendingIntent security, refer to Pending Intents and the Android security documentation on pending intents.
Steps¶
-
Run a static analysis tool ( Static Analysis on Android) to identify all usages of:
PendingIntent.getActivity()PendingIntent.getActivities()PendingIntent.getService()PendingIntent.getForegroundService()PendingIntent.getBroadcast()
-
For each identified usage, check:
- The flags parameter for the presence of
FLAG_IMMUTABLEorFLAG_MUTABLE. - The base intent construction to determine if it is explicit (specifies target component) or implicit.
- The flags parameter for the presence of
Observation¶
The output should contain a list of locations where PendingIntent creation APIs are used, along with the flags passed to the API (if identifiable) and whether the base intent appears to be explicit or implicit.
Evaluation¶
The test case fails if any of the following conditions are met:
- A
PendingIntentis created withoutFLAG_IMMUTABLEwhen the app'sminSdkVersionis below 31, unless there is a specific need for mutability that is properly justified and the app takes other precautions. - A
PendingIntentis created withFLAG_MUTABLEwithout a valid use case requiring mutability (e.g., inline reply actions). - The base intent is implicit (does not specify the target component using
setClass(),setClassName(), orsetComponent()), allowing potential hijacking by malicious apps.
Best Practices¶
MASTG-BEST-0063: Use Immutable PendingIntents with Explicit Intents