Skip to content

[http] Redact the URL in url.Error's inner error too - #1477

Merged
manugarg merged 3 commits into
mainfrom
http-redact-inner-url-error
Sep 9, 2026
Merged

manugarg merged 3 commits into
mainfrom
http-redact-inner-url-error

Conversation

@manugarg

@manugarg manugarg commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #1410. That change redacts url.Error's URL field, which covers
the common case — but net/http puts a URL in two places.

uerr (net/http/client.go:622) builds the error with the request URL in
.URL, while a redirect whose Location fails to parse puts the raw Location
in the inner error. So with redact_url_query_in_logs: true this still
reached the log in full:

Get "http://h/start?<redacted>": failed to parse Location header
"https://other.example.com:notaport/cb?code=supersecret": invalid port ...

Our own query is redacted; the redirect target's is not — which is exactly the
redirect leak the option exists to close.

Changes

  • Redact the inner error's message too. It is swapped out only when the text
    actually changes, so the Unwrap chain survives in the common case where the
    inner error holds no URL.
  • Require a quoted token to look like a URL (contain :// or start with
    /) before rewriting it. Redacting inner errors means the quoted-token path
    now sees messages it never used to; without this, x509: certificate is valid for "a?b", not h would come out as "a?<redacted>", destroying a diagnostic
    that never held a secret.
  • Correct redactedErr's doc comment, which claimed errors.Is/As are
    unaffected by redaction. True for the common *url.Error, not for the
    fallback or a rewritten inner error. Wrapping with %w to preserve them is
    not an option — it re-embeds the unredacted text and puts the query straight
    back in.

Testing

TestRedactedErrRedirectLocationInInnerError drives a real httptest server
that returns an unparseable Location carrying a secret, and asserts neither
query survives. Verified it fails without the fix ("redirect Location query
leaked via the inner error"). TestRedactedErrKeepsUnwrapWhenInnerErrIsClean
pins that errors.Is still works when the inner error needs no rewriting, and
TestRedactErrMsg gains cases for non-URL and relative-URL quoted tokens.

go build ./..., go vet, go test -race ./probes/http/... and gofmt are
clean.

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.
@manugarg
manugarg merged commit 2586834 into main Sep 9, 2026
13 checks passed
@manugarg
manugarg deleted the http-redact-inner-url-error branch September 9, 2026 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant