访问发布到 ASP.Net 的内容安全策略违规报告

fra*_*nzo 3 asp.net content-security-policy

例如,如果您有一个类似 CSP default-src 'self'; report-uri /CspViolationReport 并且/CspViolationReport由 ASP.Net 处理,您如何访问发布的 CSP 违规报告?

我们希望找到一些 JSON 发布,例如http://www.w3.org/TR/CSP11/#example-violation-report

当您检查时Request.Form,没有键,并且在 中没有任何证据Request.ServerVariables["ALL_RAW"],而是Request.ServerVariables["HTTP_METHOD"]“POST”。

用 Fiddler 截取 POST,可以看到 JSON 肯定是贴出来的,但是 .Net 好像不让你看到。

Tom*_*cki 7

问题可能与请求的内容类型有关:application/csp-report。它不是:应用程序/json。我刚刚添加到 WebApiConfig:

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(
new System.Net.Http.Headers.MediaTypeHeaderValue("application/csp-report"));
Run Code Online (Sandbox Code Playgroud)

当然,您还需要其他答案的类:CspReportContainer、CspReport


fra*_*nzo 6

这是一种方法,灵感来自http://muaz-khan.blogspot.co.nz/2012/06/exploring-csp-content-security-policy.html,谢谢!

void ProcessCspValidationReport() {
    Request.InputStream.Position = 0;
    using (StreamReader inputStream = new StreamReader(Request.InputStream))
    {
        string s = inputStream.ReadToEnd();
        if (!string.IsNullOrWhiteSpace(s))
        {
            CspPost cspPost = JsonConvert.DeserializeObject<CspPost>(s);
            //now you can access properties of cspPost.CspReport
        }
    }
}

class CspPost
{
    [JsonProperty("csp-report")]
    public CspReport CspReport { get; set; }
}

class CspReport
{
    [JsonProperty("document-uri")]
    public string DocumentUri { get; set; }

    [JsonProperty("referrer")]
    public string Referrer { get; set; }

    [JsonProperty("effective-directive")]
    public string EffectiveDirective { get; set; }

    [JsonProperty("violated-directive")]
    public string ViolatedDirective { get; set; }

    [JsonProperty("original-policy")]
    public string OriginalPolicy { get; set; }

    [JsonProperty("blocked-uri")]
    public string BlockedUri { get; set; }

    [JsonProperty("source-file")]
    public string SourceFile { get; set; }

    [JsonProperty("line-number")]
    public int LineNumber { get; set; }

    [JsonProperty("column-number")]
    public int ColumnNumber { get; set; }

    [JsonProperty("status-code")]
    public string StatusCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)