Skip to content

fix(compare): guard short filter keys against index-out-of-range panic - #2905

Open
Tyagiquamar wants to merge 1 commit into
frain-dev:mainfrom
Tyagiquamar:fix/compare-short-key-panic
Open

Tyagiquamar wants to merge 1 commit into
frain-dev:mainfrom
Tyagiquamar:fix/compare-short-key-panic

Conversation

@Tyagiquamar

Copy link
Copy Markdown

Summary

compare.Compare panicked with index 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 reading key[slen-1] and key[slen-2] unconditionally. For a one-character key such as "$", key[slen-2] is key[-1] and the runtime panics; an empty key panics on the first read. Filter maps reach Compare from stored subscription filters and from the test-filter path (CompareFlattenedPayload with isFlattened=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 return ErrTrailingDollarOpNotAllowed.

Test Plan

Commands executed (Go 1.27.3, Windows, module github.com/frain-dev/convoy):

  • Pre-fix reproduction: a temporary test calling Compare(map[string]interface{}{"a":"b"}, map[string]interface{}{"$":"x"}) failed with panic: runtime error: index out of range [-1] at compare.go:46. The temporary test was removed after reproduction.
  • Post-fix regression test added in pkg/compare/filter_regression_test.go: TestCompareShortFilterKeysDoNotPanic covers a single "$" key, an empty key, and preserves the ErrTrailingDollarOpNotAllowed case 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 -l on LF-normalized copies of the two changed files -> no output (clean). The checkout is CRLF on Windows with core.autocrlf=true, so raw gofmt -l flags line endings only.
  • golangci-lint v2.14.0 run --config=.golangci.yml ./pkg/compare/... -> 1 gci issue on compare.go. The same issue is present on unmodified HEAD (a CRLF artifact in this Windows checkout); no new issue is introduced by this change. CI runs golangci-lint v2.9 with --only-new-issues for full lint validation.

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