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
12 changes: 8 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,14 @@ func WithValidityTolerance(tolerance time.Duration) VerifierOption {
}

// WithMaxAge specifies the maximum allowed age of a signature with a
// created timestamp.
func WithMaxAge(d time.Duration) VerifierOption {
// created timestamp. The duration must be positive.
func WithMaxAge(age time.Duration) VerifierOption {
return verifierOptionFunc(func(_ *verifier, e *verificationPolicy, _ bool) error {
e.maxAge = d
if age <= 0 {
return fmt.Errorf("%w: max age must be positive", ErrParameter)
}

e.maxAge = &age

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

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

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

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

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

assert.Equal(t, 15*time.Minute, exp.maxAge)
err := WithMaxAge(tc.maxAge).applyVerifierOption(&verifier{}, policy, false)
if tc.wantError {
require.ErrorIs(t, err, ErrParameter)
assert.Nil(t, policy.maxAge)
} else {
require.NoError(t, err)
require.NotNil(t, policy.maxAge)
assert.Equal(t, tc.maxAge, *policy.maxAge)
}
})
}
}

func TestWithRequiredTag(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions verification_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type verificationPolicy struct {
tolerance *time.Duration
maxAge time.Duration
maxAge *time.Duration
identifiers []*componentIdentifier
asb *AcceptSignatureBuilder
reqCreatedTS *bool
Expand Down Expand Up @@ -61,7 +61,7 @@ func (e *verificationPolicy) inherit(globalPolicy *verificationPolicy, tag strin
e.tolerance = globalPolicy.tolerance
}

if e.maxAge == -1 {
if e.maxAge == nil {
e.maxAge = globalPolicy.maxAge
}

Expand Down Expand Up @@ -280,7 +280,7 @@ func (e *verificationPolicy) assertMetadata(si *signatureInput, msg *Message) er
return fmt.Errorf("%w: signature not yet valid", ErrValidity)
}

if created.Add(e.maxAge).Before(now) {
if e.maxAge != nil && *e.maxAge > 0 && created.Add(*e.maxAge).Before(now) {
return fmt.Errorf("%w: signature too old", ErrValidity)
}
}
Expand Down
20 changes: 12 additions & 8 deletions verification_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestVerificationPolicyInherit(t *testing.T) {
required := true
global := &verificationPolicy{
tolerance: new(2 * time.Second),
maxAge: 30 * time.Second,
maxAge: new(30 * time.Second),
identifiers: globalIDs,
reqCreatedTS: &required,
reqExpiresTS: &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{maxAge: -1}
local := &verificationPolicy{}
require.NoError(t, local.inherit(global, "profile"))

assert.Equal(t, global.tolerance, local.tolerance)
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestVerificationPolicyInherit(t *testing.T) {
localBuilder := &AcceptSignatureBuilder{label: "custom"}
local := &verificationPolicy{
tolerance: new(time.Second),
maxAge: 5 * time.Second,
maxAge: new(5 * time.Second),
identifiers: localIDs,
reqCreatedTS: &created,
reqExpiresTS: &expires,
Expand All @@ -115,7 +115,8 @@ func TestVerificationPolicyInherit(t *testing.T) {

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

local.maxAge = -1
require.ErrorIs(t, local.inherit(global, "profile"), ErrParameter)
})
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) {
}(),
},
exp: verificationPolicy{
maxAge: 30 * time.Second,
maxAge: new(30 * time.Second),
reqCreatedTS: &falseVal,
reqExpiresTS: &falseVal,
},
Expand All @@ -281,7 +281,11 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) {
return params
}(),
},
exp: verificationPolicy{reqCreatedTS: &falseVal, reqExpiresTS: &falseVal},
exp: verificationPolicy{
maxAge: new(time.Second),
reqCreatedTS: &falseVal,
reqExpiresTS: &falseVal,
},
assert: func(t *testing.T, err error) {
t.Helper()

Expand Down Expand Up @@ -377,7 +381,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) {

return verificationPolicy{
identifiers: ids,
maxAge: 5 * time.Second,
maxAge: new(5 * time.Second),
}
}(),
assert: func(t *testing.T, err error) {
Expand Down
2 changes: 1 addition & 1 deletion verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewVerifier(resolver KeyResolver, opts ...VerifierOption) (Verifier, error)
}

globalPolicy := &verificationPolicy{
maxAge: 30 * time.Second, //nolint:mnd
maxAge: new(30 * time.Second), //nolint:mnd
reqExpiresTS: new(true),
reqCreatedTS: new(true),
}
Expand Down
Loading