Skip to content

Commit e822b1e

Browse files
AlexanderYastrebovneild
authored andcommitted
net/http: omit invalid header value from error message
Updates #43631 Change-Id: I0fe3aafdf7ef889fed1a830128721393f8d020e6 GitHub-Last-Rev: c359542 GitHub-Pull-Request: #48979 Reviewed-on: https://go-review.googlesource.com/c/go/+/355929 Reviewed-by: Dmitri Shuralyov <[email protected]> Run-TryBot: Cherry Mui <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 4a2a3bc commit e822b1e

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/net/http/transport.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ func (t *Transport) roundTrip(req *Request) (*Response, error) {
525525
for _, v := range vv {
526526
if !httpguts.ValidHeaderFieldValue(v) {
527527
req.closeBody()
528-
return nil, fmt.Errorf("net/http: invalid header field value %q for key %v", v, k)
528+
// Don't include the value in the error, because it may be sensitive.
529+
return nil, fmt.Errorf("net/http: invalid header field value for %q", k)
529530
}
530531
}
531532
}

src/net/http/transport_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -6085,14 +6085,14 @@ func TestTransportClosesBodyOnInvalidRequests(t *testing.T) {
60856085
Method: " ",
60866086
URL: u,
60876087
},
6088-
wantErr: "invalid method",
6088+
wantErr: `invalid method " "`,
60896089
},
60906090
{
60916091
name: "nil URL",
60926092
req: &Request{
60936093
Method: "GET",
60946094
},
6095-
wantErr: "nil Request.URL",
6095+
wantErr: `nil Request.URL`,
60966096
},
60976097
{
60986098
name: "invalid header key",
@@ -6101,7 +6101,7 @@ func TestTransportClosesBodyOnInvalidRequests(t *testing.T) {
61016101
Header: Header{"💡": {"emoji"}},
61026102
URL: u,
61036103
},
6104-
wantErr: "invalid header field name",
6104+
wantErr: `invalid header field name "💡"`,
61056105
},
61066106
{
61076107
name: "invalid header value",
@@ -6110,23 +6110,23 @@ func TestTransportClosesBodyOnInvalidRequests(t *testing.T) {
61106110
Header: Header{"key": {"\x19"}},
61116111
URL: u,
61126112
},
6113-
wantErr: "invalid header field value",
6113+
wantErr: `invalid header field value for "key"`,
61146114
},
61156115
{
61166116
name: "non HTTP(s) scheme",
61176117
req: &Request{
61186118
Method: "POST",
61196119
URL: &url.URL{Scheme: "faux"},
61206120
},
6121-
wantErr: "unsupported protocol scheme",
6121+
wantErr: `unsupported protocol scheme "faux"`,
61226122
},
61236123
{
61246124
name: "no Host in URL",
61256125
req: &Request{
61266126
Method: "POST",
61276127
URL: &url.URL{Scheme: "http"},
61286128
},
6129-
wantErr: "no Host",
6129+
wantErr: `no Host in request URL`,
61306130
},
61316131
}
61326132

@@ -6142,8 +6142,8 @@ func TestTransportClosesBodyOnInvalidRequests(t *testing.T) {
61426142
if !bc {
61436143
t.Fatal("Expected body to have been closed")
61446144
}
6145-
if g, w := err.Error(), tt.wantErr; !strings.Contains(g, w) {
6146-
t.Fatalf("Error mismatch\n\t%q\ndoes not contain\n\t%q", g, w)
6145+
if g, w := err.Error(), tt.wantErr; !strings.HasSuffix(g, w) {
6146+
t.Fatalf("Error mismatch: %q does not end with %q", g, w)
61476147
}
61486148
})
61496149
}

0 commit comments

Comments
 (0)