feat(identity): enforce shared session and browser request security
This commit is contained in:
1 parent
7df521bd89
commit
86f562f5b6
23 files changed
+430
-121
No files matched your search
@@ -31,7 +31,7 @@ func (authenticator *sessionAuthenticatorStub) Authenticate(_ context.Context, _
|
||||
return authenticator.result, authenticator.err
|
||||
}
|
||||
|
||||
func TestSessionSecurityAddsPrincipalOnlyForTheThreeEnabledOperations(t *testing.T) {
|
||||
func TestSessionSecurityAddsVerifiedPrincipalForEveryProtectedOperationClass(t *testing.T) {
|
||||
token, err := domain.NewSecretToken()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -48,6 +48,8 @@ func TestSessionSecurityAddsPrincipalOnlyForTheThreeEnabledOperations(t *testing
|
||||
for _, operation := range []api.OperationName{
|
||||
api.GetCurrentAccountOperation,
|
||||
api.GetCurrentSessionOperation,
|
||||
api.GetMyProfileOperation,
|
||||
api.CreateOrganizationOperation,
|
||||
api.LogoutOperation,
|
||||
} {
|
||||
ctx := sessionRequestContext(t, token.Reveal(), "https://app.example.com")
|
||||
@@ -60,18 +62,12 @@ func TestSessionSecurityAddsPrincipalOnlyForTheThreeEnabledOperations(t *testing
|
||||
t.Fatalf("PrincipalFromContext(%s) = %+v, %v", operation, principal, ok)
|
||||
}
|
||||
}
|
||||
if authenticator.calls != 3 {
|
||||
t.Fatalf("authentication calls = %d, want one for each allowed operation", authenticator.calls)
|
||||
}
|
||||
|
||||
ctx := sessionRequestContext(t, token.Reveal(), "https://app.example.com")
|
||||
_, err = security.HandleSessionCookie(ctx, api.GetMyProfileOperation, api.SessionCookie{APIKey: token.Reveal()})
|
||||
if !errors.Is(err, ogenerrors.ErrSkipServerSecurity) || authenticator.calls != 3 {
|
||||
t.Fatalf("disallowed operation error=%v authentication calls=%d", err, authenticator.calls)
|
||||
if authenticator.calls != 5 {
|
||||
t.Fatalf("authentication calls = %d, want one for each protected operation class", authenticator.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogoutRequiresExactOriginAndSessionBoundCSRF(t *testing.T) {
|
||||
func TestSessionCsrfRequiresExactOriginForAnyContractOperation(t *testing.T) {
|
||||
token, err := domain.NewSecretToken()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -97,11 +93,11 @@ func TestLogoutRequiresExactOriginAndSessionBoundCSRF(t *testing.T) {
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
ctx := sessionRequestContext(t, token.Reveal(), test.origin)
|
||||
ctx, err := security.HandleSessionCookie(ctx, api.LogoutOperation, api.SessionCookie{APIKey: token.Reveal()})
|
||||
ctx, err := security.HandleSessionCookie(ctx, api.CreateOrganizationOperation, api.SessionCookie{APIKey: token.Reveal()})
|
||||
if err != nil {
|
||||
t.Fatalf("HandleSessionCookie() error = %v", err)
|
||||
}
|
||||
_, err = security.HandleSessionCsrf(ctx, api.LogoutOperation, api.SessionCsrf{APIKey: test.csrf})
|
||||
_, err = security.HandleSessionCsrf(ctx, api.CreateOrganizationOperation, api.SessionCsrf{APIKey: test.csrf})
|
||||
if !errors.Is(err, test.wantErr) {
|
||||
t.Fatalf("HandleSessionCsrf() error = %v, want %v", err, test.wantErr)
|
||||
}
|
||||
@@ -109,6 +105,41 @@ func TestLogoutRequiresExactOriginAndSessionBoundCSRF(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionSecurityRejectsInvalidAuthenticatedSession(t *testing.T) {
|
||||
token, err := domain.NewSecretToken()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
verifiedAt := time.Now()
|
||||
valid := identity.AuthenticatedSession{
|
||||
User: domain.User{ID: uuid.New(), EmailVerifiedAt: &verifiedAt},
|
||||
SessionID: uuid.New(),
|
||||
CSRFToken: "csrf-for-this-session",
|
||||
}
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
result identity.AuthenticatedSession
|
||||
}{
|
||||
{name: "unverified account", result: identity.AuthenticatedSession{User: domain.User{ID: valid.User.ID}, SessionID: valid.SessionID, CSRFToken: valid.CSRFToken}},
|
||||
{name: "missing user ID", result: identity.AuthenticatedSession{User: domain.User{EmailVerifiedAt: &verifiedAt}, SessionID: valid.SessionID, CSRFToken: valid.CSRFToken}},
|
||||
{name: "missing session ID", result: identity.AuthenticatedSession{User: valid.User, CSRFToken: valid.CSRFToken}},
|
||||
{name: "missing CSRF token", result: identity.AuthenticatedSession{User: valid.User, SessionID: valid.SessionID}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
authenticator := &sessionAuthenticatorStub{result: test.result}
|
||||
security := NewSessionSecurity(authenticator, "https://app.example.com")
|
||||
ctx := sessionRequestContext(t, token.Reveal(), "https://app.example.com")
|
||||
authenticated, err := security.HandleSessionCookie(ctx, api.GetMyProfileOperation, api.SessionCookie{APIKey: token.Reveal()})
|
||||
if !errors.Is(err, ogenerrors.ErrSkipServerSecurity) {
|
||||
t.Fatalf("HandleSessionCookie() error = %v, want skipped authentication", err)
|
||||
}
|
||||
if _, ok := PrincipalFromContext(authenticated); ok {
|
||||
t.Fatal("invalid authenticated session installed a principal")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecurityErrorsMapToUnauthenticatedOrCSRFProblem(t *testing.T) {
|
||||
logger := slog.New(slog.NewTextHandler(&strings.Builder{}, nil))
|
||||
handler := NewHandler(nil, problem.NewResponder(logger))
|
||||
|
||||
Reference in new issue
Block a user