Skip to content

Fix string.well_known_regex to reject comma in header names - #528

Merged
timostamm merged 1 commit into
mainfrom
tstamm/Fix-string.well_known_regex-to-reject-comma-in-header-names
Sep 3, 2026
Merged

Fix string.well_known_regex to reject comma in header names#528
timostamm merged 1 commit into
mainfrom
tstamm/Fix-string.well_known_regex-to-reject-comma-in-header-names

Conversation

@timostamm

Copy link
Copy Markdown
Member

RFC 7230 doesn't permit a comma (,) in header names, but (buf.validate.field).string.well_known_regex = KNOWN_REGEX_HTTP_HEADER_NAME fails to reject it.

This fixes the regular expression, and adds a test to assert rejection, and a test to cover all permitted characters.

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.
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

The latest Buf updates on your PR. Results from workflow Buf CI / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedSep 3, 2026, 11:30 AM

expression:
"rules.well_known_regex != 1 || this == '' || this.matches(!has(rules.strict) || rules.strict ?"
"'^:?[0-9a-zA-Z!#$%&\\'*+-.^_|~\\x60]+$' :"
"'^:?[0-9a-zA-Z!#$%&\\'*+.^_|~\\x60-]+$' :"

@timostamm timostamm Sep 3, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@timostamm
timostamm merged commit 63347b8 into main Sep 3, 2026
9 checks passed
@timostamm
timostamm deleted the tstamm/Fix-string.well_known_regex-to-reject-comma-in-header-names branch September 3, 2026 14:51
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