Skip to content

Commit

Permalink
Merge pull request #39 from viraptor/bogus-document-uri
Browse files Browse the repository at this point in the history
Exclude non-http documents
  • Loading branch information
jacobbednarz authored May 1, 2020
2 parents da4f36e + c0f00ba commit b907c8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions csp_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,9 @@ func validateViolation(r CSPReport) error {
}
}

if !strings.HasPrefix(r.Body.DocumentURI, "http") {
return fmt.Errorf("document URI ('%s') is invalid", r.Body.DocumentURI)
}

return nil
}
18 changes: 17 additions & 1 deletion csp_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestValidateViolationWithInvalidBlockedURIs(t *testing.T) {
t.Run(testName, func(t *testing.T) {
var rawReport = []byte(fmt.Sprintf(`{
"csp-report": {
"document-uri": "https://example.com",
"blocked-uri": "%s"
}
}`, blockedURI))
Expand All @@ -113,6 +114,7 @@ func TestValidateViolationWithInvalidBlockedURIs(t *testing.T) {
func TestValidateViolationWithValidBlockedURIs(t *testing.T) {
var rawReport = []byte(`{
"csp-report": {
"document-uri": "https://example.com",
"blocked-uri": "https://google.com/example.css"
}
}`)
Expand All @@ -129,6 +131,19 @@ func TestValidateViolationWithValidBlockedURIs(t *testing.T) {
}
}

func TestValidateNonHttpDocumentURI(t *testing.T) {
log.SetOutput(ioutil.Discard)

report := CSPReport{Body: CSPReportBody{
BlockedURI: "http://example.com/",
DocumentURI: "about",
}}
validateErr := validateViolation(report)
if validateErr.Error() != fmt.Sprintf("document URI ('about') is invalid") {
t.Errorf("expected error to include correct message string but it didn't")
}
}

func TestHandleViolationReportMultipleTypeStatusCode(t *testing.T) {
// Discard the output we create from the calls here.
log.SetOutput(ioutil.Discard)
Expand All @@ -139,7 +154,8 @@ func TestHandleViolationReportMultipleTypeStatusCode(t *testing.T) {
t.Run(fmt.Sprintf("%T", statusCode), func(t *testing.T) {
csp := CSPReport{
CSPReportBody{
StatusCode: statusCode,
DocumentURI: "https://example.com",
StatusCode: statusCode,
},
}

Expand Down

0 comments on commit b907c8c

Please sign in to comment.