简体   繁体   English

Android使广播接收器安全

[英]Android make broadcast receiver secure

I'm trying to implement a "secure" broadcast receiver which only receives broadcasts from a specific app. 我正在尝试实现一个“安全”广播接收器,它只接收来自特定应用的广播。 This is because I want to make a plugin to my app which triggers actions via broadcasts. 这是因为我想为我的应用程序制作一个插件,通过广播触发操作。 As this actions are (partly) sensible it would be nice to check if the sender is really my application. 由于这个动作(部分)是合理的,因此检查发送者是否真的是我的应用程序会很好。 As far as I have seen it's impossible to check the sender package?? 据我所知,检查发件人包是不可能的? Would it be secure to define a custom permission for that? 为此定义自定义权限是否安全? If yes, how can I do this? 如果是,我该怎么做? What other possibilities are there to achieve this? 有什么其他可能性来实现这一目标?

Thanks in advance! 提前致谢!

Would it be secure to define a custom permission for that? 为此定义自定义权限是否安全?

If both the app and the plugin are written by you, a custom permission with android:protectionLevel="signature" would seem to be the ideal solution for your problem. 如果应用程序和插件都是由您编写的,则android:protectionLevel="signature"的自定义权限似乎是您问题的理想解决方案。 No apps will be able to send broadcasts to your receiver without holding that permission, which can only be held by apps signed by the same signing key. 没有应用程序将无法在未持有该权限的情况下向您的接收方发送广播,这只能由同一签名密钥签名的应用程序保留。 As a bonus, users do not have to agree to the permission at install time. 作为奖励,用户无需在安装时同意该许可。

Pro tip: define the <permission> element in both the app and the plugin, so the install order of those two does not matter. 专业提示:在app和插件中定义<permission>元素,因此这两者的安装顺序无关紧要。

Note that custom permissions have a security flaw prior to Android 5.0, and that on Android 5.0+ no two apps can define the same permission unless they are signed by the same signing key . 请注意, 自定义权限在Android 5.0之前存在安全漏洞 ,而在Android 5.0 +上,没有两个应用可以定义相同的权限,除非它们由相同的签名密钥签名

Define a custom permission in your manifest: 在清单中定义自定义权限:

<permission android:name="com.example.myapp.permission.NAME"
    android:protectionLevel="normal" />

On your receiver, add permission attribute: 在您的接收器上,添加权限属性:

<receiver android:name="MyReceiver" 
    android:permission="com.example.myapp.permission.NAME" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM