Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed integer overflow in NumericDate.MarshalJSON #200

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions types.go
Expand Up @@ -53,9 +53,12 @@ func (date NumericDate) MarshalJSON() (b []byte, err error) {
if TimePrecision < time.Second {
prec = int(math.Log10(float64(time.Second) / float64(TimePrecision)))
}
f := float64(date.Truncate(TimePrecision).UnixNano()) / float64(time.Second)
trancatedDate := date.Truncate(TimePrecision)
qqiao marked this conversation as resolved.
Show resolved Hide resolved
whole := strconv.FormatInt(trancatedDate.Unix(), 10)
qqiao marked this conversation as resolved.
Show resolved Hide resolved
decimal := strconv.FormatFloat(float64(trancatedDate.Nanosecond())/float64(time.Second), 'f', prec, 64)
f := append([]byte(whole), []byte(decimal)[1:]...)
qqiao marked this conversation as resolved.
Show resolved Hide resolved

return []byte(strconv.FormatFloat(f, 'f', prec, 64)), nil
return f, nil
}

// UnmarshalJSON is an implementation of the json.RawMessage interface and deserializses a
Expand Down
24 changes: 20 additions & 4 deletions types_test.go
Expand Up @@ -2,6 +2,7 @@ package jwt_test

import (
"encoding/json"
"math"
"testing"
"time"

Expand Down Expand Up @@ -79,23 +80,38 @@ func TestNumericDate_MarshalJSON(t *testing.T) {
}{
{time.Unix(5243700879, 0), "5243700879", time.Second},
{time.Unix(5243700879, 0), "5243700879.000", time.Millisecond},
{time.Unix(5243700879, 0), "5243700879.000001", time.Microsecond},
{time.Unix(5243700879, 0), "5243700879.000000954", time.Nanosecond},
{time.Unix(5243700879, 0), "5243700879.000000", time.Microsecond},
{time.Unix(5243700879, 0), "5243700879.000000000", time.Nanosecond},
//
{time.Unix(4239425898, 0), "4239425898", time.Second},
{time.Unix(4239425898, 0), "4239425898.000", time.Millisecond},
{time.Unix(4239425898, 0), "4239425898.000000", time.Microsecond},
{time.Unix(4239425898, 0), "4239425898.000000000", time.Nanosecond},
//
{time.Unix(253402271999, 0), "253402271999", time.Second},
{time.Unix(253402271999, 0), "253402271999.000", time.Millisecond},
{time.Unix(253402271999, 0), "253402271999.000000", time.Microsecond},
{time.Unix(253402271999, 0), "253402271999.000000000", time.Nanosecond},
//
{time.Unix(0, 1644285000210402000), "1644285000", time.Second},
{time.Unix(0, 1644285000210402000), "1644285000.210", time.Millisecond},
{time.Unix(0, 1644285000210402000), "1644285000.210402", time.Microsecond},
{time.Unix(0, 1644285000210402000), "1644285000.210402012", time.Nanosecond},
{time.Unix(0, 1644285000210402000), "1644285000.210402000", time.Nanosecond},
//
{time.Unix(0, 1644285315063096000), "1644285315", time.Second},
{time.Unix(0, 1644285315063096000), "1644285315.063", time.Millisecond},
{time.Unix(0, 1644285315063096000), "1644285315.063096", time.Microsecond},
{time.Unix(0, 1644285315063096000), "1644285315.063096046", time.Nanosecond},
{time.Unix(0, 1644285315063096000), "1644285315.063096000", time.Nanosecond},
// Maximum time that a go time.Time can represent
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807", time.Second},
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999", time.Millisecond},
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999999", time.Microsecond},
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999999999", time.Nanosecond},
// Strange precisions
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807", time.Second},
{time.Unix(math.MaxInt64, 999999999), "9223372036854775756", time.Minute},
{time.Unix(math.MaxInt64, 999999999), "9223372036854774016", time.Hour},
{time.Unix(math.MaxInt64, 999999999), "9223372036854745216", 24 * time.Hour},
}

for i, tc := range tt {
Expand Down