Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Custom sanitize fields. Sanitize headers. #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ func NewHttp(req *http.Request) *Http {
h := &Http{
Method: req.Method,
Cookies: req.Header.Get("Cookie"),
Query: sanitizeQuery(req.URL.Query()).Encode(),
Query: url.Values(sanitizeValues(req.URL.Query())).Encode(),
URL: proto + "://" + req.Host + req.URL.Path,
Headers: make(map[string]string, len(req.Header)),
}
if addr, port, err := net.SplitHostPort(req.RemoteAddr); err == nil {
h.Env = map[string]string{"REMOTE_ADDR": addr, "REMOTE_PORT": port}
}
for k, v := range req.Header {

for k, v := range http.Header(sanitizeValues(req.Header)) {
h.Headers[k] = strings.Join(v, ",")
}
h.Headers["Host"] = req.Host
Expand All @@ -34,17 +35,24 @@ func NewHttp(req *http.Request) *Http {

var querySecretFields = []string{"password", "passphrase", "passwd", "secret"}

func sanitizeQuery(query url.Values) url.Values {
func sanitizeValues(query map[string][]string) map[string][]string {
for _, keyword := range querySecretFields {
for field := range query {
if strings.Contains(field, keyword) {
if strings.Contains(strings.ToLower(field), strings.ToLower(keyword)) {
query[field] = []string{"********"}
}
}
}
return query
}

// AddSanitizewField adds a custom sanitize field to the array of fields to
// search for and sanitize. This allows you to hide sensitive information in
// both the query string and headers.
func AddSanitizeField(field string) {
querySecretFields = append(querySecretFields, field)
}

// https://docs.getsentry.com/hosted/clientdev/interfaces/#context-interfaces
type Http struct {
// Required
Expand Down
30 changes: 29 additions & 1 deletion http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,38 @@ func parseQuery(q string) url.Values {

func TestSanitizeQuery(t *testing.T) {
for _, test := range sanitizeQueryTests {
actual := sanitizeQuery(parseQuery(test.input))
actual := url.Values(sanitizeValues(parseQuery(test.input)))
expected := parseQuery(test.output)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("incorrect sanitization: got %+v, want %+v", actual, expected)
}
}
}

var sanitizeHeadersTest = []struct {
input, output string
}{
{"foo=bar", "foo=bar"},
{"password=foo", "password=********"},
{"passphrase=foo", "passphrase=********"},
{"passwd=foo", "passwd=********"},
{"secret=foo", "secret=********"},
{"secretstuff=foo", "secretstuff=********"},
{"foo=bar&secret=foo", "foo=bar&secret=********"},
{"secret=foo&secret=bar", "secret=********"},
}

func parseHeaders(q string) http.Header {
r, _ := url.ParseQuery(q)
return http.Header(r)
}

func TestSanitizeHeaders(t *testing.T) {
for _, test := range sanitizeHeadersTest {
actual := http.Header(sanitizeValues(parseQuery(test.input)))
expected := parseHeaders(test.output)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("incorrect sanitization: got %+v, want %+v", actual, expected)
}
}
}