From 1f7305b3fe0c987611ba5ecd2acda5719ef5c1e8 Mon Sep 17 00:00:00 2001 From: Dimitrij Drus Date: Fri, 6 Nov 2026 19:01:29 +0100 Subject: [PATCH 1/2] fix: Validating of HTTP field names in component identifiers according to RFC 9110 --- component_identifier.go | 23 ++++++++++++++++++++ component_identifier_test.go | 42 ++++++++++++++++++++++++++++++++++++ integration_test.go | 4 ++-- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/component_identifier.go b/component_identifier.go index aa7b9e4..6ce82c4 100644 --- a/component_identifier.go +++ b/component_identifier.go @@ -65,6 +65,10 @@ func newComponentIdentifier(item httpsfv.Item) (*componentIdentifier, error) { return nil, fmt.Errorf("%w: component name %q must be lowercase", ErrInvalidComponentIdentifier, name) } + if !strings.HasPrefix(name, "@") && !isValidHTTPFieldName(name) { + return nil, fmt.Errorf("%w: invalid HTTP field name %q", ErrInvalidComponentIdentifier, name) + } + canonicalizer, err := canonicalizerFor(name, item.Params) if err != nil { return nil, err @@ -110,3 +114,22 @@ func equalComponentParameterValues(actual, expected any) bool { return actual == expected } + +// HTTP field names follow the token syntax of RFC 9110, Section 5.1. +func isValidHTTPFieldName(name string) bool { + if name == "" { + return false + } + + for i := 0; i < len(name); i++ { + ch := name[i] + if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || + (ch >= '0' && ch <= '9') || strings.IndexByte("!#$%&'*+-.^_`|~", ch) >= 0 { + continue + } + + return false + } + + return true +} diff --git a/component_identifier_test.go b/component_identifier_test.go index 896e99b..3ff399f 100644 --- a/component_identifier_test.go +++ b/component_identifier_test.go @@ -106,6 +106,30 @@ func TestToComponentIdentifiers(t *testing.T) { } } +func TestIsValidHTTPFieldName(t *testing.T) { + t.Parallel() + + for uc, tc := range map[string]struct { + name string + valid bool + }{ + "lowercase field name": {name: "content-type", valid: true}, + "uppercase field name": {name: "Content-Type", valid: true}, + "all permitted punctuation": {name: "!#$%&'*+-.^_`|~", valid: true}, + "empty name": {name: ""}, + "space": {name: "bad name"}, + "colon": {name: ":authority"}, + "slash": {name: "bad/name"}, + "at sign": {name: "name@other"}, + "non-ASCII": {name: "caf\u00e9"}, + "control character": {name: "bad\nname"}, + } { + t.Run(uc, func(t *testing.T) { + assert.Equal(t, tc.valid, isValidHTTPFieldName(tc.name)) + }) + } +} + func TestNewComponentIdentifier(t *testing.T) { t.Parallel() @@ -140,6 +164,24 @@ func TestNewComponentIdentifier(t *testing.T) { assert.Nil(t, ci) }, }, + "invalid HTTP field name": { + item: httpsfv.NewItem("bad name"), + assert: func(t *testing.T, ci *componentIdentifier, err error) { + t.Helper() + + require.ErrorIs(t, err, ErrInvalidComponentIdentifier) + assert.Nil(t, ci) + }, + }, + "valid HTTP field name": { + item: httpsfv.NewItem("x_test"), + assert: func(t *testing.T, ci *componentIdentifier, err error) { + t.Helper() + + require.NoError(t, err) + assert.NotNil(t, ci.c) + }, + }, "valid": { item: func() httpsfv.Item { item := httpsfv.NewItem("@method") diff --git a/integration_test.go b/integration_test.go index a897618..52a921a 100644 --- a/integration_test.go +++ b/integration_test.go @@ -95,14 +95,14 @@ func TestParseAcceptSignatureBuilderOutput(t *testing.T) { func TestParsedComponentsRoundTripThroughWithComponents(t *testing.T) { t.Parallel() - const field = `sig=("@status" "content-digest";req "@query-param";name="a;b" "a;req" "b\"c" "d\\e");created` + const field = `sig=("@status" "content-digest";req "@query-param";name="a;b" "@query-param";name="b\"c" "@query-param";name="d\\e" "x_test");created` requests, err := ParseAcceptSignature(http.Header{headerAcceptSignature: {field}}) require.NoError(t, err) require.Len(t, requests, 1) assert.Equal(t, []string{ `"@status"`, `"content-digest";req`, `"@query-param";name="a;b"`, - `"a;req"`, `"b\"c"`, `"d\\e"`, + `"@query-param";name="b\"c"`, `"@query-param";name="d\\e"`, `"x_test"`, }, requests[0].Components) builder, err := NewAcceptSignature( From f307a999ba8001b3246e6d0ccc2d805d718f2abc Mon Sep 17 00:00:00 2001 From: Dimitrij Drus Date: Fri, 6 Nov 2026 19:07:09 +0100 Subject: [PATCH 2/2] linter warnings resolved --- component_identifier.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/component_identifier.go b/component_identifier.go index 6ce82c4..4976086 100644 --- a/component_identifier.go +++ b/component_identifier.go @@ -121,7 +121,7 @@ func isValidHTTPFieldName(name string) bool { return false } - for i := 0; i < len(name); i++ { + for i := range len(name) { ch := name[i] if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || strings.IndexByte("!#$%&'*+-.^_`|~", ch) >= 0 {