如何配置manifest.json消除嵌入式Google Maps iframe的CSP告警
解决方案
你需要在manifest.json的content_security_policy字段中添加frame-src指令,允许浏览器加载谷歌地图的iframe资源,根据你使用的Manifest版本不同,配置方式如下:
如果你使用Manifest V2
修改配置为:
"permissions": [ "https://www.google.com/maps/*" ], "content_security_policy": "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://*.google.com https://*.gstatic.com; frame-src https://www.google.com/maps/ https://*.googleusercontent.com; connect-src 'self' https://*.google.com"
如果你使用Manifest V3
Manifest V3的CSP配置为对象格式,修改为:
"permissions": [ "https://www.google.com/maps/*" ], "content_security_policy": { "extension_pages": "script-src 'self'; object-src 'none'; frame-src https://www.google.com/maps/ https://*.googleusercontent.com; img-src 'self' data: https://*.google.com https://*.gstatic.com; style-src 'self' 'unsafe-inline'" }
配置说明
- 核心生效规则是
frame-src,它是CSP中专门用来限制当前页面可以嵌入哪些来源iframe的指令,添加谷歌地图相关的域名后就能消除警告 - 上述配置没有放开多余的权限,不会引入额外的安全风险
- 你原有组件代码不需要做任何修改,更新manifest后重新加载扩展即可生效
内容的提问来源于stack exchange,提问作者Lazy Loading
相关产品推荐
相关产品推荐

