Added a check for malformed headers

This commit is contained in:
Eduard Urbach 2025-02-14 22:28:29 +01:00
parent e787609322
commit a4177508f1
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 25 additions and 0 deletions

View File

@ -70,6 +70,7 @@ PASS: TestBadRequest
PASS: TestBadRequestHeader PASS: TestBadRequestHeader
PASS: TestBadRequestMethod PASS: TestBadRequestMethod
PASS: TestBadRequestProtocol PASS: TestBadRequestProtocol
PASS: TestConnectionClose
PASS: TestEarlyClose PASS: TestEarlyClose
PASS: TestUnavailablePort PASS: TestUnavailablePort
coverage: 100.0% of statements coverage: 100.0% of statements

View File

@ -177,6 +177,10 @@ func (s *server) handleConnection(conn net.Conn) {
continue continue
} }
if colon > len(message)-4 {
continue
}
key := message[:colon] key := message[:colon]
value := message[colon+2 : len(message)-2] value := message[colon+2 : len(message)-2]

View File

@ -136,6 +136,26 @@ func TestBadRequestProtocol(t *testing.T) {
s.Run(":8080") s.Run(":8080")
} }
func TestConnectionClose(t *testing.T) {
s := web.NewServer()
go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err)
defer conn.Close()
_, err = io.WriteString(conn, "GET / HTTP/1.1\r\nConnection: close\r\n\r\n")
assert.Nil(t, err)
_, err = io.ReadAll(conn)
assert.Nil(t, err)
}()
s.Run(":8080")
}
func TestEarlyClose(t *testing.T) { func TestEarlyClose(t *testing.T) {
s := web.NewServer() s := web.NewServer()