Skip to content

Commit

Permalink
Merge pull request #40 from viraptor/metadata
Browse files Browse the repository at this point in the history
Support injecting metadata as new field
  • Loading branch information
jacobbednarz authored May 3, 2020
2 parents cbb823f + 5d3c610 commit e3f9bda
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ $ CGO_ENABLED=0 go build csp_collector.go

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

### Request metadata

Additional information can be attached to each report by adding a `metadata`
url parameter to each report. That value will be copied verbatim into the
logged report.

For example a report sent to `https://collector.example.com/?metadata=foobar`
will include field `metadata` with value `foobar`.

### Output formats

The output format can be controlled by passing `--output-format <type>`
Expand Down
7 changes: 7 additions & 0 deletions csp_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ func handleViolationReport(w http.ResponseWriter, r *http.Request) {
return
}

metadatas, gotMetadata := r.URL.Query()["metadata"]
var metadata string
if gotMetadata {
metadata = metadatas[0]
}

log.WithFields(log.Fields{
"document_uri": report.Body.DocumentURI,
"referrer": report.Body.Referrer,
Expand All @@ -201,6 +207,7 @@ func handleViolationReport(w http.ResponseWriter, r *http.Request) {
"disposition": report.Body.Disposition,
"script_sample": report.Body.ScriptSample,
"status_code": report.Body.StatusCode,
"metadata": metadata,
}).Info()
}

Expand Down
44 changes: 44 additions & 0 deletions csp_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,50 @@ func TestHandlerForAllowingHealthcheck(t *testing.T) {
}
}

func TestHandlerWithMetadata(t *testing.T) {
csp := CSPReport{
CSPReportBody{
DocumentURI: "http://example.com",
BlockedURI: "http://example.com",
},
}

payload, _ := json.Marshal(csp)

for _, repeats := range []int{1, 2} {
var logBuffer bytes.Buffer
log.SetOutput(&logBuffer)

url := "/?"
for i := 0; i < repeats; i++ {
url += fmt.Sprintf("metadata=value%d&", i)
}

request, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
recorder := httptest.NewRecorder()

handleViolationReport(recorder, request)

response := recorder.Result()
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
t.Errorf("expected HTTP status %v; got %v", http.StatusOK, response.StatusCode)
}

log := logBuffer.String()
if !strings.Contains(log, "metadata=value0") {
t.Fatalf("Logged result should contain metadata value0 in '%s'", log)
}
if strings.Contains(log, "metadata=value1") {
t.Fatalf("Logged result shouldn't contain metadata value1 in '%s'", log)
}
}
}

func TestValidateViolationWithInvalidBlockedURIs(t *testing.T) {
invalidBlockedURIs := []string{
"resource://",
Expand Down

0 comments on commit e3f9bda

Please sign in to comment.