Skip to content

Commit e3f9bda

Browse files
authored
Merge pull request #40 from viraptor/metadata
Support injecting metadata as new field
2 parents cbb823f + 5d3c610 commit e3f9bda

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ $ CGO_ENABLED=0 go build csp_collector.go
4444

4545
See the sample.filterlist.txt file as an example of the filter list in a file
4646

47+
### Request metadata
48+
49+
Additional information can be attached to each report by adding a `metadata`
50+
url parameter to each report. That value will be copied verbatim into the
51+
logged report.
52+
53+
For example a report sent to `https://collector.example.com/?metadata=foobar`
54+
will include field `metadata` with value `foobar`.
55+
4756
### Output formats
4857

4958
The output format can be controlled by passing `--output-format <type>`

csp_collector.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ func handleViolationReport(w http.ResponseWriter, r *http.Request) {
191191
return
192192
}
193193

194+
metadatas, gotMetadata := r.URL.Query()["metadata"]
195+
var metadata string
196+
if gotMetadata {
197+
metadata = metadatas[0]
198+
}
199+
194200
log.WithFields(log.Fields{
195201
"document_uri": report.Body.DocumentURI,
196202
"referrer": report.Body.Referrer,
@@ -201,6 +207,7 @@ func handleViolationReport(w http.ResponseWriter, r *http.Request) {
201207
"disposition": report.Body.Disposition,
202208
"script_sample": report.Body.ScriptSample,
203209
"status_code": report.Body.StatusCode,
210+
"metadata": metadata,
204211
}).Info()
205212
}
206213

csp_collector_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,50 @@ func TestHandlerForAllowingHealthcheck(t *testing.T) {
5555
}
5656
}
5757

58+
func TestHandlerWithMetadata(t *testing.T) {
59+
csp := CSPReport{
60+
CSPReportBody{
61+
DocumentURI: "http://example.com",
62+
BlockedURI: "http://example.com",
63+
},
64+
}
65+
66+
payload, _ := json.Marshal(csp)
67+
68+
for _, repeats := range []int{1, 2} {
69+
var logBuffer bytes.Buffer
70+
log.SetOutput(&logBuffer)
71+
72+
url := "/?"
73+
for i := 0; i < repeats; i++ {
74+
url += fmt.Sprintf("metadata=value%d&", i)
75+
}
76+
77+
request, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
78+
if err != nil {
79+
t.Fatalf("failed to create request: %v", err)
80+
}
81+
recorder := httptest.NewRecorder()
82+
83+
handleViolationReport(recorder, request)
84+
85+
response := recorder.Result()
86+
defer response.Body.Close()
87+
88+
if response.StatusCode != http.StatusOK {
89+
t.Errorf("expected HTTP status %v; got %v", http.StatusOK, response.StatusCode)
90+
}
91+
92+
log := logBuffer.String()
93+
if !strings.Contains(log, "metadata=value0") {
94+
t.Fatalf("Logged result should contain metadata value0 in '%s'", log)
95+
}
96+
if strings.Contains(log, "metadata=value1") {
97+
t.Fatalf("Logged result shouldn't contain metadata value1 in '%s'", log)
98+
}
99+
}
100+
}
101+
58102
func TestValidateViolationWithInvalidBlockedURIs(t *testing.T) {
59103
invalidBlockedURIs := []string{
60104
"resource://",

0 commit comments

Comments
 (0)