From 7f94ed0b27036aa1fd677b5cf40f773198846bf1 Mon Sep 17 00:00:00 2001 From: Dimitrij Drus Date: Sat, 7 Nov 2026 23:32:28 +0100 Subject: [PATCH 1/4] component identifier representation standardized --- accept_signature_test.go | 14 ++-- component_identifier.go | 23 +----- component_identifier_test.go | 117 +++++++++++++++++++-------- integration_test.go | 6 +- options_test.go | 32 ++++---- signature_base.go | 16 +--- signature_base_test.go | 60 ++++++-------- signature_input_test.go | 34 ++++---- signer_test.go | 22 ++--- verification_policy_test.go | 10 +-- verifier_test.go | 150 +++++++++++++++++------------------ 11 files changed, 250 insertions(+), 234 deletions(-) diff --git a/accept_signature_test.go b/accept_signature_test.go index f4b48b7..64c04bd 100644 --- a/accept_signature_test.go +++ b/accept_signature_test.go @@ -45,7 +45,7 @@ func TestNewAcceptedSignature(t *testing.T) { WithKeyID("test"), WithNonceGetter(&NonceGetterMock{}), WithLabel("foo"), - WithComponents("@status", "content-digest;req"), + WithComponents(`"@status"`, `"content-digest";req`), WithContentDigestAlgorithmPreferences(AlgorithmPreference{Sha512, 10}), WithTag("bar"), WithCreatedTimestamp(false), @@ -86,7 +86,7 @@ func TestNewAcceptedSignature(t *testing.T) { }, "option raises error": { opts: []AcceptSignatureOption{ - WithComponents("@foo"), + WithComponents(`"@foo"`), }, assert: func(t *testing.T, err error, asb *AcceptSignatureBuilder) { t.Helper() @@ -277,7 +277,7 @@ func TestAcceptSignatureBuilderBuild(t *testing.T) { opts: []AcceptSignatureOption{ WithSignatureAlgorithm(EcdsaP256Sha256), WithKeyID("test"), - WithComponents("@status", "x-foo-bar;req", "@method"), + WithComponents(`"@status"`, `"x-foo-bar";req`, `"@method"`), WithLabel("foo"), WithTag("bar"), WithNonceGetter(NonceGetterFunc(func(_ context.Context) (string, error) { return "abc", nil })), @@ -296,8 +296,8 @@ func TestAcceptSignatureBuilderBuild(t *testing.T) { }, "Want-Content-Digest is omitted after overriding covered components": { opts: []AcceptSignatureOption{ - WithComponents("@status", "content-digest"), - WithComponents("@method"), + WithComponents(`"@status"`, `"content-digest"`), + WithComponents(`"@method"`), }, assert: func(t *testing.T, err error, hdr http.Header) { t.Helper() @@ -311,7 +311,7 @@ func TestAcceptSignatureBuilderBuild(t *testing.T) { opts: []AcceptSignatureOption{ WithSignatureAlgorithm(EcdsaP256Sha256), WithKeyID("test"), - WithComponents("@status", "content-digest", "@method"), + WithComponents(`"@status"`, `"content-digest"`, `"@method"`), WithLabel("foo"), WithTag("bar"), WithNonceGetter(NonceGetterFunc(func(_ context.Context) (string, error) { return "abc", nil })), @@ -334,7 +334,7 @@ func TestAcceptSignatureBuilderBuild(t *testing.T) { opts: []AcceptSignatureOption{ WithSignatureAlgorithm(EcdsaP256Sha256), WithKeyID("test"), - WithComponents("@status", "content-digest", "@method"), + WithComponents(`"@status"`, `"content-digest"`, `"@method"`), WithLabel("foo"), WithTag("bar"), WithNonceGetter(NonceGetterFunc(func(_ context.Context) (string, error) { return "abc", nil })), diff --git a/component_identifier.go b/component_identifier.go index 4976086..4dde5a9 100644 --- a/component_identifier.go +++ b/component_identifier.go @@ -8,27 +8,6 @@ import ( "github.com/dunglas/httpsfv" ) -func quoteIdentifierName(input string) string { - // nothing to do if already quoted - if strings.HasPrefix(input, `"`) { - return input - } - - // try to split the structured field - parts := strings.Split(input, ";") - name, rest := parts[0], parts[1:] - - // always quote the name - identifier := "\"" + name + "\"" - - if len(rest) == 0 { - return identifier - } - - // just add the rest - return identifier + ";" + strings.Join(rest, ";") -} - type componentIdentifier struct { httpsfv.Item @@ -39,7 +18,7 @@ func toComponentIdentifiers(identifiers []string) ([]*componentIdentifier, error cis := make([]*componentIdentifier, 0, len(identifiers)) for _, identifier := range identifiers { - item, err := httpsfv.UnmarshalItem([]string{quoteIdentifierName(identifier)}) + item, err := httpsfv.UnmarshalItem([]string{identifier}) if err != nil { return nil, fmt.Errorf("%w: %s: %w", ErrInvalidComponentIdentifier, identifiers, err) } diff --git a/component_identifier_test.go b/component_identifier_test.go index 9a68b55..b16cd35 100644 --- a/component_identifier_test.go +++ b/component_identifier_test.go @@ -8,24 +8,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestQuoteIdentifierName(t *testing.T) { - t.Parallel() - - for uc, tc := range map[string]struct { - given string - expect string - }{ - "name already quoted": {given: `"@method";req,key=val`, expect: `"@method";req,key=val`}, - "name not quoted, without parameter": {given: `@method`, expect: `"@method"`}, - "name not quoted, with parameter": {given: `@method;req,key=val`, expect: `"@method";req,key=val`}, - } { - t.Run(uc, func(t *testing.T) { - res := quoteIdentifierName(tc.given) - assert.Equal(t, tc.expect, res) - }) - } -} - func TestToComponentIdentifiers(t *testing.T) { t.Parallel() @@ -34,7 +16,7 @@ func TestToComponentIdentifiers(t *testing.T) { assert func(t *testing.T, ci []*componentIdentifier, err error) }{ "invalid identifier definition": { - identifiers: []string{"foo;#"}, + identifiers: []string{`"foo";#`}, assert: func(t *testing.T, _ []*componentIdentifier, err error) { t.Helper() @@ -42,8 +24,62 @@ func TestToComponentIdentifiers(t *testing.T) { require.ErrorIs(t, err, ErrInvalidComponentIdentifier) }, }, + "unquoted derived component": { + identifiers: []string{"@method"}, + assert: func(t *testing.T, ci []*componentIdentifier, err error) { + t.Helper() + + require.ErrorIs(t, err, ErrInvalidComponentIdentifier) + assert.Nil(t, ci) + }, + }, + "unquoted field name": { + identifiers: []string{"content-digest"}, + assert: func(t *testing.T, ci []*componentIdentifier, err error) { + t.Helper() + + require.ErrorIs(t, err, ErrInvalidComponentIdentifier) + assert.Nil(t, ci) + }, + }, + "unquoted field name with parameters": { + identifiers: []string{"content-digest;req"}, + assert: func(t *testing.T, ci []*componentIdentifier, err error) { + t.Helper() + + require.ErrorIs(t, err, ErrInvalidComponentIdentifier) + assert.Nil(t, ci) + }, + }, + "unquoted derived component with parameters": { + identifiers: []string{`@query-param;name="Pet"`}, + assert: func(t *testing.T, ci []*componentIdentifier, err error) { + t.Helper() + + require.ErrorIs(t, err, ErrInvalidComponentIdentifier) + assert.Nil(t, ci) + }, + }, + "non-string SF integer": { + identifiers: []string{"42"}, + assert: func(t *testing.T, ci []*componentIdentifier, err error) { + t.Helper() + + require.ErrorIs(t, err, ErrInvalidComponentIdentifier) + assert.Nil(t, ci) + }, + }, + "non-string SF boolean": { + identifiers: []string{"?1"}, + assert: func(t *testing.T, ci []*componentIdentifier, err error) { + t.Helper() + + require.ErrorIs(t, err, ErrInvalidComponentIdentifier) + assert.Nil(t, ci) + }, + }, "unknown identifier": { - identifiers: []string{"@foo"}, + identifiers: []string{`"@foo"`}, assert: func(t *testing.T, _ []*componentIdentifier, err error) { t.Helper() @@ -52,7 +88,7 @@ func TestToComponentIdentifiers(t *testing.T) { }, }, "preserving supported parameter types": { - identifiers: []string{`x-test;key="abc";sf;req`}, + identifiers: []string{`"x-test";key="abc";sf;req`}, assert: func(t *testing.T, ci []*componentIdentifier, err error) { t.Helper() @@ -72,8 +108,25 @@ func TestToComponentIdentifiers(t *testing.T) { assert.Equal(t, true, req) }, }, + "preserving parameter order": { + identifiers: []string{`"x-test";sf;req`, `"x-test";req;sf`}, + assert: func(t *testing.T, ci []*componentIdentifier, err error) { + t.Helper() + + require.NoError(t, err) + require.Len(t, ci, 2) + + for i, expected := range []string{`"x-test";sf;req`, `"x-test";req;sf`} { + actual, marshalErr := httpsfv.Marshal(ci[i].Item) + require.NoError(t, marshalErr) + assert.Equal(t, expected, actual) + } + + assert.True(t, ci[0].equal(ci[1])) + }, + }, "valid": { - identifiers: []string{"@method;req", "@status", `@query-param;name="Pet"`}, + identifiers: []string{`"@method";req`, `"@status"`, `"@query-param";name="Pet"`}, assert: func(t *testing.T, ci []*componentIdentifier, err error) { t.Helper() @@ -218,25 +271,25 @@ func TestComponentIdentifierEqual(t *testing.T) { equal bool }{ "same identifier with reordered parameters": { - first: `x-test;key="foo";sf`, - second: `x-test;sf;key="foo"`, + first: `"x-test";key="foo";sf`, + second: `"x-test";sf;key="foo"`, equal: true, }, "different names": { - first: `x-first`, - second: `x-second`, + first: `"x-first"`, + second: `"x-second"`, }, "different parameter names": { - first: `x-test;sf`, - second: `x-test;bs`, + first: `"x-test";sf`, + second: `"x-test";bs`, }, "different parameter counts": { - first: `x-test;req`, - second: `x-test`, + first: `"x-test";req`, + second: `"x-test"`, }, "different parameter values": { - first: `x-test;key="foo"`, - second: `x-test;key="bar"`, + first: `"x-test";key="foo"`, + second: `"x-test";key="bar"`, }, } { t.Run(uc, func(t *testing.T) { diff --git a/integration_test.go b/integration_test.go index c0bf0fb..68599d1 100644 --- a/integration_test.go +++ b/integration_test.go @@ -70,6 +70,10 @@ func TestSignAndVerifyParsedSignatureRequest(t *testing.T) { assert.Equal(t, request.Label, verified[0].Label) assert.Equal(t, DisplayString("Grüße"), verified[0].Parameters["nickname"]) assert.Equal(t, time.Unix(1794000000, 0), verified[0].Parameters["reviewed"]) + + for _, identifier := range request.Components { + assert.Contains(t, verified[0].Components, identifier) + } } func TestParseAcceptSignatureBuilderOutput(t *testing.T) { @@ -79,7 +83,7 @@ func TestParseAcceptSignatureBuilderOutput(t *testing.T) { WithLabel("bar"), WithKeyID("foo"), WithSignatureAlgorithm(EcdsaP256Sha256), - WithComponents("@status", "content-digest;req", `@query-param;name="Pet"`), + WithComponents(`"@status"`, `"content-digest";req`, `"@query-param";name="Pet"`), WithTag("awesome-app"), WithParameter("audience", "service-a"), WithParameter("nickname", DisplayString("Grüße")), diff --git a/options_test.go b/options_test.go index 2b3ccc2..837cbef 100644 --- a/options_test.go +++ b/options_test.go @@ -3,7 +3,6 @@ package httpsig import ( "context" "fmt" - "strings" "testing" "time" @@ -98,8 +97,9 @@ func TestWithComponentsForSigner(t *testing.T) { identifiers []string err error }{ - "multiple valid identifier": {identifiers: []string{"@method;req", "@status"}}, - "invalid identifier": {identifiers: []string{"@foo"}, err: ErrUnsupportedComponentIdentifier}, + "multiple valid identifier": {identifiers: []string{`"@method";req`, `"@status"`}}, + "unquoted identifier": {identifiers: []string{"@method"}, err: ErrInvalidComponentIdentifier}, + "invalid identifier": {identifiers: []string{`"@foo"`}, err: ErrUnsupportedComponentIdentifier}, } { t.Run(uc, func(t *testing.T) { s := &signer{} @@ -114,8 +114,9 @@ func TestWithComponentsForSigner(t *testing.T) { require.Len(t, s.ids, len(tc.identifiers)) for i, id := range s.ids { - vals := strings.Split(tc.identifiers[i], ";") - assert.Equal(t, vals[0], id.Value) + actual, marshalErr := httpsfv.Marshal(id.Item) + require.NoError(t, marshalErr) + assert.Equal(t, tc.identifiers[i], actual) } } }) @@ -129,8 +130,8 @@ func TestWithComponentsForVerifier(t *testing.T) { identifiers []string err error }{ - "multiple valid identifier": {identifiers: []string{"@method;req", "@status"}}, - "invalid identifier": {identifiers: []string{"@foo"}, err: ErrUnsupportedComponentIdentifier}, + "multiple valid identifier": {identifiers: []string{`"@method";req`, `"@status"`}}, + "invalid identifier": {identifiers: []string{`"@foo"`}, err: ErrUnsupportedComponentIdentifier}, } { t.Run(uc, func(t *testing.T) { v := &verifier{} @@ -147,8 +148,9 @@ func TestWithComponentsForVerifier(t *testing.T) { require.Len(t, exp.identifiers, len(tc.identifiers)) for i, id := range exp.identifiers { - vals := strings.Split(tc.identifiers[i], ";") - assert.Equal(t, vals[0], id.Value) + actual, marshalErr := httpsfv.Marshal(id.Item) + require.NoError(t, marshalErr) + assert.Equal(t, tc.identifiers[i], actual) } } }) @@ -172,7 +174,7 @@ func TestWithComponentsForAcceptSignature(t *testing.T) { }, }, "invalid identifiers provided": { - identifiers: []string{"@foo"}, + identifiers: []string{`"@foo"`}, assert: func(t *testing.T, err error, _ *AcceptSignatureBuilder) { t.Helper() @@ -181,7 +183,7 @@ func TestWithComponentsForAcceptSignature(t *testing.T) { }, }, "valid identifiers without content-digest": { - identifiers: []string{"@status", "x-foo-bar"}, + identifiers: []string{`"@status"`, `"x-foo-bar"`}, assert: func(t *testing.T, err error, asb *AcceptSignatureBuilder) { t.Helper() @@ -193,7 +195,7 @@ func TestWithComponentsForAcceptSignature(t *testing.T) { }, }, "valid identifiers with content-digest": { - identifiers: []string{"@status", "content-digest", "x-foo-bar"}, + identifiers: []string{`"@status"`, `"content-digest"`, `"x-foo-bar"`}, assert: func(t *testing.T, err error, asb *AcceptSignatureBuilder) { t.Helper() @@ -811,7 +813,7 @@ func TestWithRequiredTag(t *testing.T) { }, "with identifiers only": { opts: []VerifierOption{ - WithComponents("@status"), + WithComponents(`"@status"`), }, assert: func(t *testing.T, err error, exp *verificationPolicy) { t.Helper() @@ -839,7 +841,7 @@ func TestWithRequiredTag(t *testing.T) { }, "with all possible options": { opts: []VerifierOption{ - WithComponents("@status"), + WithComponents(`"@status"`), WithValidityTolerance(5 * time.Minute), }, assert: func(t *testing.T, err error, exp *verificationPolicy) { @@ -855,7 +857,7 @@ func TestWithRequiredTag(t *testing.T) { }, "with unsupported identifier": { opts: []VerifierOption{ - WithComponents("@foo"), + WithComponents(`"@foo"`), }, assert: func(t *testing.T, err error, exp *verificationPolicy) { t.Helper() diff --git a/signature_base.go b/signature_base.go index f4e51ab..9515063 100644 --- a/signature_base.go +++ b/signature_base.go @@ -3,7 +3,6 @@ package httpsig import ( "fmt" "slices" - "strings" "github.com/dunglas/httpsfv" ) @@ -13,9 +12,8 @@ type serializedComponent struct { value string } -// The entries correspond to input.identifiers by index. Signature parameters are separate. +// The entries preserve the covered component order. Signature parameters are separate. type signatureBase struct { - input *signatureInput entries []serializedComponent signatureParams string } @@ -31,7 +29,6 @@ func newSignatureBase(msg *Message, si *signatureInput, prepared []string) (sign } sigBase := signatureBase{ - input: si, entries: make([]serializedComponent, 0, len(si.identifiers)), signatureParams: marshalledSigParams, } @@ -114,15 +111,8 @@ func (s signatureBase) marshal() []byte { func (s signatureBase) components() map[string]string { result := make(map[string]string, len(s.entries)) - for i, item := range s.entries { - name := s.input.identifiers[i].Value.(string) // nolint: forcetypeassert - - // Remove quotes around the component name, - // preserving identifier parameters. - prefix := `"` + name + `"` - key := name + strings.TrimPrefix(item.identifier, prefix) - - result[key] = item.value + for _, item := range s.entries { + result[item.identifier] = item.value } return result diff --git a/signature_base_test.go b/signature_base_test.go index b1af6dc..44563d7 100644 --- a/signature_base_test.go +++ b/signature_base_test.go @@ -45,7 +45,7 @@ func TestNewSignatureBase(t *testing.T) { }{ "successful signature base creation": { components: []string{ - "@method", "@authority", "@path", "content-digest", "content-length", "content-type", + `"@method"`, `"@authority"`, `"@path"`, `"content-digest"`, `"content-length"`, `"content-type"`, }, assert: func(t *testing.T, err error, base signatureBase) { t.Helper() @@ -67,7 +67,7 @@ func TestNewSignatureBase(t *testing.T) { }, "failing for duplicate component identifier": { components: []string{ - "@method", "@method", + `"@method"`, `"@method"`, }, assert: func(t *testing.T, err error, _ signatureBase) { t.Helper() @@ -79,8 +79,8 @@ func TestNewSignatureBase(t *testing.T) { }, "failing for duplicate parameterized component identifier": { components: []string{ - `@query-param;name="Pet"`, - `@query-param;name="Pet"`, + `"@query-param";name="Pet"`, + `"@query-param";name="Pet"`, }, assert: func(t *testing.T, err error, _ signatureBase) { t.Helper() @@ -92,8 +92,8 @@ func TestNewSignatureBase(t *testing.T) { }, "failing for duplicate component identifier with reordered parameters": { components: []string{ - `content-digest;key="sha-512";sf`, - `content-digest;sf;key="sha-512"`, + `"content-digest";key="sha-512";sf`, + `"content-digest";sf;key="sha-512"`, }, assert: func(t *testing.T, err error, _ signatureBase) { t.Helper() @@ -103,7 +103,7 @@ func TestNewSignatureBase(t *testing.T) { }, }, "failing for unknown component identifier": { - components: []string{"@status"}, + components: []string{`"@status"`}, assert: func(t *testing.T, err error, _ signatureBase) { t.Helper() @@ -114,8 +114,8 @@ func TestNewSignatureBase(t *testing.T) { }, "allowing same component identifier with different parameters": { components: []string{ - `@query-param;name="Pet"`, - `@query-param;name="Cat"`, + `"@query-param";name="Pet"`, + `"@query-param";name="Cat"`, }, assert: func(t *testing.T, err error, base signatureBase) { t.Helper() @@ -154,11 +154,11 @@ func TestNewSignatureBaseWithPreparedIdentifiers(t *testing.T) { 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"`}, + "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() @@ -206,7 +206,7 @@ func TestNewSignatureBaseWithPreparedIdentifiers(t *testing.T) { func TestNewSignatureBaseWithInvalidPreparedIdentifierCount(t *testing.T) { t.Parallel() - ids, err := toComponentIdentifiers([]string{"@method"}) + ids, err := toComponentIdentifiers([]string{`"@method"`}) require.NoError(t, err) input := newSignatureInput(ids, httpsfv.NewParams()) @@ -263,7 +263,6 @@ func TestSignatureBaseComponents(t *testing.T) { t.Parallel() for uc, tc := range map[string]struct { - names []string entries []serializedComponent signatureParams string expected map[string]string @@ -276,7 +275,6 @@ func TestSignatureBaseComponents(t *testing.T) { expected: map[string]string{}, }, "simple component identifiers": { - names: []string{"@method", "@authority", "user-agent"}, entries: []serializedComponent{ {identifier: `"@method"`, value: "GET"}, {identifier: `"@authority"`, value: "example.com"}, @@ -284,62 +282,52 @@ func TestSignatureBaseComponents(t *testing.T) { }, signatureParams: `("@method" "@authority" "user-agent")`, expected: map[string]string{ - "@method": "GET", - "@authority": "example.com", - "user-agent": "ExampleBot/1.0", + `"@method"`: "GET", + `"@authority"`: "example.com", + `"user-agent"`: "ExampleBot/1.0", }, }, "parameterized component identifiers": { - names: []string{"@query-param", "@query-param", "signature-agent"}, 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`, + `"@query-param";name="Pet"`: "dog", + `"@query-param";name="Cat"`: "meow", + `"signature-agent";key="sig1"`: `"https://bots.example.com";type=cimd`, }, }, "boolean identifier parameter": { - names: []string{"content-type"}, entries: []serializedComponent{ {identifier: `"content-type";sf`, value: "application/json"}, }, expected: map[string]string{ - "content-type;sf": "application/json", + `"content-type";sf`: "application/json", }, }, "multiple canonicalized values": { - names: []string{"x-example"}, entries: []serializedComponent{ {identifier: `"x-example"`, value: "first, second, third"}, }, expected: map[string]string{ - "x-example": "first, second, third", + `"x-example"`: "first, second, third", }, }, "empty canonicalized value": { - names: []string{"x-empty"}, entries: []serializedComponent{ {identifier: `"x-empty"`, value: ""}, }, expected: map[string]string{ - "x-empty": "", + `"x-empty"`: "", }, }, } { t.Run(uc, func(t *testing.T) { t.Parallel() - identifiers := make([]*componentIdentifier, len(tc.names)) - for i, name := range tc.names { - identifiers[i] = &componentIdentifier{Item: httpsfv.NewItem(name)} - } - base := signatureBase{ - input: &signatureInput{identifiers: identifiers}, entries: tc.entries, signatureParams: tc.signatureParams, } diff --git a/signature_input_test.go b/signature_input_test.go index 497f61c..be13337 100644 --- a/signature_input_test.go +++ b/signature_input_test.go @@ -804,41 +804,41 @@ func TestSignatureInputHasIdentifier(t *testing.T) { expected bool }{ "no identifiers": { - identifier: "@method", + identifier: `"@method"`, }, "matching identifier": { - given: []string{"@method"}, - identifier: "@method", + given: []string{`"@method"`}, + identifier: `"@method"`, expected: true, }, "different identifier name": { - given: []string{"@method"}, - identifier: "@authority", + given: []string{`"@method"`}, + identifier: `"@authority"`, }, "matching parameter value": { - given: []string{`@query-param;name="foo"`}, - identifier: `@query-param;name="foo"`, + given: []string{`"@query-param";name="foo"`}, + identifier: `"@query-param";name="foo"`, expected: true, }, "different parameter value": { - given: []string{`@query-param;name="bar"`}, - identifier: `@query-param;name="foo"`, + given: []string{`"@query-param";name="bar"`}, + identifier: `"@query-param";name="foo"`, }, "different parameter name": { - given: []string{`x-test;sf`}, - identifier: `x-test;req`, + given: []string{`"x-test";sf`}, + identifier: `"x-test";req`, }, "additional parameter": { - given: []string{`x-test;sf;req`}, - identifier: `x-test;sf`, + given: []string{`"x-test";sf;req`}, + identifier: `"x-test";sf`, }, "missing parameter": { - given: []string{`x-test;sf`}, - identifier: `x-test;sf;req`, + given: []string{`"x-test";sf`}, + identifier: `"x-test";sf;req`, }, "different parameter order": { - given: []string{`x-test;key="foo";req`}, - identifier: `x-test;req;key="foo"`, + given: []string{`"x-test";key="foo";req`}, + identifier: `"x-test";req;key="foo"`, expected: true, }, } { diff --git a/signer_test.go b/signer_test.go index 10c2408..0b4ad74 100644 --- a/signer_test.go +++ b/signer_test.go @@ -92,7 +92,7 @@ func TestNewSigner(t *testing.T) { { uc: "error while applying configuration options", key: Key{Algorithm: EcdsaP384Sha384, Material: pkp384}, - opts: []SignerOption{WithComponents("@test")}, + opts: []SignerOption{WithComponents(`"@test"`)}, assert: func(t *testing.T, err error, _ Signer) { t.Helper() @@ -191,7 +191,7 @@ func TestNewSigner(t *testing.T) { uc: "successful with configuration", key: Key{Algorithm: EcdsaP384Sha384, Material: pkp384}, opts: []SignerOption{ - WithComponents("@status"), + WithComponents(`"@status"`), WithTag("foo"), WithTTL(1 * time.Second), WithLabel("bar"), @@ -287,7 +287,7 @@ func TestSignerSign(t *testing.T) { uc: "failed updating message (adding content-digest)", key: Key{Algorithm: EcdsaP384Sha384, Material: pkp384}, opts: []SignerOption{ - WithComponents("content-digest"), + WithComponents(`"content-digest"`), }, msg: &Message{ Method: http.MethodPost, @@ -314,7 +314,7 @@ func TestSignerSign(t *testing.T) { uc: "failed creating signature base", key: Key{Algorithm: EcdsaP384Sha384, Material: pkp384}, opts: []SignerOption{ - WithComponents("@status"), + WithComponents(`"@status"`), }, msg: &Message{ Method: http.MethodPost, @@ -523,7 +523,7 @@ func TestSignerSign(t *testing.T) { opts: []SignerOption{ WithLabel("sig-b22"), WithTTL(0), - WithComponents("@authority", "content-digest", "@query-param;name=\"Pet\""), + WithComponents(`"@authority"`, `"content-digest"`, `"@query-param";name="Pet"`), WithTag("header-example"), WithContentDigestAlgorithm(Sha512), WithKeyID("test-key-rsa-pss"), @@ -565,7 +565,7 @@ func TestSignerSign(t *testing.T) { WithLabel("sig-b23"), WithTTL(0), WithComponents( - "date", "@method", "@path", "@query", "@authority", "content-type", "content-digest", "content-length", + `"date"`, `"@method"`, `"@path"`, `"@query"`, `"@authority"`, `"content-type"`, `"content-digest"`, `"content-length"`, ), WithContentDigestAlgorithm(Sha512), WithKeyID("test-key-rsa-pss"), @@ -606,7 +606,7 @@ func TestSignerSign(t *testing.T) { opts: []SignerOption{ WithLabel("sig-b24"), WithTTL(0), - WithComponents("@status", "content-type", "content-digest", "content-length"), + WithComponents(`"@status"`, `"content-type"`, `"content-digest"`, `"content-length"`), WithContentDigestAlgorithm(Sha512), WithKeyID("test-key-ecc-p256"), }, @@ -644,7 +644,7 @@ func TestSignerSign(t *testing.T) { opts: []SignerOption{ WithLabel("sig-b25"), WithTTL(0), - WithComponents("date", "@authority", "content-type"), + WithComponents(`"date"`, `"@authority"`, `"content-type"`), WithKeyID("test-shared-secret"), }, msg: &Message{ @@ -679,7 +679,7 @@ func TestSignerSign(t *testing.T) { opts: []SignerOption{ WithLabel("sig-b26"), WithTTL(0), - WithComponents("date", "@method", "@path", "@authority", "content-type", "content-length"), + WithComponents(`"date"`, `"@method"`, `"@path"`, `"@authority"`, `"content-type"`, `"content-length"`), WithKeyID("test-key-ed25519"), }, msg: &Message{ @@ -713,7 +713,7 @@ func TestSignerSign(t *testing.T) { opts: []SignerOption{ WithLabel("sig-content-digest"), WithTTL(0), - WithComponents("content-digest"), + WithComponents(`"content-digest"`), WithContentDigestAlgorithm(Sha256), WithKeyID("test-key-rsa-pss"), }, @@ -757,7 +757,7 @@ func TestSignerSign(t *testing.T) { opts: []SignerOption{ WithLabel("sig-no-content-digest"), WithTTL(0), - WithComponents("date", "@method"), + WithComponents(`"date"`, `"@method"`), WithContentDigestAlgorithm(Sha256), WithKeyID("test-key-rsa-pss"), }, diff --git a/verification_policy_test.go b/verification_policy_test.go index 50497a4..5fa3193 100644 --- a/verification_policy_test.go +++ b/verification_policy_test.go @@ -101,9 +101,9 @@ func TestVerificationPolicyWithOptions(t *testing.T) { func TestVerificationPolicyInherit(t *testing.T) { t.Parallel() - globalIDs, err := toComponentIdentifiers([]string{"@method", "content-digest"}) + globalIDs, err := toComponentIdentifiers([]string{`"@method"`, `"content-digest"`}) require.NoError(t, err) - localIDs, err := toComponentIdentifiers([]string{"@status"}) + localIDs, err := toComponentIdentifiers([]string{`"@status"`}) require.NoError(t, err) localBuilder := &AcceptSignatureBuilder{label: "custom"} @@ -441,7 +441,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) { }}, }, exp: func() verificationPolicy { - ids, err := toComponentIdentifiers([]string{`@query-param;name="foo"`}) + ids, err := toComponentIdentifiers([]string{`"@query-param";name="foo"`}) require.NoError(t, err) return verificationPolicy{identifiers: ids, reqCreatedTS: &falseVal, reqExpiresTS: &falseVal} @@ -456,7 +456,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) { "expected component identifier missing": { params: httpsfv.InnerList{Params: httpsfv.NewParams()}, exp: func() verificationPolicy { - ids, err := toComponentIdentifiers([]string{"@method;req", "@authority"}) + ids, err := toComponentIdentifiers([]string{`"@method";req`, `"@authority"`}) require.NoError(t, err) return verificationPolicy{identifiers: ids, reqCreatedTS: &falseVal, reqExpiresTS: &falseVal} @@ -555,7 +555,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) { }, }, exp: func() verificationPolicy { - ids, err := toComponentIdentifiers([]string{"@authority", "@method"}) + ids, err := toComponentIdentifiers([]string{`"@authority"`, `"@method"`}) require.NoError(t, err) return verificationPolicy{ diff --git a/verifier_test.go b/verifier_test.go index 4a05859..a594550 100644 --- a/verifier_test.go +++ b/verifier_test.go @@ -101,7 +101,7 @@ func TestNewVerifier(t *testing.T) { "with component identifiers configuration error": { resolver: &KeyResolverMock{}, opts: []VerifierOption{ - WithComponents("@foo"), + WithComponents(`"@foo"`), }, assert: func(t *testing.T, err error, _ *verifier) { t.Helper() @@ -171,7 +171,7 @@ func TestNewVerifier(t *testing.T) { opts: []VerifierOption{ WithRequiredTag("test", WithValidityTolerance(1*time.Second), - WithComponents("@status"), + WithComponents(`"@status"`), ), }, assert: func(t *testing.T, err error, v *verifier) { @@ -196,7 +196,7 @@ func TestNewVerifier(t *testing.T) { opts: []VerifierOption{ WithRequiredTag("test"), WithValidityTolerance(1 * time.Second), - WithComponents("@status"), + WithComponents(`"@status"`), }, assert: func(t *testing.T, err error, v *verifier) { t.Helper() @@ -221,7 +221,7 @@ func TestNewVerifier(t *testing.T) { opts: []VerifierOption{ WithRequiredTag("test"), WithValidityTolerance(1 * time.Second), - WithComponents("@status", "content-digest"), + WithComponents(`"@status"`, `"content-digest"`), WithSignatureNegotiation( WithSignatureAlgorithm(RsaPssSha512), WithKeyID("bar"), @@ -261,10 +261,10 @@ func TestNewVerifier(t *testing.T) { opts: []VerifierOption{ WithRequiredTag("test", WithValidityTolerance(1*time.Second), - WithComponents("@status"), + WithComponents(`"@status"`), ), WithValidityTolerance(1 * time.Minute), - WithComponents("@method"), + WithComponents(`"@method"`), WithValidateAllSignatures(), }, assert: func(t *testing.T, err error, v *verifier) { @@ -703,7 +703,7 @@ func TestVerifierVerify(t *testing.T) { "missing required component rejected before key resolution": { opts: []VerifierOption{ WithRequiredTag("foo", - WithComponents("@method"), + WithComponents(`"@method"`), ), WithCreatedTimestamp(false), WithExpiresTimestamp(false), @@ -1084,7 +1084,7 @@ func TestVerifierVerify(t *testing.T) { "signature parameters negotiation for not present tagged signature": { opts: []VerifierOption{ WithRequiredTag("foo", - WithComponents("@method", "content-digest"), + WithComponents(`"@method"`, `"content-digest"`), WithSignatureNegotiation( WithSignatureAlgorithm(RsaPssSha512), WithKeyID("test-key"), @@ -1115,10 +1115,10 @@ func TestVerifierVerify(t *testing.T) { }, "global content-digest requirement overridden for negotiated tag": { opts: []VerifierOption{ - WithComponents("content-digest"), + WithComponents(`"content-digest"`), WithSignatureNegotiation(), WithRequiredTag("foo", - WithComponents("@method"), + WithComponents(`"@method"`), ), }, msg: &Message{Header: http.Header{}}, @@ -1141,7 +1141,7 @@ func TestVerifierVerify(t *testing.T) { "signature negotiation without nonce getter omits nonce": { opts: []VerifierOption{ WithRequiredTag("foo", - WithComponents("@method"), + WithComponents(`"@method"`), WithSignatureNegotiation( WithLabel("foo-sig"), ), @@ -1207,7 +1207,7 @@ func TestVerifierVerify(t *testing.T) { "signature parameters negotiation fails because of nonce error": { opts: []VerifierOption{ WithRequiredTag("foo", - WithComponents("@method"), + WithComponents(`"@method"`), WithSignatureNegotiation( WithSignatureAlgorithm(RsaPssSha512), WithKeyID("test-key"), @@ -1356,8 +1356,8 @@ func TestVerifierVerify(t *testing.T) { si.Message != nil && si.Message.Header.Get("Content-Length") == "18" && len(si.Components) == 3 && - si.Components["@authority"] == "example.com" && - si.Components["@query-param;name=\"Pet\""] == "dog" && + si.Components[`"@authority"`] == "example.com" && + si.Components[`"@query-param";name="Pet"`] == "dog" && si.Parameters["created"] == int64(1618884473) && si.Parameters["keyid"] == "test-key-rsa-pss" && si.Parameters["tag"] == "header-example" @@ -1381,8 +1381,8 @@ func TestVerifierVerify(t *testing.T) { assert.Equal(t, "sig-b22", sig.Label) assert.Len(t, sig.Components, 3) - assert.Equal(t, "example.com", sig.Components["@authority"]) - assert.Equal(t, "dog", sig.Components["@query-param;name=\"Pet\""]) + assert.Equal(t, "example.com", sig.Components[`"@authority"`]) + assert.Equal(t, "dog", sig.Components[`"@query-param";name="Pet"`]) assert.Len(t, sig.Parameters, 3) assert.Equal(t, int64(1618884473), sig.Parameters["created"]) @@ -1426,14 +1426,14 @@ func TestVerifierVerify(t *testing.T) { si.Message != nil && si.Message.Header.Get("Content-Length") == "18" && len(si.Components) == 8 && - si.Components["date"] == "Tue, 20 Apr 2021 02:07:55 GMT" && - si.Components["@method"] == "POST" && - si.Components["@path"] == "/foo" && - si.Components["@query"] == "?param=Value&Pet=dog" && - si.Components["@authority"] == "example.com" && - si.Components["content-type"] == "application/json" && - si.Components["content-digest"] == "sha-512=:WZDPaVn/7XgHaAy8pmojAkGWoRx2UFChF41A2svX+TaPm+AbwAgBWnrIiYllu7BNNyealdVLvRwEmTHWXvJwew==:" && - si.Components["content-length"] == "18" && + si.Components[`"date"`] == "Tue, 20 Apr 2021 02:07:55 GMT" && + si.Components[`"@method"`] == "POST" && + si.Components[`"@path"`] == "/foo" && + si.Components[`"@query"`] == "?param=Value&Pet=dog" && + si.Components[`"@authority"`] == "example.com" && + si.Components[`"content-type"`] == "application/json" && + si.Components[`"content-digest"`] == "sha-512=:WZDPaVn/7XgHaAy8pmojAkGWoRx2UFChF41A2svX+TaPm+AbwAgBWnrIiYllu7BNNyealdVLvRwEmTHWXvJwew==:" && + si.Components[`"content-length"`] == "18" && si.Parameters["created"] == int64(1618884473) && si.Parameters["keyid"] == "test-key-rsa-pss" })). @@ -1455,14 +1455,14 @@ func TestVerifierVerify(t *testing.T) { assert.Equal(t, "sig-b23", sig.Label) assert.Len(t, sig.Components, 8) - assert.Equal(t, "Tue, 20 Apr 2021 02:07:55 GMT", sig.Components["date"]) - assert.Equal(t, "POST", sig.Components["@method"]) - assert.Equal(t, "/foo", sig.Components["@path"]) - assert.Equal(t, "?param=Value&Pet=dog", sig.Components["@query"]) - assert.Equal(t, "example.com", sig.Components["@authority"]) - assert.Equal(t, "application/json", sig.Components["content-type"]) - assert.Equal(t, "sha-512=:WZDPaVn/7XgHaAy8pmojAkGWoRx2UFChF41A2svX+TaPm+AbwAgBWnrIiYllu7BNNyealdVLvRwEmTHWXvJwew==:", sig.Components["content-digest"]) - assert.Equal(t, "18", sig.Components["content-length"]) + assert.Equal(t, "Tue, 20 Apr 2021 02:07:55 GMT", sig.Components[`"date"`]) + assert.Equal(t, "POST", sig.Components[`"@method"`]) + assert.Equal(t, "/foo", sig.Components[`"@path"`]) + assert.Equal(t, "?param=Value&Pet=dog", sig.Components[`"@query"`]) + assert.Equal(t, "example.com", sig.Components[`"@authority"`]) + assert.Equal(t, "application/json", sig.Components[`"content-type"`]) + assert.Equal(t, "sha-512=:WZDPaVn/7XgHaAy8pmojAkGWoRx2UFChF41A2svX+TaPm+AbwAgBWnrIiYllu7BNNyealdVLvRwEmTHWXvJwew==:", sig.Components[`"content-digest"`]) + assert.Equal(t, "18", sig.Components[`"content-length"`]) assert.Len(t, sig.Parameters, 2) assert.Equal(t, int64(1618884473), sig.Parameters["created"]) @@ -1502,10 +1502,10 @@ func TestVerifierVerify(t *testing.T) { return si.Label == "sig-b24" && si.Message != nil && len(si.Components) == 4 && - si.Components["@status"] == "200" && - si.Components["content-type"] == "application/json" && - si.Components["content-digest"] == "sha-512=:mEWXIS7MaLRuGgxOBdODa3xqM1XdEvxoYhvlCFJ41QJgJc4GTsPp29l5oGX69wWdXymyU0rjJuahq4l5aGgfLQ==:" && - si.Components["content-length"] == "23" && + si.Components[`"@status"`] == "200" && + si.Components[`"content-type"`] == "application/json" && + si.Components[`"content-digest"`] == "sha-512=:mEWXIS7MaLRuGgxOBdODa3xqM1XdEvxoYhvlCFJ41QJgJc4GTsPp29l5oGX69wWdXymyU0rjJuahq4l5aGgfLQ==:" && + si.Components[`"content-length"`] == "23" && si.Parameters["created"] == int64(1618884473) && si.Parameters["keyid"] == "test-key-ecc-p256" })). @@ -1528,10 +1528,10 @@ func TestVerifierVerify(t *testing.T) { assert.Equal(t, "sig-b24", sig.Label) assert.Len(t, sig.Components, 4) - assert.Equal(t, "200", sig.Components["@status"]) - assert.Equal(t, "application/json", sig.Components["content-type"]) - assert.Equal(t, "sha-512=:mEWXIS7MaLRuGgxOBdODa3xqM1XdEvxoYhvlCFJ41QJgJc4GTsPp29l5oGX69wWdXymyU0rjJuahq4l5aGgfLQ==:", sig.Components["content-digest"]) - assert.Equal(t, "23", sig.Components["content-length"]) + assert.Equal(t, "200", sig.Components[`"@status"`]) + assert.Equal(t, "application/json", sig.Components[`"content-type"`]) + assert.Equal(t, "sha-512=:mEWXIS7MaLRuGgxOBdODa3xqM1XdEvxoYhvlCFJ41QJgJc4GTsPp29l5oGX69wWdXymyU0rjJuahq4l5aGgfLQ==:", sig.Components[`"content-digest"`]) + assert.Equal(t, "23", sig.Components[`"content-length"`]) assert.Len(t, sig.Parameters, 2) assert.Equal(t, int64(1618884473), sig.Parameters["created"]) @@ -1570,9 +1570,9 @@ func TestVerifierVerify(t *testing.T) { return si.Label == "sig-b25" && si.Message != nil && len(si.Components) == 3 && - si.Components["date"] == "Tue, 20 Apr 2021 02:07:55 GMT" && - si.Components["@authority"] == "example.com" && - si.Components["content-type"] == "application/json" && + si.Components[`"date"`] == "Tue, 20 Apr 2021 02:07:55 GMT" && + si.Components[`"@authority"`] == "example.com" && + si.Components[`"content-type"`] == "application/json" && si.Parameters["created"] == int64(1618884473) && si.Parameters["keyid"] == "test-shared-secret" })). @@ -1594,9 +1594,9 @@ func TestVerifierVerify(t *testing.T) { assert.Equal(t, "sig-b25", sig.Label) assert.Len(t, sig.Components, 3) - assert.Equal(t, "Tue, 20 Apr 2021 02:07:55 GMT", sig.Components["date"]) - assert.Equal(t, "example.com", sig.Components["@authority"]) - assert.Equal(t, "application/json", sig.Components["content-type"]) + assert.Equal(t, "Tue, 20 Apr 2021 02:07:55 GMT", sig.Components[`"date"`]) + assert.Equal(t, "example.com", sig.Components[`"@authority"`]) + assert.Equal(t, "application/json", sig.Components[`"content-type"`]) assert.Len(t, sig.Parameters, 2) assert.Equal(t, int64(1618884473), sig.Parameters["created"]) @@ -1636,12 +1636,12 @@ func TestVerifierVerify(t *testing.T) { return si.Label == "sig-b26" && si.Message != nil && len(si.Components) == 6 && - si.Components["date"] == "Tue, 20 Apr 2021 02:07:55 GMT" && - si.Components["@method"] == "POST" && - si.Components["@path"] == "/foo" && - si.Components["@authority"] == "example.com" && - si.Components["content-type"] == "application/json" && - si.Components["content-length"] == "18" && + si.Components[`"date"`] == "Tue, 20 Apr 2021 02:07:55 GMT" && + si.Components[`"@method"`] == "POST" && + si.Components[`"@path"`] == "/foo" && + si.Components[`"@authority"`] == "example.com" && + si.Components[`"content-type"`] == "application/json" && + si.Components[`"content-length"`] == "18" && si.Parameters["created"] == int64(1618884473) && si.Parameters["keyid"] == "test-key-ed25519" })). @@ -1663,12 +1663,12 @@ func TestVerifierVerify(t *testing.T) { assert.Equal(t, "sig-b26", sig.Label) assert.Len(t, sig.Components, 6) - assert.Equal(t, "Tue, 20 Apr 2021 02:07:55 GMT", sig.Components["date"]) - assert.Equal(t, "POST", sig.Components["@method"]) - assert.Equal(t, "/foo", sig.Components["@path"]) - assert.Equal(t, "example.com", sig.Components["@authority"]) - assert.Equal(t, "application/json", sig.Components["content-type"]) - assert.Equal(t, "18", sig.Components["content-length"]) + assert.Equal(t, "Tue, 20 Apr 2021 02:07:55 GMT", sig.Components[`"date"`]) + assert.Equal(t, "POST", sig.Components[`"@method"`]) + assert.Equal(t, "/foo", sig.Components[`"@path"`]) + assert.Equal(t, "example.com", sig.Components[`"@authority"`]) + assert.Equal(t, "application/json", sig.Components[`"content-type"`]) + assert.Equal(t, "18", sig.Components[`"content-length"`]) assert.Len(t, sig.Parameters, 2) assert.Equal(t, int64(1618884473), sig.Parameters["created"]) @@ -1822,9 +1822,9 @@ func TestVerifierVerify(t *testing.T) { mock.MatchedBy(func(si SignatureInfo) bool { return si.Label == "first" && si.Parameters["keyid"] == "test-shared-secret" && - si.Components["date"] == "Tue, 20 Apr 2021 02:07:55 GMT" && - si.Components["@authority"] == "example.com" && - si.Components["content-type"] == "application/json" + si.Components[`"date"`] == "Tue, 20 Apr 2021 02:07:55 GMT" && + si.Components[`"@authority"`] == "example.com" && + si.Components[`"content-type"`] == "application/json" })). Return(KeyInfo{ Key: Key{Material: tkMAC, Algorithm: HmacSha256}, @@ -1836,9 +1836,9 @@ func TestVerifierVerify(t *testing.T) { mock.MatchedBy(func(si SignatureInfo) bool { return si.Label == "second" && si.Parameters["keyid"] == "test-key-ed25519" && - si.Components["@method"] == "POST" && - si.Components["@path"] == "/foo" && - si.Components["@authority"] == "example.com" + si.Components[`"@method"`] == "POST" && + si.Components[`"@path"`] == "/foo" && + si.Components[`"@authority"`] == "example.com" })). Return(KeyInfo{ Key: Key{Material: tkEd25519, Algorithm: Ed25519}, @@ -1861,9 +1861,9 @@ func TestVerifierVerify(t *testing.T) { assert.Equal(t, "first", sig.Label) assert.Len(t, sig.Components, 3) - assert.Equal(t, "Tue, 20 Apr 2021 02:07:55 GMT", sig.Components["date"]) - assert.Equal(t, "example.com", sig.Components["@authority"]) - assert.Equal(t, "application/json", sig.Components["content-type"]) + assert.Equal(t, "Tue, 20 Apr 2021 02:07:55 GMT", sig.Components[`"date"`]) + assert.Equal(t, "example.com", sig.Components[`"@authority"`]) + assert.Equal(t, "application/json", sig.Components[`"content-type"`]) assert.Len(t, sig.Parameters, 2) assert.Equal(t, int64(1618884473), sig.Parameters["created"]) @@ -1880,12 +1880,12 @@ func TestVerifierVerify(t *testing.T) { assert.Equal(t, "second", sig.Label) assert.Len(t, sig.Components, 6) - assert.Equal(t, "Tue, 20 Apr 2021 02:07:55 GMT", sig.Components["date"]) - assert.Equal(t, "POST", sig.Components["@method"]) - assert.Equal(t, "/foo", sig.Components["@path"]) - assert.Equal(t, "example.com", sig.Components["@authority"]) - assert.Equal(t, "application/json", sig.Components["content-type"]) - assert.Equal(t, "18", sig.Components["content-length"]) + assert.Equal(t, "Tue, 20 Apr 2021 02:07:55 GMT", sig.Components[`"date"`]) + assert.Equal(t, "POST", sig.Components[`"@method"`]) + assert.Equal(t, "/foo", sig.Components[`"@path"`]) + assert.Equal(t, "example.com", sig.Components[`"@authority"`]) + assert.Equal(t, "application/json", sig.Components[`"content-type"`]) + assert.Equal(t, "18", sig.Components[`"content-length"`]) assert.Len(t, sig.Parameters, 2) assert.Equal(t, int64(1618884473), sig.Parameters["created"]) @@ -1976,7 +1976,7 @@ func TestVerifierVerify(t *testing.T) { inputBytes := []byte("data") signer, err := NewSigner(key, WithTag("profile"), - WithComponents("@method"), + WithComponents(`"@method"`), WithParameter("audience", "service-a"), WithParameter("score", 1.2349), WithParameter("mode", Token("example")), @@ -1998,7 +1998,7 @@ func TestVerifierVerify(t *testing.T) { ver, err := NewVerifier(key, WithRequiredTag("profile", - WithComponents("@method"), + WithComponents(`"@method"`), WithRequiredParameters("nonce"), WithForbiddenParameters("keyid", "alg"), WithParameter("score", 1.2349), From 40531bd495c920beb3cefd5b87ca3997530f3079 Mon Sep 17 00:00:00 2001 From: Dimitrij Drus Date: Sat, 7 Nov 2026 23:41:13 +0100 Subject: [PATCH 2/4] godoc updated --- httpsig.go | 6 ++++-- options.go | 9 +++++---- verifier.go | 2 ++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/httpsig.go b/httpsig.go index 354bcd7..da0a80a 100644 --- a/httpsig.go +++ b/httpsig.go @@ -309,8 +309,10 @@ type SignatureInfo struct { Message *Message // Components maps covered component identifiers to the canonicalized - // values used to construct the signature base. It excludes the synthetic - // @signature-params component. Identifier parameters are retained in keys. + // values used to construct the signature base. Map keys are serialized + // Structured Fields String Items (e.g. `"@method"` or `"content-digest";req`), + // including identifier parameters. It excludes the synthetic + // @signature-params component. Components map[string]string // Parameters contains the transmitted, decoded signature parameters. diff --git a/options.go b/options.go index 79f07cc..8f52ccb 100644 --- a/options.go +++ b/options.go @@ -125,10 +125,11 @@ func WithTTL(ttl time.Duration) SignerOption { // WithComponents specifies component identifiers to cover when signing, // require during verification, or request in Accept-Signature. -// For `@query-param`, the `name` component parameter must contain the -// canonical percent-encoded parameter name. For example, use -// `"@query-param";name="x%2By"` for a query parameter named `x+y`; a literal -// `+` in `name` is rejected. +// Each identifier must be a serialized Structured Fields String Item, such as +// `"@method"` or `"content-digest";req`. For `@query-param`, the `name` +// component parameter must contain the canonical percent-encoded parameter name. +// For example, use `"@query-param";name="x%2By"` for a query parameter named +// `x+y`; a literal `+` in `name` is rejected. func WithComponents(identifiers ...string) componentsOption { return componentsOption{identifiers: identifiers} } diff --git a/verifier.go b/verifier.go index 0240044..c14338a 100644 --- a/verifier.go +++ b/verifier.go @@ -22,6 +22,8 @@ type VerifiedSignature struct { // Components contains the canonicalized values of the covered HTTP // message components, without the synthetic @signature-params component. + // Map keys are serialized Structured Fields String Items (e.g. `"@method"` + // or `"content-digest";req`). Components map[string]string // Parameters contains the actual transmitted signature parameters, From 6a50aa88ab6db79dd234c30ef13e60577f4d6963 Mon Sep 17 00:00:00 2001 From: Dimitrij Drus Date: Sat, 7 Nov 2026 23:41:24 +0100 Subject: [PATCH 3/4] readme updated --- README.md | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 9983469..cf6b70c 100644 --- a/README.md +++ b/README.md @@ -18,16 +18,6 @@ For API details and option semantics, see the [Go reference](https://pkg.go.dev/ ## Usage -> [!NOTE] -> Component identifiers are represented as [Structured Fields Items](https://www.rfc-editor.org/rfc/rfc9421.html#section-2.5), with quoted component names (for example, `"@method"` or `"content-digest";req`). For convenience, configuration options such as `WithComponents` also accept a simplified form that omits the quotes around the component name. Both forms are equivalent: -> -> ```go -> httpsig.WithComponents(`"@method"`, `"content-digest";req`) // Structured Fields form -> httpsig.WithComponents("@method", "content-digest;req") // simplified form -> ``` -> -> When parsing an `Accept-Signature` header, `httpsig` returns the serialized Structured Fields form in `SignatureRequest.Components`. These values can be passed directly to `WithComponents`. - ### Standalone Signing and Verification The following snippets show two independent flows: signing an outgoing request and verifying a received response. They assume application-provided keys and other values; routine error handling is abbreviated. @@ -45,7 +35,7 @@ signer, err := httpsig.NewSigner( // duration for which the signature should be valid httpsig.WithTTL(5 * time.Second), // which components should be protected by a signature - httpsig.WithComponents("@authority", "@method", "x-my-fancy-header"), + httpsig.WithComponents(`"@authority"`, `"@method"`, `"x-my-fancy-header"`), // a tag for your specific application httpsig.WithTag("myapp"), // custom parameter @@ -90,7 +80,7 @@ verifier, err := httpsig.NewVerifier( keyResolver, // specify the required options // which response components are expected to be protected - httpsig.WithComponents("@status", "content-type"), + httpsig.WithComponents(`"@status"`, `"content-type"`), // validity time skew httpsig.WithValidityTolerance(5 * time.Second), // how old a signature is allowed to be @@ -122,7 +112,7 @@ verifier, err := httpsig.NewVerifier( // tag of the signature "myapp", // which components are expected to be protected by a signature - httpsig.WithComponents("@authority", "@method", "x-my-fancy-header"), + httpsig.WithComponents(`"@authority"`, `"@method"`, `"x-my-fancy-header"`), // validity time skew httpsig.WithValidityTolerance(5 * time.Second), // how old a signature is allowed to be @@ -204,7 +194,7 @@ builder, err := httpsig.NewAcceptSignature( // request the label and covered components httpsig.WithLabel("bar"), // specify which components should be covered by the signature - httpsig.WithComponents("@status", "content-digest;req", "content-digest"), + httpsig.WithComponents(`"@status"`, `"content-digest";req`, `"content-digest"`), // specify your content digest algorithm references httpsig.WithContentDigestAlgorithmPreferences(httpsig.AlgorithmPreference{Algorithm: httpsig.Sha256, Preference: 2}), // specify which tag the server should use @@ -288,7 +278,7 @@ verifier, err := httpsig.NewVerifier( // tag of the signature "myapp", // which components are expected to be protected by a signature - httpsig.WithComponents("@authority", "@method", "x-my-fancy-header"), + httpsig.WithComponents(`"@authority"`, `"@method"`, `"x-my-fancy-header"`), // validity time skew httpsig.WithValidityTolerance(5 * time.Second), // how old a signature is allowed to be @@ -370,7 +360,7 @@ signer, err := httpsig.NewSigner( }, httpsig.WithKeyID("demo-ml-dsa-44"), httpsig.WithAlg(), - httpsig.WithComponents("@method", "@path"), + httpsig.WithComponents(`"@method"`, `"@path"`), ) // error handling @@ -387,7 +377,7 @@ verifier, err := httpsig.NewVerifier( Algorithm: "ml-dsa-44", }, httpsig.WithValidateAllSignatures(), - httpsig.WithComponents("@method", "@path"), + httpsig.WithComponents(`"@method"`, `"@path"`), ) // error handling @@ -438,7 +428,7 @@ signer, err := httpsig.NewSigner( Algorithm: httpsig.EcdsaP256Sha256, }, httpsig.WithKeyID(keyID), - httpsig.WithComponents("@method", "@path"), + httpsig.WithComponents(`"@method"`, `"@path"`), ) // error handling @@ -459,7 +449,7 @@ The [WIMSE HTTP Signatures draft](https://datatracker.ietf.org/doc/html/draft-ie signer, err := httpsig.NewSigner( httpsig.Key{Material: privateKey, Algorithm: httpsig.Ed25519}, httpsig.WithTag("wimse-workload-to-workload"), - httpsig.WithComponents("@method", "@path", "@query", "workload-identity-token"), + httpsig.WithComponents(`"@method"`, `"@path"`, `"@query"`, `"workload-identity-token"`), ) req.Header, err = signer.Sign(httpsig.MessageFromRequest(req), @@ -470,7 +460,7 @@ req.Header, err = signer.Sign(httpsig.MessageFromRequest(req), // Recipient verifier, err := httpsig.NewVerifier(witKeyResolver, httpsig.WithRequiredTag("wimse-workload-to-workload", - httpsig.WithComponents("@method", "@path", "@query", "workload-identity-token"), + httpsig.WithComponents(`"@method"`, `"@path"`, `"@query"`, `"workload-identity-token"`), httpsig.WithRequiredParameters(httpsig.Nonce), httpsig.WithForbiddenParameters(httpsig.KeyID, httpsig.Alg), httpsig.WithParameter("wimse-aud", expectedAudience), @@ -494,7 +484,7 @@ signer, err := httpsig.NewSigner( httpsig.WithLabel("sig1"), httpsig.WithTag("web-bot-auth"), httpsig.WithKeyID(jwkThumbprint), - httpsig.WithComponents("@authority", `signature-agent;key="sig1"`), + httpsig.WithComponents(`"@authority"`, `"signature-agent";key="sig1"`), ) req.Header, err = signer.Sign(httpsig.MessageFromRequest(req)) @@ -502,7 +492,7 @@ req.Header, err = signer.Sign(httpsig.MessageFromRequest(req)) // Recipient: resolve the covered Signature-Agent member dynamically. verifier, err := httpsig.NewVerifier(webBotKeyResolver, httpsig.WithRequiredTag("web-bot-auth", - httpsig.WithComponents("@authority", `signature-agent;key="sig1"`), + httpsig.WithComponents(`"@authority"`, `"signature-agent";key="sig1"`), httpsig.WithRequiredParameters(httpsig.KeyID), ), ) @@ -523,14 +513,14 @@ signer, err := httpsig.NewSigner( httpsig.Key{Material: privateKey, Algorithm: httpsig.Ed25519}, httpsig.WithLabel("sig"), httpsig.WithTTL(0), - httpsig.WithComponents("@method", "@authority", "@path", "signature-key"), + httpsig.WithComponents(`"@method"`, `"@authority"`, `"@path"`, `"signature-key"`), ) req.Header, err = signer.Sign(httpsig.MessageFromRequest(req)) // Resource server verifier, err := httpsig.NewVerifier(aauthKeyResolver, httpsig.WithValidateAllSignatures(), - httpsig.WithComponents("@method", "@authority", "@path", "signature-key"), + httpsig.WithComponents(`"@method"`, `"@authority"`, `"@path"`, `"signature-key"`), httpsig.WithExpiresTimestamp(false), httpsig.WithMaxAge(time.Minute), ) @@ -549,7 +539,7 @@ signer, err := httpsig.NewSigner( httpsig.Key{Material: privateKey, Algorithm: httpsig.Ed25519}, httpsig.WithTag("httpsig-oauth"), httpsig.WithTTL(0), - httpsig.WithComponents("@method", "@target-uri", "authorization"), + httpsig.WithComponents(`"@method"`, `"@target-uri"`, `"authorization"`), ) req.Header, err = signer.Sign(httpsig.MessageFromRequest(req), @@ -559,7 +549,7 @@ req.Header, err = signer.Sign(httpsig.MessageFromRequest(req), // Resource server verifier, err := httpsig.NewVerifier(tokenBoundKeyResolver, httpsig.WithRequiredTag("httpsig-oauth", - httpsig.WithComponents("@method", "@target-uri", "authorization"), + httpsig.WithComponents(`"@method"`, `"@target-uri"`, `"authorization"`), httpsig.WithExpiresTimestamp(false), httpsig.WithRequiredParameters(httpsig.Nonce), httpsig.WithForbiddenParameters(httpsig.KeyID, httpsig.Alg, "pub"), From 7cbd6f295a0b09b04f65911af874cdbc9cb9d8f9 Mon Sep 17 00:00:00 2001 From: Dimitrij Drus Date: Sat, 7 Nov 2026 23:41:41 +0100 Subject: [PATCH 4/4] rule in agents.md updated --- AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 69b3d52..3081ab1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -27,7 +27,7 @@ This file provides repository-specific guidance for AI coding agents working on - Validate before lossy conversion: preserve parameter presence, types, and values until the relevant checks are complete. - Preserve protocol-level type distinctions across parsing, serialization, public APIs, and comparisons. When changing supported types, test round trips and assess compatibility implications. - Review untrusted-input paths for possible panics, including unchecked type assertions, nil dereferences, and dependency behavior. Malformed protocol input should produce controlled errors. -- Respect the API contracts for Structured Fields component identifiers and any supported simplified configuration syntax. +- Public component identifier strings in configuration and results use serialized Structured Fields String Items. - Never relax signature, component, timestamp, nonce, algorithm, or key validation merely to satisfy tests. Assess security and compatibility implications explicitly. ## Test design