Skip to content

Reject malformed and negative indexes in tree paths instead of panicking - #2345

Open
Saadanjum0 wants to merge 1 commit into
getsops:mainfrom
Saadanjum0:fix/tree-path-panics
Open

Saadanjum0 wants to merge 1 commit into
getsops:mainfrom
Saadanjum0:fix/tree-path-panics

Conversation

@Saadanjum0

Copy link
Copy Markdown

Problem

The path argument of --extract, sops set and sops unset crashes sops with a Go panic for some malformed inputs instead of reporting an error:

$ sops decrypt --extract '[]' file.yaml
panic: runtime error: index out of range [0] with length 0
$ sops decrypt --extract '["]' file.yaml
panic: runtime error: slice bounds out of range [1:0]
$ sops decrypt --extract '["list"][-1]' file.yaml
panic: reflect: slice index out of range
$ sops set file.yaml '["list"][-1]' '5'
panic: runtime error: index out of range [-1]
$ sops unset file.yaml '["list"][-1]'
panic: slice bounds out of range [-1::]

Cause

parseTreePath in cmd/sops/main.go indexes component[0] and slices component[1:len-1] without checking the component is long enough, and accepts negative numbers, which the tree code in sops.go then uses directly as a slice index (the bounds checks there only look at the upper bound).

Fix

parseTreePath is the single place all of these commands parse paths, so it now returns an error for an empty component, a one-character quoted component, and a negative index.

Testing

  • Added TestParseTreePath and TestParseTreePathInvalid in cmd/sops/main_test.go. The invalid-path test panics on main without the change and passes with it.
  • go test ./...: everything passes except hcvault and kms (need Docker, not available here) and pgp (TestPGP, no GnuPG test key in the keyring here). Those three are unrelated to this change.
  • Also checked manually with the built binary against an age-encrypted YAML file, before the change (panics above).

Related: #2344 also changes index handling (in sops.go, not parseTreePath). It would turn a negative index into an out-of-range error, but does not cover [] or ["]. The two changes do not conflict.

parseTreePath, which handles the path argument of --extract, set and unset,
panicked for an empty component ([]), a lone quote (["]) and negative
indexes ([-1]). Return an error for these instead.

Signed-off-by: Saadanjum0 <160885369+Saadanjum0@users.noreply.github.com>
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.

1 participant