Skip to content

Commit 33d307e

Browse files
authored
fix: Preserving structured field component parameter types (#180)
1 parent 98a606c commit 33d307e

5 files changed

Lines changed: 62 additions & 114 deletions

File tree

‎canonicalizer_test.go‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ func TestCanonicalization(t *testing.T) {
420420
},
421421
},
422422
"@query-param for request - valid": {
423-
identifier: `"@query-param";name=bar`,
423+
identifier: `"@query-param";name="bar"`,
424424
message: &Message{URL: testURL1, IsRequest: true},
425425
assert: func(t *testing.T, err error, value []string) {
426426
t.Helper()
@@ -430,7 +430,7 @@ func TestCanonicalization(t *testing.T) {
430430
},
431431
},
432432
"@query-param for request - valid with duplicate unrelated parameter": {
433-
identifier: `"@query-param";name=foo`,
433+
identifier: `"@query-param";name="foo"`,
434434
message: &Message{URL: &url.URL{RawQuery: "foo=bar&other=one&other=two"}, IsRequest: true},
435435
assert: func(t *testing.T, err error, value []string) {
436436
t.Helper()
@@ -440,7 +440,7 @@ func TestCanonicalization(t *testing.T) {
440440
},
441441
},
442442
"@query-param for request - invalid, duplicate parameter": {
443-
identifier: `"@query-param";name=foo`,
443+
identifier: `"@query-param";name="foo"`,
444444
message: &Message{URL: &url.URL{RawQuery: "foo=one&foo=two"}, IsRequest: true},
445445
assert: func(t *testing.T, err error, _ []string) {
446446
t.Helper()
@@ -450,7 +450,7 @@ func TestCanonicalization(t *testing.T) {
450450
},
451451
},
452452
"@query-param for request - invalid, duplicate encoded parameter": {
453-
identifier: `"@query-param";name=foo`,
453+
identifier: `"@query-param";name="foo"`,
454454
message: &Message{URL: &url.URL{RawQuery: "foo=one&%66oo=two"}, IsRequest: true},
455455
assert: func(t *testing.T, err error, _ []string) {
456456
t.Helper()
@@ -460,7 +460,7 @@ func TestCanonicalization(t *testing.T) {
460460
},
461461
},
462462
"@query-param for response - invalid, duplicate request parameter": {
463-
identifier: `"@query-param";name=foo;req`,
463+
identifier: `"@query-param";name="foo";req`,
464464
message: &Message{URL: &url.URL{RawQuery: "foo=one&foo=two"}, IsRequest: false},
465465
assert: func(t *testing.T, err error, _ []string) {
466466
t.Helper()
@@ -480,19 +480,19 @@ func TestCanonicalization(t *testing.T) {
480480
require.ErrorContains(t, err, "named parameter")
481481
},
482482
},
483-
"@query-param for request - invalid query param escaping": {
484-
identifier: `"@query-param";name=1`,
483+
"@query-param for request - invalid token name parameter": {
484+
identifier: `"@query-param";name=foo`,
485485
message: &Message{URL: testURL1, IsRequest: true},
486486
assert: func(t *testing.T, err error, _ []string) {
487487
t.Helper()
488488

489489
require.Error(t, err)
490-
require.ErrorIs(t, err, ErrCanonicalization)
490+
require.ErrorIs(t, err, ErrInvalidComponentIdentifier)
491491
require.ErrorContains(t, err, "must be a string")
492492
},
493493
},
494494
"@query-param for request - invalid, parameter not present in query": {
495-
identifier: `"@query-param";name=moo`,
495+
identifier: `"@query-param";name="moo"`,
496496
message: &Message{URL: testURL1, IsRequest: true},
497497
assert: func(t *testing.T, err error, _ []string) {
498498
t.Helper()
@@ -503,7 +503,7 @@ func TestCanonicalization(t *testing.T) {
503503
},
504504
},
505505
"@query-param for response - valid": {
506-
identifier: `"@query-param";name=foo;req`,
506+
identifier: `"@query-param";name="foo";req`,
507507
message: &Message{URL: testURL1, IsRequest: false},
508508
assert: func(t *testing.T, err error, value []string) {
509509
t.Helper()
@@ -513,7 +513,7 @@ func TestCanonicalization(t *testing.T) {
513513
},
514514
},
515515
"@query-param for response - invalid": {
516-
identifier: `"@query-param";name=foo`,
516+
identifier: `"@query-param";name="foo"`,
517517
message: &Message{URL: testURL1, IsRequest: false},
518518
assert: func(t *testing.T, err error, _ []string) {
519519
t.Helper()
@@ -589,7 +589,7 @@ func TestCanonicalization(t *testing.T) {
589589
},
590590
},
591591
"header component for request - invalid, serialization error": {
592-
identifier: `"X-Foo-Bar";sf;key=foo`,
592+
identifier: `"X-Foo-Bar";sf;key="foo"`,
593593
message: &Message{Header: http.Header{"X-Foo-Bar": []string{"baz"}}, IsRequest: true},
594594
assert: func(t *testing.T, err error, _ []string) {
595595
t.Helper()
@@ -637,7 +637,7 @@ func TestCanonicalization(t *testing.T) {
637637
c, err := canonicalizerFor(strings.ToLower(item.Value.(string)))
638638
require.NoError(t, err)
639639

640-
res, err := c.canonicalize(tc.message, normaliseParams(item.Params))
640+
res, err := c.canonicalize(tc.message, item.Params)
641641

642642
tc.assert(t, err, res)
643643
})

‎component_identifier.go‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package httpsig
22

33
import (
4-
"encoding/base64"
54
"fmt"
65
"strings"
76

@@ -29,29 +28,6 @@ func quoteIdentifierName(input string) string {
2928
return identifier + ";" + strings.Join(rest, ";")
3029
}
3130

32-
func normaliseParams(params *httpsfv.Params) *httpsfv.Params {
33-
if len(params.Names()) == 0 {
34-
return params
35-
}
36-
37-
ps := httpsfv.NewParams()
38-
39-
for _, name := range params.Names() {
40-
value, _ := params.Get(name)
41-
42-
if v, ok := value.([]byte); ok {
43-
encoded := base64.StdEncoding.EncodeToString(v)
44-
ps.Add(name, encoded)
45-
} else if v, ok := value.(httpsfv.Token); ok {
46-
ps.Add(name, string(v))
47-
} else {
48-
ps.Add(name, value)
49-
}
50-
}
51-
52-
return ps
53-
}
54-
5531
type componentIdentifier struct {
5632
httpsfv.Item
5733

@@ -93,8 +69,6 @@ func newComponentIdentifier(item httpsfv.Item) (*componentIdentifier, error) {
9369
return nil, err
9470
}
9571

96-
item.Params = normaliseParams(item.Params)
97-
9872
return &componentIdentifier{Item: item, c: canonicalizer}, nil
9973
}
10074

‎component_identifier_test.go‎

Lines changed: 28 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -26,78 +26,6 @@ func TestQuoteIdentifierName(t *testing.T) {
2626
}
2727
}
2828

29-
func TestNormalizeParams(t *testing.T) {
30-
t.Parallel()
31-
32-
for uc, tc := range map[string]struct {
33-
given *httpsfv.Params
34-
expect *httpsfv.Params
35-
}{
36-
"empty params": {
37-
given: httpsfv.NewParams(),
38-
expect: httpsfv.NewParams(),
39-
},
40-
"byte array params": {
41-
given: func() *httpsfv.Params {
42-
params := httpsfv.NewParams()
43-
params.Add("test", []byte("test"))
44-
45-
return params
46-
}(),
47-
expect: func() *httpsfv.Params {
48-
params := httpsfv.NewParams()
49-
params.Add("test", "dGVzdA==")
50-
51-
return params
52-
}(),
53-
},
54-
"token params": {
55-
given: func() *httpsfv.Params {
56-
params := httpsfv.NewParams()
57-
params.Add("test", httpsfv.Token("token"))
58-
59-
return params
60-
}(),
61-
expect: func() *httpsfv.Params {
62-
params := httpsfv.NewParams()
63-
params.Add("test", "token")
64-
65-
return params
66-
}(),
67-
},
68-
"bare item params": {
69-
given: func() *httpsfv.Params {
70-
params := httpsfv.NewParams()
71-
params.Add("test", 1)
72-
73-
return params
74-
}(),
75-
expect: func() *httpsfv.Params {
76-
params := httpsfv.NewParams()
77-
params.Add("test", 1)
78-
79-
return params
80-
}(),
81-
},
82-
} {
83-
t.Run(uc, func(t *testing.T) {
84-
res := normaliseParams(tc.given)
85-
86-
assert.Equal(t, tc.given.Names(), res.Names())
87-
88-
for _, name := range res.Names() {
89-
resVal, ok := res.Get(name)
90-
assert.True(t, ok)
91-
92-
expVal, ok := tc.expect.Get(name)
93-
assert.True(t, ok)
94-
95-
assert.Equal(t, expVal, resVal)
96-
}
97-
})
98-
}
99-
}
100-
10129
func TestToComponentIdentifiers(t *testing.T) {
10230
t.Parallel()
10331

@@ -132,6 +60,32 @@ func TestToComponentIdentifiers(t *testing.T) {
13260
assert.Nil(t, ci)
13361
},
13462
},
63+
"preserving structured field parameter types": {
64+
identifiers: []string{`x-test;string="abc";token=abc;bytes=:AQID:`},
65+
assert: func(t *testing.T, ci []*componentIdentifier, err error) {
66+
t.Helper()
67+
68+
require.NoError(t, err)
69+
require.Len(t, ci, 1)
70+
71+
stringValue, ok := ci[0].Params.Get("string")
72+
require.True(t, ok)
73+
assert.Equal(t, "abc", stringValue)
74+
75+
tokenValue, ok := ci[0].Params.Get("token")
76+
require.True(t, ok)
77+
assert.Equal(t, httpsfv.Token("abc"), tokenValue)
78+
79+
bytesValue, ok := ci[0].Params.Get("bytes")
80+
require.True(t, ok)
81+
assert.Equal(t, []byte{1, 2, 3}, bytesValue)
82+
83+
input := newSignatureInput(ci, nil)
84+
marshalled, err := httpsfv.Marshal(input.value)
85+
require.NoError(t, err)
86+
assert.Equal(t, `("x-test";string="abc";token=abc;bytes=:AQID:)`, marshalled)
87+
},
88+
},
13589
"valid": {
13690
identifiers: []string{"@method;foo=bar", "@status", `@query-param;name="Pet"`},
13791
assert: func(t *testing.T, ci []*componentIdentifier, err error) {
@@ -143,7 +97,7 @@ func TestToComponentIdentifiers(t *testing.T) {
14397
assert.Equal(t, "@method", ci[0].Value.(string))
14498
res, ok := ci[0].Params.Get("foo")
14599
require.True(t, ok)
146-
require.Equal(t, "bar", res.(string))
100+
require.Equal(t, httpsfv.Token("bar"), res)
147101
assert.NotNil(t, ci[0].c)
148102

149103
assert.Equal(t, "@status", ci[1].Value.(string))
@@ -206,7 +160,7 @@ func TestNewComponentIdentifier(t *testing.T) {
206160

207161
val, ok := ci.Params.Get("foo")
208162
require.True(t, ok)
209-
assert.Equal(t, "YmFy", val)
163+
assert.Equal(t, []byte("bar"), val)
210164
},
211165
},
212166
} {

‎signature_input.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package httpsig
22

33
import (
4+
"bytes"
45
"fmt"
56
"time"
67

@@ -47,7 +48,7 @@ func (si *signatureInput) hasIdentifier(id *componentIdentifier) bool {
4748
actual, present := identifier.Params.Get(name)
4849

4950
expected, _ := id.Params.Get(name)
50-
if !present || actual != expected {
51+
if !present || !equalComponentParameterValues(actual, expected) {
5152
matches = false
5253

5354
break
@@ -167,3 +168,13 @@ func validateSignatureInputParameters(params *httpsfv.Params) error {
167168

168169
return nil
169170
}
171+
172+
func equalComponentParameterValues(actual, expected any) bool {
173+
if expectedBytes, ok := expected.([]byte); ok {
174+
actualBytes, ok := actual.([]byte)
175+
176+
return ok && bytes.Equal(actualBytes, expectedBytes)
177+
}
178+
179+
return actual == expected
180+
}

‎signature_input_test.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,15 @@ func TestSignatureInputHasIdentifier(t *testing.T) {
810810
given: []string{`x-test;value=1`},
811811
identifier: `x-test;value="1"`,
812812
},
813+
"matching byte sequence parameter": {
814+
given: []string{`x-test;custom=:AQID:`},
815+
identifier: `x-test;custom=:AQID:`,
816+
expected: true,
817+
},
818+
"different byte sequence parameter": {
819+
given: []string{`x-test;custom=:AQID:`},
820+
identifier: `x-test;custom=:AQIE:`,
821+
},
813822
"different flag value": {
814823
given: []string{`x-test;sf=?0`},
815824
identifier: `x-test;sf`,

0 commit comments

Comments
 (0)