以下内容摘自 阿里巴巴Android开发手册
我们的目标是:
- 防患未然,提升质量意识,降低故障率和维护成本;
- 标准统一,提升协作效率;
- 追求卓越的工匠精神,打磨精品代码。
- 【强制】必须遵守,违反本约定或将会引起严重的后果;
- 【推荐】尽量遵守,长期遵守有助于系统稳定性和合作效率的提升;
- 【参考】充分理解,技术意识的引导,是个人学习、团队沟通、项目合作的方向。
阿里Android开发规范:资源文件命名与使用规范
阿里Android开发规范:四大基本组件
阿里Android开发规范:UI 与布局
阿里Android开发规范:进程、线程与消息通信
阿里Android开发规范:文件与数据库
阿里Android开发规范:Bitmap、Drawable 与动画
阿里Android开发规范:安全与其他
1、【强制】使用 PendingIntent 时,禁止使用空 intent,同时禁止使用隐式 Intent
说明:
- 使用 PendingIntent 时,使用了空 Intent,会导致恶意用户劫持修改 Intent 的内容。禁止使用一个空 Intent 去构造 PendingIntent,构造 PendingIntent 的 Intent一定要设置 ComponentName 或者 action。
- PendingIntent 可以让其他 APP 中的代码像是运行自己 APP 中。PendingIntent的intent接收方在使用该intent时与发送方有相同的权限。在使用PendingIntent时,PendingIntent 中包装的 intent 如果是隐式的 Intent,容易遭到劫持,导致信息泄露。
正例:
Intent intent = new Intent(this, SomeActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
反例 1:
Bundle addAccountOptions = new Bundle();
mPendingIntent = PendingTntent.getBroadcast(this, 0, new Intent, 0);
addAccountOptions.putParcelable(KEY_CALLER_IDENTITY, mPendingIntent);
addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS,
Utils.hasMultipleUsers(this));
AccountManager.get(this).addAccount(accountType,
null,
null,
addAccountOptions,
null,
mCallback,
null);
反例 2:
mPendingIntent 是通过 new Intent()构造原始 Intent 的,所以为“双无”Intent,这个PendingIntent最终被通过AccountManager.addAccount 方法传递给了恶意APP接口。
Intent intent = new Intent("com.test.test.pushservice.action.METHOD");
intent.addFlags(32);
intent.putExtra("app",msg);
PendingIntent.getBroadcast(this, 0, intent, 0));
如上代码PendingIntent.getBroadcast,PendingItent中包含的Intent为隐式intent,因此当 PendingIntent 触发执行时,发送的 intent 很可能被嗅探或者劫持,导致 intent 内容泄漏。
扩展参考:
1) https://developer.android.com/reference/android/app/PendingIntent.html
2) https://wiki.sei.cmu.edu/confluence/display/android/DRD21-J.+Always+pass+explicit+intents+to+a+PendingIntent
3)