From 79ea8561de6f419d52830d3822a331d04c77e7c3 Mon Sep 17 00:00:00 2001 From: ZverGuy Date: Thu, 5 Nov 2026 07:00:58 +0700 Subject: [PATCH] fix(observability-proxy): end a backquoted string only at an unescaped quote The string tracking treated a backslash inside backquotes as literal, but metricsql's scanString lets a backslash escape the closing quote in all three quote kinds, and a quoted label name keeps that escape ({`\``=""} parses). The proxy ended such a string early, read the rest of the query as string content, and accepted an escaped rollup_candlestick after it with the lookup ending too soon. Inside any quote a backslash now skips the next character, as scanString does, and the comment no longer claims metricsql refuses an escaped backquote. Assisted-by: LLM Signed-off-by: ZverGuy --- .../observabilityproxy/metricslookback.go | 9 +++++---- .../observabilityproxy/metricsparams_test.go | 5 +++++ .../observabilityproxy/server_app_test.go | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/internal/observabilityproxy/metricslookback.go b/internal/observabilityproxy/metricslookback.go index b478f582b..3571afc4c 100644 --- a/internal/observabilityproxy/metricslookback.go +++ b/internal/observabilityproxy/metricslookback.go @@ -243,9 +243,10 @@ func scanOffsets(lower string) (back, ahead float64, ok bool) { // rollup\_candlestick or offs\et name what the lexical scan looks for // without its spelling; "#" starts a comment to the end of the line, and // quotes or brackets inside one would mislead the string tracking here and -// the bracket and offset scans. Inside double or single quotes a backslash -// escapes the next character; inside backquotes it is literal, and a -// backquote escaped there makes metricsql refuse the string anyway. +// the bracket and offset scans. Inside any of the three quotes a backslash +// skips the next character, as metricsql's scanString does: an escaped +// closing quote does not end the string, backquotes included (a quoted +// label name keeps such an escape). func unscannableOutsideString(query string) bool { var quote byte for i := 0; i < len(query); i++ { @@ -255,7 +256,7 @@ func unscannableOutsideString(query string) bool { return true case quote == 0 && (c == '"' || c == '\'' || c == '`'): quote = c - case quote != 0 && quote != '`' && c == '\\': + case quote != 0 && c == '\\': i++ case quote != 0 && c == quote: quote = 0 diff --git a/internal/observabilityproxy/metricsparams_test.go b/internal/observabilityproxy/metricsparams_test.go index 34d36f7e5..2cc22a4ae 100644 --- a/internal/observabilityproxy/metricsparams_test.go +++ b/internal/observabilityproxy/metricsparams_test.go @@ -506,6 +506,11 @@ func TestQueryLookahead(t *testing.T) { "x # '\n+ rollup\\_candlestick(y) # '", "x # \"\n+ rollup\\_candlestick(y) # \"", "rate(x[5m]) # comment", + // metricsql lets a backslash escape the closing quote in backquotes + // too (scanString), and quoted label names keep such escapes, so + // a backquoted string must not end at an escaped backquote. + "container_cpu_usage_seconds_total{`\\``=\"\"} or rollup\\_candlestick(container_cpu_usage_seconds_total offset -5m+1h)", + "{`\\`a`} or rollup\\_candlestick(container_cpu_usage_seconds_total offset -5m+1h)", // vmselect evaluates rollup_candlestick one step ahead // (evalRollupFuncWithoutAt), whatever the offset. `rollup_candlestick(x)`, diff --git a/internal/observabilityproxy/server_app_test.go b/internal/observabilityproxy/server_app_test.go index e5c0d3710..ade81ca04 100644 --- a/internal/observabilityproxy/server_app_test.go +++ b/internal/observabilityproxy/server_app_test.go @@ -588,3 +588,22 @@ func TestServerApplicationLookupCoversLatencyOffset(t *testing.T) { t.Fatalf("lookup start = %s, want at most %s", r.form.Get("start"), formatTime(want)) } } + +// TestServerApplicationRefusesBackquoteEscapeTail: an escaped backquote +// inside a quoted label name does not end the string in metricsql, so the +// escaped rollup_candlestick after it must still be refused. +func TestServerApplicationRefusesBackquoteEscapeTail(t *testing.T) { + h := newPolicyHarness(t, Config{}, appMetricsUpstream(nil, nil, http.StatusOK), appPolicy, nil) + for _, query := range []string{ + "container_cpu_usage_seconds_total{`\\``=\"\"} or rollup\\_candlestick(container_cpu_usage_seconds_total offset -5m+1h)", + "{`\\`a`} or rollup\\_candlestick(container_cpu_usage_seconds_total offset -5m+1h)", + } { + q := url.Values{"query": {query}, "time": {unix(testNow.Add(-6 * time.Hour))}} + if got := h.status(http.MethodGet, appRoot+"db.main/metrics/api/v1/query?"+q.Encode(), "good"); got != http.StatusUnprocessableEntity { + t.Errorf("%s answered %d, want 422", query, got) + } + } + if n := len(h.requests()); n != 0 { + t.Fatalf("upstream saw %d requests for refused queries", n) + } +}