【发布时间】:2019-04-01 08:26:39
【问题描述】:
我正在使用插件通过 Google 翻译来翻译 Wordpress 网站。我已经为网站设置了内容安全策略,这将导致 Wordpress 插件无法翻译网站,因为 CSP 会阻止它。当 CSP 设置在网站上时,有没有办法启用 Google 翻译功能?我在使用 Google 地图时遇到了同样的问题,我通过将 google.com 地址添加到 CSP 解决了这个问题,但是使用翻译我不知道如何继续。
CSP 设置:
<?php
header("Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://www.google.com; img-src 'self' data:; style-src 'self' 'unsafe-inline' https://www.google.com; font-src 'self' data:; object-src 'none'; frame-src https://www.google.com; ");
?>
(index):1 拒绝加载脚本“https://translate.google.com/translate_a/element.js?cb=GoogleLanguageTranslatorInit”,因为它违反了以下内容安全策略指令:“script-src 'self' 'unsafe-inline' https://www.google.com https://translate.googleapis.com/”。请注意,'script-src-elem' 没有显式设置,因此 'script-src' 用作备用。
【问题讨论】:
-
您的插件如何联系谷歌翻译? (您当前为脚本、CSS 和 iframe 添加了
google.com,但会阻止 ajax 请求,如果您想启用它,请添加connect-src)。为 CSP 块添加完整的错误日志可能很有用 -
您在控制台中看到的错误是什么?
-
@DaImTo
(index):1 Refused to load the script 'https://translate.google.com/translate_a/element.js?cb=GoogleLanguageTranslatorInit' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-inline' https://www.google.com https://translate.googleapis.com/". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
标签: php wordpress google-api header content-security-policy