Skip to content

Commit 68bfbba

Browse files
authored
fix: Inconsistent handling of -1 between global and tag-specific verifier configurations eliminated (#185)
1 parent 5c43a2d commit 68bfbba

5 files changed

Lines changed: 46 additions & 22 deletions

File tree

‎options.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,14 @@ func WithValidityTolerance(tolerance time.Duration) VerifierOption {
381381
}
382382

383383
// WithMaxAge specifies the maximum allowed age of a signature with a
384-
// created timestamp.
385-
func WithMaxAge(d time.Duration) VerifierOption {
384+
// created timestamp. The duration must be positive.
385+
func WithMaxAge(age time.Duration) VerifierOption {
386386
return verifierOptionFunc(func(_ *verifier, e *verificationPolicy, _ bool) error {
387-
e.maxAge = d
387+
if age <= 0 {
388+
return fmt.Errorf("%w: max age must be positive", ErrParameter)
389+
}
390+
391+
e.maxAge = &age
388392

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

409-
policy := &verificationPolicy{maxAge: -1}
413+
policy := &verificationPolicy{}
410414

411415
for _, opt := range opts {
412416
if err := opt.applyVerifierOption(ver, policy, true); err != nil {

‎options_test.go‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -646,13 +646,29 @@ func TestWithValidityTolerance(t *testing.T) {
646646
func TestWithMaxAge(t *testing.T) {
647647
t.Parallel()
648648

649-
v := &verifier{}
650-
exp := &verificationPolicy{}
651-
652-
err := WithMaxAge(15*time.Minute).applyVerifierOption(v, exp, false)
653-
require.NoError(t, err)
649+
for uc, tc := range map[string]struct {
650+
maxAge time.Duration
651+
wantError bool
652+
}{
653+
"positive max age": {maxAge: 15 * time.Minute},
654+
"zero max age": {wantError: true},
655+
"negative max age": {maxAge: -time.Second, wantError: true},
656+
"minus one nanosecond": {maxAge: -1, wantError: true},
657+
} {
658+
t.Run(uc, func(t *testing.T) {
659+
policy := &verificationPolicy{}
654660

655-
assert.Equal(t, 15*time.Minute, exp.maxAge)
661+
err := WithMaxAge(tc.maxAge).applyVerifierOption(&verifier{}, policy, false)
662+
if tc.wantError {
663+
require.ErrorIs(t, err, ErrParameter)
664+
assert.Nil(t, policy.maxAge)
665+
} else {
666+
require.NoError(t, err)
667+
require.NotNil(t, policy.maxAge)
668+
assert.Equal(t, tc.maxAge, *policy.maxAge)
669+
}
670+
})
671+
}
656672
}
657673

658674
func TestWithRequiredTag(t *testing.T) {

‎verification_policy.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
type verificationPolicy struct {
1515
tolerance *time.Duration
16-
maxAge time.Duration
16+
maxAge *time.Duration
1717
identifiers []*componentIdentifier
1818
asb *AcceptSignatureBuilder
1919
reqCreatedTS *bool
@@ -61,7 +61,7 @@ func (e *verificationPolicy) inherit(globalPolicy *verificationPolicy, tag strin
6161
e.tolerance = globalPolicy.tolerance
6262
}
6363

64-
if e.maxAge == -1 {
64+
if e.maxAge == nil {
6565
e.maxAge = globalPolicy.maxAge
6666
}
6767

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

283-
if created.Add(e.maxAge).Before(now) {
283+
if e.maxAge != nil && *e.maxAge > 0 && created.Add(*e.maxAge).Before(now) {
284284
return fmt.Errorf("%w: signature too old", ErrValidity)
285285
}
286286
}

‎verification_policy_test.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestVerificationPolicyInherit(t *testing.T) {
6161
required := true
6262
global := &verificationPolicy{
6363
tolerance: new(2 * time.Second),
64-
maxAge: 30 * time.Second,
64+
maxAge: new(30 * time.Second),
6565
identifiers: globalIDs,
6666
reqCreatedTS: &required,
6767
reqExpiresTS: &required,
@@ -74,7 +74,7 @@ func TestVerificationPolicyInherit(t *testing.T) {
7474
t.Run("inherits global configuration", func(t *testing.T) {
7575
t.Parallel()
7676

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

8080
assert.Equal(t, global.tolerance, local.tolerance)
@@ -105,7 +105,7 @@ func TestVerificationPolicyInherit(t *testing.T) {
105105
localBuilder := &AcceptSignatureBuilder{label: "custom"}
106106
local := &verificationPolicy{
107107
tolerance: new(time.Second),
108-
maxAge: 5 * time.Second,
108+
maxAge: new(5 * time.Second),
109109
identifiers: localIDs,
110110
reqCreatedTS: &created,
111111
reqExpiresTS: &expires,
@@ -115,7 +115,8 @@ func TestVerificationPolicyInherit(t *testing.T) {
115115

116116
require.NotNil(t, local.tolerance)
117117
assert.Equal(t, time.Second, *local.tolerance)
118-
assert.Equal(t, 5*time.Second, local.maxAge)
118+
require.NotNil(t, local.maxAge)
119+
assert.Equal(t, 5*time.Second, *local.maxAge)
119120
assert.Equal(t, localIDs, local.identifiers)
120121
assert.False(t, *local.reqCreatedTS)
121122
assert.False(t, *local.reqExpiresTS)
@@ -140,7 +141,6 @@ func TestVerificationPolicyInherit(t *testing.T) {
140141
t.Run(uc, func(t *testing.T) {
141142
t.Parallel()
142143

143-
local.maxAge = -1
144144
require.ErrorIs(t, local.inherit(global, "profile"), ErrParameter)
145145
})
146146
}
@@ -262,7 +262,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) {
262262
}(),
263263
},
264264
exp: verificationPolicy{
265-
maxAge: 30 * time.Second,
265+
maxAge: new(30 * time.Second),
266266
reqCreatedTS: &falseVal,
267267
reqExpiresTS: &falseVal,
268268
},
@@ -281,7 +281,11 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) {
281281
return params
282282
}(),
283283
},
284-
exp: verificationPolicy{reqCreatedTS: &falseVal, reqExpiresTS: &falseVal},
284+
exp: verificationPolicy{
285+
maxAge: new(time.Second),
286+
reqCreatedTS: &falseVal,
287+
reqExpiresTS: &falseVal,
288+
},
285289
assert: func(t *testing.T, err error) {
286290
t.Helper()
287291

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

378382
return verificationPolicy{
379383
identifiers: ids,
380-
maxAge: 5 * time.Second,
384+
maxAge: new(5 * time.Second),
381385
}
382386
}(),
383387
assert: func(t *testing.T, err error) {

‎verifier.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func NewVerifier(resolver KeyResolver, opts ...VerifierOption) (Verifier, error)
6565
}
6666

6767
globalPolicy := &verificationPolicy{
68-
maxAge: 30 * time.Second, //nolint:mnd
68+
maxAge: new(30 * time.Second), //nolint:mnd
6969
reqExpiresTS: new(true),
7070
reqCreatedTS: new(true),
7171
}

0 commit comments

Comments
 (0)