Skip to content
Merged
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
21 changes: 20 additions & 1 deletion pkg/logging/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"io"
"net/http"
"regexp"
"strings"
"time"

"github.com/interuss/dss/pkg/auth/claims"
Expand Down Expand Up @@ -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 {
Comment thread
the-glu marked this conversation as resolved.
if strings.ToLower(key) == "authorization" {
for i, val := range values {
newHeaders[key][i] = tokenRegex.ReplaceAllString(val, "$1[REDACTED]")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.
Since this is already an improvement compared to the current state, I propose to move forward and @mickmis feel free to propose another PR or open issue when you come back if there is still any concern.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 {
Expand Down Expand Up @@ -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),
Expand Down
Loading