Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update setLogger Authorization header filtering logic #111

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})

}
}