【发布时间】:2021-04-01 16:06:08
【问题描述】:
我必须启用 CSP 并允许运行内联脚本。 我一直在尝试各种事情,但我仍然在开发者控制台中收到 CSP 警告。
内容安全策略:页面的设置阻止了内联资源的加载(“default-src”)
内容安全政策:页面的设置阻止了内联资源的加载(“script-src”)
我一直在尝试使用 nonce 实现 CSP,并且在我的 apache 设置中有以下内容。
LoadModule unique_id_module modules/mod_unique_id.so //generates unique nonce for each http request
Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'nonce-%{UNIQUE_ID}e' 'unsafe-eval';"
上述 apache 设置生成一个唯一的 id,然后我将其传递给我的内联 JS <script nonce="<?= $_SERVER['UNIQUE_ID'] ?>">
但我仍然在控制台中遇到错误。
Content Security Policy: Couldn’t parse invalid host 'nonce-X@KFhfNmoeAb3yfsstqrgQAAAMc'
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”)
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“script-src”)
请注意,我不打算使用 unsafe-inline,因为这不能解决 CSP 的目的。
根据格兰蒂的回答,我现在尝试使用 csp_nonce 模块。 并在我的 apache 配置中有以下内容
LoadModule headers_module modules/mod_headers.so
LoadModule cspnonce_module modules/mod_cspnonce.so
Header set Content-Security-Policy "script-src 'self' 'nonce-%{CSP_NONCE}e' 'unsafe-eval';"
内联脚本标签现在包含 CSP_NONCE 变量。
但是我仍然在控制台中遇到一些错误。
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“script-src”).
感谢您的帮助,TIA
【问题讨论】:
标签: php apache content-security-policy