Skip to content

fix(bind): treat an empty header value slice as an absent key - #3175

Open
methakon wants to merge 1 commit into
labstack:masterfrom
methakon:fix/3169-empty-header-value-panic
Open

methakon wants to merge 1 commit into
labstack:masterfrom
methakon:fix/3169-empty-header-value-panic

Conversation

@methakon

Copy link
Copy Markdown

Problem

BindHeaders panics when a header's value slice is nil or empty:

panic: runtime error: index out of range [0] with length 0

BindHeaders passes the request header map straight to bindData, which reads
v[0] on the scalar-map paths (bind.go:245, bind.go:249) and inputValue[0]
on the tagged-struct paths (bind.go:337, bind.go:364) without checking the
length. Header["X-Empty"] = nil or []string{} therefore panics instead of
binding nothing.

Note this is not reachable from a raw incoming HTTP request — net/http never
produces an empty value slice — so it is not a remote denial of service. It is
reachable for any in-process caller that builds or mutates a Header map, and
for anything calling bindData with a map it controls.

Change

Guard both reads. An empty value slice is treated like an absent key.

[]string{""} — a present but empty value — still binds, so the check keys on
length rather than on the value being empty. TestBindHeadersEmptyStringValueStillBinds
pins that distinction.

Testing

Four tests in bind_empty_header_test.go:

  • TestBindHeadersEmptyValueSlice — nil and []string{} into a tagged struct,
    as subtests, since they are distinct states reaching the same panic.
  • TestBindHeadersEmptyValueSliceIntoMap — the scalar-map destination path.
  • TestBindHeadersPresentValueStillBinds — the guard does not swallow a real value.
  • TestBindHeadersEmptyStringValueStillBinds — []string{""} still binds.

With bind.go reverted and the tests kept, the run fails with the exact panic
above. With the change, all four pass and the full root suite stays green
(ok github.com/labstack/echo/v5), including the 86 existing binding tests.

gofmt -l is clean on both changed files; go vet is clean. echo.go and
middleware/decompress.go are flagged by gofmt on master and are untouched here.

Closes #3169

bindData indexed inputValue[0] on the scalar-map paths and on the
tagged-struct paths without checking the length, so a header whose value
slice was nil or empty panicked with "index out of range [0] with length 0"
instead of binding nothing. BindHeaders passes the request header map
straight to bindData, so BindHeaders inherited the panic.

Guard both reads. An empty slice now behaves like an absent key, while
[]string{""} - a present but empty value - still binds, so the check keys
on length rather than on the value being empty.

Closes labstack#3169
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BindHeaders panics on nil or empty header value slices

1 participant