net/http/httptest: panic on non-3 digit (XXX) status code in Recorder.WriteHeader

This change conforms Recorder with net/http servers, to panic
when a handler writes a non-3 digit XXX status code.

Fixes #45353

Change-Id: Id5ed4af652e8c150ae86bf50402b800d935e2203
Reviewed-on: https://go-review.googlesource.com/c/go/+/308950
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
ian woolf 2021-04-10 15:41:13 +08:00 committed by Emmanuel Odeke
parent cbf9caaf22
commit c3931ab1b7
2 changed files with 44 additions and 0 deletions

View file

@ -122,11 +122,30 @@ func (rw *ResponseRecorder) WriteString(str string) (int, error) {
return len(str), nil
}
func checkWriteHeaderCode(code int) {
// Issue 22880: require valid WriteHeader status codes.
// For now we only enforce that it's three digits.
// In the future we might block things over 599 (600 and above aren't defined
// at https://httpwg.org/specs/rfc7231.html#status.codes)
// and we might block under 200 (once we have more mature 1xx support).
// But for now any three digits.
//
// We used to send "HTTP/1.1 000 0" on the wire in responses but there's
// no equivalent bogus thing we can realistically send in HTTP/2,
// so we'll consistently panic instead and help people find their bugs
// early. (We can't return an error from WriteHeader even if we wanted to.)
if code < 100 || code > 999 {
panic(fmt.Sprintf("invalid WriteHeader code %v", code))
}
}
// WriteHeader implements http.ResponseWriter.
func (rw *ResponseRecorder) WriteHeader(code int) {
if rw.wroteHeader {
return
}
checkWriteHeaderCode(code)
rw.Code = code
rw.wroteHeader = true
if rw.HeaderMap == nil {

View file

@ -345,3 +345,28 @@ func TestParseContentLength(t *testing.T) {
}
}
}
// Ensure that httptest.Recorder panics when given a non-3 digit (XXX)
// status HTTP code. See https://golang.org/issues/45353
func TestRecorderPanicsOnNonXXXStatusCode(t *testing.T) {
badCodes := []int{
-100, 0, 99, 1000, 20000,
}
for _, badCode := range badCodes {
badCode := badCode
t.Run(fmt.Sprintf("Code=%d", badCode), func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("Expected a panic")
}
}()
handler := func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(badCode)
}
r, _ := http.NewRequest("GET", "http://example.org/", nil)
rw := NewRecorder()
handler(rw, r)
})
}
}