-
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
Merged
Tyler Pina (tylerpina)
merged 3 commits into
master
from
fix/redact-auth-header-in-log-interceptor
Jul 21, 2026
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/test/kotlin/com/contentful/java/cma/interceptor/LogInterceptorTests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package com.contentful.java.cma.interceptor | ||
|
|
||
| import com.contentful.java.cma.Logger | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.Request | ||
| import okhttp3.mockwebserver.MockResponse | ||
| import okhttp3.mockwebserver.MockWebServer | ||
| import org.junit.After | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class LogInterceptorTests { | ||
| private lateinit var server: MockWebServer | ||
|
|
||
| @Before fun setUp() { | ||
| server = MockWebServer() | ||
| server.start() | ||
| } | ||
|
|
||
| @After fun tearDown() { | ||
| server.shutdown() | ||
| } | ||
|
|
||
| @Test fun testAuthorizationHeaderIsRedacted() { | ||
| val token = "super-secret-token" | ||
| val logs = mutableListOf<String>() | ||
|
tylerpina marked this conversation as resolved.
Outdated
|
||
| val logger = Logger { message -> logs.add(message) } | ||
|
|
||
| val client = OkHttpClient.Builder() | ||
| .addInterceptor(LogInterceptor(logger)) | ||
| .build() | ||
|
|
||
| server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) | ||
|
|
||
| val request = Request.Builder() | ||
| .url(server.url("/")) | ||
| .header(AuthorizationHeaderInterceptor.HEADER_NAME, "Bearer $token") | ||
| .build() | ||
|
|
||
| client.newCall(request).execute().use { it.body?.string() } | ||
|
|
||
| val requestLog = logs.first { it.startsWith("Sending request") } | ||
|
|
||
| assertFalse(requestLog.contains(token), "Raw token must not appear in logs") | ||
| assertTrue(requestLog.contains("Bearer [REDACTED]"), "Redacted marker expected") | ||
| } | ||
|
|
||
| @Test fun testOtherHeadersAreStillLogged() { | ||
| val logs = mutableListOf<String>() | ||
| val logger = Logger { message -> logs.add(message) } | ||
|
|
||
| val client = OkHttpClient.Builder() | ||
| .addInterceptor(LogInterceptor(logger)) | ||
| .build() | ||
|
|
||
| server.enqueue(MockResponse().setResponseCode(200).setBody("{}")) | ||
|
|
||
| val request = Request.Builder() | ||
| .url(server.url("/")) | ||
| .header(AuthorizationHeaderInterceptor.HEADER_NAME, "Bearer secret") | ||
| .header("X-Custom-Header", "custom-value") | ||
| .build() | ||
|
|
||
| client.newCall(request).execute().use { it.body?.string() } | ||
|
|
||
| val requestLog = logs.first { it.startsWith("Sending request") } | ||
|
|
||
| assertTrue(requestLog.contains("X-Custom-Header: custom-value"), | ||
| "Non-authorization headers must still be logged") | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.