-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttprequest.go
161 lines (136 loc) · 3.36 KB
/
httprequest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package sentry
import (
"encoding/json"
"net"
"net/http"
"net/url"
"os"
"strings"
)
// An HTTPRequestOption describes an HTTP request's data
type HTTPRequestOption interface {
Option
WithCookies() HTTPRequestOption
WithHeaders() HTTPRequestOption
WithEnv() HTTPRequestOption
WithData(data interface{}) HTTPRequestOption
Sanitize(fields ...string) HTTPRequestOption
}
// HTTPRequest passes the request context from a net/http
// request object to Sentry. It exposes a number of options
// to control what information is exposed and how it is
// sanitized.
func HTTPRequest(req *http.Request) HTTPRequestOption {
return &httpRequestOption{
request: req,
sanitize: []string{
"password",
"passwd",
"passphrase",
"secret",
},
}
}
const sanitizationString = "********"
type httpRequestOption struct {
request *http.Request
withCookies bool
withHeaders bool
withEnv bool
data interface{}
sanitize []string
}
func (h *httpRequestOption) Class() string {
return "request"
}
func (h *httpRequestOption) WithCookies() HTTPRequestOption {
h.withCookies = true
return h
}
func (h *httpRequestOption) WithHeaders() HTTPRequestOption {
h.withHeaders = true
return h
}
func (h *httpRequestOption) WithEnv() HTTPRequestOption {
h.withEnv = true
return h
}
func (h *httpRequestOption) WithData(data interface{}) HTTPRequestOption {
h.data = data
return h
}
func (h *httpRequestOption) Sanitize(fields ...string) HTTPRequestOption {
h.sanitize = append(h.sanitize, fields...)
return h
}
func (h *httpRequestOption) Omit() bool {
return h.request == nil
}
func (h *httpRequestOption) MarshalJSON() ([]byte, error) {
return json.Marshal(h.buildData())
}
func (h *httpRequestOption) buildData() *HTTPRequestInfo {
proto := "http"
if h.request.TLS != nil || h.request.Header.Get("X-Forwarded-Proto") == "https" {
proto = "https"
}
p := &HTTPRequestInfo{
Method: h.request.Method,
Query: sanitizeQuery(h.request.URL.Query(), h.sanitize).Encode(),
URL: proto + "://" + h.request.Host + h.request.URL.Path,
Headers: make(map[string]string, 0),
Env: make(map[string]string, 0),
Data: h.data,
}
if h.withCookies {
p.Cookies = h.request.Header.Get("Cookie")
}
if h.withEnv {
p.Env = make(map[string]string, 0)
if addr, port, err := net.SplitHostPort(h.request.RemoteAddr); err == nil {
p.Env["REMOTE_ADDR"] = addr
p.Env["REMOTE_PORT"] = port
}
for _, env := range os.Environ() {
ps := strings.SplitN(env, "=", 2)
k := ps[0]
v := ""
if len(ps) > 1 {
v = ps[1]
}
if shouldSanitize(k, h.sanitize) {
v = sanitizationString
}
p.Env[k] = v
}
}
if h.withHeaders {
p.Headers = make(map[string]string, len(h.request.Header))
for k, v := range h.request.Header {
p.Headers[k] = strings.Join(v, ",")
if shouldSanitize(k, h.sanitize) {
p.Headers[k] = sanitizationString
}
}
}
return p
}
func shouldSanitize(s string, fields []string) bool {
for _, keyword := range fields {
if strings.Contains(strings.ToLower(s), strings.ToLower(keyword)) {
return true
}
}
return false
}
func sanitizeQuery(query url.Values, fields []string) url.Values {
for field := range query {
for _, keyword := range fields {
if strings.Contains(strings.ToLower(field), strings.ToLower(keyword)) {
query[field] = []string{sanitizationString}
break
}
}
}
return query
}