fix: enforce ownership when reading ChatUI sessions (#9141)

This commit is contained in:
VectorPeak
2026-07-05 09:34:38 +08:00
committed by GitHub
parent b43cc6dee0
commit 041fba4df4
2 changed files with 50 additions and 1 deletions

View File

@@ -1196,7 +1196,11 @@ class ChatService:
async def get_session(self, username: str, session_id: str) -> dict:
session = await self.db.get_platform_session_by_id(session_id)
platform_id = session.platform_id if session else "webchat"
if not session:
raise ChatServiceError(f"Session {session_id} not found")
if session.creator != username:
raise ChatServiceError("Permission denied")
platform_id = session.platform_id
project_info = await self.db.get_project_by_session(
session_id=session_id, creator=username

View File

@@ -2217,6 +2217,51 @@ async def test_batch_delete_sessions_uses_batch_lookup(
assert called["batch_lookup_count"] == 1
@pytest.mark.asyncio
@pytest.mark.parametrize(
"path_template",
[
"/api/chat/get_session?session_id={session_id}",
"/api/v1/chat/sessions/{session_id}",
],
)
async def test_get_chat_session_rejects_session_owned_by_another_user(
app: FastAPIAppAdapter,
authenticated_header: dict,
core_lifecycle_td: AstrBotCoreLifecycle,
path_template: str,
):
test_client = app.test_client()
session_id = f"foreign_get_session_{uuid.uuid4().hex[:8]}"
await core_lifecycle_td.db.create_platform_session(
creator="not_dashboard_user",
platform_id="webchat",
session_id=session_id,
display_name="Foreign Session",
is_group=0,
)
await core_lifecycle_td.platform_message_history_manager.insert(
platform_id="webchat",
user_id=session_id,
content={
"type": "user",
"message": [{"type": "text", "text": "foreign session secret"}],
},
sender_id="not_dashboard_user",
sender_name="not_dashboard_user",
)
response = await test_client.get(
path_template.format(session_id=session_id),
headers=authenticated_header,
)
assert response.status_code == 200
data = await response.get_json()
assert data["status"] == "error"
assert data["message"] == "Permission denied"
@pytest.mark.asyncio
async def test_plugins(
app: FastAPIAppAdapter,