Skip to content

Commit 613deeb

Browse files
committed
fix: Rejecting negative validity tolerances and eliminate inconsistent handling of -1 between global and tag-specific verifier configurations
1 parent 4dedf74 commit 613deeb

5 files changed

Lines changed: 62 additions & 34 deletions

File tree

‎options.go‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,15 @@ func validateNonceOption(value any) (string, error) {
366366
}
367367

368368
// WithValidityTolerance specifies the allowed clock skew for created and
369-
// expires timestamp checks.
369+
// expires timestamp checks. The duration must not be negative; zero allows
370+
// no clock skew.
370371
func WithValidityTolerance(d time.Duration) VerifierOption {
371372
return verifierOptionFunc(func(_ *verifier, e *verificationPolicy, _ bool) error {
372-
e.tolerance = d
373+
if d < 0 {
374+
return fmt.Errorf("%w: validity tolerance must not be negative", ErrParameter)
375+
}
376+
377+
e.tolerance = &d
373378

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

404-
policy := &verificationPolicy{
405-
tolerance: -1,
406-
maxAge: -1,
407-
}
409+
policy := &verificationPolicy{maxAge: -1}
408410

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

‎options_test.go‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -618,14 +618,29 @@ func TestWithParameterForAcceptSignatureBuild(t *testing.T) {
618618
func TestWithValidityTolerance(t *testing.T) {
619619
t.Parallel()
620620

621-
v := &verifier{}
622-
exp := &verificationPolicy{}
623-
624-
err := WithValidityTolerance(1*time.Minute).applyVerifierOption(v, exp, false)
625-
require.NoError(t, err)
621+
for uc, tc := range map[string]struct {
622+
tolerance time.Duration
623+
wantError bool
624+
}{
625+
"positive tolerance": {tolerance: time.Minute},
626+
"zero tolerance": {},
627+
"negative tolerance": {tolerance: -time.Second, wantError: true},
628+
"minus one nanosecond": {tolerance: -1, wantError: true},
629+
} {
630+
t.Run(uc, func(t *testing.T) {
631+
policy := &verificationPolicy{}
626632

627-
assert.Empty(t, v.tagPolicies)
628-
assert.Equal(t, 1*time.Minute, exp.tolerance)
633+
err := WithValidityTolerance(tc.tolerance).applyVerifierOption(&verifier{}, policy, false)
634+
if tc.wantError {
635+
require.ErrorIs(t, err, ErrParameter)
636+
assert.Nil(t, policy.tolerance)
637+
} else {
638+
require.NoError(t, err)
639+
require.NotNil(t, policy.tolerance)
640+
assert.Equal(t, tc.tolerance, *policy.tolerance)
641+
}
642+
})
643+
}
629644
}
630645

631646
func TestWithMaxAge(t *testing.T) {
@@ -654,7 +669,7 @@ func TestWithRequiredTag(t *testing.T) {
654669
require.NoError(t, err)
655670
require.NotNil(t, exp)
656671
assert.Empty(t, exp.identifiers)
657-
assert.Equal(t, time.Duration(-1), exp.tolerance) //nolint:testifylint
672+
assert.Nil(t, exp.tolerance)
658673
},
659674
},
660675
"with identifiers only": {
@@ -668,7 +683,7 @@ func TestWithRequiredTag(t *testing.T) {
668683
require.NotNil(t, exp)
669684
assert.Len(t, exp.identifiers, 1)
670685
assert.Equal(t, "@status", exp.identifiers[0].Value)
671-
assert.Equal(t, time.Duration(-1), exp.tolerance) //nolint:testifylint
686+
assert.Nil(t, exp.tolerance)
672687
},
673688
},
674689
"with tolerance only": {
@@ -681,7 +696,8 @@ func TestWithRequiredTag(t *testing.T) {
681696
require.NoError(t, err)
682697
require.NotNil(t, exp)
683698
assert.Empty(t, exp.identifiers)
684-
assert.Equal(t, 5*time.Minute, exp.tolerance)
699+
require.NotNil(t, exp.tolerance)
700+
assert.Equal(t, 5*time.Minute, *exp.tolerance)
685701
},
686702
},
687703
"with all possible options": {
@@ -696,7 +712,8 @@ func TestWithRequiredTag(t *testing.T) {
696712
require.NotNil(t, exp)
697713
assert.Len(t, exp.identifiers, 1)
698714
assert.Equal(t, "@status", exp.identifiers[0].Value)
699-
assert.Equal(t, 5*time.Minute, exp.tolerance)
715+
require.NotNil(t, exp.tolerance)
716+
assert.Equal(t, 5*time.Minute, *exp.tolerance)
700717
},
701718
},
702719
"with unsupported identifier": {

‎verification_policy.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
type verificationPolicy struct {
15-
tolerance time.Duration
15+
tolerance *time.Duration
1616
maxAge time.Duration
1717
identifiers []*componentIdentifier
1818
asb *AcceptSignatureBuilder
@@ -57,7 +57,7 @@ func (e *verificationPolicy) withOptions(tag string, opts []VerifyOption) (*veri
5757
// inherit applies global requirements to a tag-specific configuration.
5858
// Tag-specific values take precedence, while parameter requirements remain additive.
5959
func (e *verificationPolicy) inherit(globalPolicy *verificationPolicy, tag string) error {
60-
if e.tolerance == -1 {
60+
if e.tolerance == nil {
6161
e.tolerance = globalPolicy.tolerance
6262
}
6363

@@ -252,16 +252,20 @@ func (e *verificationPolicy) assertMetadata(si *signatureInput, msg *Message) er
252252
missingComponents []string
253253
cmv compositeMessageVerifier
254254
mv messageVerifier
255+
tolerance time.Duration
255256
)
256257

257258
now := time.Now().UTC()
259+
if e.tolerance != nil {
260+
tolerance = *e.tolerance
261+
}
258262

259263
expires, expiresPresent := si.expires()
260264
if !expiresPresent {
261265
if *e.reqExpiresTS {
262266
return fmt.Errorf("%w: expected expires parameter not present", ErrMissingParameter)
263267
}
264-
} else if now.After(expires.Add(e.tolerance)) {
268+
} else if now.After(expires.Add(tolerance)) {
265269
return fmt.Errorf("%w: signature expired", ErrValidity)
266270
}
267271

@@ -271,7 +275,7 @@ func (e *verificationPolicy) assertMetadata(si *signatureInput, msg *Message) er
271275
return fmt.Errorf("%w: expected created parameter not present", ErrMissingParameter)
272276
}
273277
} else {
274-
if now.Before(created.Add(-1 * e.tolerance)) {
278+
if now.Before(created.Add(-1 * tolerance)) {
275279
return fmt.Errorf("%w: signature not yet valid", ErrValidity)
276280
}
277281

‎verification_policy_test.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestVerificationPolicyInherit(t *testing.T) {
6060

6161
required := true
6262
global := &verificationPolicy{
63-
tolerance: 2 * time.Second,
63+
tolerance: new(2 * time.Second),
6464
maxAge: 30 * time.Second,
6565
identifiers: globalIDs,
6666
reqCreatedTS: &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{tolerance: -1, maxAge: -1}
77+
local := &verificationPolicy{maxAge: -1}
7878
require.NoError(t, local.inherit(global, "profile"))
7979

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

105105
localBuilder := &AcceptSignatureBuilder{label: "custom"}
106106
local := &verificationPolicy{
107-
tolerance: time.Second,
107+
tolerance: new(time.Second),
108108
maxAge: 5 * time.Second,
109109
identifiers: localIDs,
110110
reqCreatedTS: &created,
@@ -113,7 +113,8 @@ func TestVerificationPolicyInherit(t *testing.T) {
113113
}
114114
require.NoError(t, local.inherit(global, "profile"))
115115

116-
assert.Equal(t, time.Second, local.tolerance)
116+
require.NotNil(t, local.tolerance)
117+
assert.Equal(t, time.Second, *local.tolerance)
117118
assert.Equal(t, 5*time.Second, local.maxAge)
118119
assert.Equal(t, localIDs, local.identifiers)
119120
assert.False(t, *local.reqCreatedTS)
@@ -139,7 +140,6 @@ func TestVerificationPolicyInherit(t *testing.T) {
139140
t.Run(uc, func(t *testing.T) {
140141
t.Parallel()
141142

142-
local.tolerance = -1
143143
local.maxAge = -1
144144
require.ErrorIs(t, local.inherit(global, "profile"), ErrParameter)
145145
})
@@ -184,7 +184,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) {
184184
}(),
185185
},
186186
exp: verificationPolicy{
187-
tolerance: 3 * time.Second,
187+
tolerance: new(3 * time.Second),
188188
reqCreatedTS: &falseVal,
189189
reqExpiresTS: &falseVal,
190190
},
@@ -204,7 +204,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) {
204204
}(),
205205
},
206206
exp: verificationPolicy{
207-
tolerance: 2 * time.Second,
207+
tolerance: new(2 * time.Second),
208208
reqCreatedTS: &falseVal,
209209
reqExpiresTS: &falseVal,
210210
},
@@ -242,7 +242,7 @@ func TestVerificationPolicyAssertMetadata(t *testing.T) {
242242
}(),
243243
},
244244
exp: verificationPolicy{
245-
tolerance: 3 * time.Second,
245+
tolerance: new(3 * time.Second),
246246
reqCreatedTS: &falseVal,
247247
reqExpiresTS: &falseVal,
248248
},

‎verifier_test.go‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ func TestNewVerifier(t *testing.T) {
186186
exp := v.tagPolicies["test"]
187187
assert.Len(t, exp.identifiers, 1)
188188
assert.Equal(t, "@status", exp.identifiers[0].Value)
189-
assert.Equal(t, 1*time.Second, exp.tolerance)
189+
require.NotNil(t, exp.tolerance)
190+
assert.Equal(t, 1*time.Second, *exp.tolerance)
190191
},
191192
},
192193
"with global defaults without signature negotiation": {
@@ -209,7 +210,8 @@ func TestNewVerifier(t *testing.T) {
209210
exp := v.tagPolicies["test"]
210211
assert.Len(t, exp.identifiers, 1)
211212
assert.Equal(t, "@status", exp.identifiers[0].Value)
212-
assert.Equal(t, 1*time.Second, exp.tolerance)
213+
require.NotNil(t, exp.tolerance)
214+
assert.Equal(t, 1*time.Second, *exp.tolerance)
213215
assert.Nil(t, exp.asb)
214216
},
215217
},
@@ -238,7 +240,8 @@ func TestNewVerifier(t *testing.T) {
238240
assert.Len(t, exp.identifiers, 2)
239241
assert.Equal(t, "@status", exp.identifiers[0].Value)
240242
assert.Equal(t, "content-digest", exp.identifiers[1].Value)
241-
assert.Equal(t, 1*time.Second, exp.tolerance)
243+
require.NotNil(t, exp.tolerance)
244+
assert.Equal(t, 1*time.Second, *exp.tolerance)
242245
require.NotNil(t, exp.asb)
243246
assert.Nil(t, exp.asb.nonceGetter)
244247
assert.Equal(t, "sig", exp.asb.label)
@@ -275,13 +278,15 @@ func TestNewVerifier(t *testing.T) {
275278
exp := v.tagPolicies[""]
276279
assert.Len(t, exp.identifiers, 1)
277280
assert.Equal(t, "@method", exp.identifiers[0].Value)
278-
assert.Equal(t, 1*time.Minute, exp.tolerance)
281+
require.NotNil(t, exp.tolerance)
282+
assert.Equal(t, 1*time.Minute, *exp.tolerance)
279283

280284
require.Contains(t, v.tagPolicies, "test")
281285
exp = v.tagPolicies["test"]
282286
assert.Len(t, exp.identifiers, 1)
283287
assert.Equal(t, "@status", exp.identifiers[0].Value)
284-
assert.Equal(t, 1*time.Second, exp.tolerance)
288+
require.NotNil(t, exp.tolerance)
289+
assert.Equal(t, 1*time.Second, *exp.tolerance)
285290
},
286291
},
287292
"parameter rules inherited from global configuration": {

0 commit comments

Comments
 (0)