Skip to content

Commit 57d57a5

Browse files
authored
Hide sensitive header values in show (#1127)
With this patch the values for the headers `Authorization`, `Proxy-Authorization`, `Cookie` and `Set-Cookie` are masked with `*`s when `HTTP.Request`s and `HTTP.Response`s are `show`n. These headers may contain sensitive information and masking the values reduces the risk of leaking something.
1 parent f9389ae commit 57d57a5

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Changed
1010
- Server errors are no longer serialized back to the client since this might leak sensitive
1111
information through the error message. ([#1126])
12+
- When `show`ing `HTTP.Request` and `HTTP.Response` the values for the headers
13+
`Authorization`, `Proxy-Authorization`, `Cookie`, and `Set-Cookie` are masked with `*`s
14+
since they might include sensitive information. ([#1127])
1215
### Fixed
1316
- Restrict `HTTP.isredirect` to arguments of integer types. ([#1117])
1417
- Fix `HTTP.getcookies` error when key doesn't exist. ([#1119])

src/Messages.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,16 @@ function Base.show(io::IO, m::Message)
609609
end
610610
println(io, typeof(m), ":")
611611
println(io, "\"\"\"")
612-
writeheaders(io, m)
612+
613+
# Mask the following (potentially) sensitive headers with "******":
614+
# - Authorization
615+
# - Proxy-Authorization
616+
# - Cookie
617+
# - Set-Cookie
618+
header_str = sprint(writeheaders, m)
619+
header_str = replace(header_str, r"(*CRLF)^((?>(?>Proxy-)?Authorization|(?>Set-)?Cookie): ).+$"mi => s"\1******")
620+
write(io, header_str)
621+
613622
summary = bodysummary(m.body)
614623
validsummary = isvalidstr(summary)
615624
validsummary && write(io, summary)

test/messages.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ using JSON
197197
# https://github.com/JuliaWeb/HTTP.jl/issues/828
198198
# don't include empty headers in request when writing
199199
@test repr(Request("GET", "/", ["Accept" => ""])) == "Request:\n\"\"\"\nGET / HTTP/1.1\r\n\r\n\"\"\""
200+
201+
# Test that sensitive header values are masked when `show`ing HTTP.Request and HTTP.Response
202+
for H in ["Authorization", "Proxy-Authorization", "Cookie", "Set-Cookie"], h in (lowercase(H), H)
203+
req = HTTP.Request("GET", "https://xyz.com", [h => "secret", "User-Agent" => "HTTP.jl"])
204+
req_str = sprint(show, req)
205+
@test !occursin("secret", req_str)
206+
@test occursin("$h: ******", req_str)
207+
@test occursin("HTTP.jl", req_str)
208+
resp = HTTP.Response(200, [h => "secret", "Server" => "HTTP.jl"])
209+
resp_str = sprint(show, resp)
210+
@test !occursin("secret", resp_str)
211+
@test occursin("$h: ******", req_str)
212+
@test occursin("HTTP.jl", resp_str)
213+
end
200214
end
201215

202216
@testset "queryparams" begin

0 commit comments

Comments
 (0)