Repository navigation
Expand file tree
/
Copy pathsignature_base_test.go
More file actions
340 lines (293 loc) · 9.79 KB
/
Copy pathsignature_base_test.go
File metadata and controls
340 lines (293 loc) · 9.79 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package httpsig
import (
"net/http"
"net/url"
"testing"
"time"
"github.com/dunglas/httpsfv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewSignatureBase(t *testing.T) {
t.Parallel()
// test is based on the non-normative example from
// https://www.rfc-editor.org/rfc/rfc9421.html#name-creating-the-signature-base
reqURL, err := url.Parse("http://example.com/foo?param=Value&Pet=dog&Cat=meow")
require.NoError(t, err)
msg := &Message{
Method: "POST",
Authority: "example.com",
URL: reqURL,
Header: http.Header{
"Host": []string{"example.com"},
"Date": []string{"Tue, 20 Apr 2021 02:07:55 GMT"},
"Content-Type": []string{"application/json"},
"Content-Digest": []string{"sha-512=:WZDPaVn/7XgHaAy8pmojAkGWoRx2UFChF41A2svX+TaPm+AbwAgBWnrIiYllu7BNNyealdVLvRwEmTHWXvJwew==:"},
"Content-Length": []string{"18"},
},
StatusCode: 0,
IsRequest: true,
}
keyID := "test-key-rsa-pss"
created, err := time.Parse(time.RFC1123, "Tue, 20 Apr 2021 02:07:53 GMT")
require.NoError(t, err)
for uc, tc := range map[string]struct {
components []string
assert func(t *testing.T, err error, base signatureBase)
}{
"successful signature base creation": {
components: []string{
`"@method"`, `"@authority"`, `"@path"`, `"content-digest"`, `"content-length"`, `"content-type"`,
},
assert: func(t *testing.T, err error, base signatureBase) {
t.Helper()
require.NoError(t, err)
expected := `"@method": POST
"@authority": example.com
"@path": /foo
"content-digest": sha-512=:WZDPaVn/7XgHaAy8pmojAkGWoRx2UFChF41A2svX+TaPm+AbwAgBWnrIiYllu7BNNyealdVLvRwEmTHWXvJwew==:
"content-length": 18
"content-type": application/json
"@signature-params": ("@method" "@authority" "@path" "content-digest" "content-length" "content-type");created=1618884473;keyid="test-key-rsa-pss"`
sigBase := base.marshal()
assert.Equal(t, expected, string(sigBase))
},
},
"failing for duplicate component identifier": {
components: []string{
`"@method"`, `"@method"`,
},
assert: func(t *testing.T, err error, _ signatureBase) {
t.Helper()
require.Error(t, err)
require.ErrorIs(t, err, ErrMalformedData)
require.ErrorContains(t, err, "duplicate component identifier")
},
},
"failing for duplicate parameterized component identifier": {
components: []string{
`"@query-param";name="Pet"`,
`"@query-param";name="Pet"`,
},
assert: func(t *testing.T, err error, _ signatureBase) {
t.Helper()
require.Error(t, err)
require.ErrorIs(t, err, ErrMalformedData)
require.ErrorContains(t, err, "duplicate component identifier")
},
},
"failing for duplicate component identifier with reordered parameters": {
components: []string{
`"content-digest";key="sha-512";sf`,
`"content-digest";sf;key="sha-512"`,
},
assert: func(t *testing.T, err error, _ signatureBase) {
t.Helper()
require.ErrorIs(t, err, ErrMalformedData)
require.ErrorContains(t, err, "duplicate component identifier")
},
},
"failing for unknown component identifier": {
components: []string{`"@status"`},
assert: func(t *testing.T, err error, _ signatureBase) {
t.Helper()
require.Error(t, err)
require.ErrorIs(t, err, ErrCanonicalization)
require.ErrorContains(t, err, "@status")
},
},
"allowing same component identifier with different parameters": {
components: []string{
`"@query-param";name="Pet"`,
`"@query-param";name="Cat"`,
},
assert: func(t *testing.T, err error, base signatureBase) {
t.Helper()
require.NoError(t, err)
expected := `"@query-param";name="Pet": dog
"@query-param";name="Cat": meow
"@signature-params": ("@query-param";name="Pet" "@query-param";name="Cat");created=1618884473;keyid="test-key-rsa-pss"`
sigBase := base.marshal()
assert.Equal(t, expected, string(sigBase))
},
},
} {
t.Run(uc, func(t *testing.T) {
identifiers, err := toComponentIdentifiers(tc.components)
require.NoError(t, err)
params := httpsfv.NewParams()
params.Add(string(Created), created.Unix())
params.Add(string(KeyID), keyID)
si := newSignatureInput(identifiers, params)
sigBase, err := newSignatureBase(msg, si, nil)
tc.assert(t, err, sigBase)
})
}
}
func TestNewSignatureBaseWithPreparedIdentifiers(t *testing.T) {
t.Parallel()
for name, identifiers := range map[string][]string{
"no covered components": {},
"simple component": {`"@method"`},
"several components": {`"@method"`, `"@authority"`, `"x-example"`},
"parameterized components": {`"@method"`, `"@query-param";name="Pet"`, `"@query-param";name="Cat"`},
"duplicate component": {`"@method"`, `"@method"`},
"duplicate parameterized item": {`"@query-param";name="Pet"`, `"@query-param";name="Pet"`},
} {
t.Run(name, func(t *testing.T) {
t.Parallel()
sgnr, err := NewSigner(
Key{Algorithm: HmacSha256, Material: []byte("test-hmac-key")},
WithComponents(identifiers...),
)
require.NoError(t, err)
s := sgnr.(*signer)
require.Len(t, s.serializedIdentifiers, len(s.ids))
for i, id := range s.ids {
want, marshalErr := httpsfv.Marshal(id.Item)
require.NoError(t, marshalErr)
assert.Equal(t, want, s.serializedIdentifiers[i])
}
msg := &Message{
Method: "GET", Authority: "example.com",
URL: &url.URL{Path: "/foo", RawQuery: "Pet=dog&Cat=meow"},
Header: http.Header{"X-Example": []string{"value"}},
IsRequest: true,
}
input := s.signatureInput(signOptions{})
uncached, uncachedErr := newSignatureBase(msg, input, nil)
prepared, preparedErr := newSignatureBase(msg, input, s.serializedIdentifiers)
if uncachedErr != nil {
require.ErrorIs(t, preparedErr, ErrMalformedData)
require.ErrorIs(t, uncachedErr, ErrMalformedData)
return
}
require.NoError(t, preparedErr)
assert.Equal(t, uncached.marshal(), prepared.marshal())
assert.Equal(t, uncached.components(), prepared.components())
})
}
}
func TestNewSignatureBaseWithInvalidPreparedIdentifierCount(t *testing.T) {
t.Parallel()
ids, err := toComponentIdentifiers([]string{`"@method"`})
require.NoError(t, err)
input := newSignatureInput(ids, httpsfv.NewParams())
msg := &Message{Method: "GET", IsRequest: true}
_, err = newSignatureBase(msg, input, []string{"one", "two"})
require.ErrorIs(t, err, ErrMalformedData)
}
func TestSignatureBaseMarshal(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
base signatureBase
want string
}{
"empty": {},
"signature parameters only": {
base: signatureBase{signatureParams: `("@method");created=123`},
want: `"@signature-params": ("@method");created=123`,
},
"single component without parameters": {
base: signatureBase{entries: []serializedComponent{{identifier: `"@method"`, value: "GET"}}},
want: `"@method": GET`,
},
"multiple components without parameters": {
base: signatureBase{entries: []serializedComponent{
{identifier: `"@authority"`, value: "foo.bar"},
{identifier: `"@method"`, value: "GET"},
}},
want: "\"@authority\": foo.bar\n\"@method\": GET",
},
"empty and UTF-8 component values with parameters": {
base: signatureBase{
entries: []serializedComponent{
{identifier: `"x-empty"`, value: ""},
{identifier: `"x-name"`, value: "Grüße"},
},
signatureParams: `("x-empty" "x-name");created=123`,
},
want: "\"x-empty\": \n\"x-name\": Grüße\n\"@signature-params\": (\"x-empty\" \"x-name\");created=123",
},
} {
t.Run(name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, string(tc.base.marshal()))
})
}
}
func TestSignatureBaseComponents(t *testing.T) {
t.Parallel()
for uc, tc := range map[string]struct {
entries []serializedComponent
signatureParams string
expected map[string]string
}{
"empty signature base": {
expected: map[string]string{},
},
"excluding signature parameters": {
signatureParams: `("@method");created=123`,
expected: map[string]string{},
},
"simple component identifiers": {
entries: []serializedComponent{
{identifier: `"@method"`, value: "GET"},
{identifier: `"@authority"`, value: "example.com"},
{identifier: `"user-agent"`, value: "ExampleBot/1.0"},
},
signatureParams: `("@method" "@authority" "user-agent")`,
expected: map[string]string{
`"@method"`: "GET",
`"@authority"`: "example.com",
`"user-agent"`: "ExampleBot/1.0",
},
},
"parameterized component identifiers": {
entries: []serializedComponent{
{identifier: `"@query-param";name="Pet"`, value: "dog"},
{identifier: `"@query-param";name="Cat"`, value: "meow"},
{identifier: `"signature-agent";key="sig1"`, value: `"https://bots.example.com";type=cimd`},
},
expected: map[string]string{
`"@query-param";name="Pet"`: "dog",
`"@query-param";name="Cat"`: "meow",
`"signature-agent";key="sig1"`: `"https://bots.example.com";type=cimd`,
},
},
"boolean identifier parameter": {
entries: []serializedComponent{
{identifier: `"content-type";sf`, value: "application/json"},
},
expected: map[string]string{
`"content-type";sf`: "application/json",
},
},
"multiple canonicalized values": {
entries: []serializedComponent{
{identifier: `"x-example"`, value: "first, second, third"},
},
expected: map[string]string{
`"x-example"`: "first, second, third",
},
},
"empty canonicalized value": {
entries: []serializedComponent{
{identifier: `"x-empty"`, value: ""},
},
expected: map[string]string{
`"x-empty"`: "",
},
},
} {
t.Run(uc, func(t *testing.T) {
t.Parallel()
base := signatureBase{
entries: tc.entries,
signatureParams: tc.signatureParams,
}
components := base.components()
assert.Equal(t, tc.expected, components)
})
}
}