如何安全实现Android深度链接(规避android:exported="true"风险)
更安全的Android深度链接实现方案
针对你遇到的android:exported="true"带来的安全风险,这里有几个更安全的深度链接实现思路:
缩小Intent Filter的匹配范围
不要使用过于宽泛的Intent Filter配置,比如只指定scheme却不限制host或path,这会导致任意应用都能构造Intent唤起你的Activity。正确的做法是明确限定匹配规则:<activity android:name=".DeepLinkActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- 明确指定scheme、host和path前缀,仅匹配你的业务链接 --> <data android:scheme="https" android:host="your-domain.com" android:pathPrefix="/app/deeplink" /> </intent-filter> </activity>启用Android App Links(推荐)
Android 6.0及以上支持的App Links可以让系统验证链接的域名归属,只有你拥有的域名下的链接才能唤起你的App,彻底避免恶意应用伪造链接。配置步骤:- 在Manifest的Intent Filter中添加
android:autoVerify="true",并指定httpsscheme和你的域名:<intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="https" android:host="your-domain.com" /> </intent-filter> - 在你的域名服务器根目录或
.well-known目录下放置assetlinks.json文件,验证你的App与域名的关联关系,文件格式示例:[{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "com.your.app.package", "sha256_cert_fingerprints": ["你的App签名的SHA256指纹"] } }]
配置完成后,系统会自动验证,只有该域名的链接能唤起你的App,其他应用无法构造合法Intent。
- 在Manifest的Intent Filter中添加
对传入的Intent进行严格校验
即使Activity是exported="true",也要在处理链接前对Intent的数据做校验,不符合规则的直接拒绝处理:override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) intent.data?.let { uri -> // 验证域名是否为信任的域名 if (uri.host != "your-domain.com") { finish() return } // 验证path是否符合业务规则 if (!uri.path?.startsWith("/app/deeplink")!!) { finish() return } // 校验参数合法性,比如避免恶意参数注入 val param = uri.getQueryParameter("id") if (param.isNullOrEmpty() || !param.matches(Regex("[0-9]+"))) { finish() return } // 合法则继续处理业务逻辑 handleDeepLink(uri) } ?: finish() }避免通过深度链接执行敏感操作
绝对不要让深度链接直接触发删除用户数据、支付、修改账户信息等敏感操作。如果业务需要,必须增加二次确认步骤,或者先验证用户的登录状态/身份,确保操作是用户主动发起的。
内容的提问来源于stack exchange,提问作者KiritoCode
相关产品推荐
相关产品推荐

