Repository navigation
fix(compare): guard short filter keys against index-out-of-range panic - #2905
Open
Tyagiquamar wants to merge 1 commit into
Open
Tyagiquamar wants to merge 1 commit into
Tyagiquamar wants to merge 1 commit into
Conversation
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.
Summary
compare.Comparepanicked withindex out of range [-1]when a filter contained a key shorter than two characters (for example"$"or""). This adds a length guard so malformed filter keys are handled as non-matches instead of crashing the process, plus a regression test.Root Cause & Problem
compare()checks whether a filter key ends with the array-selector token.$by readingkey[slen-1]andkey[slen-2]unconditionally. For a one-character key such as"$",key[slen-2]iskey[-1]and the runtime panics; an empty key panics on the first read. Filter maps reachComparefrom stored subscription filters and from the test-filter path (CompareFlattenedPayloadwithisFlattened=true), so a malformed key can take down the request goroutine.Technical Solution
Guard the trailing-selector check with
slen >= 2. Keys of length 0 or 1 now fall through the normal missing-field path and evaluate to a non-match without an error. Legitimate.$keys still returnErrTrailingDollarOpNotAllowed.Test Plan
Commands executed (Go 1.27.3, Windows, module
github.com/frain-dev/convoy):Compare(map[string]interface{}{"a":"b"}, map[string]interface{}{"$":"x"})failed withpanic: runtime error: index out of range [-1]atcompare.go:46. The temporary test was removed after reproduction.pkg/compare/filter_regression_test.go:TestCompareShortFilterKeysDoNotPaniccovers a single"$"key, an empty key, and preserves theErrTrailingDollarOpNotAllowedcase for"field.$".go test ./pkg/compare/... -count=1->ok github.com/frain-dev/convoy/pkg/compare 1.852s.go vet ./pkg/compare/...-> exit 0.gofmt -lon LF-normalized copies of the two changed files -> no output (clean). The checkout is CRLF on Windows withcore.autocrlf=true, so rawgofmt -lflags line endings only.golangci-lint v2.14.0 run --config=.golangci.yml ./pkg/compare/...-> 1gciissue oncompare.go. The same issue is present on unmodifiedHEAD(a CRLF artifact in this Windows checkout); no new issue is introduced by this change. CI runs golangci-lint v2.9 with--only-new-issuesfor full lint validation.