-
Notifications
You must be signed in to change notification settings - Fork 96
[auth] Remove token signatures in logs #1398
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ import ( | |
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "regexp" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/interuss/dss/pkg/auth/claims" | ||
|
|
@@ -40,6 +42,23 @@ func (w *tracingResponseWriter) WriteHeader(statusCode int) { | |
| w.next.WriteHeader(statusCode) | ||
| } | ||
|
|
||
| var tokenRegex = regexp.MustCompile(`(?i)(Bearer\s+[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.)([A-Za-z0-9-_]+)`) | ||
|
|
||
| func redactHeaders(headers http.Header) http.Header { | ||
|
|
||
| newHeaders := headers.Clone() | ||
|
|
||
| for key, values := range newHeaders { | ||
| if strings.ToLower(key) == "authorization" { | ||
| for i, val := range values { | ||
| newHeaders[key][i] = tokenRegex.ReplaceAllString(val, "$1[REDACTED]") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are targeting the authorization header, might as well redact the whole value and not try to match the token itself, no?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I understand, idea was to keep the base of the token for debugging, but make it useless without the signature.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is value is keeping the content of the token safely by redacting the signature.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! I missed in the regex the last capture group and misread the title: I thought the intent was to redact the whole token. SGTM then. BTW, looking at it again, it might be worth adding unit tests covering different scenarios for this function. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| return newHeaders | ||
| } | ||
|
|
||
| // HTTPMiddleware installs a logging http.Handler that logs requests and | ||
| // selected aspects of responses to 'logger'. | ||
| func HTTPMiddleware(logger *zap.Logger, dump bool, handler http.Handler) http.Handler { | ||
|
|
@@ -88,7 +107,7 @@ func HTTPMiddleware(logger *zap.Logger, dump bool, handler http.Handler) http.Ha | |
|
|
||
| logger.Info( | ||
| fmt.Sprintf("%s %s %s", r.Method, r.URL.Path, r.Proto), | ||
| zap.Any("req_headers", r.Header), | ||
| zap.Any("req_headers", redactHeaders(r.Header)), | ||
| zap.Int("resp_status_code", trw.statusCode), | ||
| zap.String("resp_status_text", http.StatusText(trw.statusCode)), | ||
| zap.String("peer_address", r.RemoteAddr), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.