Skip to content

Commit

Permalink
fix(jwt): set issued_at to nil prior to validating claims (#813)
Browse files Browse the repository at this point in the history
* fix(jwt): set issued_at to nil prior to validating claims

* update comment to retrigger test job
  • Loading branch information
ecrupper authored Apr 11, 2023
1 parent a3ab6f0 commit ad79b9a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions internal/token/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (tm *Manager) ParseToken(token string) (*Claims, error) {
claims = t.Claims.(*Claims)
name := claims.Subject

// according to JWT, the iat field is optional for security purposes and is purely informational.
// setting it to nil avoids any worries of race conditions.
claims.IssuedAt = nil

// check if subject has a value in claims;
// we can save a db lookup attempt
if len(name) == 0 {
Expand Down
6 changes: 3 additions & 3 deletions internal/token/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestTokenManager_ParseToken(t *testing.T) {
TokenType: constants.UserAccessTokenType,
RegisteredClaims: jwt.RegisteredClaims{
Subject: u.GetName(),
IssuedAt: jwt.NewNumericDate(now),
IssuedAt: nil,
ExpiresAt: jwt.NewNumericDate(now.Add(time.Minute * 5)),
},
},
Expand All @@ -69,7 +69,7 @@ func TestTokenManager_ParseToken(t *testing.T) {
TokenType: constants.UserRefreshTokenType,
RegisteredClaims: jwt.RegisteredClaims{
Subject: u.GetName(),
IssuedAt: jwt.NewNumericDate(now),
IssuedAt: nil,
ExpiresAt: jwt.NewNumericDate(now.Add(time.Minute * 30)),
},
},
Expand All @@ -89,7 +89,7 @@ func TestTokenManager_ParseToken(t *testing.T) {
TokenType: constants.WorkerBuildTokenType,
RegisteredClaims: jwt.RegisteredClaims{
Subject: "worker",
IssuedAt: jwt.NewNumericDate(now),
IssuedAt: nil,
ExpiresAt: jwt.NewNumericDate(now.Add(time.Minute * 90)),
},
},
Expand Down
4 changes: 2 additions & 2 deletions router/middleware/claims/claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestClaims_Establish(t *testing.T) {
IsActive: true,
RegisteredClaims: jwt.RegisteredClaims{
Subject: "foo",
IssuedAt: jwt.NewNumericDate(now),
IssuedAt: nil,
ExpiresAt: jwt.NewNumericDate(now.Add(time.Minute * 5)),
},
},
Expand All @@ -108,7 +108,7 @@ func TestClaims_Establish(t *testing.T) {
Repo: "foo/bar",
RegisteredClaims: jwt.RegisteredClaims{
Subject: "host",
IssuedAt: jwt.NewNumericDate(now),
IssuedAt: nil,
ExpiresAt: jwt.NewNumericDate(now.Add(time.Minute * 35)),
},
},
Expand Down

0 comments on commit ad79b9a

Please sign in to comment.