fix(observability-proxy): refuse comments outside strings on the app root

The escape check tracked string literals but not comments: in
metricsql a "#" outside a string comments out the rest of the line, so
a quote inside a comment made the check treat the next line's code as
string content, and rollup\_candlestick there was accepted again. The
bracket and offset scans do not understand comments either.

A "#" outside a string now makes the lookahead unknown as well, which
answers 422 on the application root, rather than teaching every scan
about comments; API clients do not send them.

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 a54760fd9c
commit cf45a23bf3
3 files changed
+19 -11

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, 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")
return nil, errorf(http.StatusUnprocessableEntity, "the query reads samples at a time the application scope cannot cover (an @ anywhere in the query, a backslash or a comment 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)) {
+13 -10
View File
@@ -145,12 +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, and a backslash outside a string, which can spell an
// identifier the scan looks for differently. vmselect also evaluates
// several times, and a backslash or a comment outside a string, which can
// hide what the scan looks for. 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, "@") || escapeOutsideString(query) {
if strings.Contains(query, "@") || unscannableOutsideString(query) {
return 0, false
}
lower := strings.ToLower(query)
@@ -238,17 +238,20 @@ 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 {
// unscannableOutsideString reports a backslash or a "#" outside a string
// literal. metricsql unescapes identifiers (unescapeIdent), so
// 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.
func unscannableOutsideString(query string) bool {
var quote byte
for i := 0; i < len(query); i++ {
c := query[i]
switch {
case quote == 0 && c == '\\':
case quote == 0 && (c == '\\' || c == '#'):
return true
case quote == 0 && (c == '"' || c == '\'' || c == '`'):
quote = c
@@ -501,6 +501,11 @@ func TestQueryLookahead(t *testing.T) {
`rollup_candle\stick(x)`,
`rollup\x5fcandlestick(x)`,
`x offs\et -1h`,
// A comment runs to the end of the line, so quotes in it would
// confuse the string tracking and brackets in it the scans.
"x # '\n+ rollup\\_candlestick(y) # '",
"x # \"\n+ rollup\\_candlestick(y) # \"",
"rate(x[5m]) # comment",
// vmselect evaluates rollup_candlestick one step ahead
// (evalRollupFuncWithoutAt), whatever the offset.
`rollup_candlestick(x)`,