允许使用 Apache 的内联脚本 CSP
Allow inline scripts CSP with Apache
我必须启用 CSP 并允许 运行 内联脚本。
我一直在尝试各种方法,但我仍然在开发人员控制台中收到 CSP 警告。
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”)
我一直在尝试使用 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 的目的。
根据 Granty 的回答,我现在尝试使用 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
mod_unique_id
不适合生成 'nonce'
因为生成的 ID 中有 @ 字符。
您的 'nonce-X@KFhfNmoeAb3yfsstqrgQAAAMc'
令牌有 non allowed character @。
您可以使用 mod_cspnonce 代替。它根据 CSP 规范通过加密算法生成有效的 nonce 值。
PS:正如我所见 Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”)
- 使用 default-src
作为 script-src
的后备时要小心。
Firefox 有一个错误:'nonce-value'
在 default-src
指令中执行 not override 'unsafe-inline'
。
我必须启用 CSP 并允许 运行 内联脚本。 我一直在尝试各种方法,但我仍然在开发人员控制台中收到 CSP 警告。
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”)
我一直在尝试使用 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 的目的。
根据 Granty 的回答,我现在尝试使用 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
mod_unique_id
不适合生成 'nonce'
因为生成的 ID 中有 @ 字符。
您的 'nonce-X@KFhfNmoeAb3yfsstqrgQAAAMc'
令牌有 non allowed character @。
您可以使用 mod_cspnonce 代替。它根据 CSP 规范通过加密算法生成有效的 nonce 值。
PS:正如我所见 Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”)
- 使用 default-src
作为 script-src
的后备时要小心。
Firefox 有一个错误:'nonce-value'
在 default-src
指令中执行 not override 'unsafe-inline'
。