MM-41236: Sentry crash: Fix nil reference to token (#19417)
The AND condition would mean that it would try to dereference token.Valid if there was an error. And there's no guarantee to always have a non-nil token in case of an error. We need to track those conditions separately. https://mattermost.atlassian.net/browse/MM-41236 ```release-note NONE ```
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
8f35243604
Коммит
0c9262c4d1
@@ -358,9 +358,12 @@ func (s *Server) renewalTokenValid(tokenString, signingKey string) (bool, error)
|
|||||||
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
||||||
return []byte(signingKey), nil
|
return []byte(signingKey), nil
|
||||||
})
|
})
|
||||||
if err != nil && !token.Valid {
|
if err != nil {
|
||||||
return false, errors.Wrapf(err, "Error validating JWT token")
|
return false, errors.Wrapf(err, "Error validating JWT token")
|
||||||
}
|
}
|
||||||
|
if !token.Valid {
|
||||||
|
return false, errors.New("invalid JWT token")
|
||||||
|
}
|
||||||
expirationTime := time.Unix(claims.ExpiresAt, 0)
|
expirationTime := time.Unix(claims.ExpiresAt, 0)
|
||||||
if expirationTime.Before(time.Now().UTC()) {
|
if expirationTime.Before(time.Now().UTC()) {
|
||||||
return false, nil
|
return false, nil
|
||||||
|
|||||||
@@ -4,9 +4,11 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/dgrijalva/jwt-go"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
@@ -80,6 +82,12 @@ func TestGenerateRenewalToken(t *testing.T) {
|
|||||||
th := Setup(t)
|
th := Setup(t)
|
||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
|
|
||||||
|
t.Run("test invalid token", func(t *testing.T) {
|
||||||
|
_, err := th.App.Srv().renewalTokenValid("badtoken", "")
|
||||||
|
var vErr *jwt.ValidationError
|
||||||
|
require.True(t, errors.As(err, &vErr))
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("renewal token generated correctly", func(t *testing.T) {
|
t.Run("renewal token generated correctly", func(t *testing.T) {
|
||||||
setLicense(th, nil)
|
setLicense(th, nil)
|
||||||
token, appErr := th.App.Srv().GenerateRenewalToken(JWTDefaultTokenExpiration)
|
token, appErr := th.App.Srv().GenerateRenewalToken(JWTDefaultTokenExpiration)
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user