Repository navigation
fix(chat): enforce project membership on chat endpoints and websocket messages - #21
Merged
Merged
Conversation
… messages - Move chat data access from the router into ChatRepository and ChatService - Validate project membership on GET /chat, POST /chat and the group_message websocket event; non-members receive 404 to avoid leaking project existence - Reuse ChatService.send_message from the websocket handler and broadcast only after a successful save - Fix websocket error handling (SQLAlchemy DatabaseError import, elif branch) and update chat tests
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.
Context
B5 of the portfolio fixes: the chat endpoints had an IDOR.
GET /chat/{project_id}returned any project's messages without checking membership, and thegroup_messagewebsocket event allowed any authenticated user to write into a project they did not belong to. Data access also lived directly in the router, mixing HTTP concerns with queries.Changes
repositories/chat_repositories.pywithChatRepository(get_chat,save_message) plus its dependency factory. Queries filter byproject_idonly;SQLAlchemyErroris wrapped intoDatabaseErrorwith rollback on save.services/chat_services.pywithChatService, which validates project membership throughProjectService.found_user_in_project_or_404before touching data, and mapsUserNotInProjectErrortoProjectNotFoundError(404) so project existence is not leaked to non-members.GET /chat/{project_id}andPOST /chat/{project_id}now delegate toChatService; the router no longer contains data logic.group_messagereusesChatService.send_messageand only broadcasts after a successful save. Non-members receive anerrorevent.exceptions.DatabaseErrorinstead of the SQLAlchemy import, and the event dispatch chain now useselif.test/test_services/test_chat_services.pycovering the authorization mapping in the service layer (non-member get/send, empty chat), and updatedtest/test_routers/test_ws.py.Notes
UserNotInProjectErrorkeeps its global 400 semantics; the 404 conversion is scoped toChatServiceonly. The websocketnotificationbranch is intentionally untouched (out of B5 scope).Verification
pytest -q-> 172 passed (baseline was 155).