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

Fix Request.toString leaking sensitive headers. #7458

Merged
merged 1 commit into from
Oct 16, 2022
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
3 changes: 2 additions & 1 deletion okhttp/src/jvmMain/kotlin/okhttp3/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import okhttp3.internal.commonPost
import okhttp3.internal.commonPut
import okhttp3.internal.commonRemoveHeader
import okhttp3.internal.commonTag
import okhttp3.internal.isSensitiveHeader

actual class Request internal actual constructor(builder: Builder) {
@get:JvmName("url")
Expand Down Expand Up @@ -166,7 +167,7 @@ actual class Request internal actual constructor(builder: Builder) {
}
append(name)
append(':')
append(value)
append(if (isSensitiveHeader(name)) "██" else value)
}
append(']')
}
Expand Down
26 changes: 26 additions & 0 deletions okhttp/src/jvmTest/java/okhttp3/RequestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,32 @@ class RequestTest {
assertThat(requestB.tag(String::class.java)).isSameAs("b")
assertThat(requestC.tag(String::class.java)).isSameAs("c")
}

@Test
fun requestToStringRedactsSensitiveHeaders() {
val headers = Headers.Builder()
.add("content-length", "99")
.add("authorization", "peanutbutter")
.add("proxy-authorization", "chocolate")
.add("cookie", "drink=coffee")
.add("set-cookie", "accessory=sugar")
.add("user-agent", "OkHttp")
.build()
val request = Request(
"https://square.com".toHttpUrl(),
headers
)
assertThat(request.toString()).isEqualTo(
"Request{method=GET, url=https://square.com/, headers=[" +
"content-length:99," +
" authorization:██," +
" proxy-authorization:██," +
" cookie:██," +
" set-cookie:██," +
" user-agent:OkHttp" +
"]}"
)
}

private fun bodyToHex(body: RequestBody): String {
val buffer = Buffer()
Expand Down