Skip to content
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

fix infinite loop when parsing specially-crafted headers #521

Merged
merged 1 commit into from
Feb 21, 2024
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
22 changes: 10 additions & 12 deletions pkg/headers/keyval.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,17 @@ func keyValParse(str string, separator byte) (map[string]string, error) {
var k string
k, str = readKey(str, separator)

if len(k) > 0 {
if len(str) > 0 && str[0] == '=' {
var v string
var err error
v, str, err = readValue(origstr, str[1:], separator)
if err != nil {
return nil, err
}

ret[k] = v
} else {
ret[k] = ""
if len(str) > 0 && str[0] == '=' {
var v string
var err error
v, str, err = readValue(origstr, str[1:], separator)
if err != nil {
return nil, err
}

ret[k] = v
} else {
ret[k] = ""
}

// skip separator
Expand Down
21 changes: 4 additions & 17 deletions pkg/headers/keyval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,8 @@ func TestKeyValParse(t *testing.T) {
}
}

func TestKeyValParseErrors(t *testing.T) {
for _, ca := range []struct {
name string
s string
err string
}{
{
"apexes not closed",
`key1="v,1`,
"apexes not closed (key1=\"v,1)",
},
} {
t.Run(ca.name, func(t *testing.T) {
_, err := keyValParse(ca.s, ',')
require.EqualError(t, err, ca.err)
})
}
func FuzzKeyValParse(f *testing.F) {
f.Fuzz(func(t *testing.T, b string) {
keyValParse(b, ',') //nolint:errcheck
})
}
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzKeyValParse/15422aae47040b82
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("=\"")
2 changes: 2 additions & 0 deletions pkg/headers/testdata/fuzz/FuzzKeyValParse/32a837cd12bd2cfa
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("=\x84")
Loading