From b899363d17a80b66a58f07ca40a67f97afb8c697 Mon Sep 17 00:00:00 2001 From: JukLee0ira Date: Sun, 27 Apr 2025 19:02:03 +0800 Subject: [PATCH] node: set JWT expiry to 60 seconds #25416 --- node/jwt_handler.go | 6 ++++-- node/rpcstack_test.go | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/node/jwt_handler.go b/node/jwt_handler.go index 28d5b87c60..363f6b3aad 100644 --- a/node/jwt_handler.go +++ b/node/jwt_handler.go @@ -24,6 +24,8 @@ import ( "github.com/golang-jwt/jwt/v4" ) +const jwtExpiryTimeout = 60 * time.Second + type jwtHandler struct { keyFunc func(token *jwt.Token) (interface{}, error) next http.Handler @@ -68,9 +70,9 @@ func (handler *jwtHandler) ServeHTTP(out http.ResponseWriter, r *http.Request) { http.Error(out, "token is expired", http.StatusForbidden) case claims.IssuedAt == nil: http.Error(out, "missing issued-at", http.StatusForbidden) - case time.Since(claims.IssuedAt.Time) > 5*time.Second: + case time.Since(claims.IssuedAt.Time) > jwtExpiryTimeout: http.Error(out, "stale token", http.StatusForbidden) - case time.Until(claims.IssuedAt.Time) > 5*time.Second: + case time.Until(claims.IssuedAt.Time) > jwtExpiryTimeout: http.Error(out, "future token", http.StatusForbidden) default: handler.next.ServeHTTP(out, r) diff --git a/node/rpcstack_test.go b/node/rpcstack_test.go index f50c80d921..51215d5a36 100644 --- a/node/rpcstack_test.go +++ b/node/rpcstack_test.go @@ -324,9 +324,9 @@ func TestJWT(t *testing.T) { } expFail := []string{ // future - fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() + 6})), + fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() + int64(jwtExpiryTimeout.Seconds()) + 1})), // stale - fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() - 6})), + fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() - int64(jwtExpiryTimeout.Seconds()) - 1})), // wrong algo fmt.Sprintf("Bearer %v", issueToken(secret, jwt.SigningMethodHS512, testClaim{"iat": time.Now().Unix() + 4})), // expired