zulip/zproject
Zixuan James Li a081428ad2 user_groups: Make locks required for updating user group memberships.
**Background**

User groups are expected to comply with the DAG constraint for the
many-to-many inter-group membership. The check for this constraint has
to be performed recursively so that we can find all direct and indirect
subgroups of the user group to be added.

This kind of check is vulnerable to phantom reads which is possible at
the default read committed isolation level because we cannot guarantee
that the check is still valid when we are adding the subgroups to the
user group.

**Solution**

To avoid having another transaction concurrently update one of the
to-be-subgroup after the recursive check is done, and before the subgroup
is added, we use SELECT FOR UPDATE to lock the user group rows.

The lock needs to be acquired before a group membership change is about
to occur before any check has been conducted.

Suppose that we are adding subgroup B to supergroup A, the locking protocol
is specified as follows:

1. Acquire a lock for B and all its direct and indirect subgroups.
2. Acquire a lock for A.

For the removal of user groups, we acquire a lock for the user group to
be removed with all its direct and indirect subgroups. This is the special
case A=B, which is still complaint with the protocol.

**Error handling**

We currently rely on Postgres' deadlock detection to abort transactions
and show an error for the users. In the future, we might need some
recovery mechanism or at least better error handling.

**Notes**

An important note is that we need to reuse the recursive CTE query that
finds the direct and indirect subgroups when applying the lock on the
rows. And the lock needs to be acquired the same way for the addition and
removal of direct subgroups.

User membership change (as opposed to user group membership) is not
affected. Read-only queries aren't either. The locks only protect
critical regions where the user group dependency graph might violate
the DAG constraint, where users are not participating.

**Testing**

We implement a transaction test case targeting some typical scenarios
when an internal server error is expected to happen (this means that the
user group view makes the correct decision to abort the transaction when
something goes wrong with locks).

To achieve this, we add a development view intended only for unit tests.
It has a global BARRIER that can be shared across threads, so that we
can synchronize them to consistently reproduce certain potential race
conditions prevented by the database locks.

The transaction test case lanuches pairs of threads initiating possibly
conflicting requests at the same time. The tests are set up such that exactly N
of them are expected to succeed with a certain error message (while we don't
know each one).

**Security notes**

get_recursive_subgroups_for_groups will no longer fetch user groups from
other realms. As a result, trying to add/remove a subgroup from another
realm results in a UserGroup not found error response.

We also implement subgroup-specific checks in has_user_group_access to
keep permission managing in a single place. Do note that the API
currently don't have a way to violate that check because we are only
checking the realm ID now.
2023-08-24 17:21:08 -07:00
..
jinja2 jinja2: Make sure that 500 pages have support_email set. 2023-04-12 16:41:07 -07:00
__init__.py
backends.py ruff: Fix RUF015 Prefer `next(...)` over single element slice. 2023-07-23 15:20:53 -07:00
computed_settings.py error_notify: Remove custom email error reporting handler. 2023-07-20 11:00:09 -07:00
config.py coverage: Clean up coverage configuration. 2023-05-31 13:53:04 -07:00
configured_settings.py coverage: Clean up coverage configuration. 2023-05-31 13:53:04 -07:00
default_settings.py typing: Add typing constants to the post register api response. 2023-08-23 16:36:44 -07:00
dev_settings.py dev_settings: Remove `THUMBNAIL_IMAGES` from test_extra_settings. 2023-06-12 16:26:55 -07:00
dev_urls.py user_groups: Make locks required for updating user group memberships. 2023-08-24 17:21:08 -07:00
email_backends.py email: Replace `uri` with `url` in templates and backend. 2023-04-26 16:37:16 -07:00
legacy_urls.py legacy_urls: Add type annotation to legacy_urls. 2022-08-12 17:08:04 -07:00
prod_settings.pyi lint: Replace pycodestyle and pyflakes with ruff. 2022-11-03 12:10:15 -07:00
prod_settings_template.py uploads: Allow uploads to set storage class. 2023-07-19 16:19:34 -07:00
sentry.py sentry: Remove dependence on get_current_request. 2023-07-11 22:23:47 -07:00
settings.py ruff: Fix M001 Unused `noqa` directive. 2022-11-03 12:10:15 -07:00
settings_types.py saml: Rework SP-initiated logout config to support IdP-level config. 2023-05-23 13:01:15 -07:00
template_loaders.py template_loaders: Extract TwoFactorLoader to new module. 2022-09-26 12:42:36 -07:00
test_extra_settings.py error_notify: Remove custom email error reporting handler. 2023-07-20 11:00:09 -07:00
test_settings.py ruff: Fix M001 Unused `noqa` directive. 2022-11-03 12:10:15 -07:00
urls.py linkifier: Support reordering linkifiers. 2023-08-14 15:21:48 -07:00
wsgi.py python: Normalize quotes with Black. 2021-02-12 13:11:19 -08:00