Skip to content

Commit

Permalink
streamline int overflow check for token timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobozzo committed Dec 2, 2024
1 parent 28272e6 commit 5b816cc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
32 changes: 9 additions & 23 deletions token/delegation/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/pkg/meta"
"github.com/ucan-wg/go-ucan/pkg/policy"
"github.com/ucan-wg/go-ucan/pkg/policy/limits"
"github.com/ucan-wg/go-ucan/token/internal/nonce"
"github.com/ucan-wg/go-ucan/token/internal/parse"
)
Expand Down Expand Up @@ -177,18 +176,6 @@ func (t *Token) validate() error {
errs = errors.Join(errs, fmt.Errorf("token nonce too small"))
}

if t.notBefore != nil {
if err := validateTimestamp(t.notBefore.Unix(), "nbf"); err != nil {
errs = errors.Join(errs, err)
}
}

if t.expiration != nil {
if err := validateTimestamp(t.expiration.Unix(), "exp"); err != nil {
errs = errors.Join(errs, err)
}
}

return errs
}

Expand Down Expand Up @@ -228,20 +215,19 @@ func tokenFromModel(m tokenPayloadModel) (*Token, error) {

tkn.meta = m.Meta

tkn.notBefore = parse.OptionalTimestamp(m.Nbf)
tkn.expiration = parse.OptionalTimestamp(m.Exp)
tkn.notBefore, err = parse.OptionalTimestamp(m.Nbf)
if err != nil {
return nil, fmt.Errorf("parse notBefore: %w", err)
}

tkn.expiration, err = parse.OptionalTimestamp(m.Exp)
if err != nil {
return nil, fmt.Errorf("parse expiration: %w", err)
}

if err := tkn.validate(); err != nil {
return nil, err
}

return &tkn, nil
}

func validateTimestamp(ts int64, field string) error {
if ts > limits.MaxInt53 || ts < limits.MinInt53 {
return fmt.Errorf("token %s timestamp %d exceeds safe integer bounds", field, ts)
}

return nil
}
13 changes: 10 additions & 3 deletions token/internal/parse/parse.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package parse

import (
"fmt"
"time"

"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/pkg/policy/limits"
)

func OptionalDID(s *string) (did.DID, error) {
Expand All @@ -13,10 +15,15 @@ func OptionalDID(s *string) (did.DID, error) {
return did.Parse(*s)
}

func OptionalTimestamp(sec *int64) *time.Time {
func OptionalTimestamp(sec *int64) (*time.Time, error) {
if sec == nil {
return nil
return nil, nil
}

if *sec > limits.MaxInt53 || *sec < limits.MinInt53 {
return nil, fmt.Errorf("timestamp value %d exceeds safe integer bounds", *sec)
}

t := time.Unix(*sec, 0)
return &t
return &t, nil
}
11 changes: 9 additions & 2 deletions token/invocation/invocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,15 @@ func tokenFromModel(m tokenPayloadModel) (*Token, error) {
tkn.proof = m.Prf
tkn.meta = m.Meta

tkn.expiration = parse.OptionalTimestamp(m.Exp)
tkn.invokedAt = parse.OptionalTimestamp(m.Iat)
tkn.expiration, err = parse.OptionalTimestamp(m.Exp)
if err != nil {
return nil, fmt.Errorf("parse expiration: %w", err)
}

tkn.invokedAt, err = parse.OptionalTimestamp(m.Iat)
if err != nil {
return nil, fmt.Errorf("parse invokedAt: %w", err)
}

tkn.cause = m.Cause

Expand Down

0 comments on commit 5b816cc

Please sign in to comment.