动态注册broadcast的安全考虑

一、android service通知activity更新
方式有
1. service 通过广播的形式发送broadcast,向这个activity的内部类发广播的消息来更新界面
2. service直接向activity发intent,把activity的launchMode设置为singleInstance

二、安全性
这边关注第1种方式的广播和接收intent安全,如果不对广播的发送和接收进行判断,会有很大的安全隐患
在我的场景中,是动态注册broadcast,考虑安全如下:

A、针对sendBroadcast
1.通常采用的安全方式有setPackage设置包名
intent.setAction("com.example.tianqitong.recv");
intent.setPackage("com.example.tianqitong");
sendBroadcast(intent);

2.可以通过使用LocalBroadcastManager,确保了应用程序外部的任何组件都收不到你广播的Intent

3.设置权限
sendBroadcast(intent,"broadcast.permission");

还要加上android:protectionLevel权限级别

<uses-permission android:name="broadcast.permission" /> //声明使用权限
<permission android:name="broadcast.permission" android:protectionLevel="signature" /> //自定义权限

B、针对Receive
1.可以通过使用LocalBroadcastManager,使其他应用程序也不能向你的接收器发送广播

2.对于动态注册的广播可以通过类似registerReceiver(BroadcastReceiver, IntentFilter, String, android.os.Handler)的接口指定发送者必须具备的permission
其中string为指定的permission,必须加android:protectionLevel
譬如:
<permission android:name="broadcast.permission" android:protectionLevel="signature" />


参考:
http://blog.csdn.net/t12x3456/article/details/9256609
http://blog.csdn.net/hotdogzu/article/details/7940820
http://blog.csdn.net/abc13939746593/article/details/8186916
http://my.eoe.cn/weiblog/archive/4590.html
http://www.sectop.com/?p=187
https://developer.android.com/training/articles/security-tips.html
https://developer.android.com/reference/android/content/BroadcastReceiver.html#Security
http://book.51cto.com/art/201304/389200.htm

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章