-
Notifications
You must be signed in to change notification settings - Fork 69
/
request_extractor.go
115 lines (100 loc) · 2.95 KB
/
request_extractor.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
package bugsnag
import (
"context"
"net/http"
"net/url"
"strings"
)
const requestContextKey requestKey = iota
type requestKey int
// AttachRequestData returns a child of the given context with the request
// object attached for later extraction by the notifier in order to
// automatically record request data
func AttachRequestData(ctx context.Context, r *http.Request) context.Context {
return context.WithValue(ctx, requestContextKey, r)
}
// extractRequestInfo looks for the request object that the notifier
// automatically attaches to the context when using any of the supported
// frameworks or bugsnag.HandlerFunc or bugsnag.Handler, and returns sub-object
// supported by the notify API.
func extractRequestInfo(ctx context.Context) (*RequestJSON, *http.Request) {
if req := getRequestIfPresent(ctx); req != nil {
return extractRequestInfoFromReq(req), req
}
return nil, nil
}
// extractRequestInfoFromReq extracts the request information the notify API
// understands from the given HTTP request. Returns the sub-object supported by
// the notify API.
func extractRequestInfoFromReq(req *http.Request) *RequestJSON {
return &RequestJSON{
ClientIP: req.RemoteAddr,
HTTPMethod: req.Method,
URL: sanitizeURL(req),
Referer: req.Referer(),
Headers: parseRequestHeaders(req.Header),
}
}
// sanitizeURL will build up the URL matching the request. It will filter query parameters to remove sensitive fields.
// The query part of the URL might appear differently (different order of parameters) if any filtering was done.
func sanitizeURL(req *http.Request) string {
scheme := "http"
if req.TLS != nil {
scheme = "https"
}
rawQuery := req.URL.RawQuery
parsedQuery, err := url.ParseQuery(req.URL.RawQuery)
if err != nil {
return scheme "://" req.Host req.RequestURI
}
changed := false
for key, values := range parsedQuery {
if contains(Config.ParamsFilters, key) {
for i := range values {
values[i] = "BUGSNAG_URL_FILTERED"
changed = true
}
}
}
if changed {
rawQuery = parsedQuery.Encode()
rawQuery = strings.Replace(rawQuery, "BUGSNAG_URL_FILTERED", "[FILTERED]", -1)
}
u := url.URL{
Scheme: scheme,
Host: req.Host,
Path: req.URL.Path,
RawQuery: rawQuery,
}
return u.String()
}
func parseRequestHeaders(header map[string][]string) map[string]string {
headers := make(map[string]string)
for k, v := range header {
// Headers can have multiple values, in which case we report them as csv
if contains(Config.ParamsFilters, k) {
headers[k] = "[FILTERED]"
} else {
headers[k] = strings.Join(v, ",")
}
}
return headers
}
func contains(slice []string, e string) bool {
for _, s := range slice {
if strings.Contains(strings.ToLower(e), strings.ToLower(s)) {
return true
}
}
return false
}
func getRequestIfPresent(ctx context.Context) *http.Request {
if ctx == nil {
return nil
}
val := ctx.Value(requestContextKey)
if val == nil {
return nil
}
return val.(*http.Request)
}