-
Notifications
You must be signed in to change notification settings - Fork 24
fix: redact Authorization header in LogInterceptor [AIS-247] #212
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 1 commit
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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| import java.io.IOException; | ||
|
|
||
| import okhttp3.Headers; | ||
| import okhttp3.Interceptor; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
|
|
@@ -46,7 +47,7 @@ public LogInterceptor(Logger logger) { | |
|
|
||
| long t1 = System.nanoTime(); | ||
| logger.log(String.format("Sending request %s on %s%n%s", | ||
| request.url(), chain.connection(), request.headers())); | ||
| request.url(), chain.connection(), redactHeaders(request.headers()))); | ||
|
|
||
| Response response = chain.proceed(request); | ||
|
|
||
|
|
@@ -56,4 +57,17 @@ public LogInterceptor(Logger logger) { | |
|
|
||
| return response; | ||
| } | ||
|
|
||
| private static Headers redactHeaders(Headers headers) { | ||
| Headers.Builder redacted = new Headers.Builder(); | ||
| for (int i = 0; i < headers.size(); i++) { | ||
| String name = headers.name(i); | ||
| if ("Authorization".equalsIgnoreCase(name)) { | ||
|
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. Small one:
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. Done β |
||
| redacted.add(name, "Bearer [REDACTED]"); | ||
| } else { | ||
| redacted.add(name, headers.value(i)); | ||
| } | ||
| } | ||
| return redacted.build(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following up on coverage β there's no existing test file for
LogInterceptor, so this redaction doesn't have anything guarding it going forward. A quick test that mocks the logger and asserts the token never shows up unredacted in the logged string could be cheap insurance for a fix like this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
LogInterceptorTests.ktunderinterceptor/. It drives a real request through the interceptor viaMockWebServerwith a capturingLogger:testAuthorizationHeaderIsRedactedβ asserts the raw token never appears andBearer [REDACTED]does.testOtherHeadersAreStillLoggedβ asserts a non-auth header (X-Custom-Header) is still present, so redaction is surgical.Heads up: I could not run the suite locally (no JDK on my machine at the moment), so I am leaning on CI to confirm it goes green β the test mirrors the existing
MockWebServerpattern used across the Kotlin tests, so I would expect it to, but flagging that I have not executed it myself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ran the suite locally after all (installed a JDK) β and it caught something worth flagging.
The end-to-end test I first wrote asserted
Bearer [REDACTED]appears in the log. It failed: okhttp >= 4.10 already masksAuthorizationtoββinHeaders.toString()by header name, regardless of value. So on our okhttp (4.12):Bearer [REDACTED]gets overwritten toββtoo, so it is not observable end-to-end.Two takeaways:
redactHeadersis defense-in-depth here, not the thing preventing the leak on 4.12 β it guarantees the value is stripped independent of okhttp's internal masking (which is an undocumented debug convenience, not a security guarantee, and would disappear if okhttp were pinned < 4.10). Added a comment saying so.redactHeaderswere deleted, so it does not actually guard the code. Reworked the tests to assertredactHeadersdirectly (Authorization value βBearer [REDACTED], case-insensitive match, other headers untouched) plus keep one end-to-end raw-token-absent invariant.3/3 green locally (
mvn -Dtest=LogInterceptorTests test). Pushed in b1031c4.