Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,15 @@ func validateNonceOption(value any) (string, error) {
}

// WithValidityTolerance specifies the allowed clock skew for created and
// expires timestamp checks.
func WithValidityTolerance(d time.Duration) VerifierOption {
// expires timestamp checks. The duration must not be negative; zero allows
// no clock skew.
func WithValidityTolerance(tolerance time.Duration) VerifierOption {
return verifierOptionFunc(func(_ *verifier, e *verificationPolicy, _ bool) error {
e.tolerance = d
if tolerance < 0 {
return fmt.Errorf("%w: validity tolerance must not be negative", ErrParameter)
}

e.tolerance = &tolerance

return nil
})
Expand Down Expand Up @@ -401,10 +406,7 @@ func WithRequiredTag(tag string, opts ...VerifierOption) VerifierOption {
return fmt.Errorf("%w: requirements for tag %s are already configured", ErrParameter, tag)
}

policy := &verificationPolicy{
tolerance: -1,
maxAge: -1,
}
policy := &verificationPolicy{maxAge: -1}

for _, opt := range opts {
if err := opt.applyVerifierOption(ver, policy, true); err != nil {
Expand Down
39 changes: 28 additions & 11 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,14 +618,29 @@ func TestWithParameterForAcceptSignatureBuild(t *testing.T) {
func TestWithValidityTolerance(t *testing.T) {
t.Parallel()

v := &verifier{}
exp := &verificationPolicy{}

err := WithValidityTolerance(1*time.Minute).applyVerifierOption(v, exp, false)
require.NoError(t, err)
for uc, tc := range map[string]struct {
tolerance time.Duration
wantError bool
}{
"positive tolerance": {tolerance: time.Minute},
"zero tolerance": {},
"negative tolerance": {tolerance: -time.Second, wantError: true},
"minus one nanosecond": {tolerance: -1, wantError: true},
} {
t.Run(uc, func(t *testing.T) {
policy := &verificationPolicy{}

assert.Empty(t, v.tagPolicies)
assert.Equal(t, 1*time.Minute, exp.tolerance)
err := WithValidityTolerance(tc.tolerance).applyVerifierOption(&verifier{}, policy, false)
if tc.wantError {
require.ErrorIs(t, err, ErrParameter)
assert.Nil(t, policy.tolerance)
} else {
require.NoError(t, err)
require.NotNil(t, policy.tolerance)
assert.Equal(t, tc.tolerance, *policy.tolerance)
}
})
}
}

func TestWithMaxAge(t *testing.T) {
Expand Down Expand Up @@ -654,7 +669,7 @@ func TestWithRequiredTag(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, exp)
assert.Empty(t, exp.identifiers)
assert.Equal(t, time.Duration(-1), exp.tolerance) //nolint:testifylint
assert.Nil(t, exp.tolerance)
},
},
"with identifiers only": {
Expand All @@ -668,7 +683,7 @@ func TestWithRequiredTag(t *testing.T) {
require.NotNil(t, exp)
assert.Len(t, exp.identifiers, 1)
assert.Equal(t, "@status", exp.identifiers[0].Value)
assert.Equal(t, time.Duration(-1), exp.tolerance) //nolint:testifylint
assert.Nil(t, exp.tolerance)
},
},
"with tolerance only": {
Expand All @@ -681,7 +696,8 @@ func TestWithRequiredTag(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, exp)
assert.Empty(t, exp.identifiers)
assert.Equal(t, 5*time.Minute, exp.tolerance)
require.NotNil(t, exp.tolerance)
assert.Equal(t, 5*time.Minute, *exp.tolerance)
},
},
"with all possible options": {
Expand All @@ -696,7 +712,8 @@ func TestWithRequiredTag(t *testing.T) {
require.NotNil(t, exp)
assert.Len(t, exp.identifiers, 1)
assert.Equal(t, "@status", exp.identifiers[0].Value)
assert.Equal(t, 5*time.Minute, exp.tolerance)
require.NotNil(t, exp.tolerance)
assert.Equal(t, 5*time.Minute, *exp.tolerance)
},
},
"with unsupported identifier": {
Expand Down
13 changes: 9 additions & 4 deletions verification_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type verificationPolicy struct {
tolerance time.Duration
tolerance *time.Duration
maxAge time.Duration
identifiers []*componentIdentifier
asb *AcceptSignatureBuilder
Expand Down Expand Up @@ -57,7 +57,7 @@ func (e *verificationPolicy) withOptions(tag string, opts []VerifyOption) (*veri
// inherit applies global requirements to a tag-specific configuration.
// Tag-specific values take precedence, while parameter requirements remain additive.
func (e *verificationPolicy) inherit(globalPolicy *verificationPolicy, tag string) error {
if e.tolerance == -1 {
if e.tolerance == nil {
e.tolerance = globalPolicy.tolerance
}

Expand Down Expand Up @@ -252,16 +252,21 @@ func (e *verificationPolicy) assertMetadata(si *signatureInput, msg *Message) er
missingComponents []string
cmv compositeMessageVerifier
mv messageVerifier
tolerance time.Duration
)

now := time.Now().UTC()

if e.tolerance != nil {
tolerance = *e.tolerance
}

expires, expiresPresent := si.expires()
if !expiresPresent {
if *e.reqExpiresTS {
return fmt.Errorf("%w: expected expires parameter not present", ErrMissingParameter)
}
} else if now.After(expires.Add(e.tolerance)) {
} else if now.After(expires.Add(tolerance)) {
return fmt.Errorf("%w: signature expired", ErrValidity)
}

Expand All @@ -271,7 +276,7 @@ func (e *verificationPolicy) assertMetadata(si *signatureInput, msg *Message) er
return fmt.Errorf("%w: expected created parameter not present", ErrMissingParameter)
}
} else {
if now.Before(created.Add(-1 * e.tolerance)) {
if now.Before(created.Add(-1 * tolerance)) {
return fmt.Errorf("%w: signature not yet valid", ErrValidity)
}

Expand Down
16 changes: 8 additions & 8 deletions verification_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestVerificationPolicyInherit(t *testing.T) {

required := true
global := &verificationPolicy{
tolerance: 2 * time.Second,
tolerance: new(2 * time.Second),
maxAge: 30 * time.Second,
identifiers: globalIDs,
reqCreatedTS: &required,
Expand All @@ -74,7 +74,7 @@ func TestVerificationPolicyInherit(t *testing.T) {
t.Run("inherits global configuration", func(t *testing.T) {
t.Parallel()

local := &verificationPolicy{tolerance: -1, maxAge: -1}
local := &verificationPolicy{maxAge: -1}
require.NoError(t, local.inherit(global, "profile"))

assert.Equal(t, global.tolerance, local.tolerance)
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestVerificationPolicyInherit(t *testing.T) {

localBuilder := &AcceptSignatureBuilder{label: "custom"}
local := &verificationPolicy{
tolerance: time.Second,
tolerance: new(time.Second),
maxAge: 5 * time.Second,
identifiers: localIDs,
reqCreatedTS: &created,
Expand All @@ -113,7 +113,8 @@ func TestVerificationPolicyInherit(t *testing.T) {
}
require.NoError(t, local.inherit(global, "profile"))

assert.Equal(t, time.Second, local.tolerance)
require.NotNil(t, local.tolerance)
assert.Equal(t, time.Second, *local.tolerance)
assert.Equal(t, 5*time.Second, local.maxAge)
assert.Equal(t, localIDs, local.identifiers)
assert.False(t, *local.reqCreatedTS)
Expand All @@ -139,7 +140,6 @@ func TestVerificationPolicyInherit(t *testing.T) {
t.Run(uc, func(t *testing.T) {
t.Parallel()

local.tolerance = -1
local.maxAge = -1
require.ErrorIs(t, local.inherit(global, "profile"), ErrParameter)
})
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) {
}(),
},
exp: verificationPolicy{
tolerance: 3 * time.Second,
tolerance: new(3 * time.Second),
reqCreatedTS: &falseVal,
reqExpiresTS: &falseVal,
},
Expand All @@ -204,7 +204,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) {
}(),
},
exp: verificationPolicy{
tolerance: 2 * time.Second,
tolerance: new(2 * time.Second),
reqCreatedTS: &falseVal,
reqExpiresTS: &falseVal,
},
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) {
}(),
},
exp: verificationPolicy{
tolerance: 3 * time.Second,
tolerance: new(3 * time.Second),
reqCreatedTS: &falseVal,
reqExpiresTS: &falseVal,
},
Expand Down
15 changes: 10 additions & 5 deletions verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ func TestNewVerifier(t *testing.T) {
exp := v.tagPolicies["test"]
assert.Len(t, exp.identifiers, 1)
assert.Equal(t, "@status", exp.identifiers[0].Value)
assert.Equal(t, 1*time.Second, exp.tolerance)
require.NotNil(t, exp.tolerance)
assert.Equal(t, 1*time.Second, *exp.tolerance)
},
},
"with global defaults without signature negotiation": {
Expand All @@ -209,7 +210,8 @@ func TestNewVerifier(t *testing.T) {
exp := v.tagPolicies["test"]
assert.Len(t, exp.identifiers, 1)
assert.Equal(t, "@status", exp.identifiers[0].Value)
assert.Equal(t, 1*time.Second, exp.tolerance)
require.NotNil(t, exp.tolerance)
assert.Equal(t, 1*time.Second, *exp.tolerance)
assert.Nil(t, exp.asb)
},
},
Expand Down Expand Up @@ -238,7 +240,8 @@ func TestNewVerifier(t *testing.T) {
assert.Len(t, exp.identifiers, 2)
assert.Equal(t, "@status", exp.identifiers[0].Value)
assert.Equal(t, "content-digest", exp.identifiers[1].Value)
assert.Equal(t, 1*time.Second, exp.tolerance)
require.NotNil(t, exp.tolerance)
assert.Equal(t, 1*time.Second, *exp.tolerance)
require.NotNil(t, exp.asb)
assert.Nil(t, exp.asb.nonceGetter)
assert.Equal(t, "sig", exp.asb.label)
Expand Down Expand Up @@ -275,13 +278,15 @@ func TestNewVerifier(t *testing.T) {
exp := v.tagPolicies[""]
assert.Len(t, exp.identifiers, 1)
assert.Equal(t, "@method", exp.identifiers[0].Value)
assert.Equal(t, 1*time.Minute, exp.tolerance)
require.NotNil(t, exp.tolerance)
assert.Equal(t, 1*time.Minute, *exp.tolerance)

require.Contains(t, v.tagPolicies, "test")
exp = v.tagPolicies["test"]
assert.Len(t, exp.identifiers, 1)
assert.Equal(t, "@status", exp.identifiers[0].Value)
assert.Equal(t, 1*time.Second, exp.tolerance)
require.NotNil(t, exp.tolerance)
assert.Equal(t, 1*time.Second, *exp.tolerance)
},
},
"parameter rules inherited from global configuration": {
Expand Down
Loading