feat(identity): enforce shared session and browser request security

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 11:33:40 +03:00
1 parent 7df521bd89
commit 86f562f5b6
23 files changed
+430 -121

No files matched your search

@@ -5,7 +5,6 @@ import (
"errors"
"math"
"net/http"
"strings"
"time"
"lincodex/backend/internal/adapter/primary/http/api"
@@ -73,6 +72,9 @@ func (handler *IdentityHandler) RegisterAccount(
request *api.RegistrationRequest,
params api.RegisterAccountParams,
) (api.RegisterAccountRes, error) {
if !originAllowed(ctx, handler.publicOrigin) {
return (*api.RegisterAccountForbidden)(&problem.New(ctx, api.ProblemCodeCsrfCheckFailed).Response), nil
}
err := handler.registration.Register(ctx, identity.RegisterCommand{
IdempotencyKey: params.IdempotencyKey,
Email: string(request.Email),
@@ -99,6 +101,9 @@ func (handler *IdentityHandler) ResendEmailVerification(
ctx context.Context,
request *api.EmailVerificationResendRequest,
) (api.ResendEmailVerificationRes, error) {
if !originAllowed(ctx, handler.publicOrigin) {
return (*api.ResendEmailVerificationForbidden)(&problem.New(ctx, api.ProblemCodeCsrfCheckFailed).Response), nil
}
if handler.registration == nil {
return nil, errors.New("IdentityHandler.ResendEmailVerification - registration is not configured")
}
@@ -121,13 +126,12 @@ func (handler *IdentityHandler) ResendEmailVerification(
func (handler *IdentityHandler) ConfirmEmail(
ctx context.Context,
request *api.EmailVerificationRequest,
params api.ConfirmEmailParams,
_ api.ConfirmEmailParams,
) (api.ConfirmEmailRes, error) {
if handler.confirmation == nil {
return nil, errors.New("IdentityHandler.ConfirmEmail - confirmation is not configured")
}
origin, ok := params.Origin.Get()
if !ok || strings.TrimSpace(origin) == "" || origin != handler.confirmationOrigin {
if !originAllowed(ctx, handler.publicOrigin) {
return (*api.ConfirmEmailForbidden)(&problem.New(ctx, api.ProblemCodeCsrfCheckFailed).Response), nil
}
@@ -155,7 +159,7 @@ func (handler *IdentityHandler) ConfirmEmail(
}
func (handler *IdentityHandler) Login(ctx context.Context, request *api.LoginRequest) (api.LoginRes, error) {
if !originAllowed(ctx, handler.sessionOrigin) {
if !originAllowed(ctx, handler.publicOrigin) {
return (*api.LoginForbidden)(&problem.New(ctx, api.ProblemCodeCsrfCheckFailed).Response), nil
}
result, err := handler.sessions.Login(ctx, identity.LoginCommand{
@@ -254,8 +258,11 @@ func wholeSeconds(wait time.Duration) int32 {
}
func originAllowed(ctx context.Context, expected string) bool {
if expected == "" {
return false
}
origin, ok := httpmiddleware.RequestOrigin(ctx)
return ok && origin == expected
return ok && origin != "" && origin == expected
}
func unauthenticatedProblemResponse(ctx context.Context) *api.Problem {