diff options
| author | Paschalis Tsilias <paschalis.tsilias@gmail.com> | 2020-05-21 15:33:39 +0300 |
|---|---|---|
| committer | Emmanuel Odeke <emm.odeke@gmail.com> | 2020-05-31 00:55:05 +0000 |
| commit | 8da78625b1fe2a6141d331f54248913936dc49c7 (patch) | |
| tree | 55017fb53dbf51ece3f9abef5d8e68a87864c2f3 /src/net/http/httptest | |
| parent | fc40beb987fa503f3452e2e311f765241f5a3cf0 (diff) | |
| download | go-8da78625b1fe2a6141d331f54248913936dc49c7.tar.xz | |
net/http: reject HTTP/1.1 Content-Length with sign in response
Enforces section 14.13 of RFC 2616 so that Content-Length header
values with a sign such as "+5" will be rejected.
Updates #39017
Change-Id: Icce9f00d03c8475fe704b33f9bed9089ff8802f0
Reviewed-on: https://go-review.googlesource.com/c/go/+/234817
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Diffstat (limited to 'src/net/http/httptest')
| -rw-r--r-- | src/net/http/httptest/recorder.go | 4 | ||||
| -rw-r--r-- | src/net/http/httptest/recorder_test.go | 36 |
2 files changed, 38 insertions, 2 deletions
diff --git a/src/net/http/httptest/recorder.go b/src/net/http/httptest/recorder.go index 13697454cb..66e67e78b3 100644 --- a/src/net/http/httptest/recorder.go +++ b/src/net/http/httptest/recorder.go @@ -226,9 +226,9 @@ func parseContentLength(cl string) int64 { if cl == "" { return -1 } - n, err := strconv.ParseInt(cl, 10, 64) + n, err := strconv.ParseUint(cl, 10, 63) if err != nil { return -1 } - return n + return int64(n) } diff --git a/src/net/http/httptest/recorder_test.go b/src/net/http/httptest/recorder_test.go index 0986554aa8..e9534894b6 100644 --- a/src/net/http/httptest/recorder_test.go +++ b/src/net/http/httptest/recorder_test.go @@ -310,3 +310,39 @@ func TestRecorder(t *testing.T) { }) } } + +// issue 39017 - disallow Content-Length values such as "+3" +func TestParseContentLength(t *testing.T) { + tests := []struct { + cl string + want int64 + }{ + { + cl: "3", + want: 3, + }, + { + cl: "+3", + want: -1, + }, + { + cl: "-3", + want: -1, + }, + { + // max int64, for safe conversion before returning + cl: "9223372036854775807", + want: 9223372036854775807, + }, + { + cl: "9223372036854775808", + want: -1, + }, + } + + for _, tt := range tests { + if got := parseContentLength(tt.cl); got != tt.want { + t.Errorf("%q:\n\tgot=%d\n\twant=%d", tt.cl, got, tt.want) + } + } +} |
