如何修复不安全受保护的广播接收器?



关注我的广播接收器:

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// my code
}
}

它在AndroidManifest中注册:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:enabled="true"
android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

linter 在 MyBroadcastReceiver 的 onReceive 方法中报告以下错误:

此广播接收器为受保护的 广播操作字符串,只能由系统发送,不能 第三方应用程序。但是,接收方的 onReceive 方法 似乎没有调用 getAction 以确保收到的意图 操作字符串与预期值匹配,可能会使其 其他参与者可能发送欺骗意图而不执行任何操作 字符串或其他操作字符串,并导致意外行为。 声明意图过滤器的广播接收器 受保护的广播操作字符串必须检查收到的 Intent 的操作字符串与预期值匹配,否则为 恶意行为者可能欺骗意图。

问题 ID:不安全受保护的广播接收器

如何修复不安全受保护的广播接收器?

过滤操作,就像它说的那样:

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case Intent.ACTION_DATE_CHANGED:
//what you want to do
break;
case Intent.ACTION_BOOT_COMPLETED:
//what you want to do
break;
}
}
}

如果您不检查这一点,任何应用程序都可以通过指定类名来"调用"接收器上的BOOT_COMPLETED,因为这会绕过过滤器。

最新更新