Skip to content

Commit 60bcca7

Browse files
committed
net/http: disallow empty Content-Length header
The Content-Length must be a valid numeric value, empty values should not be accepted. See: https://www.rfc-editor.org/rfc/rfc9110.html#name-content-length Fixes golang#61679
1 parent be0e0b0 commit 60bcca7

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/net/http/transfer.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -707,18 +707,17 @@ func fixLength(isResponse bool, status int, requestMethod string, header Header,
707707
return -1, nil
708708
}
709709

710-
// Logic based on Content-Length
711-
var cl string
710+
// only parse content length if it has a value
712711
if len(contentLens) == 1 {
713-
cl = textproto.TrimString(contentLens[0])
714-
}
715-
if cl != "" {
712+
// Logic based on Content-Length
713+
cl := textproto.TrimString(contentLens[0])
716714
n, err := parseContentLength(cl)
717715
if err != nil {
718716
return -1, err
719717
}
720718
return n, nil
721719
}
720+
722721
header.Del("Content-Length")
723722

724723
if isRequest {
@@ -1039,11 +1038,13 @@ func (bl bodyLocked) Read(p []byte) (n int, err error) {
10391038
}
10401039

10411040
// parseContentLength trims whitespace from s and returns -1 if no value
1042-
// is set, or the value if it's >= 0.
1041+
// is set or the header is empty, otherwise the value if it's >= 0.
10431042
func parseContentLength(cl string) (int64, error) {
10441043
cl = textproto.TrimString(cl)
1044+
// The Content-Length must be a valid numeric value.
1045+
// See: https://datatracker.ietf.org/doc/html/rfc2616/#section-14.13
10451046
if cl == "" {
1046-
return -1, nil
1047+
return -1, badStringError("invalid empty Content-Length", cl)
10471048
}
10481049
n, err := strconv.ParseUint(cl, 10, 63)
10491050
if err != nil {

src/net/http/transfer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ func TestParseContentLength(t *testing.T) {
332332
cl string
333333
wantErr error
334334
}{
335+
{
336+
cl: "",
337+
wantErr: badStringError("invalid Content-Length", ""),
338+
},
335339
{
336340
cl: "3",
337341
wantErr: nil,

0 commit comments

Comments
 (0)