jp: bounds-check the index in the at() function#593
Open
arpitjain099 wants to merge 1 commit into
Open
Conversation
jpfAt guards both argument type assertions with comma-ok but then does slice[int(index)] with no bounds check, so an out-of-range or negative index panics. The array argument can be payload-derived, so a crafted assertion expression like at(@, `999`) against a shorter array crashes the evaluation. Return an error for an out-of-range or negative index, which the engine already handles, instead of indexing blindly. Adds tests for the out-of-range and negative cases. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explanation
The
atjmespath function can panic with an index-out-of-range when an assertion expression requests an index outside the array's bounds (or a negative index). Because the array argument can be derived from the payload being evaluated, a crafted assertion expression can crash the assertion evaluation. This PR is a bug fix that turns that panic into a normal error the engine already handles.Related issue
n/a (no existing issue; found by code review). Happy to open a tracking issue if you prefer.
Milestone of this PR
n/a
What type of PR is this
/kind bug
Proposed Changes
jpfAtinpkg/jp/functions/at.goguards both of its argument type assertions with comma-ok, but then indexes the slice withslice[int(index)]without a bounds check, so an out-of-range or negative index panics. A crafted assertion expression such asat(@, `999`)(orat(@, `-1`)) evaluated against a shorter array crashes the assertion evaluation, and there is no recover on that path. This returns an error for an out-of-range or negative index, which the engine already surfaces, instead of indexing blindly.Proof Manifests
This is an internal robustness fix to the JMESPath
atfunction rather than a policy-behavior change, so there are no Kubernetes/Kyverno manifests to attach. The fix is covered by unit tests inpkg/jp/functions/at_test.gothat assert an out-of-range index and a negative index each return an error instead of panicking. Before the fix, evaluatingat(@, `999`)against a two-element array panics; after, it returnsindex out of range: 999 (array length 2).Checklist
Further Comments
The sibling functions (
concat,wildcard,json_parse) all guard their argument access; onlyatindexed unchecked, so this brings it in line with them.