Repository navigation
[http] Redact the URL in url.Error's inner error too - #1477
Merged
Merged
Conversation
Redacting only url.Error's URL field misses a redirect leak. net/http
builds that error with the *request* URL in the field, but when a
redirect's Location fails to parse it puts the raw Location -- a
different URL, with a different query -- in the inner error instead
(client.go's uerr). So this still reached the log in full:
Get "http://h/start?<redacted>": failed to parse Location header
"https://other/cb?code=supersecret": invalid port ...
which is the redirect case the option exists to cover. Redact the inner
error's message as well, replacing it only when the text actually
changes so the Unwrap chain survives in the common case, where the inner
error holds no URL at all.
Redacting inner errors means the quoted-token rewrite now sees messages
it never used to, so it also has to stop assuming every quoted token is
a URL. An x509 error naming "a?b" would otherwise come out as "a?
<redacted>", losing a diagnostic that never held a secret. Require the
token to look like a URL -- to contain "://" or start with "/".
Also correct redactedErr's doc comment, which claimed errors.Is/As are
unaffected. That holds for the common *url.Error, but not for the
fallback or for an inner error that had to be rewritten. Wrapping with
%w to keep them is not an option: it re-embeds the unredacted text and
puts the query straight back into the message.
redactURL cut at the first '?' and took everything after it, so it
dropped any fragment, while redactedURL -- which works from a parsed URL
-- kept it. The same URL then logged two ways:
url=http://h/a?<redacted>#frag (the "url" attribute)
Get "http://h/a?<redacted>": ... (the error from that same request)
Cutting at the first '?' also mis-fired when the only '?' was inside a
fragment, redacting "http://h/a#x?y" as though it had a query.
Split the fragment off first, then redact between '?' and it, and share
that as redactRawURL between redactURL and the quoted-token rewrite so
all three paths agree by construction. Everything from '?' to the
fragment still goes regardless of content, so a query containing a space
is redacted whole, as before.
Three functions were redacting a URL three ways. Now that redactRawURL keeps the fragment, redactedURL's parsed-URL path produces the same string as redactURL(u.String()) for every URL I could construct -- fragments, userinfo, encoded '%3F', ForceQuery -- so it is gone and its two callers pass req.URL.String(). redactErrMsg never used its receiver once it delegated to redactRawURL, so it is a plain function; the Probe its test was building fell out with it. The comments had grown to a third of the block, much of it restating the code or re-arguing decisions. Kept the parts that record something the code cannot show -- why we don't stop at whitespace, that net/http puts a URL in two places, that %w would re-embed the unredacted text -- and dropped the rest, including a worked example that the tests already cover.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to #1410. That change redacts
url.Error'sURLfield, which coversthe common case — but
net/httpputs a URL in two places.uerr(net/http/client.go:622) builds the error with the request URL in.URL, while a redirect whoseLocationfails to parse puts the rawLocationin the inner error. So with
redact_url_query_in_logs: truethis stillreached the log in full:
Our own query is redacted; the redirect target's is not — which is exactly the
redirect leak the option exists to close.
Changes
actually changes, so the
Unwrapchain survives in the common case where theinner error holds no URL.
://or start with/) before rewriting it. Redacting inner errors means the quoted-token pathnow sees messages it never used to; without this,
x509: certificate is valid for "a?b", not hwould come out as"a?<redacted>", destroying a diagnosticthat never held a secret.
redactedErr's doc comment, which claimederrors.Is/Asareunaffected by redaction. True for the common
*url.Error, not for thefallback or a rewritten inner error. Wrapping with
%wto preserve them isnot an option — it re-embeds the unredacted text and puts the query straight
back in.
Testing
TestRedactedErrRedirectLocationInInnerErrordrives a realhttptestserverthat returns an unparseable
Locationcarrying a secret, and asserts neitherquery survives. Verified it fails without the fix ("redirect Location query
leaked via the inner error").
TestRedactedErrKeepsUnwrapWhenInnerErrIsCleanpins that
errors.Isstill works when the inner error needs no rewriting, andTestRedactErrMsggains cases for non-URL and relative-URL quoted tokens.go build ./...,go vet,go test -race ./probes/http/...andgofmtareclean.