Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fixed auth event creation

Saurav Panda4a41caaa9d48
diff --git a/browser_use/agent/cloud_events.py b/browser_use/agent/cloud_events.py
index d29d73384..83ca52a66 100644
--- a/browser_use/agent/cloud_events.py
+++ b/browser_use/agent/cloud_events.py
@@ -272,6 +272,7 @@ class CreateAgentSessionEvent(BaseEvent):
class UpdateAgentSessionEvent(BaseEvent):
"""Event to update an existing agent session"""
+
# Model fields
id: str # Session ID to update
user_id: str = Field(max_length=255)
diff --git a/browser_use/sync/auth.py b/browser_use/sync/auth.py
index a9cd4f807..70a790cde 100644
--- a/browser_use/sync/auth.py
+++ b/browser_use/sync/auth.py
@@ -354,5 +354,4 @@ class DeviceAuthClient:
# Remove the config file entirely instead of saving empty values
config_path = CONFIG.BROWSER_USE_CONFIG_DIR / 'cloud_auth.json'
- if config_path.exists():
- config_path.unlink()
+ config_path.unlink(missing_ok=True)
diff --git a/browser_use/sync/service.py b/browser_use/sync/service.py
index a10a73ca6..863042342 100644
--- a/browser_use/sync/service.py
+++ b/browser_use/sync/service.py
@@ -53,9 +53,12 @@ class CloudSync:
if event.event_type == 'CreateAgentSessionEvent':
self.auth_flow_active = True
elif self.auth_task and not self.auth_task.done():
- # Authentication is in progress - send all events to preserve first-run data
- # The backend will handle pre-auth events and upgrade them to the correct user_id once auth completes
- await self._send_event(event)
+ # Authentication is in progress - only send session creation events
+ # to preserve session context, but don't leak other data
+ if event.event_type in ['CreateAgentSessionEvent']:
+ await self._send_event(event)
+ else:
+ logger.debug(f'Skipping event {event.event_type} during auth - not authenticated yet')
else:
# User is not authenticated and no auth in progress - don't send anything
logger.debug(f'Skipping event {event.event_type} - user not authenticated')
@@ -103,8 +106,6 @@ class CloudSync:
logger.debug(
f'Failed to send sync event: POST {response.request.url} {response.status_code} - {response.text}'
)
- # Also log the payload for debugging
- logger.debug(f'Event payload was: {event_data}')
except httpx.TimeoutException:
logger.warning(f'Event send timed out after 10 seconds: {event}')
except httpx.ConnectError as e:
diff --git a/tests/ci/test_sync_client_auth.py b/tests/ci/test_sync_client_auth.py
index a5da695f4..11160396a 100644
--- a/tests/ci/test_sync_client_auth.py
+++ b/tests/ci/test_sync_client_auth.py
@@ -304,10 +304,10 @@ class TestDeviceAuthClient:
auth.clear_auth()
assert auth.is_authenticated is False
- # Note: clear_auth() saves an empty config, so file still exists
- assert (temp_config_dir / 'cloud_auth.json').exists()
+ # Note: clear_auth() deletes the config file entirely for security
+ assert not (temp_config_dir / 'cloud_auth.json').exists()
- # Verify the file contains empty credentials
+ # Verify a new client loads empty credentials when no file exists
auth2 = DeviceAuthClient(base_url=httpserver.url_for(''))
assert auth2.auth_config.api_token is None
assert auth2.auth_config.user_id is None
@@ -423,8 +423,8 @@ class TestCloudSync:
# Check that no requests were made
assert len(requests) == 0
- async def test_send_event_during_auth_progress(self, httpserver: HTTPServer, temp_config_dir):
- """Test that task events ARE sent when authentication is in progress (fixes first-run data loss)."""
+ async def test_block_events_during_auth_progress(self, httpserver: HTTPServer, temp_config_dir):
+ """Test that task events are BLOCKED when authentication is in progress (prevents data leak)."""
requests = []
def capture_request(request):
@@ -483,7 +483,7 @@ class TestCloudSync:
# Set session ID
service.session_id = 'test-session-id'
- # Send task event while auth is in progress (should NOW be sent due to our fix)
+ # Send task event while auth is in progress (should be BLOCKED for security)
await service.handle_event(
CreateAgentTaskEvent(
agent_session_id='test-session',
@@ -498,10 +498,16 @@ class TestCloudSync:
)
)
- # Check that the task event was sent (our fix allows it during auth progress)
- assert len(requests) == 1
- assert requests[0]['json']['events'][0]['event_type'] == 'CreateAgentTaskEvent'
- assert requests[0]['json']['events'][0]['task'] == 'Test task during auth'
+ # Check that the task event was NOT sent (blocked for security during auth)
+ assert len(requests) == 0
+
+ # Clean up the background task to avoid test flakiness
+ if service.auth_task and not service.auth_task.done():
+ service.auth_task.cancel()
+ try:
+ await service.auth_task
+ except asyncio.CancelledError:
+ pass
async def test_authenticate_then_send(self, httpserver: HTTPServer, temp_config_dir):
"""Test that events are only sent after authentication."""