Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

push_notifications: Delete E2EE device records on API key regeneration.

Prakhar Pratyush52a80268c863
diff --git a/api_docs/changelog.md b/api_docs/changelog.md
index a907b9db115..0d373ba8d3e 100644
--- a/api_docs/changelog.md
+++ b/api_docs/changelog.md
@@ -20,6 +20,13 @@ format used by the Zulip server that they are interacting with.
## Changes in Zulip 12.0
+**Feature level 492**
+
+* [`POST /users/me/api_key/regenerate`](/api/regenerate-api-key): When
+ a user's API key is regenerated, the server now also removes all of
+ the user's E2EE push device registrations, stopping E2EE push
+ notifications to those devices.
+
**Feature level 491**
* [`GET /events`](/api/get-events): A new `individual_emoji_changes`
diff --git a/version.py b/version.py
index 94f13253a82..84b25e30efd 100644
--- a/version.py
+++ b/version.py
@@ -33,7 +33,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"
# https://zulip.readthedocs.io/en/latest/documentation/api.html#step-by-step-guide
# Also available at docs/documentation/api.md.
-API_FEATURE_LEVEL = 491
+API_FEATURE_LEVEL = 492
# Bump the minor PROVISION_VERSION to indicate that folks should provision
# only when going from an old version of the code to a newer version. Bump
diff --git a/zerver/actions/user_settings.py b/zerver/actions/user_settings.py
index 5a2de2aac8b..23590030026 100644
--- a/zerver/actions/user_settings.py
+++ b/zerver/actions/user_settings.py
@@ -35,6 +35,7 @@ from zerver.lib.users import (
)
from zerver.lib.utils import generate_api_key
from zerver.models import (
+ Device,
Draft,
EmailChangeStatus,
Realm,
@@ -339,6 +340,9 @@ def do_regenerate_api_key(user_profile: UserProfile, acting_user: UserProfile) -
event = {"type": "clear_push_device_tokens", "user_profile_id": user_profile.id}
queue_event_on_commit("deferred_work", event)
+ # Delete all of the user's Device records to stop sending E2EE push notifications.
+ Device.objects.filter(user_id=user_profile.id).delete()
+
return new_api_key
diff --git a/zerver/openapi/python_examples.py b/zerver/openapi/python_examples.py
index f1a6a3d84b4..c15b0b31817 100644
--- a/zerver/openapi/python_examples.py
+++ b/zerver/openapi/python_examples.py
@@ -801,6 +801,8 @@ def regenerate_api_key(client: Client) -> None:
# Update the client with the new API key so subsequent tests don't fail.
client.api_key = result["api_key"]
+ # Reset the session so it is re-created with the new API key.
+ client.session = None
@openapi_test_function("/bots/{bot_id}/api_key:get")
@@ -2265,6 +2267,10 @@ def test_api_key_endpoints(client: Client) -> None:
def test_the_api(client: Client, nonadmin_client: Client, owner_client: Client) -> None:
get_user_agent(client)
+ # Run `test_api_key_endpoints` before `test_users` so Device records created by
+ # `register_push_device` & `register_device` are not bulk-deleted by
+ # `regenerate_api_key`, since they are needed for curl tests.
+ test_api_key_endpoints(client)
test_users(client, owner_client)
test_streams(client, nonadmin_client)
test_messages(client, nonadmin_client)
@@ -2273,7 +2279,6 @@ def test_the_api(client: Client, nonadmin_client: Client, owner_client: Client)
test_errors(client)
test_invitations(client)
test_welcome_bot_custom_message(client)
- test_api_key_endpoints(client)
sys.stdout.flush()
if REGISTERED_TEST_FUNCTIONS != CALLED_TEST_FUNCTIONS:
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index 2ea62af44a0..4f14032e818 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -11741,6 +11741,11 @@ paths:
Changing a user's API key will immediately log them out of Zulip
on devices registered for [mobile push notifications][mobile-push].
+ **Changes**: Before Zulip 12.0 (feature level 492),
+ regenerating a user's API key didn't remove all of the user's
+ [E2EE push device registrations](/api/register-push-device),
+ so E2EE push notifications could still be sent.
+
[mobile-push]: https://zulip.readthedocs.io/en/latest/production/mobile-push-notifications.html
responses:
"200":
diff --git a/zerver/tests/test_devices.py b/zerver/tests/test_devices.py
index 4d3450be1ca..b5e7845d5a2 100644
--- a/zerver/tests/test_devices.py
+++ b/zerver/tests/test_devices.py
@@ -28,3 +28,18 @@ class TestDeviceRegistration(ZulipTestCase):
self.assert_json_success(result)
self.assertEqual(Device.objects.count(), 0)
+
+ def test_remove_device_on_api_key_regeneration(self) -> None:
+ user = self.example_user("hamlet")
+
+ for _ in range(3):
+ result = self.api_post(user, "/api/v1/register_client_device")
+ self.assert_json_success(result)
+
+ self.assertEqual(Device.objects.filter(user=user).count(), 3)
+
+ # Verify all of the user's Device records deleted on API key
+ # regeneration, to stop sending E2EE push notifications.
+ result = self.api_post(user, "/api/v1/users/me/api_key/regenerate")
+ self.assert_json_success(result)
+ self.assertEqual(Device.objects.filter(user=user).count(), 0)