Repository navigation
Expand file tree
/
Copy pathserializer.go
More file actions
165 lines (129 loc) · 3.88 KB
/
Copy pathserializer.go
File metadata and controls
165 lines (129 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package httpsig
import (
"errors"
"fmt"
"regexp"
"strings"
"github.com/dunglas/httpsfv"
)
var errSerialization = errors.New("serialization error")
//nolint:gochecknoglobals
var fieldValueObsFold = regexp.MustCompile("[ \t]*\r\n[ \t]+")
type serializer interface {
serialize(v []string) ([]string, error)
}
func newSerializer(params *httpsfv.Params, isRequest bool) (serializer, error) {
if _, hasReqParam := params.Get("req"); hasReqParam && isRequest {
return nil, fmt.Errorf("%w: 'req' parameter not valid for requests",
errSerialization)
}
_, isBs := params.Get("bs")
_, isSf := params.Get("sf")
keyParam, isKey := params.Get("key")
key, _ := keyParam.(string) // validated by canonicalizerFor
switch {
case isSf || isKey:
return &strictSerializer{key: key}, nil
case isBs:
return &byteSequenceSerializer{}, nil
default:
return &rawSerializer{}, nil
}
}
type rawSerializer struct{}
//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{}
func (s *byteSequenceSerializer) serialize(v []string) ([]string, error) {
encoded := make([]string, len(v))
for i, sv := range v {
// 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 = fieldValueObsFold.ReplaceAllString(value, " ")
item := httpsfv.NewItem([]byte(value))
marshalled, err := httpsfv.Marshal(item)
if err != nil {
return nil, fmt.Errorf("%w: %w", errSerialization, err)
}
encoded[i] = marshalled
}
return encoded, nil
}
type strictSerializer struct {
key string
}
func (s *strictSerializer) serialize(values []string) ([]string, error) {
var parsed httpsfv.StructuredFieldValue
if len(s.key) != 0 {
// The 'key' parameter requires dictionary parsing, even when the
// field value could also be parsed as a list (e.g. a bare boolean member).
dict, err := httpsfv.UnmarshalDictionary(values)
if err != nil {
return nil, fmt.Errorf("%w: unable to parse value as dictionary: %w", errSerialization, err)
}
val, ok := dict.Get(s.key)
if !ok {
return nil, fmt.Errorf("%w: unable to find key '%s' in structured field", errSerialization, s.key)
}
parsed = val
} else {
var err error
parsed, err = s.parse(values)
if err != nil {
return nil, err
}
}
marshalled, err := httpsfv.Marshal(parsed)
if err != nil {
return nil, fmt.Errorf("%w: %w", errSerialization, err)
}
return []string{marshalled}, nil
}
func (s *strictSerializer) parse(values []string) (httpsfv.StructuredFieldValue, error) {
list, err := httpsfv.UnmarshalList(values)
if err == nil {
return list, nil
}
dict, err := httpsfv.UnmarshalDictionary(values)
if err == nil {
return dict, nil
}
item, err := httpsfv.UnmarshalItem(values)
if err == nil {
return item, nil
}
return nil, fmt.Errorf("%w: unable to parse structured header", errSerialization)
}