fix(observability-proxy): accept only a token end after an offset

metricsql also reads a trailing $__interval as part of an offset's
duration: offset -1h$__interval is -(1h + step). The scan stopped at
the "$" and read one hour, so the lookup missed pods created in the
last step and the query answered 200 without them. Refusing a minus
alone left every other continuation open.

Only what certainly ends the duration token is now accepted after it:
whitespace, a closing bracket or separator, or a binary operator other
than minus; anything else answers 422 on the application root. The
grammar comment no longer claims upper-case offset units are refused,
as offsets are scanned lower-cased like metricsql reads 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 054fac6ab0
commit 8e491d15dd
3 files changed
+18 -6

No files matched your search

+13 -6
View File
@@ -41,14 +41,20 @@ const latencyOffset = 30 * time.Second
// queryDurationPart is the subset of the MetricsQL duration grammar
// (scanSingleDuration in metricsql) the scan understands. Step multiples
// (5i, $__interval), negative parts and upper-case units are left out on
// purpose: anything else makes the bound unknown.
// (5i, $__interval) and negative parts are left out on purpose: anything
// else makes the bound unknown. Offsets are scanned lower-cased, as
// metricsql reads units in any case; windows and steps in brackets are
// not, so an upper-case unit there is unknown.
var (
queryDurationPart = regexp.MustCompile(`(\d+(?:\.\d+)?)(ms|s|m|h|d|w|y)`)
queryDuration = regexp.MustCompile(`^(?:\d+(?:\.\d+)?(?:ms|s|m|h|d|w|y))+$`)
querySeconds = regexp.MustCompile(`^\d+(?:\.\d+)?$`)
)
// offsetTerminators may follow an offset's duration: whitespace, a
// closing bracket or separator, or a binary operator other than minus.
const offsetTerminators = " \t\n\r)]},+*/^%<>=!"
var queryDurationUnits = map[string]time.Duration{
"ms": time.Millisecond, "s": time.Second, "m": time.Minute, "h": time.Hour,
"d": 24 * time.Hour, "w": 7 * 24 * time.Hour, "y": 365 * 24 * time.Hour,
@@ -212,10 +218,11 @@ func scanOffsets(lower string) (back, ahead float64, ok bool) {
// metricsql lexes 1h-30m as one duration whose parts after the
// first negative one all subtract (DurationValue), so -1h-30m is
// -90m and 1h-2h is -1h; the scan does not read such compounds.
// metricsql continues a duration after a minus in more forms than
// the scan can tell from a subtraction (-1.h and -.5h among them),
// so any minus right after the duration is refused.
if end < len(arg) && arg[end] == '-' {
// metricsql continues a duration token in more forms than the scan
// reads: after a minus (-30m, -1.h, -.5h) and with a trailing
// $__interval, all summed by DurationValue. Only what certainly
// ends the token is accepted after the duration.
if end < len(arg) && !strings.ContainsRune(offsetTerminators, rune(arg[end])) {
return 0, 0, false
}
d, ok := parseQueryDuration(arg[:end])
@@ -480,6 +480,10 @@ func TestQueryLookahead(t *testing.T) {
`x offset 5m-.5h`,
`x offset 1h-1`,
`x offset 5m-y`,
// metricsql also reads a trailing $__interval as part of the
// duration: -1h$__interval is -(1h + step).
`x offset -1h$__interval`,
`rate(x[5m]) offset -1h$__interval`,
// A subquery step the scan cannot read, an unclosed bracket, and a
// sum past the duration range.
`rate(x[1h:5i])`,
@@ -523,6 +523,7 @@ func TestServerApplicationRefusesForwardReads(t *testing.T) {
for _, query := range []string{
"container_cpu_usage_seconds_total offset -1h-30m",
"container_cpu_usage_seconds_total offset 1h-2h",
"container_cpu_usage_seconds_total offset -1h$__interval",
"rollup_candlestick(container_cpu_usage_seconds_total)",
} {
q := url.Values{"query": {query}, "time": {unix(testNow.Add(-2 * time.Hour))}}