Skip to content

Commit

Permalink
fix(models): potential panic generating jti (#2669)
Browse files Browse the repository at this point in the history
This ensures that at the time the JWT is generated for identity verification requests that a panic can't occur and instead an error will be returned.
  • Loading branch information
james-d-elliott committed Dec 4, 2021
1 parent 5a223b5 commit c017597
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
3 changes: 2 additions & 1 deletion internal/handlers/handler_register_u2f_step1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/golang-jwt/jwt/v4"
"github.com/golang/mock/gomock"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -35,7 +36,7 @@ func (s *HandlerRegisterU2FStep1Suite) TearDownTest() {
}

func createToken(ctx *mocks.MockAutheliaCtx, username, action string, expiresAt time.Time) (data string, verification models.IdentityVerification) {
verification = models.NewIdentityVerification(username, action, ctx.Ctx.RemoteIP())
verification = models.NewIdentityVerification(uuid.New(), username, action, ctx.Ctx.RemoteIP())

verification.ExpiresAt = expiresAt

Expand Down
10 changes: 9 additions & 1 deletion internal/middlewares/identity_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"

"github.com/authelia/authelia/v4/internal/models"
"github.com/authelia/authelia/v4/internal/templates"
Expand All @@ -27,7 +28,14 @@ func IdentityVerificationStart(args IdentityVerificationStartArgs) RequestHandle
return
}

verification := models.NewIdentityVerification(identity.Username, args.ActionClaim, ctx.RemoteIP())
var jti uuid.UUID

if jti, err = uuid.NewUUID(); err != nil {
ctx.Error(err, messageOperationFailed)
return
}

verification := models.NewIdentityVerification(jti, identity.Username, args.ActionClaim, ctx.RemoteIP())

// Create the claim with the action to sign it.
claims := verification.ToIdentityVerificationClaim()
Expand Down
3 changes: 2 additions & 1 deletion internal/middlewares/identity_verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/golang-jwt/jwt/v4"
"github.com/golang/mock/gomock"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -166,7 +167,7 @@ func (s *IdentityVerificationFinishProcess) TearDownTest() {
}

func createToken(ctx *mocks.MockAutheliaCtx, username, action string, expiresAt time.Time) (data string, verification models.IdentityVerification) {
verification = models.NewIdentityVerification(username, action, ctx.Ctx.RemoteIP())
verification = models.NewIdentityVerification(uuid.New(), username, action, ctx.Ctx.RemoteIP())

verification.ExpiresAt = expiresAt

Expand Down
4 changes: 2 additions & 2 deletions internal/models/identity_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

// NewIdentityVerification creates a new IdentityVerification from a given username and action.
func NewIdentityVerification(username, action string, ip net.IP) (verification IdentityVerification) {
func NewIdentityVerification(jti uuid.UUID, username, action string, ip net.IP) (verification IdentityVerification) {
return IdentityVerification{
JTI: uuid.New(),
JTI: jti,
IssuedAt: time.Now(),
ExpiresAt: time.Now().Add(5 * time.Minute),
Action: action,
Expand Down
5 changes: 4 additions & 1 deletion internal/storage/sql_provider_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ func (p *SQLProvider) getEncryptionValue(ctx context.Context, name string) (valu
}

func (p *SQLProvider) setNewEncryptionCheckValue(ctx context.Context, key *[32]byte, e sqlx.ExecerContext) (err error) {
valueClearText := uuid.New()
valueClearText, err := uuid.NewUUID()
if err != nil {
return err
}

value, err := utils.Encrypt([]byte(valueClearText.String()), key)
if err != nil {
Expand Down

0 comments on commit c017597

Please sign in to comment.