Skip to content

Check CEL literal arguments in PROTOVALIDATE lint - #4798

Merged
pkwarren merged 1 commit into
bufbuild:mainfrom
stefanvanburen:svanburen/protovalidate-literal-validators
Nov 18, 2026
Merged

pkwarren merged 1 commit into
bufbuild:mainfrom
stefanvanburen:svanburen/protovalidate-literal-validators

Conversation

@stefanvanburen

Copy link
Copy Markdown
Contributor

Noticed buf lint happily accepts protovalidate rules like this.matches('^[a-z+$') or this < duration('1d') (CEL duration strings only support h, m, s, ms, us, and ns, no d), even though they can never succeed, while the equivalent string.pattern = "^[a-z+$" gets flagged. This turns on cel-go's literal validators for duration, timestamp, and matches, which only look at constant arguments, so they shouldn't flag anything that could actually pass.

With this message:

message Example {
  string name = 1 [(buf.validate.field).cel = {id: "name", expression: "this.matches('^[a-z+$')"}];
  google.protobuf.Duration ttl = 2 [(buf.validate.field).cel = {id: "ttl", expression: "this < duration('1d')"}];
  google.protobuf.Timestamp start = 3 [(buf.validate.field).cel = {id: "start", expression: "this > timestamp('2020-13-01T00:00:00Z')"}];
}

buf lint on v1.75.0 passes, and on this branch:

$ buf lint
rules.proto:8:20:(buf.validate.field).cel.expression on field "name" fails to compile: invalid matches argument
rules.proto:9:37:(buf.validate.field).cel.expression on field "ttl" fails to compile: invalid duration argument
rules.proto:10:40:(buf.validate.field).cel.expression on field "start" fails to compile: invalid timestamp argument

A couple of caveats on the cel-go side. The message doesn't say what's wrong yet; cel-expr/cel-go#1674 adds the underlying error (e.g. invalid matches argument: error parsing regexp: missing closing ]: ...). And the global matches(this, '...') form isn't checked until we're on a cel-go release with cel-expr/cel-go#1658, which isn't in v0.33.4. Both should come along for free with a future make upgrade.

`PROTOVALIDATE` compiles and type-checks CEL expressions, but a string
literal passed to `duration`, `timestamp`, or `matches` type-checks
regardless of its contents. An expression like `duration('1d')` (CEL
duration strings have no `d` unit) or `this.matches('^[a-z+$')` passes
lint, and only fails when protovalidate builds or evaluates the rule.

Enable cel-go's `ValidateDurationLiterals`, `ValidateTimestampLiterals`,
and `ValidateRegexLiterals` on the base CEL environment, so message,
field, and predefined rules all get the check. Each one only looks at
constant arguments and fails when the call fails for every input, so
these don't flag expressions that can succeed.
@stefanvanburen
stefanvanburen marked this pull request as ready for review November 18, 2026 22:37
@pkwarren
pkwarren merged commit 6fbdb3c into bufbuild:main Nov 18, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants