Skip to content
Myr

refactor(recovery): extract Authorization header masking into maskAuthorization func (#4143)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* refactor(recovery): extract Authorization header masking into maskAuthorization func * test(recovery): Add a test for maskAuthorization

authored by OHZEKI Naoki · committed by GitHub · parent 3c12d2a · Browse files · 2 files+29 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+29 added−6 removed

  • other:+29 added−6 removed

2 files+29 added−6 removed

modified:recovery_test.go+18 added
86 unmodified lines
87 // TEST88 assert.Equal(t, http.StatusBadRequest, w.Code)89}9091func TestMaskAuthorization(t *testing.T) {92 secret := "Bearer aaaabbbbccccddddeeeeffff"93 headers := []string{94 "Host: www.example.com",95 "Authorization: " + secret,96 "User-Agent: curl/7.51.0",97 "Accept: */*",98 "Content-Type: application/json",99 "Content-Length: 1",100 }101 maskAuthorization(headers)102103 for _, h := range headers {104 assert.NotContains(t, h, secret)105 }106 assert.Contains(t, headers, "Authorization: *")107}108109func TestSource(t *testing.T) {110 bs := source(nil, 0)111 assert.Equal(t, dunnoBytes, bs)112
153 unmodified lines
modified:recovery.go+11 added-6 removed
68 unmodified lines
69 if logger != nil {70 stack := stack(3)71 httpRequest, _ := httputil.DumpRequest(c.Request, false)72 headers := strings.Split(string(httpRequest), "\r\n")73 for idx, header := range headers {74 key, _, _ := strings.Cut(header, ":")75 if key == "Authorization" {76 headers[idx] = key + ": *"77 }78 }73 maskAuthorization(headers)74 headersToStr := strings.Join(headers, "\r\n")75 if brokenPipe {76 logger.Printf("%s\n%s%s", err, headersToStr, reset)77 } else if IsDebugging() {
47 unmodified lines
125 }126 return buf.Bytes()127}128129// maskAuthorization replaces any "Authorization: <token>" header with "Authorization: *", hiding sensitive credentials.130func maskAuthorization(headers []string) {131 for idx, header := range headers {132 key, _, _ := strings.Cut(header, ":")133 if strings.EqualFold(key, "Authorization") {134 headers[idx] = key + ": *"135 }136 }137}138139// source returns a space-trimmed slice of the n'th line.140func source(lines [][]byte, n int) []byte {141 n-- // in stack trace, lines are 1-indexed but our array is 0-indexed142 if n < 0 || n >= len(lines) {
34 unmodified lines