如何使用 Content-Security-Policy 解决 Chrome 的 XSS 审计员误报?

How to work around Chrome's XSS auditor false positive with Content-Security-Policy?

在 Google Chrome XSS Auditor 中存在一个已知的 false positive,涉及带有一些 js-ish 文本的文本区域,例如 action="":

The XSS Auditor refused to execute a script in 'https://www.dokuwiki.org/sandbox:chrome_xss_auditor?do=edit' because its source code was found within the request. The auditor was enabled as the server sent neither an 'X-XSS-Protection' nor 'Content-Security-Policy' header.

错误消息表明它可以用 X-XSS-Protection-header 或 Content-Security-Policy-header 解决。实际上 a related question on SO 建议使用 header("X-XSS-Protection: 0");.

停用 XSS 保护

然而,我对简单地停用 XSS 保护并尝试使用 Content-Security-Policy-header 来处理这个问题感到有些不安:

header('Content-Security-Policy: script-src * \'unsafe-inline\' \'unsafe-eval\'');

但不幸的是,这并没有影响问题。

我的问题是:为什么会失败,我如何使用 Content-Security-Policy-header 来处理问题而不完全停用 XSS-Protection?

PS:供参考,另见 https://github.com/splitbrain/dokuwiki/issues/1182

Why did it fail and how can I use a Content-Security-Policy-header to handle the issue without deactivating XSS-Protection completely?

它没有用,因为你允许 unsafe-inline

明确允许不安全内联比通过 X-XSS-Protection 禁用 XSS 保护更糟糕,因为前者可能会发生存储 XSS 攻击(与锁定的 CSP 相比),而后者对它们没有影响。

解决方法 - 根据链接 post.

,通过禁止 unsafe 指令或完全禁用 XSS 保护来实现安全 CSP

前者是更理想的选择,但是您可能需要重新设计您的页面或网站,具体取决于您是否使用了内联 JavaScript。如果您将所有 JS 移动到外部文件,这将使这成为可能,并且您将拥有一个更安全的系统。