Fix string.well_known_regex to reject comma in header names - #528
Merged
timostamm merged 1 commit intoSep 3, 2026
Merged
Conversation
RFC 7230 doesn't permit a comma (`,`) in header names, but `(buf.validate.field).string.well_known_regex = KNOWN_REGEX_HTTP_HEADER_NAME` doesn't reject it. This fixes the regular expression, and adds a test to assert rejection, and a test to cover all permitted characters.
|
The latest Buf updates on your PR. Results from workflow Buf CI / buf (pull_request).
|
timostamm
commented
Sep 3, 2026
| expression: | ||
| "rules.well_known_regex != 1 || this == '' || this.matches(!has(rules.strict) || rules.strict ?" | ||
| "'^:?[0-9a-zA-Z!#$%&\\'*+-.^_|~\\x60]+$' :" | ||
| "'^:?[0-9a-zA-Z!#$%&\\'*+.^_|~\\x60-]+$' :" |
Member
Author
There was a problem hiding this comment.
The cause of the bug was +-. in the character class. It's a range that expands to +,-..
The most direct fix is to quote the hyphen, but it would need to be \\\\- in validate.proto because of the two layers of escaping (prototext and CEL).
Moving the hyphen to the end has the same effect (no longer a range) but doesn't require escaping.
Reproduction:
header/v1/header.proto:
syntax = "proto3";
package header.v1;
import "buf/validate/validate.proto";
message Header {
// The regular expression in `string.well_known_regex.header_name`
string name = 1 [(buf.validate.field).cel = {
// original, broken:
//expression: "this.matches('^:?[0-9a-zA-Z!#$%&\\'*+-.^_|~\\x60]+$')"
// fixed:
expression: "this.matches('^:?[0-9a-zA-Z!#$%&\\'*+.^_|~\\x60-]+$')"
}];
}main.go:
package main
import (
headerv1 "example/internal/gen/header/v1"
"fmt"
"buf.build/go/protovalidate"
)
func main() {
h := &headerv1.Header{Name: ","}
err := protovalidate.Validate(h)
fmt.Printf("%q %t\n", h.GetName(), err == nil)
}The output is "," true with the original, "," false with the fixed expression.
sudorandom
approved these changes
Sep 3, 2026
jonbodner-buf
approved these changes
Sep 3, 2026
timostamm
deleted the
tstamm/Fix-string.well_known_regex-to-reject-comma-in-header-names
branch
September 3, 2026 14:51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RFC 7230 doesn't permit a comma (
,) in header names, but(buf.validate.field).string.well_known_regex = KNOWN_REGEX_HTTP_HEADER_NAMEfails to reject it.This fixes the regular expression, and adds a test to assert rejection, and a test to cover all permitted characters.