Skip to content

Commit

Permalink
ref: Set headers inside client check (getsentry#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikorihn committed Jan 12, 2023
1 parent 85ab572 commit de5abac
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions interfaces.go
Expand Up @@ -165,23 +165,33 @@ func NewRequest(r *http.Request) *Request {
}
url := fmt.Sprintf("%s://%s%s", protocol, r.Host, r.URL.Path)

sendDefaultPii := CurrentHub().Client() != nil && CurrentHub().Client().Options().SendDefaultPII

var cookies string
var env map[string]string
if sendDefaultPii {
headers := map[string]string{}

if client := CurrentHub().Client(); client != nil && client.Options().SendDefaultPII {
// We read only the first Cookie header because of the specification:
// https://tools.ietf.org/html/rfc6265#section-5.4
// When the user agent generates an HTTP request, the user agent MUST NOT
// attach more than one Cookie header field.
cookies = r.Header.Get("Cookie")

for k, v := range r.Header {
headers[k] = strings.Join(v, ",")
}

if addr, port, err := net.SplitHostPort(r.RemoteAddr); err == nil {
env = map[string]string{"REMOTE_ADDR": addr, "REMOTE_PORT": port}
}
} else {
sensitiveHeaders := getSensitiveHeaders()
for k, v := range r.Header {
if _, ok := sensitiveHeaders[k]; !ok {
headers[k] = strings.Join(v, ",")
}
}
}

headers := filterHeaders(r.Header, sendDefaultPii)
headers["Host"] = r.Host

return &Request{
Expand All @@ -194,20 +204,6 @@ func NewRequest(r *http.Request) *Request {
}
}

func filterHeaders(header http.Header, sendDefaultPii bool) map[string]string {
headers := map[string]string{}

sensitiveHeaders := getSensitiveHeaders()
for k, v := range header {
_, sensitive := sensitiveHeaders[k]
if sendDefaultPii || !sensitive {
headers[k] = strings.Join(v, ",")
}
}

return headers
}

// Exception specifies an error that occurred.
type Exception struct {
Type string `json:"type,omitempty"` // used as the main issue title
Expand Down

0 comments on commit de5abac

Please sign in to comment.