【发布时间】:2013-08-13 14:00:50
【问题描述】:
Content Security Policy 的一个有用功能是能够检测违规并将其作为(违规报告)发送到特定的 URI。根据CSP 1.1 Sec. 3.2.4 Reporting的文档:
要发送违规报告,用户代理必须使用算法 相当于以下内容:
- 从 受保护资源的来源,未设置同步标志, 使用 HTTP 方法 POST,其 Content-Type 标头字段为 application/json 具有由报告正文组成的实体正文。如果 报告 URI 的来源与受保护的来源不同 资源,还必须设置块 cookie 标志。用户代理必须 获取此资源时不遵循重定向。 (注:用户 代理会忽略获取的资源。)
然后,在5.2 Sample Violation Report 部分提供了一个示例:
在以下示例中,用户代理呈现了 具有以下 CSP 的资源 http://example.org/page.html 政策:
default-src 'self'; report-uri http://example.org/csp-report.cgi
受保护的资源从 http://evil.example.com/image.png,违反政策。
{
"csp-report": {
"document-uri": "http://example.org/page.html",
"referrer": "http://evil.example.com/haxor.html",
"blocked-uri": "http://evil.example.com/image.png",
"violated-directive": "default-src 'self'",
"effective-directive": "img-src",
"original-policy": "default-src 'self'; report-uri http://example.org/csp-report.cgi"
}
}
示例:
test.php
<?php
header("X-Content-Security-Policy: default-src 'self'; report-uri http://127.0.0.1/csp-report.php");
?>
<img src="http://evil.example.com/image.png">
csp-report.php
<?php
$content = "
Keys: ".implode("\n", array_keys($_POST))."\n
\n--------------------------\n\n
Values: ".implode("\n", $_POST)."\n
";
file_put_contents('csp-report.txt', $content, FILE_APPEND | LOCK_EX);
?>
csp-report.txt
Keys:
--------------------------
Values:
如您所见,该文件中没有保存任何内容!但是,使用 Firebug,报告似乎发送到该文件:
注意:我希望得到分析性答案,其中会提到为什么正常帖子不起作用以及应该使用什么替代方法,为什么?此外,提供如何解码 JSON 也是一个优点。
【问题讨论】:
标签: php html post content-security-policy raw-post