fix: clear stale connectionId after impersonation changes - #121
Merged
Merged
Conversation
Root cause: When an admin uses 'View as' to impersonate a user,
the backend correctly returns only the connections visible to the
target user. However, the frontend's useConnectionManager hook
had an early return when connectionId was set, without validating
that the connection was actually in the new list.
This caused the UI to keep a stale connectionId from before the
impersonation, resulting in 403 errors when chat/agent/editor
APIs were called with a connection the effective user cannot access.
Fix:
1. In useConnectionManager.js: Added validation that connectionId
exists in the connections array. If not, clear it and auto-select
from valid options.
2. In all section components using useConnectionManager:
- Added isLoading check to prevent rendering during connection refresh
- Added selectedConnection validation alongside connectionId check
- This ensures we never render a section with a stale connection
that's not in the effective user's accessible list
Affected sections:
- AgentChatSection
- EditorSection
- DashboardsSection
- SchemaSection
- SchemaDocsSection
- SlowQueriesSection
- DigestSection
- CompanyKnowledgeSection
- MonitorSection
Co-authored-by: Venkat SF <venkatesh.sakamuri@stayflexi.com>
Verifies that when an admin uses 'View as' to impersonate a user who lacks access to a connection, the backend correctly denies access to that connection. This is the backend counterpart to the frontend fix - both layers must refuse access to ensure the security model holds. Co-authored-by: Venkat SF <venkatesh.sakamuri@stayflexi.com>
venkateshsakamuri-lab
marked this pull request as ready for review
September 23, 2026 18:51
venkateshsakamuri-lab
deleted the
cursor/view-as-connection-403-d535
branch
September 23, 2026 18:57
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When an admin uses "View as" to impersonate a user who doesn't have access to the currently selected connection, the UI shows a 403 error ("chat and editor access denied for this connection").
Reproduction Steps
aws-rds-masterconnection (only Arun/Gaurav have grants)User Experience
The user is stuck on a connection they cannot access, with no clear way to recover. The connection appears selected in the sidebar, but all API calls fail with 403.
Root Cause
The frontend's
useConnectionManagerhook had an early return whenconnectionIdwas set, without validating that the connection was actually in the current user'sconnectionsarray.When impersonation changes:
ConnectionAccessService.getVisibleConnections()useEffectreturned early ifconnectionIdwas set, keeping the stale connectionconnectionId, getting 403Fix
1. Core fix in
useConnectionManager.jsAdded validation that
connectionIdexists in theconnectionsarray. If not, clear it and auto-select from valid options:2. Defensive guards in section components
Added guards in all connection-scoped sections to prevent rendering with invalid connections:
isLoadingfirst - prevents rendering during connection list refreshconnectionIdANDselectedConnection- ensures the connection is validThis pattern was applied to all affected sections:
AgentChatSectionEditorSectionDashboardsSectionSchemaSectionSchemaDocsSectionSlowQueriesSectionDigestSectionCompanyKnowledgeSectionMonitorSection3. Backend test added
Added
impersonatingUserWithoutAccessDeniesConnection()test to verify the backend correctly denies access when an admin views as a user who lacks access to a connection.Security Considerations
This fix does NOT weaken security:
assertCanReadConnectionContent,assertCanUseChatEditor, etc.) remain in placeTesting
The fix ensures:
Scenarios Covered
Test Results
AccessControlServiceTestpasses (including new impersonation test)Note for Stayflexi Deployment
On Stayflexi, only Arun and Gaurav have grants on the
aws-rds-masterconnection. Other users viewing as them or accessing that connection will now see a proper "no connection selected" state rather than a confusing 403 error. If broader access is needed, connection grants should be added via Manage Connections → Share.Files Changed
src/lib/hooks/useConnectionManager.jssrc/components/sections/*.jsxsrc/components/sections/*.module.cssbackend/.../AccessControlServiceTest.java