mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-15 12:58:33 +00:00
A telemetry field key name arrives as JSON on /api/v5/query_range and is
replayed out of stored dashboards and alert rules. It lands in two syntactic
positions and a bound parameter can occupy NEITHER:
- a backtick-quoted IDENTIFIER — the SELECT alias, the GROUP BY term, the
ORDER BY term;
- a single-quoted LITERAL — the accessors that read one attribute out of a
column, JSONExtractString(labels, '<name>') and friends.
Nothing quoted it. The rendering was written inline about forty times, always
as "`"+name+"`" or '%s', and the one call that sat next to a name —
sqlbuilder.Escape — replaces "$" with "$$" for the builder's own placeholder
syntax and is not SQL escaping. Measured against the real builder, a group-by
name of
le`,(select/**/grouparray(name)/**/from/**/system.tables)/**/as/**/`x
emitted that subquery as an ADDITIONAL SELECT COLUMN, whose rows come back in
the response body; "/**/" stands in for the spaces the builder strips and the
payload is all-lowercase so nothing folds it. A name carrying "'" breaks the
labels accessor the same way, and one ending in "\" eats whichever delimiter
closes it.
The request validator is not where this can be fixed. The rule evaluator builds
a QueryRangeRequest and calls the querier directly, so a stored rule's name
never meets it; and validateOrderBy, which does gate the DIRECTION strictly to
asc|desc, gates the sibling key NAME against an allowlist it builds out of the
caller's own GroupBy — so a name repeated in both fields validates itself.
Metrics settles it: a metric label is free-form, an unrecognised name falls back
to the labels column BY DESIGN, and there is no key allowlist to fail against.
So the guard goes where the name becomes text. QuoteIdent and EscapeLiteral are
the one renderer, in querybuilder, which every signal package already imports;
the ~40 inline spellings now call them. Both escape the backslash BEFORE the
delimiter, or a trailing backslash would consume the delimiter being added. A
name with none of those characters — every real attribute key, which is a dotted
identifier — renders byte for byte as it did, which is why the golden-SQL suites
in all six signal packages are unchanged.
The test asserts on emitted SQL and structurally rather than textually: the
payload MAY appear, quoted, as the inert name of a label nobody has, but it must
never appear in the executable skeleton — the text left once every quoted region
is removed. That is the property an injection breaks, so it fails if any layer
underneath stops escaping.
Co-authored-by: Hanzo Dev <[email protected]>
160 lines
5.0 KiB
Go
160 lines
5.0 KiB
Go
package telemetrymetrics
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
|
|
"github.com/hanzoai/o11y/pkg/errors"
|
|
"github.com/hanzoai/o11y/pkg/querybuilder"
|
|
qbtypes "github.com/hanzoai/o11y/pkg/types/querybuildertypes/querybuildertypesv5"
|
|
"github.com/hanzoai/o11y/pkg/types/telemetrytypes"
|
|
|
|
"github.com/hanzo-ds/sqlbuilder"
|
|
)
|
|
|
|
type conditionBuilder struct {
|
|
fm qbtypes.FieldMapper
|
|
}
|
|
|
|
func NewConditionBuilder(fm qbtypes.FieldMapper) *conditionBuilder {
|
|
return &conditionBuilder{fm: fm}
|
|
}
|
|
|
|
func (c *conditionBuilder) conditionFor(
|
|
ctx context.Context,
|
|
startNs uint64,
|
|
endNs uint64,
|
|
key *telemetrytypes.TelemetryFieldKey,
|
|
operator qbtypes.FilterOperator,
|
|
value any,
|
|
sb *sqlbuilder.SelectBuilder,
|
|
) (string, error) {
|
|
|
|
if operator.IsStringSearchOperator() {
|
|
value = querybuilder.FormatValueForContains(value)
|
|
}
|
|
|
|
fieldExpression, err := c.fm.FieldFor(ctx, startNs, endNs, key)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// TODO(srikanthccv): use the same data type collision handling when metrics schemas are updated
|
|
switch v := value.(type) {
|
|
case float64:
|
|
fieldExpression = fmt.Sprintf("toFloat64OrNull(%s)", fieldExpression)
|
|
case []any:
|
|
if len(v) > 0 && (operator == qbtypes.FilterOperatorBetween || operator == qbtypes.FilterOperatorNotBetween) {
|
|
if _, ok := v[0].(float64); ok {
|
|
fieldExpression = fmt.Sprintf("toFloat64OrNull(%s)", fieldExpression)
|
|
}
|
|
}
|
|
}
|
|
|
|
switch operator {
|
|
case qbtypes.FilterOperatorEqual:
|
|
return sb.E(fieldExpression, value), nil
|
|
case qbtypes.FilterOperatorNotEqual:
|
|
return sb.NE(fieldExpression, value), nil
|
|
case qbtypes.FilterOperatorGreaterThan:
|
|
return sb.G(fieldExpression, value), nil
|
|
case qbtypes.FilterOperatorGreaterThanOrEq:
|
|
return sb.GE(fieldExpression, value), nil
|
|
case qbtypes.FilterOperatorLessThan:
|
|
return sb.LT(fieldExpression, value), nil
|
|
case qbtypes.FilterOperatorLessThanOrEq:
|
|
return sb.LE(fieldExpression, value), nil
|
|
|
|
// like and not like
|
|
case qbtypes.FilterOperatorLike:
|
|
return sb.Like(fieldExpression, value), nil
|
|
case qbtypes.FilterOperatorNotLike:
|
|
return sb.NotLike(fieldExpression, value), nil
|
|
case qbtypes.FilterOperatorILike:
|
|
return sb.ILike(fieldExpression, value), nil
|
|
case qbtypes.FilterOperatorNotILike:
|
|
return sb.NotILike(fieldExpression, value), nil
|
|
|
|
case qbtypes.FilterOperatorContains:
|
|
return sb.ILike(fieldExpression, fmt.Sprintf("%%%s%%", value)), nil
|
|
case qbtypes.FilterOperatorNotContains:
|
|
return sb.NotILike(fieldExpression, fmt.Sprintf("%%%s%%", value)), nil
|
|
|
|
case qbtypes.FilterOperatorRegexp:
|
|
// Note: Escape $$ to $$$$ to avoid sqlbuilder interpreting materialized $ signs
|
|
// Only needed because we are using sprintf instead of sb.Match (not implemented in sqlbuilder)
|
|
return fmt.Sprintf(`match(%s, %s)`, sqlbuilder.Escape(fieldExpression), sb.Var(value)), nil
|
|
case qbtypes.FilterOperatorNotRegexp:
|
|
// Note: Escape $$ to $$$$ to avoid sqlbuilder interpreting materialized $ signs
|
|
// Only needed because we are using sprintf instead of sb.Match (not implemented in sqlbuilder)
|
|
return fmt.Sprintf(`NOT match(%s, %s)`, sqlbuilder.Escape(fieldExpression), sb.Var(value)), nil
|
|
// between and not between
|
|
case qbtypes.FilterOperatorBetween:
|
|
values, ok := value.([]any)
|
|
if !ok {
|
|
return "", qbtypes.ErrBetweenValues
|
|
}
|
|
if len(values) != 2 {
|
|
return "", qbtypes.ErrBetweenValues
|
|
}
|
|
return sb.Between(fieldExpression, values[0], values[1]), nil
|
|
case qbtypes.FilterOperatorNotBetween:
|
|
values, ok := value.([]any)
|
|
if !ok {
|
|
return "", qbtypes.ErrBetweenValues
|
|
}
|
|
if len(values) != 2 {
|
|
return "", qbtypes.ErrBetweenValues
|
|
}
|
|
return sb.NotBetween(fieldExpression, values[0], values[1]), nil
|
|
|
|
// in and not in
|
|
case qbtypes.FilterOperatorIn:
|
|
values, ok := value.([]any)
|
|
if !ok {
|
|
return "", qbtypes.ErrInValues
|
|
}
|
|
return sb.In(fieldExpression, values), nil
|
|
case qbtypes.FilterOperatorNotIn:
|
|
values, ok := value.([]any)
|
|
if !ok {
|
|
return "", qbtypes.ErrInValues
|
|
}
|
|
return sb.NotIn(fieldExpression, values), nil
|
|
|
|
// exists and not exists
|
|
// in the UI based query builder, `exists` and `not exists` are used for
|
|
// key membership checks, so depending on the column type, the condition changes
|
|
case qbtypes.FilterOperatorExists, qbtypes.FilterOperatorNotExists:
|
|
|
|
// if the field is intrinsic, it always exists
|
|
if slices.Contains(IntrinsicFields, key.Name) {
|
|
return "true", nil
|
|
}
|
|
|
|
if operator == qbtypes.FilterOperatorExists {
|
|
return fmt.Sprintf("has(JSONExtractKeys(labels), '%s')", querybuilder.EscapeLiteral(key.Name)), nil
|
|
}
|
|
return fmt.Sprintf("not has(JSONExtractKeys(labels), '%s')", querybuilder.EscapeLiteral(key.Name)), nil
|
|
}
|
|
return "", errors.NewInvalidInputf(errors.CodeInvalidInput, "unsupported operator: %v", operator)
|
|
}
|
|
|
|
func (c *conditionBuilder) ConditionFor(
|
|
ctx context.Context,
|
|
startNs uint64,
|
|
endNs uint64,
|
|
key *telemetrytypes.TelemetryFieldKey,
|
|
operator qbtypes.FilterOperator,
|
|
value any,
|
|
sb *sqlbuilder.SelectBuilder,
|
|
) (string, error) {
|
|
condition, err := c.conditionFor(ctx, startNs, endNs, key, operator, value, sb)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return condition, nil
|
|
}
|