From 62f69538c360f3d15ed7ddb5a61815c7a1297c55 Mon Sep 17 00:00:00 2001 From: Dimitrij Drus Date: Sat, 7 Nov 2026 00:43:00 +0100 Subject: [PATCH] fix: HTTP field value canonicalization corrected --- serializer.go | 47 ++++++++++++++++++++++++--- serializer_test.go | 79 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 116 insertions(+), 10 deletions(-) diff --git a/serializer.go b/serializer.go index 8bb80b8..4653946 100644 --- a/serializer.go +++ b/serializer.go @@ -12,7 +12,7 @@ import ( var errSerialization = errors.New("serialization error") //nolint:gochecknoglobals -var byteSequenceObsFold = regexp.MustCompile("[ \t]*\r\n[ \t]+") +var fieldValueObsFold = regexp.MustCompile("[ \t]*\r\n[ \t]+") type serializer interface { serialize(v []string) ([]string, error) @@ -41,8 +41,47 @@ func newSerializer(params *httpsfv.Params, isRequest bool) (serializer, error) { type rawSerializer struct{} -func (s *rawSerializer) serialize(v []string) ([]string, error) { - return v, nil +//nolint:cyclop +func (s *rawSerializer) serialize(values []string) ([]string, error) { + var normalized []string + + for i, sv := range values { + // RFC 9421, Sections 2.1 and 2.5: normalize field values and + // ensure that the signature base contains only ASCII. + value := strings.Trim(sv, " \t") + if strings.HasPrefix(value, "\r\n") { + return nil, fmt.Errorf("%w: invalid HTTP field value", errSerialization) + } + + if strings.Contains(value, "\r\n") { + value = fieldValueObsFold.ReplaceAllString(value, " ") + } + + for j := range len(value) { + ch := value[j] + if ch >= 0x80 { //nolint:mnd + return nil, fmt.Errorf("%w: non-ASCII HTTP field value (use 'bs' for binary values)", errSerialization) + } + + if (ch < 0x20 && ch != '\t') || ch == 0x7f { + return nil, fmt.Errorf("%w: invalid HTTP field value", errSerialization) + } + } + + if value != sv && normalized == nil { + normalized = append([]string(nil), values...) + } + + if normalized != nil { + normalized[i] = value + } + } + + if normalized != nil { + return normalized, nil + } + + return values, nil } type byteSequenceSerializer struct{} @@ -54,7 +93,7 @@ func (s *byteSequenceSerializer) serialize(v []string) ([]string, error) { // RFC 9421, Section 2.1.3: preserve field-value bytes except for // surrounding HTTP whitespace and obsolete line folding. value := strings.Trim(sv, " \t") - value = byteSequenceObsFold.ReplaceAllString(value, " ") + value = fieldValueObsFold.ReplaceAllString(value, " ") item := httpsfv.NewItem([]byte(value)) marshalled, err := httpsfv.Marshal(item) diff --git a/serializer_test.go b/serializer_test.go index f1851c5..eccb631 100644 --- a/serializer_test.go +++ b/serializer_test.go @@ -105,9 +105,10 @@ func TestRawSerialization(t *testing.T) { t.Parallel() for _, tc := range []struct { - uc string - input []string - result []string + uc string + input []string + result []string + errContains string }{ { uc: "dictionary field", @@ -115,9 +116,69 @@ func TestRawSerialization(t *testing.T) { result: []string{"a=1, b=2;x=1;y=2, c=(a b c)"}, }, { - uc: "list field", + uc: "list field with surrounding whitespace", input: []string{" value, with, lots, of, commas, and, spaces"}, - result: []string{" value, with, lots, of, commas, and, spaces"}, + result: []string{"value, with, lots, of, commas, and, spaces"}, + }, + { + uc: "strip HTTP whitespace from each field value", + input: []string{" \t alpha \t", "\tbeta "}, + result: []string{"alpha", "beta"}, + }, + { + uc: "empty field value", + input: []string{"", " \t "}, + result: []string{"", ""}, + }, + { + uc: "preserve internal whitespace and commas", + input: []string{"alpha \t beta, gamma"}, + result: []string{"alpha \t beta, gamma"}, + }, + { + uc: "unfold obsolete line folding with preceding whitespace", + input: []string{"alpha \t\r\n\t beta"}, + result: []string{"alpha beta"}, + }, + { + uc: "reject leading obsolete line folding", + input: []string{"\r\n value"}, + errContains: "invalid HTTP field value", + }, + { + uc: "reject leading obsolete line folding after whitespace", + input: []string{" \t\r\n value"}, + errContains: "invalid HTTP field value", + }, + { + uc: "reject non-ASCII UTF-8", + input: []string{"caf\u00e9"}, + errContains: "non-ASCII", + }, + { + uc: "reject non-ASCII octet", + input: []string{"\xff"}, + errContains: "non-ASCII", + }, + { + uc: "reject bare newline", + input: []string{"alpha\nbeta"}, + errContains: "invalid HTTP field value", + }, + { + uc: "reject NUL", + input: []string{"alpha\x00beta"}, + errContains: "invalid HTTP field value", + }, + { + uc: "reject other ASCII control characters", + input: []string{"alpha\x01beta"}, + errContains: "invalid HTTP field value", + }, + { + uc: "reject DEL", + input: []string{"alpha\x7fbeta"}, + errContains: "invalid HTTP field value", }, } { t.Run(tc.uc, func(t *testing.T) { @@ -127,8 +188,14 @@ func TestRawSerialization(t *testing.T) { require.NoError(t, err) result, err := s.serialize(tc.input) - require.NoError(t, err) + if tc.errContains != "" { + require.ErrorIs(t, err, errSerialization) + require.ErrorContains(t, err, tc.errContains) + + return + } + require.NoError(t, err) assert.Equal(t, tc.result, result) }) }