fix: sanitize metric label values to prevent panic on invalid UTF-8#233
Open
insider89 wants to merge 1 commit into
Open
fix: sanitize metric label values to prevent panic on invalid UTF-8#233insider89 wants to merge 1 commit into
insider89 wants to merge 1 commit into
Conversation
A malformed public request whose URI contains non-UTF-8 bytes (e.g.
`GET /\xc0`) produces a REST request method of "GET#/\xc0". That method
is used verbatim as a Prometheus label value in the request flow and
dimension metrics. client_golang panics on invalid-UTF-8 label values,
and the panic happens inside the detached goroutine in
processRequest with no recover, so it takes down the whole process
(exit 2). Because the endpoint is public, any client can trigger a
crash loop.
Add utils.SanitizeMetricLabel, which leaves valid UTF-8 untouched and
replaces invalid byte sequences with the Unicode replacement character,
and apply it to every label value derived from request.Method():
- internal/upstreams/flow/execution_flow.go (requestTotal, requestErrors)
- internal/upstreams/flow/request_processor.go (hedge)
- internal/dimensions/dims.go (request total/errors/retries/duration,
sanitized once where the dimension key is built)
The fix is confined to the metric boundary, so request routing,
spec-method resolution and upstream forwarding are unchanged.
Stack from production:
panic: label value "GET#/\xc0" is not valid UTF-8
prometheus.(*CounterVec).WithLabelValues
flow.(*BaseExecutionFlow).processRequest.func1
internal/upstreams/flow/execution_flow.go
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.
Problem
A malformed public request whose URI contains non-UTF-8 bytes (e.g.
GET /\xc0) is turned into a REST request method string of the form"<VERB>#<path>", i.e."GET#/\xc0". That method string is then used verbatim as a Prometheus label value.client_golangpanics when a label value is not valid UTF-8:The panic happens inside the detached goroutine spawned in
processRequest, which has norecover(), so it tears down the whole process (exit 2). Because the REST endpoint is public, any client can send a single malformed request and trigger a crash loop.Affected label sites
Every label value derived from
request.Method():internal/upstreams/flow/execution_flow.go—requestTotalMetric,requestErrorsMetricinternal/upstreams/flow/request_processor.go—hedgeMetricinternal/dimensions/dims.go— request total/errors/retries/duration (via the dimension key)Fix
Add
utils.SanitizeMetricLabel, which returns valid UTF-8 unchanged and replaces invalid byte sequences with the Unicode replacement character (U+FFFD), and apply it to every label value derived fromrequest.Method().The change is confined to the metric boundary — request routing, spec-method resolution and upstream forwarding all still see the original, unmodified method string, so there is no behavioral change for legitimate traffic.
Tests
pkg/utils/strings_test.go:GET#/+0xC0) becomes valid UTF-8CounterVec.WithLabelValuescall with the sanitized value does not panic (direct regression for the crash)go build ./...andgo test ./pkg/utils/ ./internal/upstreams/flow/ ./internal/dimensions/pass.