Skip to content

querier: fix active query tracker panic on UTF-8 continuation bytes (fixes #7729) - #7771

Open
mehrdadbn9 wants to merge 1 commit into
cortexproject:masterfrom
mehrdadbn9:fix/request-tracker-continuation-bytes-panic
Open

querier: fix active query tracker panic on UTF-8 continuation bytes (fixes #7729)#7771
mehrdadbn9 wants to merge 1 commit into
cortexproject:masterfrom
mehrdadbn9:fix/request-tracker-continuation-bytes-panic

Conversation

@mehrdadbn9

Copy link
Copy Markdown
Contributor

Summary

Fixes #7729.

trimStringByBytes (pkg/util/request_tracker/request_extractor.go) scans backwards from the truncation point to find a UTF-8 rune start, but the loop had no lower bound. When a request field consists only of UTF-8 continuation bytes (0x80–0xBF) there is no rune start to land on, so size underflows past zero and bytesStr[-1] panics with index out of range [-1]. Since the active query tracker is enabled by default and the panic occurs on a goroutine without recover(), an attacker-supplied match[]/query value can crash the querier (crash-loop on repeat).

The fix bounds the scan with size > 0. When no rune start exists, the field is truncated to an empty string, which is safe and still produces valid JSON.

Verification

  • Added TestTrimForJsonMarshalContinuationBytes with 1200 continuation bytes (\x80) truncated to size 900.
  • Confirmed RED before the fix: runtime error: index out of range [-1].
  • Confirmed GREEN after the fix; full pkg/util/request_tracker suite passes.
  • gofmt -l clean on changed files.
go test ./pkg/util/request_tracker/ -run TestTrimForJsonMarshalContinuationBytes -count=1
go test ./pkg/util/request_tracker/ -count=1

This is the same class of panic #7640 addressed in trimForJsonMarshalRecursive; that PR added a repeatSize <= 0 guard but did not touch the underflow inside trimStringByBytes, and its regression tests used only valid multi-byte UTF-8 () which always terminates the backwards scan at index 0.

Signed-off-by: ...

trimStringByBytes scans backwards from the truncation point to find a
UTF-8 rune start, but the loop had no lower bound. When a request field
consists only of UTF-8 continuation bytes (0x80-0xBF) there is no rune
start to land on, so size underflows past zero and bytesStr[-1] panics
with 'index out of range [-1]'. Because the active query tracker is
enabled by default and the panic happens on a goroutine without recover(),
an attacker-supplied match[]/query value can crash the querier.

Bound the scan with 'size > 0' so it stops at the start of the string
instead of underflowing. When no rune start exists the field is truncated
to an empty string, which is safe.

Add TestTrimForJsonMarshalContinuationBytes covering the all-continuation-
byte case; existing multi-byte tests use valid UTF-8 ('世') which always
terminates the scan at index 0 and therefore never exercised the underflow.

Fixes cortexproject#7729

Signed-off-by: ...
Signed-off-by: Mehrdad Biukian Naeini <mehrdad.biu@mtnirancell.ir>
@dosubot dosubot Bot added go Pull requests that update Go code type/bug type/security labels Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update Go code size/S type/bug type/security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Querier crash: invalid UTF-8 in match[]/query panics the active request tracker (index out of range [-1])

1 participant