Skip to content

fix(chat): enforce project membership on chat endpoints and websocket messages - #21

Merged
SrStamm merged 1 commit into
mainfrom
fix/chat-authorization
Oct 20, 2026
Merged

SrStamm merged 1 commit into
mainfrom
fix/chat-authorization

Conversation

@SrStamm

@SrStamm SrStamm commented Oct 20, 2026

Copy link
Copy Markdown
Owner

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 the group_message websocket 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

  • New repositories/chat_repositories.py with ChatRepository (get_chat, save_message) plus its dependency factory. Queries filter by project_id only; SQLAlchemyError is wrapped into DatabaseError with rollback on save.
  • New services/chat_services.py with ChatService, which validates project membership through ProjectService.found_user_in_project_or_404 before touching data, and maps UserNotInProjectError to ProjectNotFoundError (404) so project existence is not leaked to non-members.
  • GET /chat/{project_id} and POST /chat/{project_id} now delegate to ChatService; the router no longer contains data logic.
  • Websocket group_message reuses ChatService.send_message and only broadcasts after a successful save. Non-members receive an error event.
  • Fixed websocket error handling: use exceptions.DatabaseError instead of the SQLAlchemy import, and the event dispatch chain now uses elif.
  • Tests: added test/test_services/test_chat_services.py covering the authorization mapping in the service layer (non-member get/send, empty chat), and updated test/test_routers/test_ws.py.

Notes

UserNotInProjectError keeps its global 400 semantics; the 404 conversion is scoped to ChatService only. The websocket notification branch is intentionally untouched (out of B5 scope).

Verification

pytest -q -> 172 passed (baseline was 155).

… 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
@SrStamm
SrStamm merged commit a8d5a8d into main Oct 20, 2026
1 check passed
@SrStamm
SrStamm deleted the fix/chat-authorization branch October 20, 2026 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant