net/http: Redirect hardening #71161
Labels
LibraryProposal
Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool
NeedsFix
The path to resolution is known, but the work has not been done.
Security
The
http.Redirect
function writes a 3xx redirect to aResponseWriter
.Redirect
takes a URL parameter, of type string. The URL parameter has only minimal sanitization applied, and is not safe for use with attacker-controlled inputs.One example of possibly-surprising behavior is that a redirect to
\\example.com
is a relative-path reference according to RFC 3986, but will be interpreted by most browsers as a network-path reference./\example.com
is an absolute-path reference according to the RFC, but will also be interpreted by browsers as a network-path reference. (Thanks to Jingcheng Yang (Sichuan University), Enze Wang@IPASSLAB(@zer0yu), Jianjun Chen (Tsinghua University & Zhongguancun Laboratory) for reporting this case.)We should document that
Redirect
does not sanitize its URL parameter. Users who wish to useRedirect
with untrusted URLs should parse the URL withnet/url
, perform whatever validation they may wish, and then reassemble the parsed and validated URL into a string withnet/url.URL.String
.We should also consider, as a hardening measure, %-encoding backslashes at the start of
Redirect
's URL parameter to prevent browsers from interpreting them as part of an absolute-path reference.The text was updated successfully, but these errors were encountered: