Skip to content

fix: reject out-of-range list indexes in --state-values-set - #2887

Merged
yxxhero merged 1 commit into
helmfile:mainfrom
lbarrosby:fix/state-values-set-negative-index
Nov 11, 2026
Merged

yxxhero merged 1 commit into
helmfile:mainfrom
lbarrosby:fix/state-values-set-negative-index

Conversation

@lbarrosby

Copy link
Copy Markdown
Contributor

--state-values-set and --state-values-set-string crash on a negative list index such as servers[-1].host=a. Both end in maputil.Set, which panics:

--- FAIL: TestZZRepro (0.00s)
panic: runtime error: index out of range [-1] [recovered, repanicked]

(Set(m, ParseKey("foo[-1]"), "x", false) on main.)

getCursor parses [-1] as index -1 and indexedKeyArg.getArray then indexes the slice with it. A large index such as servers[100000000000]=a goes the other way and makes getArray allocate a slice of that length. setAssignment was added to return errors instead of panicking on malformed assignments, but maputil.Set had no way to report this case.

Changes:

  • maputil.Set now returns an error. It checks every key segment first and rejects an index below 0 or above maputil.MaxIndex (65536, the same limit and wording as Helm's strvals for --set), so the map is not left half-updated.
  • parseStateValuesSet / parseStateValuesSetString wrap that error as --state-values-set: invalid assignment "<assignment>": ..., the format used for the other malformed-assignment errors.

Tests:

  • pkg/maputil: TestSet_RejectsOutOfRangeIndex (negative index at the first and a nested segment, index above the limit; map unchanged) and TestSet_AcceptsMaxIndex. Existing Set calls in the tests now check the returned error.
  • pkg/config: new cases in TestNewCLIConfigImplStateValuesSet and TestNewCLIConfigImplStateValuesSetString.
$ go test ./pkg/maputil/ ./pkg/config/ -count=1
ok  github.com/helmfile/helmfile/pkg/maputil
ok  github.com/helmfile/helmfile/pkg/config
$ go vet ./pkg/maputil/ ./pkg/config/ && golangci-lint run ./pkg/maputil/... ./pkg/config/...
0 issues.

A negative index such as foo[-1]=x panicked with index out of range, and
a very large one tried to allocate the whole list. Set now validates
every index before changing the map, with the same 65536 limit as Helm's
--set, and both flags report the error.

Signed-off-by: Lucas Barros <[email protected]>
@yxxhero
yxxhero merged commit 12a197c into helmfile:main Nov 11, 2026
17 checks passed
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.

2 participants