TraceQL filters can add LogsQL pipes and escape their scope

perfloop-oss/victoriatraces · Unclassified

https://perfloop.ai/t/oss/case_zcg62mvrkx

Verdict

VERIFIED · settled 2026-11-23

What happened: The check fails before this change and passes with it.

Hypothesis

Caller-supplied TraceQL text is copied into the LogsQL string in places where it should only supply a value, so a filter can add pipes or move a branch outside the filters that are supposed to confine it. The submitter reports that Tempo API callers can thereby alter query structure, bypass stream boundaries and terminate the server. Parser-level probes at this revision confirm the structural escapes; production exposure, cross-tenant reach and the claimed live process termination are unconfirmed and belong to the Case.

The duration value is the clearest case. `Query.String` at lib/traceql/parser.go:25-47 converts a `traceDuration` value with `tryParseDuration` and, when that fails, interpolates the raw string into `AND duration :<op> <value>` inside the generated `join` subquery. Quoted TraceQL tokens pass the lexer verbatim (`nextCompoundToken`, parser.go:672-678), and `HasNonHintPipe` (parser.go:60-72) inspects only parsed TraceQL pipes, so the guard at tempo.go:562-565 never sees the injected text. `go test ./lib/traceql -run TestProbeDurationInjection` (probe) prints `* | join by (trace_id) ({trace_id_idx_stream!=""} AND duration :> 5s | fields trace_id, span_id | fields trace_id_idx | rename trace_id_idx as trace_id) inner` for the value `5s | fields trace_id, span_id`. On the accepted search path the same value with a trailing `#` comment produces, after `logstorage.ParseQueryAtTimestamp`, the valid query `{"resource_attr:service.name"!=""} | join by (trace_id) ({trace_id_idx_stream!=""} duration:>5s) inner | fields trace_id, span_id`; the comment removes the deduplication and sort pipes and the `_time` column that `GetTraceList` later requires (query.go:117-119). A value of `5s or duration:>1` likewise escapes the subquery's `trace_id_idx_stream!=""` restriction.

The tag endpoints share the same owner. `searchTags` and `searchTagValues` (tempo.go:422-424 and 493-495) append their pipes after `Query.String()`, so the same payload with `#` leaves `| fields trace_id, span_id` as the final pipe. `singleFieldQueryHelper` (tempo.go:527-533) calls `logger.Panicf` when a returned block has more than one column; at this revision `Panicf` panics for text log format (vendor .../lib/logger/logger.go:323-331) and `httpserver.handlerWrapper` turns that into `os.Exit(1)` (vendor .../lib/httpserver/httpserver.go:354-365), or exits `-1` directly with json log format. Whether two columns actually arrive for live span data is not measured here.

The Boolean scope case is source-evident. `(*filterOr).String` (lib/traceql/filter_or.go:14-22) joins branches with ` or ` and adds no parentheses. `filterAnd.String` parenthesizes a nested OR, but a top-level OR is concatenated directly after `{resource_attr:service.name!=""} AND ` in `getTraceIDList` (query.go:124), and LogsQL parses `and` tighter than `or` (vendor .../lib/logstorage/parser.go:2128-2168). A probe over `{ span_attr:a = "1" } || { span_attr:b = "2" }` reparses as `{"resource_attr:service.name"!=""} "span_attr:a":=1 or "span_attr:b":=2`, so the second branch is not confined by the base stream filter. What data an escaped branch can actually reach needs measurement; source alone does not show cross-tenant access. Existing golden expectations at lib/traceql/parser_test.go:69 encode the unparenthesized output and would be updated by the repair.

The metrics translation has the same raw-interpolation shape. `metricsToLogsQLStats` (metrics_query.go:148-167) writes each quantile string straight into `quantile(<q>, <field>)`, and `parsePipeQuantileOverTime` (pipe_metrics.go:146-163) keeps arguments as raw strings. `go test ./app/vtselect/traces/tempo -run TestProbeMetricsEndToEnd` shows that `{} | quantile_over_time(duration, "0.5, duration) as value | stats count() as x #")` translates to the parseable query `... | stats quantile(0.5, duration) as value | stats count() as x`, and the same string is returned as the `p` label value. `quoteLogsQLField` (metrics_query.go:181-188) escapes `"` but not backslashes, so the field name `a:"b\` produces `* | stats by ("a:\"b\") count() as value`, which the LogsQL parser rejects. Ordinary filter values already go through `strconv.Quote` (parser.go:820-825), so the raw-interpolation class is specific to the duration branch plus these two metrics helpers. These sites are outside the trace-search hop but share the translation layer the submitter reported.

Upstream overlap: VictoriaTraces PR 320 (open) implements the OR-parenthesization repair plus a metrics regression, PR 333 (open) changes trace-duration handling inside OR branches, and PR 322 (open) preserves quoted attribute strings. None of them changes `Query.String`'s join interpolation, quantile validation or `quoteLogsQLField`, so the validation and escaping work remains distinct, while the OR repair overlaps PR 320 and should be coordinated rather than repeated silently.

The proof target is a deterministic regression test that rejects or safely quotes a non-duration trace value, a non-numeric or out-of-range quantile and a backslash-bearing field name, and that parenthesizes a top-level OR before concatenation. Any test placement is provisional; the smallest next check asserts that the generated LogsQL for those payloads keeps one value in one filter position and one output column for the tag endpoints.

Change to test: Keep caller text in value position: reject unparseable or non-numeric trace-duration values before building the generated duration filter, validate quantile_over_time arguments as finite numbers in [0, 1] before emitting quantile(...), quote LogsQL field names with LogsQL's own escaping so backslashes, newlines, quotes and delimiters round-trip, and parenthesize OR output so its branches stay inside the enclosing stream and filter scope. Add regression tests for each payload and for a top-level OR, including a check that the generated query cannot gain extra pipes or lose its single output column.

Where it lives

perfloop-oss/victoriatraces · app/vtselect/traces/tempo/tempo.go

Evidence

The check fails before this change and passes with it: `For every TraceQL query, a traceDuration comparison value is written into the generated join subquery as one normalized number: a duration or a finite number is converted, and any other value is rejected at parse time, so it cannot add LogsQL pipes or filters there.`

The check fails before this change and passes with it: `For every TraceQL metrics query, a quantile_over_time argument that is not a finite number in [0, 1] is rejected before it is emitted into quantile(), so it cannot add LogsQL pipes or stats expressions.`

The check fails before this change and passes with it: `For every TraceQL metrics query, a by() field name that is not a plain LogsQL word is written in Go-quoted form, so a backslash, quote, newline, wildcard or keyword in the name cannot end the quoted name or add syntax to the generated LogsQL.`

The check fails before this change and passes with it: `For every TraceQL query with a top-level OR filter, the trace ID search query wraps the OR in parentheses before appending it to the {resource_attr:service.name!=""} AND stream filter, so both branches stay inside that filter and the result columns are unchanged.`

The check fails before this change and passes with it: `For every TraceQL metrics query with a top-level OR filter, the generated LogsQL wraps the OR in parentheses before appending it to the {resource_attr:service.name!=""} AND stream filter, so both branches stay inside that filter.`

Timeline