Skip to content

Commit 4052e05

Browse files
committed
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 <[email protected]>
1 parent b9dce01 commit 4052e05

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

‎internal/observabilityproxy/metricslookback.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,10 @@ func scanOffsets(lower string) (back, ahead float64, ok bool) {
245245
// rollup\_candlestick or offs\et name what the lexical scan looks for
246246
// without its spelling; "#" starts a comment to the end of the line, and
247247
// quotes or brackets inside one would mislead the string tracking here and
248-
// the bracket and offset scans. Inside double or single quotes a backslash
249-
// escapes the next character; inside backquotes it is literal, and a
250-
// backquote escaped there makes metricsql refuse the string anyway.
248+
// the bracket and offset scans. Inside any of the three quotes a backslash
249+
// skips the next character, as metricsql's scanString does: an escaped
250+
// closing quote does not end the string, backquotes included (a quoted
251+
// label name keeps such an escape).
251252
func unscannableOutsideString(query string) bool {
252253
var quote byte
253254
for i := 0; i < len(query); i++ {
@@ -257,7 +258,7 @@ func unscannableOutsideString(query string) bool {
257258
return true
258259
case quote == 0 && (c == '"' || c == '\'' || c == '`'):
259260
quote = c
260-
case quote != 0 && quote != '`' && c == '\\':
261+
case quote != 0 && c == '\\':
261262
i++
262263
case quote != 0 && c == quote:
263264
quote = 0

‎internal/observabilityproxy/metricsparams_test.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,11 @@ func TestQueryLookahead(t *testing.T) {
506506
"x # '\n+ rollup\\_candlestick(y) # '",
507507
"x # \"\n+ rollup\\_candlestick(y) # \"",
508508
"rate(x[5m]) # comment",
509+
// metricsql lets a backslash escape the closing quote in backquotes
510+
// too (scanString), and quoted label names keep such escapes, so
511+
// a backquoted string must not end at an escaped backquote.
512+
"container_cpu_usage_seconds_total{`\\``=\"\"} or rollup\\_candlestick(container_cpu_usage_seconds_total offset -5m+1h)",
513+
"{`\\`a`} or rollup\\_candlestick(container_cpu_usage_seconds_total offset -5m+1h)",
509514
// vmselect evaluates rollup_candlestick one step ahead
510515
// (evalRollupFuncWithoutAt), whatever the offset.
511516
`rollup_candlestick(x)`,

‎internal/observabilityproxy/server_app_test.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,22 @@ func TestServerApplicationLookupCoversLatencyOffset(t *testing.T) {
588588
t.Fatalf("lookup start = %s, want at most %s", r.form.Get("start"), formatTime(want))
589589
}
590590
}
591+
592+
// TestServerApplicationRefusesBackquoteEscapeTail: an escaped backquote
593+
// inside a quoted label name does not end the string in metricsql, so the
594+
// escaped rollup_candlestick after it must still be refused.
595+
func TestServerApplicationRefusesBackquoteEscapeTail(t *testing.T) {
596+
h := newPolicyHarness(t, Config{}, appMetricsUpstream(nil, nil, http.StatusOK), appPolicy, nil)
597+
for _, query := range []string{
598+
"container_cpu_usage_seconds_total{`\\``=\"\"} or rollup\\_candlestick(container_cpu_usage_seconds_total offset -5m+1h)",
599+
"{`\\`a`} or rollup\\_candlestick(container_cpu_usage_seconds_total offset -5m+1h)",
600+
} {
601+
q := url.Values{"query": {query}, "time": {unix(testNow.Add(-6 * time.Hour))}}
602+
if got := h.status(http.MethodGet, appRoot+"db.main/metrics/api/v1/query?"+q.Encode(), "good"); got != http.StatusUnprocessableEntity {
603+
t.Errorf("%s answered %d, want 422", query, got)
604+
}
605+
}
606+
if n := len(h.requests()); n != 0 {
607+
t.Fatalf("upstream saw %d requests for refused queries", n)
608+
}
609+
}

0 commit comments

Comments
 (0)