Skip to content

fix: authorize ownership and validate before deleting in DELETE /documents - #304

Open
nangelovv wants to merge 1 commit into
danny-avila:mainfrom
nangelovv:fix/delete-documents-authorization
Open

fix: authorize ownership and validate before deleting in DELETE /documents#304
nangelovv wants to merge 1 commit into
danny-avila:mainfrom
nangelovv:fix/delete-documents-authorization

Conversation

@nangelovv

Copy link
Copy Markdown

What

Add an ownership check to DELETE /documents and move both the existence and ownership checks before the destructive delete.

Why

Fixes #303.

  1. The endpoint had no user_id check, so any authenticated caller could delete another tenant's chunks by file_id.
  2. It deleted first and validated existence afterwards, so a request mixing valid and unknown ids destroyed the valid rows and still returned 404.

Change

  • Resolve the requester with get_user_id(request, entity_id) (adds an optional entity_id form/query param, consistent with the embed endpoints).
  • Fetch the target documents and:
    • 404 if any requested id does not exist — before deleting;
    • 403 if any chunk's user_id is neither None (unowned) nor the requester — before deleting.
  • Only then call vector_store.delete(...).

Note — contract change

DELETE /documents now requires the caller to own (or the document to be unowned) every id. Self-deletes via the user's identity are unaffected; public-mode documents (user_id="public") still match. An unauthenticated process deleting arbitrary ids would now be rejected. (Related closed PR: #262.)

Testing

  • New tests/test_main.py:
    • test_delete_documents_rejects_other_users — another user's docs → 403 and delete() is never called.
    • test_delete_documents_validates_before_deleting — an unknown id → 404 and delete() is never called.
  • Existing test_delete_documents (self-owned/unowned) still passes. tests/test_main.py: 12 passed.

…ments

DELETE /documents had two problems:

1. No ownership check — any authenticated caller could delete any file's chunks
   by passing its file_id, allowing cross-tenant data destruction.
2. It deleted first and validated existence afterwards, so a request mixing
   valid and unknown ids destroyed the valid rows and then returned 404
   ("not found"), implying nothing had happened.

Resolve the requester's identity (get_user_id, with optional entity_id), fetch
the target documents, and verify existence (404) and ownership (403) BEFORE the
destructive delete; nothing is deleted when either check fails. Unowned chunks
(user_id is None) remain deletable, consistent with the rest of the per-user
model.
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.

DELETE /documents has no ownership check and deletes before validating (cross-tenant data destruction)

1 participant