-
Notifications
You must be signed in to change notification settings - Fork 15
/
CSPReport.cs
78 lines (72 loc) · 2.93 KB
/
CSPReport.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Runtime.Serialization.Json;
using Fiddler;
namespace FiddlerCSP
{
[DataContract]
public class CSPReport
{
public static CSPReport Parse(Stream postData)
{
try
{
return (CSPReport)new DataContractJsonSerializer(typeof(CSPReport)).ReadObject(postData);
}
catch (Exception exception)
{
postData.Seek(0, SeekOrigin.Begin);
string postDataAsString = new StreamReader(postData).ReadToEnd();
throw new Exception("Invalid CSP Report - JSON: " + postDataAsString + " exception: " + exception, exception);
}
}
public static CSPReport Parse(string postData)
{
return Parse(new MemoryStream(Encoding.UTF8.GetBytes(postData)));
}
public override string ToString()
{
return "{ csp-report: {\n" +
(cspReport.blockedUri != null ? "blocked-uri: \"" + cspReport.blockedUri + "\",\n" : "") +
(cspReport.documentUri != null ? "document-uri: \"" + cspReport.documentUri + "\",\n" : "") +
(cspReport.effectiveDirective != null ? "effective-directive: \"" + cspReport.effectiveDirective + "\",\n" : "") +
(cspReport.originalPolicy != null ? "original-policy: \"" + cspReport.originalPolicy + "\",\n" : "") +
(cspReport.referrer != null ? "referrer: \"" + cspReport.referrer + "\",\n" : "") +
(cspReport.statusCode != 0 ? "status-code: " + cspReport.statusCode + ",\n" : "") +
(cspReport.violatedDirective != null ? "violated-directive: \"" + cspReport.violatedDirective + "\",\n" : "") +
"} }";
}
[DataMember(Name = "csp-report")]
public CSPReportData cspReport;
[DataContract]
public class CSPReportData
{
[DataMember(Name = "blocked-uri")]
public string blockedUri;
[DataMember(Name = "document-uri")]
public string documentUri;
[DataMember(Name = "effective-directive")]
public string effectiveDirective;
[DataMember(Name = "original-policy")]
public string originalPolicy;
[DataMember(Name = "referrer")]
public string referrer;
[DataMember(Name = "status-code")]
public int statusCode;
[DataMember(Name = "violated-directive")]
public string violatedDirective;
[DataMember(Name = "source-file")]
public string sourceFile;
[DataMember(Name = "line-number")]
public int lineNumber;
[DataMember(Name = "column-number")]
public int columnNumber;
};
}
}