【问题标题】:CSP block google translateCSP 阻止谷歌翻译
【发布时间】: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


【解决方案1】:

您需要将要允许的每个域添加到您的 CSP 标头中:

  • https://translate.google.comhttps://www.google.com 不是一回事
  • https://translate.google.comhttps://translate.googleapis.com/ 不是一回事

尝试:

<?php 
    header("Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://www.google.com https://translate.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; ");
?>

注意一旦脚本被允许,CSP 可能会阻止来自同一域的其他类型的资源(例如 CSS、iframe),如果是这种情况,只需将此域添加到这些类型也一样。

【讨论】:

  • 使用“unsafe-inline”胜过首先使用 CSP 的目的
  • 遗憾的是,如果没有 unsafe-inline,谷歌翻译就无法工作,因为他们在页面中添加了内联脚本,谷歌做得很好!
猜你喜欢
  • 2015-11-10
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 2013-06-12
  • 2011-12-26
  • 2015-05-19
相关资源
最近更新 更多