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 <maximbel2003@gmail.com>
This commit is contained in:
ZverGuy committed 2026-11-10 04:03:24 +07:00
1 parent 8e491d15dd
commit 3198de893a
3 files changed
+39 -5

No files matched your search

+1 -1
View File
@@ -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)) {
+28 -4
View File
@@ -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 {
@@ -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)`,