fix(net): guard HeaderToMap against header keys with empty value slices#4855
fix(net): guard HeaderToMap against header keys with empty value slices#4855anxkhn wants to merge 1 commit into
Conversation
HeaderToMap indexed v[0] for every header value, which panics with
"index out of range [0] with length 0" when an http.Header key has an
empty value slice (e.g. h["X-Empty"] = []string{}). Such entries are
preserved by http.Header.Clone(). Skip keys with no values, mirroring the
defensive style of the sibling MapToHeader. Add a table-driven test case
covering a key with an empty value slice.
Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4855 +/- ##
=======================================
Coverage 28.04% 28.04%
=======================================
Files 232 232
Lines 23148 23149 +1
=======================================
+ Hits 6491 6492 +1
Misses 16218 16218
Partials 439 439
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
yeah this would panic if a header key somehow ended up with an empty slice. one-liner fix + test, clean. one thought should the empty-slice key just be skipped entirely (like you did), or should it be set to |
|
Good question. I went with dropping the key rather than mapping it to |
There was a problem hiding this comment.
Pull request overview
This PR hardens the exported helper HeaderToMap in pkg/net/http to avoid a panic when an http.Header contains a key whose value slice is empty (a valid state for map[string][]string). It aligns HeaderToMap with defensive handling expectations for reusable utilities and adds a regression test for the edge case described in issue #4854.
Changes:
- Prevent
HeaderToMapfrom indexingv[0]when a header key has an empty value slice (skip such keys). - Add a table-driven test case covering headers that include an empty value slice entry.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pkg/net/http/http.go | Adds a len(v) > 0 guard to prevent HeaderToMap panics on empty value slices. |
| pkg/net/http/http_test.go | Adds a regression test ensuring keys with empty value slices are skipped. |
Description
HeaderToMapconverted anhttp.Headerto amap[string]stringby indexingv[0]for every key.http.Headerismap[string][]string, so a key with anempty value slice (e.g.
h["X-Empty"] = []string{}) makesv[0]panic withindex out of range [0] with length 0. Such empty entries are preserved byhttp.Header.Clone(). This guards the index withif len(v) > 0, mirroring thedefensive style of the sibling
MapToHeader, and skips valueless keys. Atable-driven test case for a key with an empty value slice is added.
Related Issue
Fixes #4854
Motivation and Context
HeaderToMapis an exported helper in a reusable package. Indexingv[0]unconditionally panics on a valid
http.Headerwhose key has an empty valueslice (legal per the
map[string][]stringshape and kept byClone()). Droppingthose keys instead of crashing keeps the helper safe on any valid input.
The sole production caller currently builds headers via
MapToHeader(map[string]string)(internal/job/image.go), so every key has atleast one value and cannot trigger the panic today. This is a defensive
correctness fix, not a known live crash.
Types of changes
Checklist