Skip to content

Commit e421cf4

Browse files
committed
Never re-use a session UUID
1 parent de30ede commit e421cf4

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/reactpy_django/auth/components.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ def session_manager(child: Any):
2828

2929
synchronize_requested, set_synchronize_requested = hooks.use_state(False)
3030
_, set_rerender = hooks.use_state(uuid4)
31-
uuid_ref = hooks.use_ref(str(uuid4()))
32-
uuid = uuid_ref.current
31+
uuid = hooks.use_ref("")
3332
scope = hooks.use_connection().scope
3433

3534
@hooks.use_effect(dependencies=[])
@@ -60,18 +59,20 @@ async def synchronize_session():
6059
if not session or not session.session_key:
6160
return
6261

63-
# Delete any sessions currently associated with this UUID, which also resets
64-
# the SynchronizeSession validity time.
62+
# Delete any sessions currently associated with the previous UUID.
6563
# This exists to fix scenarios where...
66-
# 1) The developer manually rotates the session key.
67-
# 2) A component tree requests multiple logins back-to-back before they finish.
68-
# 3) A login is requested, but the server failed to respond to the HTTP request.
69-
with contextlib.suppress(SynchronizeSession.DoesNotExist):
70-
obj = await SynchronizeSession.objects.aget(uuid=uuid)
71-
await obj.adelete()
64+
# 1) A component tree performs multiple login commands for different users.
65+
# 2) A login is requested, but the server failed to respond to the HTTP request.
66+
if uuid.current:
67+
with contextlib.suppress(SynchronizeSession.DoesNotExist):
68+
obj = await SynchronizeSession.objects.aget(uuid=uuid.current)
69+
await obj.adelete()
70+
71+
# Create a fresh UUID
72+
uuid.set_current(str(uuid4()))
7273

7374
# Begin the process of synchronizing HTTP and websocket sessions
74-
obj = await SynchronizeSession.objects.acreate(uuid=uuid, session_key=session.session_key)
75+
obj = await SynchronizeSession.objects.acreate(uuid=uuid.current, session_key=session.session_key)
7576
await obj.asave()
7677
set_synchronize_requested(True)
7778

@@ -96,7 +97,7 @@ async def rerender():
9697
http_request = HttpRequest(
9798
{
9899
"method": "GET",
99-
"url": reverse("reactpy:session_manager", args=[uuid]),
100+
"url": reverse("reactpy:session_manager", args=[uuid.current]),
100101
"body": None,
101102
"callback": synchronize_session_callback,
102103
},

0 commit comments

Comments
 (0)