File: capture_request.go

package info (click to toggle)
golang-gitlab-gitlab-org-labkit 1.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: sh: 210; javascript: 49; makefile: 4
file content (48 lines) | stat: -rw-r--r-- 1,121 bytes parent folder | download | duplicates (4)
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
package errortracking

import (
	"net/http"

	"github.com/getsentry/sentry-go"
	"gitlab.com/gitlab-org/labkit/mask"
)

// WithRequest will capture details of the request along with the error.
func WithRequest(r *http.Request) CaptureOption {
	return func(config *captureConfig, event *sentry.Event) {
		event.Request = redactRequestInfo(r)
		event.Request.URL = r.URL.String()
		event.Request.Headers["host"] = r.URL.Hostname()
		event.Request.Method = r.Method
	}
}

// redactRequestInfo strips out information that shouldn't be send by the client.
func redactRequestInfo(r *http.Request) *sentry.Request {
	if r == nil {
		return &sentry.Request{}
	}

	req := &sentry.Request{
		Headers: make(map[string]string),
	}

	for header, v := range r.Header {
		req.Headers[header] = v[0]
		if mask.IsSensitiveHeader(header) {
			req.Headers[header] = mask.RedactionString
		}
	}

	params := r.URL.Query()
	for paramName := range params {
		if mask.IsSensitiveParam(paramName) {
			for i := range params[paramName] {
				params[paramName][i] = mask.RedactionString
			}
		}
	}
	req.QueryString = params.Encode()

	return req
}