Skip to content

Commit

Permalink
Update setLogger Authorization header filtering logic (#111)
Browse files Browse the repository at this point in the history
* update sanitize headers logic

* add release note
  • Loading branch information
joe94 authored Jun 29, 2021
1 parent 19ae566 commit 33e69a8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Update setLogger Authorization header filtering logic. [#111](https://github.com/xmidt-org/bascule/pull/111)

## [v0.10.1]
- Added raw parsers for bearer acquirer. [#110](https://github.com/xmidt-org/bascule/pull/110)
Expand Down
19 changes: 13 additions & 6 deletions basculehttp/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,27 @@ func getZapLogger(f func(context.Context) *zap.Logger) func(context.Context) log
}
}

func sanitizeHeaders(headers http.Header) (filtered http.Header) {
filtered = headers.Clone()
if authHeader := filtered.Get("Authorization"); authHeader != "" {
filtered.Del("Authorization")
parts := strings.Split(authHeader, " ")
if len(parts) == 2 {
filtered.Set("Authorization-Type", parts[0])
}
}
return
}

// SetLogger creates an alice constructor that sets up a zap logger that can be
// used for all logging related to the current request. The logger is added to
// the request's context.
func SetLogger(logger *zap.Logger) alice.Constructor {
return func(delegate http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logHeader := r.Header.Clone()
if str := logHeader.Get("Authorization"); str != "" {
logHeader.Del("Authorization")
logHeader.Set("Authorization-Type", strings.Split(str, " ")[0])
}
r = r.WithContext(sallust.With(r.Context(),
logger.With(
zap.Reflect("requestHeaders", logHeader), //lgtm [go/clear-text-logging]
zap.Reflect("requestHeaders", sanitizeHeaders(r.Header)), //lgtm [go/clear-text-logging]
zap.String("requestURL", r.URL.EscapedPath()),
zap.String("method", r.Method))))
delegate.ServeHTTP(w, r)
Expand Down
34 changes: 34 additions & 0 deletions basculehttp/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package basculehttp

import (
"context"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -36,3 +37,36 @@ func TestGetZapLogger(t *testing.T) {
result.Log("msg", "testing", "error", "nope", "level", "debug")
})
}

func TestSanitizeHeaders(t *testing.T) {
testCases := []struct {
Description string
Input http.Header
Expected http.Header
}{
{
Description: "Filtered",
Input: http.Header{"Authorization": []string{"Basic xyz"}, "HeaderA": []string{"x"}},
Expected: http.Header{"HeaderA": []string{"x"}, "Authorization-Type": []string{"Basic"}},
},
{
Description: "Handled human error",
Input: http.Header{"Authorization": []string{"BasicXYZ"}, "HeaderB": []string{"y"}},
Expected: http.Header{"HeaderB": []string{"y"}},
},
{
Description: "Not a perfect system",
Input: http.Header{"Authorization": []string{"MySecret IWantToLeakIt"}},
Expected: http.Header{"Authorization-Type": []string{"MySecret"}},
},
}

for _, tc := range testCases {
t.Run(tc.Description, func(t *testing.T) {
assert := assert.New(t)
actual := sanitizeHeaders(tc.Input)
assert.Equal(tc.Expected, actual)
})

}
}

0 comments on commit 33e69a8

Please sign in to comment.