From 3198de893ad0869995111f8879a62a3155e14225 Mon Sep 17 00:00:00 2001 From: ZverGuy Date: Thu, 5 Nov 2026 06:37:04 +0700 Subject: [PATCH] fix(observability-proxy): refuse escapes outside strings on the app root metricsql unescapes identifiers (unescapeIdent in parser.go), so rollup\_candlestick(x + y) is rollup_candlestick to vmselect while the lexical scan saw another name: the lookup ended ten minutes past the evaluation time and vmselect read nearly thirteen. Any escaped keyword the scan looks for, offset included, had the same hole. A backslash outside a string literal now makes the scan unknown, which answers 422 on the application root; escapes inside label values are unaffected. With rollup_candlestick refused, two steps per "(" stay enough: a subquery evaluates at most one step past its end plus the alignment of that end to the step (evalRollupFuncWithSubquery), and the one step a candlestick over a non-selector would add on top cannot occur. Assisted-by: LLM Signed-off-by: ZverGuy --- internal/observabilityproxy/appscope.go | 2 +- .../observabilityproxy/metricslookback.go | 32 ++++++++++++++++--- .../observabilityproxy/metricsparams_test.go | 10 ++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/internal/observabilityproxy/appscope.go b/internal/observabilityproxy/appscope.go index 4918c9588..2f0f23751 100644 --- a/internal/observabilityproxy/appscope.go +++ b/internal/observabilityproxy/appscope.go @@ -103,7 +103,7 @@ func applicationSelectors(ns, kind, name string, pods, pvcs []string) []string { // application's series. func (s *Server) applicationScope(ctx context.Context, r *http.Request, store string, req metricsRequest, ns, kind, name string) ([]string, *HTTPError) { if req.last.IsZero() { - return nil, errorf(http.StatusUnprocessableEntity, "the query reads samples at a time the application scope cannot cover (an @ anywhere in the query, rollup_candlestick, WITH with a negative offset, an offset that is not a plain duration, or a subquery step that is not one); use the namespace root") + return nil, errorf(http.StatusUnprocessableEntity, "the query reads samples at a time the application scope cannot cover (an @ anywhere in the query, a backslash outside a string, rollup_candlestick, WITH with a negative offset, an offset that is not a plain duration, or a subquery step that is not one); use the namespace root") } lim := s.cfg.Metrics.App if req.start.IsZero() || req.start.Before(req.from.Add(-lim.Lookback)) { diff --git a/internal/observabilityproxy/metricslookback.go b/internal/observabilityproxy/metricslookback.go index 55e78a0c2..7f936307f 100644 --- a/internal/observabilityproxy/metricslookback.go +++ b/internal/observabilityproxy/metricslookback.go @@ -145,11 +145,12 @@ func queryLookback(query string, step time.Duration) (time.Duration, bool) { // lookback, every "(" counts as a possible implicit subquery and each // term uses the largest step seen. Any @ makes the bound unknown, as it // does for queryLookback, and so does WITH, which can apply one offset -// several times. vmselect also evaluates rollup_candlestick one step ahead -// of the evaluation time (evalRollupFuncWithoutAt), so it makes the bound -// unknown too. +// several times, and a backslash outside a string, which can spell an +// identifier the scan looks for differently. vmselect also evaluates +// rollup_candlestick one step ahead of the evaluation time +// (evalRollupFuncWithoutAt), so it makes the bound unknown too. func queryLookahead(query string, step time.Duration) (time.Duration, bool) { - if strings.Contains(query, "@") { + if strings.Contains(query, "@") || escapeOutsideString(query) { return 0, false } lower := strings.ToLower(query) @@ -237,6 +238,29 @@ func scanOffsets(lower string) (back, ahead float64, ok bool) { } } +// escapeOutsideString reports a backslash outside a string literal. +// metricsql unescapes identifiers (unescapeIdent), so rollup\_candlestick +// or offs\et name what the lexical scan looks for without its spelling. +// Inside double or single quotes a backslash escapes the next character; +// inside backquotes it is literal. +func escapeOutsideString(query string) bool { + var quote byte + for i := 0; i < len(query); i++ { + c := query[i] + switch { + case quote == 0 && c == '\\': + return true + case quote == 0 && (c == '"' || c == '\'' || c == '`'): + quote = c + case quote != 0 && quote != '`' && c == '\\': + i++ + case quote != 0 && c == quote: + quote = 0 + } + } + return false +} + // isKeyword reports whether lower[start:end] stands on its own rather than // inside an identifier. func isKeyword(lower string, start, end int) bool { diff --git a/internal/observabilityproxy/metricsparams_test.go b/internal/observabilityproxy/metricsparams_test.go index 0fbbcd7a9..cf452a74f 100644 --- a/internal/observabilityproxy/metricsparams_test.go +++ b/internal/observabilityproxy/metricsparams_test.go @@ -444,6 +444,9 @@ func TestQueryLookahead(t *testing.T) { {`rate(x[5m] offset -30m) offset -1h`, 92 * time.Minute}, {`x offset -1h + y offset 2h`, time.Hour}, {`node_offset_seconds - 1`, 0}, + // An escape inside a string literal is only part of a label value. + {`x{a=~"a\\.b"}`, 0}, + {`x{a="\"rollup\\_candlestick\""}`, 0}, // metricsql lexes a duration as one token, so a sign with spaces, // or a plus, is an operator after the offset, not part of it. {`x offset 1h+30m`, 0}, @@ -491,6 +494,13 @@ func TestQueryLookahead(t *testing.T) { `rate(x[1h`, `max_over_time(x[1h:100y])`, `rate(x[5m] offset 1h-30m)`, + // metricsql unescapes identifiers (unescapeIdent), so an escaped + // rollup_candlestick, or any escape outside a string, could hide + // what the scan looks for. + `rollup\_candlestick(x + y)`, + `rollup_candle\stick(x)`, + `rollup\x5fcandlestick(x)`, + `x offs\et -1h`, // vmselect evaluates rollup_candlestick one step ahead // (evalRollupFuncWithoutAt), whatever the offset. `rollup_candlestick(x)`,