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 <maximbel2003@gmail.com>
This commit is contained in:
ZverGuy committed 2026-11-10 04:03:24 +07:00
1 parent ac5a3c88c4
commit 79ea8561de
3 files changed
+29 -4

No files matched your search

@@ -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
@@ -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)`,
@@ -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)
}
}