广播接收器对意图的验证不当¶
ID: java/improper-intent-verification
Kind: problem
Security severity: 8.2
Severity: warning
Precision: high
Tags:
- security
- external/cwe/cwe-925
Query suites:
- java-code-scanning.qls
- java-security-extended.qls
- java-security-and-quality.qls
当 Android 应用程序使用 BroadcastReceiver
接收意图时,它还可以接收直接发送给它的显式意图,无论其过滤器如何。某些意图操作只能由操作系统发送,而不是第三方应用程序发送。但是,已注册为接收系统意图的 BroadcastReceiver
仍然能够接收来自第三方应用程序的意图,因此它应该检查接收的意图是否具有预期的操作。否则,第三方应用程序可以以这种方式冒充系统来导致意外行为,例如拒绝服务。
示例¶
在以下代码中,ShutdownReceiver
在接收意图后启动关闭程序,而没有检查接收的操作是否确实是 ACTION_SHUTDOWN
。这允许第三方应用程序向此接收器发送显式意图以导致拒绝服务。
public class ShutdownReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
mainActivity.saveLocalData();
mainActivity.stopActivity();
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test">
<application>
<receiver android:name=".BootReceiverXml">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
建议¶
在 BroadcastReceiver
的 onReceive
方法中,应检查接收的 Intent 的操作。以下代码演示了这一点。
public class ShutdownReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if (!intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
return;
}
mainActivity.saveLocalData();
mainActivity.stopActivity();
}
}
参考¶
常见弱点枚举:CWE-925。