8000 net/http: reject newlines in chunk-size lines (backport to 1.17) by drehak · Pull Request #293 · golang-fips/go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

net/http: reject newlines in chunk-size lines (backport to 1.17) #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions 8000 src/net/http/internal/chunked.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,34 @@ func readChunkLine(b *bufio.Reader) ([]byte, error) {
}
return nil, err
}

// RFC 9112 permits parsers to accept a bare \n as a line ending in headers,
// but not in chunked encoding lines. See https://www.rfc-editor.org/errata/eid7633,
// which explicitly rejects a clarification permitting \n as a chunk terminator.
//
// Verify that the line ends in a CRLF, and that no CRs appear before the end.
if idx := bytes.IndexByte(p, '\r'); idx == -1 {
return nil, errors.New("chunked line ends with bare LF")
} else if idx != len(p)-2 {
return nil, errors.New("invalid CR in chunked line")
}
p = p[:len(p)-2] // trim CRLF

if len(p) >= maxLineLength {
return nil, ErrLineTooLong
}
return p, nil
}

func trimTrailingWhitespace(b []byte) []byte {
for len(b) > 0 && isASCIISpace(b[len(b)-1]) {
for len(b) > 0 && isOWS(b[len(b)-1]) {
b = b[:len(b)-1]
}
return b
}

func isASCIISpace(b byte) bool {
return b == ' ' || b == '\t' || b == '\n' || b == '\r'
func isOWS(b byte) bool {
return b == ' ' || b == '\t'
}

// removeChunkExtension removes any chunk-extension from p.
Expand Down
27 changes: 27 additions & 0 deletions src/net/http/internal/chunked_test.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,33 @@ func TestChunkReaderByteAtATime(t *testing.T) {
}
}

func TestChunkInvalidInputs(t *testing.T) {
for _, test := range []struct {
name string
b string
}{{
name: "bare LF in chunk size",
b: "1\na\r\n0\r\n",
}, {
name: "extra LF in chunk size",
b: "1\r\r\na\r\n0\r\n",
}, {
name: "bare LF in chunk data",
b: "1\r\na\n0\r\n",
}, {
name: "bare LF in chunk extension",
b: "1;\na\r\n0\r\n",
}} {
t.Run(test.name, func(t *testing.T) {
r := NewChunkedReader(strings.NewReader(test.b))
got, err := io.ReadAll(r)
if err == nil {
t.Fatalf("unexpectedly parsed invalid chunked data:\n%q", got)
}
})
}
}

type funcReader struct {
f func(iteration int) ([]byte, error)
i int
Expand Down
49 changes: 49 additions & 0 deletions src/net/http/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6610,3 +6610,52 @@ func testQuerySemicolon(t *testing.T, query string, wantX string, allowSemicolon
}
}
}

func TestInvalidChunkedBodies(t *testing.T) {
for _, test := range []struct {
name string
b string
}{{
name: "bare LF in chunk size",
b: "1\na\r\n0\r\n\r\n",
}, {
name: "bare LF at body end",
b: "1\r\na\r\n0\r\n\n",
}} {
t.Run(test.name, func(t *testing.T) {
reqc := make(chan error)
ts := newClientServerTest(t, false, HandlerFunc(func(w ResponseWriter, r *Request) {
got, err := io.ReadAll(r.Body)
if err == nil {
t.Logf("read body: %q", got)
}
reqc <- err
})).ts

serverURL, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}

conn, err := net.Dial("tcp", serverURL.Host)
if err != nil {
t.Fatal(err)
}

if _, err := conn.Write([]byte(
"POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"Transfer-Encoding: chunked\r\n" +
"Connection: close\r\n" +
"\r\n" +
test.b)); err != nil {
t.Fatal(err)
}
conn.(*net.TCPConn).CloseWrite()

if err := <-reqc; err == nil {
t.Errorf("server handler: io.ReadAll(r.Body) succeeded, want error")
}
})
}
}
Loading
0