通过隐式 Intent 泄露敏感信息¶
ID: java/android/sensitive-communication
Kind: path-problem
Security severity: 8.2
Severity: warning
Precision: medium
Tags:
- security
- external/cwe/cwe-927
Query suites:
- java-security-extended.qls
- java-security-and-quality.qls
当隐式 Intent 与 startActivity
、startService
或 sendBroadcast
等方法一起使用时,设备上的其他应用程序可能会读取它。
这意味着这些 Intent 中的敏感数据可能会泄露。
建议¶
对于 sendBroadcast
方法,可以指定接收者权限,以便只有具有特定权限的应用程序才能接收 Intent;或者可以使用 LocalBroadcastManager
。否则,请确保包含敏感数据的 Intent 设置了显式接收者类。
示例¶
以下示例展示了广播 Intent 的两种方式。在“BAD”情况下,未指定“接收者权限”。在“GOOD”情况下,指定了“接收者权限”或“接收者应用程序”。
public void sendBroadcast1(Context context, String token, String refreshToken)
{
{
// BAD: broadcast sensitive information to all listeners
Intent intent = new Intent();
intent.setAction("com.example.custom_action");
intent.putExtra("token", token);
intent.putExtra("refreshToken", refreshToken);
context.sendBroadcast(intent);
}
{
// GOOD: broadcast sensitive information only to those with permission
Intent intent = new Intent();
intent.setAction("com.example.custom_action");
intent.putExtra("token", token);
intent.putExtra("refreshToken", refreshToken);
context.sendBroadcast(intent, "com.example.user_permission");
}
{
// GOOD: broadcast sensitive information to a specific application
Intent intent = new Intent();
intent.setAction("com.example.custom_action");
intent.setClassName("com.example2", "com.example2.UserInfoHandler");
intent.putExtra("token", token);
intent.putExtra("refreshToken", refreshToken);
context.sendBroadcast(intent);
}
}
参考¶
Android 开发者:发送和接收广播的安全注意事项和最佳做法
SonarSource:广播 intent 具有安全风险
Android 开发者基础:限制广播
卡内基梅隆大学:DRD03-J. 不要使用隐式 intent 广播敏感信息
Android 开发者:Android LiveData 概览
Oversecured:拦截 Android 隐式意图
通用弱点枚举:CWE-927。