SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
fix(user): revoke sessions when a user is deleted (#12943)
Vikrant Guptae2e9173986df
diff --git a/pkg/modules/user/impluser/setter.go b/pkg/modules/user/impluser/setter.goindex dcd3af0dde..d7ac6e28f5 100644--- a/pkg/modules/user/impluser/setter.go+++ b/pkg/modules/user/impluser/setter.go@@ -248,8 +248,18 @@ func (module *setter) DeleteUser(ctx context.Context, orgID valuer.UUID, id strireturn err}- // for now we are only soft deleting users- if err := module.store.SoftDeleteUser(ctx, orgID.String(), user.ID.StringValue()); err != nil {+ if err := module.store.RunInTx(ctx, func(ctx context.Context) error {+ if err := module.tokenizer.DeleteTokensByUserID(ctx, user.ID); err != nil {+ return err+ }++ if err := module.tokenizer.DeleteIdentity(ctx, user.ID); err != nil {+ return err+ }++ // for now we are only soft deleting users+ return module.store.SoftDeleteUser(ctx, orgID.String(), user.ID.StringValue())+ }); err != nil {return err}diff --git a/pkg/modules/user/impluser/store.go b/pkg/modules/user/impluser/store.goindex a8ccc05a33..8397b8e898 100644--- a/pkg/modules/user/impluser/store.go+++ b/pkg/modules/user/impluser/store.go@@ -218,19 +218,12 @@ func (store *store) DeleteUser(ctx context.Context, orgID string, id string) err}func (store *store) SoftDeleteUser(ctx context.Context, orgID string, id string) error {- tx, err := store.sqlstore.BunDB().BeginTx(ctx, nil)- if err != nil {- return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to start transaction")- }-- defer func() {- _ = tx.Rollback()- }()+ tx := store.sqlstore.BunDBCtx(ctx)// get the password idvar password types.FactorPassword- err = tx.NewSelect().+ err := tx.NewSelect().Model(&password).Where("user_id = ?", id).Scan(ctx)@@ -296,11 +289,6 @@ func (store *store) SoftDeleteUser(ctx context.Context, orgID string, id string)return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to delete user")}- err = tx.Commit()- if err != nil {- return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to commit transaction")- }-return nil}diff --git a/pkg/tokenizer/tokenizerstore/sqltokenizerstore/store.go b/pkg/tokenizer/tokenizerstore/sqltokenizerstore/store.goindex 0591f211ef..9c009eb767 100644--- a/pkg/tokenizer/tokenizerstore/sqltokenizerstore/store.go+++ b/pkg/tokenizer/tokenizerstore/sqltokenizerstore/store.go@@ -233,6 +233,10 @@ func (store *store) ListByUserID(ctx context.Context, userID valuer.UUID) ([]*au}func (store *store) UpdateLastObservedAtByAccessToken(ctx context.Context, accessTokenToLastObservedAt []map[string]any) error {+ if len(accessTokenToLastObservedAt) == 0 {+ return nil+ }+values := store.sqlstore.BunDBCtx(ctx).diff --git a/tests/integration/tests/passwordauthn/08_user.py b/tests/integration/tests/passwordauthn/08_user.pyindex 7c6024442f..22b90213be 100644--- a/tests/integration/tests/passwordauthn/08_user.py+++ b/tests/integration/tests/passwordauthn/08_user.py@@ -10,6 +10,7 @@ from fixtures.auth import (USER_EDITOR_EMAIL,USER_EDITOR_PASSWORD,assert_user_has_role,+ create_active_user,find_user_by_email,find_user_with_roles_by_email,)@@ -185,3 +186,31 @@ def test_editor_cannot_update_other_user(signoz: types.SigNoz, get_token: Callabtimeout=5,)assert response.status_code == HTTPStatus.FORBIDDEN+++def test_deleted_user_session_is_revoked(signoz: types.SigNoz, get_token: Callable[[str, str], str]) -> None:+ """Verify a deleted user's existing session stops authenticating immediately."""+ admin_token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)+ user_id = create_active_user(signoz, admin_token, "[email protected]", "signoz-viewer", "password123Z$", "revoked")+ user_token = get_token("[email protected]", "password123Z$")++ response = requests.get(+ signoz.self.host_configs["8080"].get("/api/v2/users/me"),+ headers={"Authorization": f"Bearer {user_token}"},+ timeout=5,+ )+ assert response.status_code == HTTPStatus.OK++ response = requests.delete(+ signoz.self.host_configs["8080"].get(f"/api/v2/users/{user_id}"),+ headers={"Authorization": f"Bearer {admin_token}"},+ timeout=5,+ )+ assert response.status_code == HTTPStatus.NO_CONTENT++ response = requests.get(+ signoz.self.host_configs["8080"].get("/api/v2/users/me"),+ headers={"Authorization": f"Bearer {user_token}"},+ timeout=5,+ )+ assert response.status_code == HTTPStatus.UNAUTHORIZED