CSP块谷歌翻译

时间:2019-04-01 08:26:40

标签: php wordpress google-api header content-security-policy

我正在使用插件通过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; ");
?>
  

(索引):1拒绝加载脚本“ https://translate.google.com/translate_a/element.js?cb=GoogleLanguageTranslatorInit”,因为它违反了以下内容安全策略指令:“ script-src'self''unsafe-inline'https://www.google.com {{3 }}”。请注意,未明确设置“ script-src-elem”,因此将“ script-src”用作备用。

1 个答案:

答案 0 :(得分: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),如果是这种情况,只需将该域添加到这些域中也可以输入类型。