Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix: an upload can no longer replace a shared document without the permission to delete one (#2479)

Rico Furtado94c0ea500aed
diff --git a/src/api/connectors.py b/src/api/connectors.py
index a982e6a53..266723bf9 100644
--- a/src/api/connectors.py
+++ b/src/api/connectors.py
@@ -2406,6 +2406,12 @@ async def connector_webhook(
# shared=None like the other re-sync paths: no user is in the
# loop to express an intent, so each file keeps the sharing state
# it already has.
+ # allow_anonymous_delete=False for the same reason: nobody is
+ # here to authorize removing a document the whole instance can
+ # see, so a webhook replaces only what the connection's owner
+ # owns. A file indexed as shared is left as it is until someone
+ # holding the permission syncs it; no connector that delivers
+ # webhooks can index one today.
task_id = await connector_service.sync_specific_files(
connection.connection_id,
connection.user_id,
@@ -2413,6 +2419,7 @@ async def connector_webhook(
jwt_token=jwt_token,
replace_duplicates=_connector_sync_should_replace(connector_type),
shared=None,
+ allow_anonymous_delete=False,
)
result = {
diff --git a/src/api/langflow_files.py b/src/api/langflow_files.py
index 876eaf881..c91a2a9ac 100644
--- a/src/api/langflow_files.py
+++ b/src/api/langflow_files.py
@@ -162,6 +162,9 @@ async def upload_and_ingest_user_file(
session_id=session_id,
tweaks=tweaks,
settings=settings,
+ # This endpoint never replaces (replace_duplicates stays
+ # False), so no delete is attempted; False keeps it that way.
+ allow_anonymous_delete=False,
)
return JSONResponse(
diff --git a/src/api/router.py b/src/api/router.py
index 8b76ba27a..426ff60a7 100644
--- a/src/api/router.py
+++ b/src/api/router.py
@@ -5,7 +5,7 @@ import mimetypes
import os
import tempfile
-from fastapi import Depends, File, Form, UploadFile
+from fastapi import Depends, File, Form, Request, UploadFile
from fastapi.responses import JSONResponse
from config.settings import get_openrag_config
@@ -13,8 +13,10 @@ from dependencies import (
get_current_user,
get_document_service,
get_langflow_file_service,
+ get_rbac_service,
get_session_manager,
get_task_service,
+ has_effective_permission,
)
from session_manager import User
from utils.ingest_preview_flag import is_ingest_preview_enabled
@@ -24,6 +26,7 @@ logger = get_logger(__name__)
async def upload_ingest_router(
+ request: Request,
file: list[UploadFile] = File(...),
session_id: str | None = Form(None),
settings_json: str | None = Form(None, alias="settings"),
@@ -36,6 +39,7 @@ async def upload_ingest_router(
session_manager=Depends(get_session_manager),
task_service=Depends(get_task_service),
user: User = Depends(get_current_user),
+ rbac=Depends(get_rbac_service),
):
"""
Router endpoint that automatically routes upload requests based on configuration.
@@ -54,6 +58,14 @@ async def upload_ingest_router(
preview_mode=preview_mode,
)
+ # Resolved here, where the request and the RBAC service are, and carried
+ # down to the processor. Overwriting a file whose name belongs to a shared
+ # (ownerless) document deletes that document for the whole instance, so it
+ # takes the same permission the explicit delete endpoints ask for.
+ allow_anonymous_delete = await has_effective_permission(
+ request, user, rbac, "knowledge:delete:anonymous"
+ )
+
if disable_ingest_with_langflow:
logger.debug("Routing to traditional OpenRAG upload via task service")
return await _traditional_upload_ingest_task(
@@ -64,6 +76,7 @@ async def upload_ingest_router(
session_manager=session_manager,
task_service=task_service,
user=user,
+ allow_anonymous_delete=allow_anonymous_delete,
settings_json=settings_json,
)
@@ -80,6 +93,7 @@ async def upload_ingest_router(
session_manager=session_manager,
task_service=task_service,
user=user,
+ allow_anonymous_delete=allow_anonymous_delete,
)
@@ -91,6 +105,7 @@ async def _traditional_upload_ingest_task(
session_manager,
task_service,
user: User,
+ allow_anonymous_delete: bool,
settings_json: str | None = None,
):
"""Task-based traditional upload and ingest for single/multiple files"""
@@ -153,6 +168,7 @@ async def _traditional_upload_ingest_task(
replace_duplicates=replace_duplicates,
settings=settings,
preview_mode=preview_mode,
+ allow_anonymous_delete=allow_anonymous_delete,
)
return JSONResponse(
@@ -197,6 +213,7 @@ async def _langflow_upload_ingest_task(
session_manager,
task_service,
user: User,
+ allow_anonymous_delete: bool,
):
"""Task-based langflow upload and ingest for single/multiple files"""
try:
@@ -265,6 +282,7 @@ async def _langflow_upload_ingest_task(
settings=settings,
replace_duplicates=replace_duplicates,
preview_mode=preview_mode,
+ allow_anonymous_delete=allow_anonymous_delete,
)
return JSONResponse(
diff --git a/src/api/upload.py b/src/api/upload.py
index 04c0415a8..e51a37bdc 100644
--- a/src/api/upload.py
+++ b/src/api/upload.py
@@ -3,7 +3,7 @@ from typing import Annotated, Any
from urllib.parse import urlparse
import boto3
-from fastapi import Depends, File, Form, UploadFile
+from fastapi import Depends, File, Form, Request, UploadFile
from fastapi.responses import JSONResponse
from pydantic import BaseModel
@@ -13,8 +13,10 @@ from dependencies import (
get_docling_service,
get_document_service,
get_models_service,
+ get_rbac_service,
get_session_manager,
get_task_service,
+ has_effective_permission,
require_all_permissions,
require_permission,
)
@@ -106,6 +108,9 @@ async def upload_path(
jwt_token=jwt_token,
owner_name=owner_name,
owner_email=owner_email,
+ # This endpoint never replaces (replace_duplicates stays False), so no
+ # delete is attempted; False keeps it that way.
+ allow_anonymous_delete=False,
)
return JSONResponse(
@@ -183,11 +188,13 @@ async def upload_options(
async def upload_bucket(
body: UploadBucketBody,
+ request: Request,
task_service: Annotated[Any, Depends(get_task_service)],
models_service: Annotated[Any, Depends(get_models_service)],
docling_service: Annotated[Any, Depends(get_docling_service)],
session_manager: Annotated[Any, Depends(get_session_manager)],
user: Annotated[User, Depends(require_permission("knowledge:upload"))],
+ rbac: Annotated[Any, Depends(get_rbac_service)],
):
"""Process all files from an S3 bucket URL"""
if not os.getenv("AWS_ACCESS_KEY_ID") or not os.getenv("AWS_SECRET_ACCESS_KEY"):
@@ -242,6 +249,11 @@ async def upload_bucket(
owner_name=owner_name,
owner_email=owner_email,
replace_duplicates=body.replace_duplicates,
+ # An S3 key that matches a shared (ownerless) document replaces it for
+ # the whole instance, so that takes the permission to delete one.
+ allow_anonymous_delete=await has_effective_permission(
+ request, user, rbac, "knowledge:delete:anonymous"
+ ),
)
task_id = await task_service.create_custom_task(task_user_id, keys, processor)
diff --git a/src/api/v1/documents.py b/src/api/v1/documents.py
index e58590e2f..256552cbb 100644
--- a/src/api/v1/documents.py
+++ b/src/api/v1/documents.py
@@ -39,6 +39,7 @@ class DeleteDocV1Body(BaseModel):
async def ingest_endpoint(
+ request: Request,
file: list[UploadFile] = File(...),
session_id: str | None = Form(None),
settings: str | None = Form(None),
@@ -50,6 +51,7 @@ async def ingest_endpoint(
session_manager=Depends(get_session_manager),
task_service=Depends(get_task_service),
user: User = Depends(require_api_key_permission("knowledge:upload")),
+ rbac=Depends(get_rbac_service),
):
"""
Ingest a document into the knowledge base.
@@ -63,6 +65,7 @@ async def ingest_endpoint(
ingestion behavior, and the v1 SDK does not expose this option.
"""
return await upload_ingest_router(
+ request=request,
file=file,
session_id=session_id,
settings_json=settings,
@@ -78,6 +81,7 @@ async def ingest_endpoint(
session_manager=session_manager,
task_service=task_service,
user=user,
+ rbac=rbac,
)
diff --git a/src/connectors/service.py b/src/connectors/service.py
index 99ec51a3f..a2be1e9cc 100644
--- a/src/connectors/service.py
+++ b/src/connectors/service.py
@@ -243,7 +243,8 @@ class ConnectorService:
ingest_settings: dict[str, Any] | None = None,
replace_duplicates: bool = False,
shared: bool | None = False,
- allow_anonymous_delete: bool = True,
+ *,
+ allow_anonymous_delete: bool,
) -> str:
"""
Sync files from a connector connection using existing task tracking system.
@@ -258,6 +259,9 @@ class ConnectorService:
from being re-synced.
ingest_settings: Optional UI-style dict (``embeddingModel``, ``chunkSize``, …)
forwarded to ``ConnectorFileProcessor``.
+ allow_anonymous_delete: The syncing user's resolved
+ ``knowledge:delete:anonymous``. No default; see
+ ``TaskProcessor.delete_document_by_filename``.
"""
jwt_token = await self._get_effective_sync_jwt(user_id, jwt_token)
@@ -387,7 +391,8 @@ class ConnectorService:
replace_duplicates: bool = False,
preview_mode: bool = False,
shared: bool | None = False,
- allow_anonymous_delete: bool = True,
+ *,
+ allow_anonymous_delete: bool,
) -> str:
"""
Sync specific files by their IDs (used for webhook-triggered syncs or manual selection).
@@ -402,6 +407,9 @@ class ConnectorService:
When provided, download URLs can be used directly without Graph API calls.
ingest_settings: Optional UI-style dict (``embeddingModel``, ``chunkSize``, …) passed to
``ConnectorFileProcessor`` when Langflow ingest is disabled.
+ allow_anonymous_delete: The syncing user's resolved
+ ``knowledge:delete:anonymous``. No default; see
+ ``TaskProcessor.delete_document_by_filename``.
"""
jwt_token = await self._get_effective_sync_jwt(user_id, jwt_token)
diff --git a/src/models/processors.py b/src/models/processors.py
index 2fca67024..b14aa7f94 100644
--- a/src/models/processors.py
+++ b/src/models/processors.py
@@ -265,9 +265,9 @@ class TaskProcessor:
*,
replace: bool,
owner_user_id: str | None,
+ allow_anonymous_delete: bool,
shared: bool = False,
claim_holder: str | None = None,
- allow_anonymous_delete: bool = True,
) -> Literal["proceed", "skip", "skip_in_flight", "replaced"]:
"""Single duplicate-filename policy shared by every processor.
@@ -293,6 +293,9 @@ class TaskProcessor:
racing the index check. TaskService releases the claim when the file
reaches a terminal state. Callers that pass None get the OpenSearch
policy alone — every processor passes one.
+
+ ``allow_anonymous_delete`` is handed to ``delete_document_by_filename``
+ and, like there, has no default: see that docstring.
"""
if claim_holder is not None and not filename_claims.claim(
claim_holder, filename, owner_user_id=owner_user_id, shared=shared
@@ -357,14 +360,16 @@ class TaskProcessor:
opensearch_client,
owner_user_id: str | None = None,
shared: bool = False,
- allow_anonymous_delete: bool = True,
+ *,
+ allow_anonymous_delete: bool,
) -> int:
"""Delete all chunks of a document with the given filename from
OpenSearch. Returns the number of chunks deleted.
``shared`` describes how the replacement is about to be *written*, not
what is already indexed, so it must not narrow what we delete: with an
- owner in hand the scope is "owned by this user OR ownerless".
+ owner in hand, and the permission below, the scope is "owned by this
+ user OR ownerless".
Choosing an owner-only scope for a shared document matched none of its
chunks, and the caller read that zero as "nothing to replace" and
skipped the file — leaving the stale copy in the index even though the
@@ -376,12 +381,19 @@ class TaskProcessor:
in the instance, so replacing a document that turns out to be one is a
deletion of shared content: without that permission the scope stays
owner-only and someone else's shared document is left alone (the file
- then resolves as a duplicate the user may not replace). It defaults to
- True because callers that have not resolved the permission — uploads,
- the Langflow path, sample docs — keep their existing behaviour; see the
- note in the PR about closing that across every entry point.
- Deliberately ignored when ``shared`` is True: a shared write already
- required the permission upstream."""
+ then resolves as a duplicate the user may not replace). It has no
+ default on purpose. It used to default to True, and the entry points
+ that never resolved the permission — local upload, S3, the Langflow
+ path — inherited that silently, so any user could replace a shared
+ document through them. Every caller now has to say which it is, down
+ to the processor constructors and the task-service helpers that feed
+ them; one acting for the system rather than for a user passes True and
+ says why.
+ ``shared`` does not stand in for it. An explicit shared sync already
+ holds the permission (connector_sync refuses one without it), but on
+ the re-sync paths ``shared`` is inferred from what is indexed, and an
+ inferred answer authorizes nothing: honouring it let a caller without
+ the permission replace a shared document simply by re-syncing it."""
from config.settings import clients, get_index_name
from utils.opensearch_delete import collect_visible_document_ids, delete_document_ids
from utils.opensearch_queries import (
@@ -407,7 +419,7 @@ class TaskProcessor:
)
return 0
- elif shared or allow_anonymous_delete:
+ elif allow_anonymous_delete:
build_query = build_replace_filename_query
else:
build_query = build_owned_filename_query
@@ -806,6 +818,8 @@ class DocumentFileProcessor(TaskProcessor):
replace_duplicates: bool = False,
session_manager=None,
settings: dict | None = None,
+ *,
+ allow_anonymous_delete: bool,
):
super().__init__(
document_service,
@@ -824,6 +838,9 @@ class DocumentFileProcessor(TaskProcessor):
document_service.session_manager if document_service else None
)
self.settings = settings
+ # The uploading user's resolved knowledge:delete:anonymous; see
+ # delete_document_by_filename.
+ self.allow_anonymous_delete = allow_anonymous_delete
if self.session_manager is None:
raise ValueError("session_manager is required for DocumentFileProcessor")
@@ -850,6 +867,7 @@ class DocumentFileProcessor(TaskProcessor):
replace=self.replace_duplicates,
owner_user_id=self.owner_user_id,
claim_holder=self._claim_holder(upload_task, file_task),
+ allow_anonymous_delete=self.allow_anonymous_delete,
)
if duplicate_action in DUPLICATE_SKIP_ACTIONS:
self.mark_duplicate_skipped(upload_task, file_task)
@@ -957,7 +975,8 @@ class ConnectorFileProcessor(TaskProcessor):
connector_type: str | None = None,
preview_mode: bool = False,
shared: bool | None = False,
- allow_anonymous_delete: bool = True,
+ *,
+ allow_anonymous_delete: bool,
):
super().__init__(
document_service=document_service,
@@ -1584,6 +1603,8 @@ class S3FileProcessor(TaskProcessor):
models_service=None,
docling_service=None,
replace_duplicates: bool = False,
+ *,
+ allow_anonymous_delete: bool,
):
import boto3
@@ -1599,6 +1620,9 @@ class S3FileProcessor(TaskProcessor):
self.owner_name = owner_name
self.owner_email = owner_email
self.replace_duplicates = replace_duplicates
+ # The uploading user's resolved knowledge:delete:anonymous; see
+ # delete_document_by_filename.
+ self.allow_anonymous_delete = allow_anonymous_delete
async def process_item(self, upload_task: UploadTask, item: str, file_task: FileTask) -> None:
"""Download an S3 object and process it using DocumentService"""
@@ -1621,6 +1645,7 @@ class S3FileProcessor(TaskProcessor):
replace=self.replace_duplicates,
owner_user_id=self.owner_user_id,
claim_holder=self._claim_holder(upload_task, file_task),
+ allow_anonymous_delete=self.allow_anonymous_delete,
)
if duplicate_action in DUPLICATE_SKIP_ACTIONS:
self.mark_duplicate_skipped(upload_task, file_task)
@@ -1691,6 +1716,8 @@ class LangflowFileProcessor(TaskProcessor):
connector_type: str = "local",
docling_polling_service=None,
preview_mode: bool = False,
+ *,
+ allow_anonymous_delete: bool,
):
super().__init__()
self.langflow_file_service = langflow_file_service
@@ -1706,6 +1733,9 @@ class LangflowFileProcessor(TaskProcessor):
self.connector_type = connector_type
self.docling_polling_service = docling_polling_service
self.preview_mode = preview_mode
+ # The uploading user's resolved knowledge:delete:anonymous; see
+ # delete_document_by_filename.
+ self.allow_anonymous_delete = allow_anonymous_delete
async def process_item(self, upload_task: UploadTask, item: str, file_task: FileTask) -> None:
"""Process a file path using LangflowFileService upload_and_ingest_file"""
@@ -1729,6 +1759,7 @@ class LangflowFileProcessor(TaskProcessor):
replace=self.replace_duplicates,
owner_user_id=self.owner_user_id,
claim_holder=self._claim_holder(upload_task, file_task),
+ allow_anonymous_delete=self.allow_anonymous_delete,
)
if duplicate_action in DUPLICATE_SKIP_ACTIONS:
self.mark_duplicate_skipped(upload_task, file_task)
diff --git a/src/models/url.py b/src/models/url.py
index 9f9dcc8cc..f9f1a985c 100644
--- a/src/models/url.py
+++ b/src/models/url.py
@@ -61,6 +61,9 @@ class UrlProcessor(TaskProcessor):
owner_email=self.owner_email,
is_sample_data=self.is_sample_data,
connector_type=self.connector_type,
+ # Only process_document_standard runs on this instance; the
+ # duplicate gate, and so the delete, never does.
+ allow_anonymous_delete=False,
)
result = await processor.process_document_standard(
file_path=temp_file_path,
diff --git a/src/services/default_docs_service.py b/src/services/default_docs_service.py
index ce2e6a67a..ee35d6998 100644
--- a/src/services/default_docs_service.py
+++ b/src/services/default_docs_service.py
@@ -225,6 +225,9 @@ async def _ingest_default_documents_langflow(
replace_duplicates=True,
connector_type=connector_type,
temp_file_paths=[],
+ # The system refreshing its own ownerless sample documents, not a user
+ # replacing someone else's — no user permission applies.
+ allow_anonymous_delete=True,
)
logger.info(
@@ -653,6 +656,9 @@ async def _ingest_default_documents_openrag(
owner_email=anonymous_user.email,
is_sample_data=True,
connector_type=connector_type,
+ # The system ingesting its own ownerless sample documents, not a user
+ # replacing someone else's — no user permission applies.
+ allow_anonymous_delete=True,
)
task_id = await task_service.create_custom_task("anonymous", file_paths, processor)
diff --git a/src/services/task_service.py b/src/services/task_service.py
index 5f6c43386..de0cd1be8 100644
--- a/src/services/task_service.py
+++ b/src/services/task_service.py
@@ -346,8 +346,15 @@ class TaskService:
replace_duplicates: bool = False,
settings: dict | None = None,
preview_mode: bool = False,
+ *,
+ allow_anonymous_delete: bool,
) -> str:
- """Create a new upload task for bulk file processing"""
+ """Create a new upload task for bulk file processing.
+
+ ``allow_anonymous_delete`` is the uploading user's resolved
+ ``knowledge:delete:anonymous`` and has no default; see
+ ``TaskProcessor.delete_document_by_filename``.
+ """
# Use default DocumentFileProcessor with user context
from models.processors import DocumentFileProcessor
@@ -362,6 +369,7 @@ class TaskService:
replace_duplicates=replace_duplicates,
session_manager=self.session_manager,
settings=settings,
+ allow_anonymous_delete=allow_anonymous_delete,
)
return await self.create_custom_task(
user_id,
@@ -390,8 +398,15 @@ class TaskService:
existing_task_id: str = None,
temp_file_paths: list | None = None,
preview_mode: bool = False,
+ *,
+ allow_anonymous_delete: bool,
) -> str:
- """Create a new upload task for Langflow file processing with upload and ingest"""
+ """Create a new upload task for Langflow file processing with upload and ingest.
+
+ ``allow_anonymous_delete`` is the uploading user's resolved
+ ``knowledge:delete:anonymous`` and has no default; see
+ ``TaskProcessor.delete_document_by_filename``.
+ """
# Use LangflowFileProcessor with user context
from models.processors import LangflowFileProcessor
@@ -409,6 +424,7 @@ class TaskService:
connector_type=connector_type,
docling_polling_service=self.docling_polling_service,
preview_mode=preview_mode,
+ allow_anonymous_delete=allow_anonymous_delete,
)
return await self.create_custom_task(
user_id,
diff --git a/tests/unit/api/test_upload_index_preflight.py b/tests/unit/api/test_upload_index_preflight.py
index 9c6347500..a80beceb8 100644
--- a/tests/unit/api/test_upload_index_preflight.py
+++ b/tests/unit/api/test_upload_index_preflight.py
@@ -100,6 +100,8 @@ async def test_upload_bucket_returns_5xx_and_skips_task_when_index_check_fails(m
docling_service=MagicMock(),
session_manager=MagicMock(),
user=_make_user(),
+ request=MagicMock(),
+ rbac=MagicMock(),
)
assert 500 <= response.status_code < 600
diff --git a/tests/unit/api/test_v1_documents_ingest_preview.py b/tests/unit/api/test_v1_documents_ingest_preview.py
index d097d262c..9495bff1d 100644
--- a/tests/unit/api/test_v1_documents_ingest_preview.py
+++ b/tests/unit/api/test_v1_documents_ingest_preview.py
@@ -30,6 +30,8 @@ async def test_v1_ingest_forwards_preview_as_string():
session_manager=MagicMock(),
task_service=MagicMock(),
user=user,
+ request=MagicMock(),
+ rbac=MagicMock(),
)
call_kwargs = mock_router.await_args.kwargs
diff --git a/tests/unit/config/test_disable_ingest_setting.py b/tests/unit/config/test_disable_ingest_setting.py
index 7e45509b1..c462ad995 100644
--- a/tests/unit/config/test_disable_ingest_setting.py
+++ b/tests/unit/config/test_disable_ingest_setting.py
@@ -104,6 +104,7 @@ async def test_traditional_upload_ingest_task(monkeypatch):
session_manager=mock_session_manager,
task_service=mock_task_service,
user=mock_user,
+ allow_anonymous_delete=True,
)
assert response.status_code == 202
@@ -168,6 +169,7 @@ async def test_langflow_upload_ingest_task(monkeypatch):
session_manager=mock_session_manager,
task_service=mock_task_service,
user=mock_user,
+ allow_anonymous_delete=True,
)
assert response.status_code == 202
@@ -230,6 +232,7 @@ async def test_traditional_upload_ingest_mime_fallback(monkeypatch):
session_manager=mock_session_manager,
task_service=mock_task_service,
user=mock_user,
+ allow_anonymous_delete=True,
)
assert response.status_code == 202
diff --git a/tests/unit/connectors/test_connector_file_type_validation.py b/tests/unit/connectors/test_connector_file_type_validation.py
index 68ecc82fe..5ff67f7d5 100644
--- a/tests/unit/connectors/test_connector_file_type_validation.py
+++ b/tests/unit/connectors/test_connector_file_type_validation.py
@@ -57,7 +57,11 @@ async def test_sync_specific_files_does_not_raise_on_incompatible_type():
# Verify that calling sync_specific_files succeeds (no ValueError raised!)
task_id = await service.sync_specific_files(
- connection_id="conn-id", user_id="user-id", file_ids=["folder-id"], jwt_token="jwt"
+ connection_id="conn-id",
+ user_id="user-id",
+ file_ids=["folder-id"],
+ jwt_token="jwt",
+ allow_anonymous_delete=True,
)
assert task_id == "dummy-task-id"
@@ -83,6 +87,7 @@ async def test_connector_file_processor_fails_incompatible_file():
jwt_token="jwt",
document_service=MagicMock(),
models_service=MagicMock(),
+ allow_anonymous_delete=True,
)
upload_task = UploadTask(task_id="task-id", total_files=1)
diff --git a/tests/unit/connectors/test_sync_connector_files_paging.py b/tests/unit/connectors/test_sync_connector_files_paging.py
index 95f4f3d15..63be86183 100644
--- a/tests/unit/connectors/test_sync_connector_files_paging.py
+++ b/tests/unit/connectors/test_sync_connector_files_paging.py
@@ -104,7 +104,7 @@ async def test_uncapped_sync_takes_every_file_not_just_the_first_hundred():
connector, requested_max_files = _internally_paginating_connector(total=150)
service = _service(connector)
- await service.sync_connector_files("conn-1", "alice")
+ await service.sync_connector_files("conn-1", "alice", allow_anonymous_delete=True)
assert len(_processed_ids(service)) == 150
# No cap was requested, so the connector was free to return everything.
@@ -116,7 +116,7 @@ async def test_explicit_max_files_still_caps():
connector, requested_max_files = _internally_paginating_connector(total=150)
service = _service(connector)
- await service.sync_connector_files("conn-1", "alice", max_files=20)
+ await service.sync_connector_files("conn-1", "alice", max_files=20, allow_anonymous_delete=True)
assert len(_processed_ids(service)) == 20
assert requested_max_files == [20]
@@ -132,7 +132,7 @@ async def test_max_files_zero_lists_nothing():
connector, requested_max_files = _internally_paginating_connector(total=150)
service = _service(connector)
- await service.sync_connector_files("conn-1", "alice", max_files=0)
+ await service.sync_connector_files("conn-1", "alice", max_files=0, allow_anonymous_delete=True)
assert _processed_ids(service) == []
assert requested_max_files == []
@@ -146,7 +146,7 @@ async def test_token_paginated_connector_is_followed_across_pages():
connector, requested_tokens = _token_paginating_connector(pages)
service = _service(connector)
- await service.sync_connector_files("conn-1", "alice")
+ await service.sync_connector_files("conn-1", "alice", allow_anonymous_delete=True)
assert len(_processed_ids(service)) == 5
assert requested_tokens == [None, "1", "2"]
@@ -158,7 +158,7 @@ async def test_token_paginated_connector_continues_after_empty_page():
connector, requested_tokens = _token_paginating_connector(pages)
service = _service(connector)
- await service.sync_connector_files("conn-1", "alice")
+ await service.sync_connector_files("conn-1", "alice", allow_anonymous_delete=True)
assert _processed_ids(service) == ["b1::k0.pdf", "b1::k1.pdf"]
assert requested_tokens == [None, "1"]
@@ -170,7 +170,7 @@ async def test_token_paginated_connector_stops_at_aggregate_max_files():
connector, requested_tokens = _token_paginating_connector(pages)
service = _service(connector)
- await service.sync_connector_files("conn-1", "alice", max_files=20)
+ await service.sync_connector_files("conn-1", "alice", max_files=20, allow_anonymous_delete=True)
assert _processed_ids(service) == [f"b1::k{i}.pdf" for i in range(20)]
assert requested_tokens == [None, "1"]
@@ -194,7 +194,7 @@ async def test_camel_case_token_still_paginates():
connector.list_files = AsyncMock(side_effect=list_files)
service = _service(connector)
- await service.sync_connector_files("conn-1", "alice")
+ await service.sync_connector_files("conn-1", "alice", allow_anonymous_delete=True)
assert len(_processed_ids(service)) == 2
assert calls == [None, "p2"]
@@ -211,6 +211,8 @@ async def test_filename_filter_matches_beyond_the_first_page():
connector, _ = _internally_paginating_connector(total=150)
service = _service(connector)
- await service.sync_connector_files("conn-1", "alice", filename_filter={"k0.pdf", "k130.pdf"})
+ await service.sync_connector_files(
+ "conn-1", "alice", filename_filter={"k0.pdf", "k130.pdf"}, allow_anonymous_delete=True
+ )
assert sorted(_processed_ids(service)) == ["b1::k0.pdf", "b1::k130.pdf"]
diff --git a/tests/unit/connectors/test_sync_specific_files_deleted_ids.py b/tests/unit/connectors/test_sync_specific_files_deleted_ids.py
index 7be77eaab..74a19267d 100644
--- a/tests/unit/connectors/test_sync_specific_files_deleted_ids.py
+++ b/tests/unit/connectors/test_sync_specific_files_deleted_ids.py
@@ -57,6 +57,7 @@ async def test_deleted_ids_are_kept_alongside_live_files():
user_id="user-1",
file_ids=["live-1", "deleted-1"],
jwt_token="jwt",
+ allow_anonymous_delete=True,
)
assert task_id == "task-1"
@@ -72,6 +73,7 @@ async def test_delete_only_batch_no_longer_raises():
user_id="user-1",
file_ids=["deleted-1", "deleted-2"],
jwt_token="jwt",
+ allow_anonymous_delete=True,
)
assert task_id == "task-1"
@@ -90,6 +92,7 @@ async def test_known_folder_ids_are_not_readded():
file_ids=["folder-1"],
jwt_token="jwt",
file_infos=[{"id": "folder-1", "name": "Folder", "isFolder": True}],
+ allow_anonymous_delete=True,
)
assert _synced_ids(service) == ["child-1"]
diff --git a/tests/unit/connectors/test_webhook_type_alias.py b/tests/unit/connectors/test_webhook_type_alias.py
index 31776e257..fc7db8e88 100644
--- a/tests/unit/connectors/test_webhook_type_alias.py
+++ b/tests/unit/connectors/test_webhook_type_alias.py
@@ -150,6 +150,37 @@ async def test_webhook_sync_replaces_existing_files(monkeypatch):
assert sync_kwargs["replace_duplicates"] is True
+async def test_webhook_sync_runs_without_the_permission_to_delete_shared_documents(monkeypatch):
+ """Nobody is in the loop on a webhook to authorize removing a document the
+ whole instance can see, so the replace it triggers must not reach one: a
+ private file's re-sync would otherwise delete a same-named shared document
+ that came from another source."""
+ from api.connectors import connector_webhook
+
+ connection = _scope_guard_connection()
+ service = _scope_guard_service(connection)
+ handler = MagicMock()
+ handler.handle_webhook = AsyncMock(return_value=["file-1"])
+ service._get_connector = AsyncMock(return_value=handler)
+ _mock_indexed_files(monkeypatch, ["file-1"])
+
+ request = _FakeRequest({"content-type": "application/json", "x-goog-channel-id": "chan-1"})
+ await connector_webhook(
+ "google_drive",
+ request,
+ connector_service=service,
+ session_manager=MagicMock(),
+ session=MagicMock(),
+ )
+
+ sync_kwargs = service.sync_specific_files.await_args.kwargs
+ assert sync_kwargs["allow_anonymous_delete"] is False
+ # Sharing is still inherited per file: a shared one is skipped as a
+ # duplicate the webhook may not replace, not re-ingested as private.
+ assert sync_kwargs["shared"] is None
+
+
# ---------------------------------------------------------------------------
# connector_webhook — scope guard (only ingest files already indexed)
# ---------------------------------------------------------------------------
diff --git a/tests/unit/test_anonymous_delete_gate_on_every_ingest_path.py b/tests/unit/test_anonymous_delete_gate_on_every_ingest_path.py
new file mode 100644
index 000000000..cad618974
--- /dev/null
+++ b/tests/unit/test_anonymous_delete_gate_on_every_ingest_path.py
@@ -0,0 +1,487 @@
+"""Replacing a shared document takes knowledge:delete:anonymous on every ingest path.
+
+Ownerless ("share-all") chunks are visible to the whole instance, so an
+overwrite whose filename belongs to one deletes it for everybody. The delete
+that decides this — ``TaskProcessor.delete_document_by_filename`` — took the
+caller's permission as a parameter that defaulted to True, and only connector
+sync resolved it. Local upload, the Langflow pipeline and S3 never did, so
+through them any user could replace a shared document: it vanished for everyone
+else and came back owned by the uploader.
+
+Three things are pinned here. Each of those processors hands its caller's
+permission to the delete. Each entry point resolves that permission, or says
+outright why it does not need to. And nothing on the way down has a default to
+fall back on again.
+"""
+
+import inspect
+import json
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+from fastapi import FastAPI, UploadFile
+from fastapi.testclient import TestClient
+
+from connectors.service import ConnectorService
+from models.processors import (
+ ConnectorFileProcessor,
+ DocumentFileProcessor,
+ LangflowFileProcessor,
+ S3FileProcessor,
+ TaskProcessor,
+)
+from models.tasks import FileTask, TaskStatus, UploadTask
+from services.task_service import TaskService
+from session_manager import User
+from utils.opensearch_queries import build_owner_or_shared_filter
+
+SHARED_DOC = "Leave.Policy.pdf"
+UPLOADER = "user-b"
+PERMISSION = "knowledge:delete:anonymous"
+
+
+# ---------------------------------------------------------------------------
+# The processors: the permission reaches the delete
+# ---------------------------------------------------------------------------
+
+
+def _index_holding_one_shared_document(monkeypatch):
+ """One ownerless document under SHARED_DOC, and nothing UPLOADER owns.
+
+ The user client answers a delete scope with the shared chunk only when that
+ scope can match an ownerless one, which is the whole question. Deletes and
+ the refresh that follows go through the admin client.
+ """
+ admin_client = AsyncMock()
+ admin_client.indices = AsyncMock()
+ admin_client.delete = AsyncMock(return_value={"result": "deleted"})
+ # delete_document_by_filename imports these at call time; the refresh after
+ # a delete reads the module-level one.
+ monkeypatch.setattr("config.settings.clients", SimpleNamespace(opensearch=admin_client))
+ monkeypatch.setattr("config.settings.get_index_name", lambda: "test-index")
+ monkeypatch.setattr("models.processors.clients", SimpleNamespace(opensearch=admin_client))
+
+ async def search(*, index, body, **kwargs):
+ reaches_shared = build_owner_or_shared_filter(UPLOADER) in body["query"]["bool"]["filter"]
+ hits = [{"_id": "shared-chunk-1"}] if reaches_shared else []
+ return {"_scroll_id": None, "hits": {"hits": hits}}
+
+ user_client = AsyncMock()
+ user_client.search = AsyncMock(side_effect=search)
+ return user_client, admin_client
+
+
+def _local_upload(user_client, tmp_path, allow_anonymous_delete):
+ session_manager = MagicMock()
+ session_manager.get_user_opensearch_client = MagicMock(return_value=user_client)
+ processor = DocumentFileProcessor(
+ document_service=MagicMock(),
+ models_service=MagicMock(),
+ owner_user_id=UPLOADER,
+ jwt_token="token",
+ replace_duplicates=True,
+ session_manager=session_manager,
+ allow_anonymous_delete=allow_anonymous_delete,
+ )
+ processor.process_document_standard = AsyncMock(return_value={"status": "indexed", "id": "h"})
+ item = tmp_path / "upload.pdf"
+ item.write_bytes(b"new content")
+ return processor, str(item), processor.process_document_standard
+
+
+def _langflow_upload(user_client, tmp_path, allow_anonymous_delete):
+ session_manager = MagicMock()
+ session_manager.get_user_opensearch_client = MagicMock(return_value=user_client)
+ langflow_file_service = MagicMock()
+ langflow_file_service.upload_and_ingest_file = AsyncMock(
+ return_value={"status": "indexed", "id": "h"}
+ )
+ processor = LangflowFileProcessor(
+ langflow_file_service=langflow_file_service,
+ session_manager=session_manager,
+ owner_user_id=UPLOADER,
+ jwt_token="token",
+ replace_duplicates=True,
+ allow_anonymous_delete=allow_anonymous_delete,
+ )
+ item = tmp_path / "upload.pdf"
+ item.write_bytes(b"new content")
+ return processor, str(item), langflow_file_service.upload_and_ingest_file
+
+
+def _s3_upload(user_client, tmp_path, allow_anonymous_delete):
+ document_service = MagicMock()
+ document_service.session_manager.get_user_opensearch_client = MagicMock(
+ return_value=user_client
+ )
+ processor = S3FileProcessor(
+ document_service,
+ bucket="bucket",
+ s3_client=MagicMock(),
+ owner_user_id=UPLOADER,
+ jwt_token="token",
+ models_service=MagicMock(),
+ docling_service=MagicMock(),
+ replace_duplicates=True,
+ allow_anonymous_delete=allow_anonymous_delete,
+ )
+ processor.s3_client.head_object = MagicMock(return_value={"ContentLength": 10})
+ processor.process_document_standard = AsyncMock(return_value={"status": "indexed", "id": "h"})
+ # The S3 key is the indexed filename.
+ return processor, SHARED_DOC, processor.process_document_standard
+
+
+# ConnectorFileProcessor already carried the permission; its own tests cover it.
+PREVIOUSLY_UNGATED = [
+ pytest.param(_local_upload, id="local-upload"),
+ pytest.param(_langflow_upload, id="langflow"),
+ pytest.param(_s3_upload, id="s3"),
+]
+
+
+async def _overwrite(processor, item):
+ # The duplicate check is owner-agnostic: a shared document is visible to
+ # everyone, so every uploader is told the name is taken.
+ processor.check_filename_exists = AsyncMock(return_value=True)
+ upload_task = UploadTask(task_id="task-1", total_files=1)
+ file_task = FileTask(file_path=item, filename=SHARED_DOC)
+ with patch("models.processors.hash_id", return_value="hash-1"):
+ await processor.process_item(upload_task, item, file_task)
+ return file_task
+
+
[email protected]("build", PREVIOUSLY_UNGATED)
+async def test_overwrite_without_the_permission_leaves_the_shared_document_alone(
+ build, monkeypatch, tmp_path
+):
+ user_client, admin_client = _index_holding_one_shared_document(monkeypatch)
+ processor, item, ingest = build(user_client, tmp_path, allow_anonymous_delete=False)
+
+ file_task = await _overwrite(processor, item)
+
+ admin_client.delete.assert_not_awaited()
+ ingest.assert_not_awaited()
+ # Nothing was deleted, so there is nothing to replace: the file resolves as
+ # a duplicate the uploader may not overwrite.
+ assert file_task.status == TaskStatus.SKIPPED
+ assert file_task.result["reason"] == "duplicate_filename"
+
+
[email protected]("build", PREVIOUSLY_UNGATED)
+async def test_overwrite_with_the_permission_still_replaces_it(build, monkeypatch, tmp_path):
+ """Otherwise the gate would simply be breaking overwrite for everyone."""
+ user_client, admin_client = _index_holding_one_shared_document(monkeypatch)
+ processor, item, ingest = build(user_client, tmp_path, allow_anonymous_delete=True)
+
+ file_task = await _overwrite(processor, item)
+
+ admin_client.delete.assert_awaited_once()
+ assert admin_client.delete.await_args.kwargs["id"] == "shared-chunk-1"
+ ingest.assert_awaited_once()
+ assert file_task.status == TaskStatus.COMPLETED
+
+
+# ---------------------------------------------------------------------------
+# The entry points: the permission is resolved, or explicitly not needed
+# ---------------------------------------------------------------------------
+
+
+def _rbac(granted: bool):
+ # Unit tests run with RBAC enforced (tests/unit/conftest.py), so the lookup
+ # really goes to the RBAC service.
+ rbac = MagicMock()
+ rbac.has_permission = AsyncMock(return_value=granted)
+ return rbac
+
+
+def _request():
+ return SimpleNamespace(state=SimpleNamespace())
+
+
+def _user():
+ return User(user_id=UPLOADER, email="[email protected]", name="User B", jwt_token="Bearer tok")
+
+
+def _upload_file():
+ upload = MagicMock(spec=UploadFile)
+ upload.filename = SHARED_DOC
+ upload.content_type = "application/pdf"
+ upload.read = AsyncMock(return_value=b"%PDF-new")
+ return upload
+
+
[email protected]("granted", [True, False])
+ "langflow_disabled, creates_task_with",
+ [
+ pytest.param(True, "create_upload_task", id="traditional"),
+ pytest.param(False, "create_langflow_upload_task", id="langflow"),
+ ],
+)
+async def test_upload_router_resolves_the_permission_for_both_pipelines(
+ granted, langflow_disabled, creates_task_with, monkeypatch
+):
+ from api.router import upload_ingest_router
+
+ monkeypatch.setattr("api.documents._ensure_index_exists", AsyncMock())
+ task_service = MagicMock()
+ create_task = AsyncMock(return_value="task-1")
+ setattr(task_service, creates_task_with, create_task)
+ rbac = _rbac(granted)
+ temp_file = MagicMock()
+ temp_file.name = "/tmp/upload.pdf"
+
+ with (
+ patch("api.router.get_openrag_config") as config,
+ patch("api.router.tempfile.NamedTemporaryFile", return_value=temp_file),
+ patch("api.router.open", create=True),
+ patch("utils.file_utils.safe_unlink"),
+ ):
+ config.return_value.knowledge.disable_ingest_with_langflow = langflow_disabled
+ response = await upload_ingest_router(
+ request=_request(),
+ file=[_upload_file()],
+ session_id=None,
+ settings_json=None,
+ tweaks_json=None,
+ replace_duplicates="true",
+ create_filter="false",
+ preview="false",
+ document_service=MagicMock(),
+ langflow_file_service=MagicMock(),
+ session_manager=MagicMock(),
+ task_service=task_service,
+ user=_user(),
+ rbac=rbac,
+ )
+
+ assert response.status_code == 202
+ assert rbac.has_permission.await_args.args[1] == PERMISSION
+ assert create_task.await_args.kwargs["allow_anonymous_delete"] is granted
+
+
+def test_over_http_the_permission_comes_from_rbac_and_never_from_the_caller(monkeypatch, tmp_path):
+ """Through FastAPI rather than as a function call, because this is the
+ binding a direct call cannot show: a parameter FastAPI does not recognise
+ as a dependency is read from the query string, where the caller could
+ simply grant it to themselves."""
+ from api import router as router_api
+
+ monkeypatch.setattr("tempfile.tempdir", str(tmp_path))
+ task_service = MagicMock()
+ task_service.create_langflow_upload_task = AsyncMock(return_value="task-1")
+ rbac = _rbac(False)
+
+ app = FastAPI()
+ app.add_api_route("/router/upload_ingest", router_api.upload_ingest_router, methods=["POST"])
+ app.dependency_overrides[router_api.get_document_service] = lambda: MagicMock()
+ app.dependency_overrides[router_api.get_langflow_file_service] = lambda: MagicMock()
+ app.dependency_overrides[router_api.get_session_manager] = lambda: MagicMock()
+ app.dependency_overrides[router_api.get_task_service] = lambda: task_service
+ app.dependency_overrides[router_api.get_current_user] = _user
+ app.dependency_overrides[router_api.get_rbac_service] = lambda: rbac
+
+ with patch("api.router.get_openrag_config") as config, TestClient(app) as client:
+ config.return_value.knowledge.disable_ingest_with_langflow = False
+ response = client.post(
+ "/router/upload_ingest?allow_anonymous_delete=true",
+ files={"file": (SHARED_DOC, b"%PDF-new", "application/pdf")},
+ data={"replace_duplicates": "true", "allow_anonymous_delete": "true"},
+ )
+
+ assert response.status_code == 202, response.text
+ kwargs = task_service.create_langflow_upload_task.await_args.kwargs
+ assert kwargs["replace_duplicates"] is True
+ assert kwargs["allow_anonymous_delete"] is False
+
+
+async def test_v1_ingest_hands_the_router_what_it_needs_to_resolve_the_permission():
+ """The v1 endpoint calls the router as a plain function, where a Depends()
+ default is never resolved: left out, ``rbac`` would arrive as the Depends
+ marker itself."""
+ from api.v1.documents import ingest_endpoint
+
+ request, rbac = _request(), _rbac(True)
+
+ with patch("api.v1.documents.upload_ingest_router", new=AsyncMock()) as router:
+ await ingest_endpoint(
+ request=request,
+ file=[MagicMock()],
+ session_id=None,
+ settings=None,
+ tweaks=None,
+ replace_duplicates="true",
+ create_filter="false",
+ document_service=MagicMock(),
+ langflow_file_service=MagicMock(),
+ session_manager=MagicMock(),
+ task_service=MagicMock(),
+ user=_user(),
+ rbac=rbac,
+ )
+
+ assert router.await_args.kwargs["request"] is request
+ assert router.await_args.kwargs["rbac"] is rbac
+
+
[email protected]("granted", [True, False])
+async def test_s3_bucket_upload_resolves_the_permission(granted, monkeypatch):
+ from api import upload as upload_api
+
+ monkeypatch.setenv("AWS_ACCESS_KEY_ID", "fake-key")
+ monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "fake-secret")
+ monkeypatch.setattr("api.documents._ensure_index_exists", AsyncMock())
+ paginator = MagicMock()
+ paginator.paginate.return_value = [{"Contents": [{"Key": SHARED_DOC}]}]
+ s3_client = MagicMock()
+ s3_client.get_paginator.return_value = paginator
+ monkeypatch.setattr(upload_api.boto3, "client", MagicMock(return_value=s3_client))
+
+ built: dict = {}
+
+ class FakeProcessor:
+ def __init__(self, *args, **kwargs):
+ built.update(kwargs)
+
+ monkeypatch.setattr("models.processors.S3FileProcessor", FakeProcessor)
+ task_service = MagicMock()
+ task_service.create_custom_task = AsyncMock(return_value="task-1")
+ rbac = _rbac(granted)
+
+ response = await upload_api.upload_bucket(
+ upload_api.UploadBucketBody(s3_url="s3://bucket/prefix", replace_duplicates=True),
+ request=_request(),
+ task_service=task_service,
+ models_service=MagicMock(),
+ docling_service=MagicMock(),
+ session_manager=MagicMock(),
+ user=_user(),
+ rbac=rbac,
+ )
+
+ assert response.status_code == 201
+ assert rbac.has_permission.await_args.args[1] == PERMISSION
+ assert built["allow_anonymous_delete"] is granted
+
+
+async def test_directory_upload_cannot_replace_so_it_asks_for_nothing(monkeypatch, tmp_path):
+ from api import upload as upload_api
+
+ (tmp_path / "doc.txt").write_text("hello")
+ monkeypatch.setattr("api.documents._ensure_index_exists", AsyncMock())
+ task_service = MagicMock()
+ task_service.create_upload_task = AsyncMock(return_value="task-1")
+
+ await upload_api.upload_path(
+ upload_api.UploadPathBody(path=str(tmp_path)),
+ task_service=task_service,
+ session_manager=MagicMock(),
+ user=_user(),
+ )
+
+ kwargs = task_service.create_upload_task.await_args.kwargs
+ # The two go together: with no way to ask for a replacement, no delete is
+ # ever attempted, and the flag stays off in case that changes.
+ assert "replace_duplicates" not in kwargs
+ assert kwargs["allow_anonymous_delete"] is False
+
+
+async def test_langflow_single_file_upload_cannot_replace_so_it_asks_for_nothing(
+ monkeypatch, tmp_path
+):
+ from api import langflow_files
+
+ monkeypatch.setattr(langflow_files.tempfile, "gettempdir", lambda: str(tmp_path))
+ task_service = MagicMock()
+ task_service.create_langflow_upload_task = AsyncMock(return_value="task-1")
+
+ response = await langflow_files.upload_and_ingest_user_file(
+ file=_upload_file(),
+ session_id=None,
+ settings_json=None,
+ tweaks_json=None,
+ langflow_file_service=MagicMock(),
+ session_manager=MagicMock(),
+ task_service=task_service,
+ user=_user(),
+ )
+
+ assert response.status_code == 202, json.loads(response.body)
+ kwargs = task_service.create_langflow_upload_task.await_args.kwargs
+ assert "replace_duplicates" not in kwargs
+ assert kwargs["allow_anonymous_delete"] is False
+
+
+# ---------------------------------------------------------------------------
+# The way down: the task service hands it on, and nothing defaults it
+# ---------------------------------------------------------------------------
+
+
[email protected]("allowed", [True, False])
+ "create_task, processor_class, extra",
+ [
+ pytest.param("create_upload_task", "DocumentFileProcessor", {}, id="traditional"),
+ pytest.param(
+ "create_langflow_upload_task",
+ "LangflowFileProcessor",
+ {"langflow_file_service": AsyncMock(), "session_manager": AsyncMock()},
+ id="langflow",
+ ),
+ ],
+)
+async def test_task_service_hands_the_permission_to_the_processor(
+ allowed, create_task, processor_class, extra, monkeypatch
+):
+ built: dict = {}
+
+ class FakeProcessor:
+ def __init__(self, *args, **kwargs):
+ built.update(kwargs)
+
+ monkeypatch.setattr(f"models.processors.{processor_class}", FakeProcessor)
+ service = TaskService()
+ monkeypatch.setattr(service, "create_custom_task", AsyncMock(return_value="task-1"))
+
+ await getattr(service, create_task)(
+ user_id=UPLOADER,
+ file_paths=["/tmp/x.pdf"],
+ allow_anonymous_delete=allowed,
+ **extra,
+ )
+
+ assert built["allow_anonymous_delete"] is allowed
+
+
+ "takes_the_permission",
+ [
+ TaskProcessor.resolve_duplicate_filename,
+ TaskProcessor.delete_document_by_filename,
+ DocumentFileProcessor.__init__,
+ ConnectorFileProcessor.__init__,
+ S3FileProcessor.__init__,
+ LangflowFileProcessor.__init__,
+ TaskService.create_upload_task,
+ TaskService.create_langflow_upload_task,
+ ConnectorService.sync_connector_files,
+ ConnectorService.sync_specific_files,
+ ],
+ ids=lambda function: function.__qualname__,
+)
+def test_the_permission_has_no_default_anywhere_on_the_way_down(takes_the_permission):
+ """A default is how this was lost: it was True, and every caller that never
+ resolved the permission inherited it without a line of code saying so. With
+ none, a caller that leaves it out fails where it is written."""
+ parameter = inspect.signature(takes_the_permission).parameters["allow_anonymous_delete"]
+
+ assert parameter.default is inspect.Parameter.empty
diff --git a/tests/unit/test_claim_winner_failure.py b/tests/unit/test_claim_winner_failure.py
index 33347c07f..2cfc4dbff 100644
--- a/tests/unit/test_claim_winner_failure.py
+++ b/tests/unit/test_claim_winner_failure.py
@@ -41,6 +41,7 @@ class _RacingProcessor(TaskProcessor):
replace=False,
owner_user_id="user-1",
claim_holder=self._claim_holder(upload_task, file_task),
+ allow_anonymous_delete=True,
)
if action in DUPLICATE_SKIP_ACTIONS:
# Both skips finish a file the same way; only the reconcile in
@@ -141,6 +142,7 @@ class _HeldClaimProcessor(TaskProcessor):
replace=False,
owner_user_id="user-1",
claim_holder=self._claim_holder(upload_task, file_task),
+ allow_anonymous_delete=True,
)
assert action == "proceed"
self.claimed.set()
@@ -161,6 +163,7 @@ class _LosingProcessor(TaskProcessor):
replace=False,
owner_user_id="user-1",
claim_holder=self._claim_holder(upload_task, file_task),
+ allow_anonymous_delete=True,
)
assert action in DUPLICATE_SKIP_ACTIONS
self.mark_duplicate_skipped(upload_task, file_task)
@@ -288,6 +291,7 @@ async def test_an_ordinary_duplicate_skip_still_cleans_up(tmp_path):
replace=False,
owner_user_id="user-1",
claim_holder=self._claim_holder(upload_task, file_task),
+ allow_anonymous_delete=True,
)
assert action in DUPLICATE_SKIP_ACTIONS
self.mark_duplicate_skipped(upload_task, file_task)
diff --git a/tests/unit/test_connector_processor_filename_dedupe.py b/tests/unit/test_connector_processor_filename_dedupe.py
index 39f61f104..3c1d4945f 100644
--- a/tests/unit/test_connector_processor_filename_dedupe.py
+++ b/tests/unit/test_connector_processor_filename_dedupe.py
@@ -80,6 +80,7 @@ def _build_connector_processor(replace_duplicates: bool) -> ConnectorFileProcess
document_service=document_service,
models_service=MagicMock(),
replace_duplicates=replace_duplicates,
+ allow_anonymous_delete=True,
)
diff --git a/tests/unit/test_connector_sync_shared_resync.py b/tests/unit/test_connector_sync_shared_resync.py
index c66106008..8aa0a12c4 100644
--- a/tests/unit/test_connector_sync_shared_resync.py
+++ b/tests/unit/test_connector_sync_shared_resync.py
@@ -52,7 +52,9 @@ def _make_document() -> ConnectorDocument:
)
-def _build_processor(*, shared: bool | None) -> ConnectorFileProcessor:
+def _build_processor(
+ *, shared: bool | None, allow_anonymous_delete: bool = True
+) -> ConnectorFileProcessor:
document_service = MagicMock()
document_service.docling_service = MagicMock()
document_service.session_manager = MagicMock()
@@ -73,6 +75,7 @@ def _build_processor(*, shared: bool | None) -> ConnectorFileProcessor:
replace_duplicates=True,
connector_type="ibm_cos",
shared=shared,
+ allow_anonymous_delete=allow_anonymous_delete,
)
@@ -185,6 +188,33 @@ async def test_resync_replaces_shared_cos_file_and_keeps_it_shared(
assert mock_process.await_args.kwargs["shared"] is True
+async def test_resync_without_the_permission_leaves_a_shared_file_alone(
+ monkeypatch, backend_write_client
+):
+ """Inheriting a file's sharing state is not being allowed to replace it.
+
+ The files a sync walks come from a query scoped by connector type and read
+ through DLS, so they include shared files that another user's connection
+ ingested. A caller without knowledge:delete:anonymous must not be able to
+ delete and rewrite one of those by running an ordinary sync.
+ """
+ monkeypatch.setattr("config.settings.DISABLE_INGEST_WITH_LANGFLOW", True)
+ processor = _build_processor(shared=None, allow_anonymous_delete=False)
+ _wire(processor, _make_document(), indexed_owner=None)
+
+ file_task, _, mock_process = await _run(processor)
+
+ backend_write_client.delete.assert_not_awaited()
+ mock_process.assert_not_awaited()
+ assert file_task.status == TaskStatus.SKIPPED
+ assert file_task.result["reason"] == "duplicate_filename"
+ # ...and it is still shared afterwards: the owner fields reconciled on the
+ # way out are the shared ones, not this user's.
+ script = backend_write_client.update_by_query.await_args.kwargs["body"]["script"]
+ assert script["params"]["shared"] is True
+
+
@pytest.mark.asyncio
async def test_resync_keeps_private_cos_file_private(monkeypatch):
"""A file indexed with an owner is re-ingested private, not shared."""
diff --git a/tests/unit/test_langflow_file_service_two_phase.py b/tests/unit/test_langflow_file_service_two_phase.py
index 4131072dd..ac1f543ae 100644
--- a/tests/unit/test_langflow_file_service_two_phase.py
+++ b/tests/unit/test_langflow_file_service_two_phase.py
@@ -215,8 +215,7 @@ def test_processor_default_polling_service_is_none():
lf_svc = LangflowFileService(docling_service=AsyncMock())
processor = LangflowFileProcessor(
- langflow_file_service=lf_svc,
- session_manager=None,
+ langflow_file_service=lf_svc, session_manager=None, allow_anonymous_delete=True
)
assert processor.docling_polling_service is None
@@ -231,6 +230,7 @@ def test_processor_accepts_injected_polling_service():
langflow_file_service=lf_svc,
session_manager=None,
docling_polling_service=injected,
+ allow_anonymous_delete=True,
)
assert processor.docling_polling_service is injected
@@ -319,6 +319,7 @@ async def test_task_service_threads_polling_service_to_processor(monkeypatch):
file_paths=["/tmp/x.pdf"],
langflow_file_service=AsyncMock(),
session_manager=AsyncMock(),
+ allow_anonymous_delete=True,
)
assert captured["docling_polling_service"] is injected
diff --git a/tests/unit/test_langflow_processor_content_dedupe.py b/tests/unit/test_langflow_processor_content_dedupe.py
index 3adb50a0c..3dae55906 100644
--- a/tests/unit/test_langflow_processor_content_dedupe.py
+++ b/tests/unit/test_langflow_processor_content_dedupe.py
@@ -35,6 +35,7 @@ def _build_processor() -> LangflowFileProcessor:
session_manager=session_manager,
owner_user_id="user-1",
jwt_token="Bearer token",
+ allow_anonymous_delete=True,
)
@@ -225,6 +226,7 @@ async def test_langflow_processor_concurrent_same_hash_only_one_proceeds(tmp_pat
session_manager=session_manager,
owner_user_id="user-1",
jwt_token="Bearer token",
+ allow_anonymous_delete=True,
)
processor.resolve_duplicate_filename = AsyncMock(return_value="proceed")
processor.check_document_exists = fake_check_document_exists
diff --git a/tests/unit/test_langflow_processor_preview_document_id.py b/tests/unit/test_langflow_processor_preview_document_id.py
index 8c3a86cbc..919451de5 100644
--- a/tests/unit/test_langflow_processor_preview_document_id.py
+++ b/tests/unit/test_langflow_processor_preview_document_id.py
@@ -24,6 +24,7 @@ async def test_langflow_processor_threads_document_id_for_index_proof(tmp_path):
session_manager=session_manager,
owner_user_id="user-1",
jwt_token="Bearer user-token",
+ allow_anonymous_delete=True,
)
processor.check_filename_exists = AsyncMock(side_effect=[False, True])
diff --git a/tests/unit/test_processor_refresh_uses_admin_client.py b/tests/unit/test_processor_refresh_uses_admin_client.py
index bfdcf21f6..a9bfc7f18 100644
--- a/tests/unit/test_processor_refresh_uses_admin_client.py
+++ b/tests/unit/test_processor_refresh_uses_admin_client.py
@@ -52,6 +52,7 @@ async def test_document_processor_refresh_uses_admin_client(monkeypatch, tmp_pat
jwt_token="Bearer user-token",
replace_duplicates=True,
session_manager=session_manager,
+ allow_anonymous_delete=True,
)
processor.check_filename_exists = AsyncMock(return_value=True)
processor.delete_document_by_filename = AsyncMock(return_value=1)
@@ -96,6 +97,7 @@ async def test_langflow_processor_refresh_uses_admin_client(monkeypatch, tmp_pat
owner_user_id="user-1",
jwt_token="Bearer user-token",
replace_duplicates=True,
+ allow_anonymous_delete=True,
)
processor.check_filename_exists = AsyncMock(return_value=True)
processor.delete_document_by_filename = AsyncMock(return_value=1)
diff --git a/tests/unit/test_reconcile_shared_owner.py b/tests/unit/test_reconcile_shared_owner.py
index 9b981cca1..96e73574f 100644
--- a/tests/unit/test_reconcile_shared_owner.py
+++ b/tests/unit/test_reconcile_shared_owner.py
@@ -26,6 +26,7 @@ def _make_processor(*, shared: bool, user_id: str = "user-1"):
owner_name="Alice",
owner_email="[email protected]",
shared=shared,
+ allow_anonymous_delete=True,
)
@@ -234,6 +235,7 @@ async def test_the_gate_reports_the_two_skips_apart():
replace=False,
owner_user_id="user-1",
claim_holder="task-1:only",
+ allow_anonymous_delete=True,
)
assert action == "skip"
diff --git a/tests/unit/test_router_preview_upload.py b/tests/unit/test_router_preview_upload.py
index 506f693aa..988b39a6a 100644
--- a/tests/unit/test_router_preview_upload.py
+++ b/tests/unit/test_router_preview_upload.py
@@ -42,6 +42,7 @@ async def test_langflow_upload_passes_preview_mode_to_task_service():
session_manager=MagicMock(),
task_service=mock_task_service,
user=user,
+ allow_anonymous_delete=True,
)
assert response.status_code == 202
@@ -91,6 +92,8 @@ async def test_upload_ingest_router_ignores_preview_when_disabled():
task_service=mock_task_service,
document_service=MagicMock(),
user=user,
+ request=MagicMock(),
+ rbac=MagicMock(has_permission=AsyncMock(return_value=True)),
)
assert response.status_code == 202
diff --git a/tests/unit/test_same_name_files_in_one_batch.py b/tests/unit/test_same_name_files_in_one_batch.py
index 1895e4d87..61b586891 100644
--- a/tests/unit/test_same_name_files_in_one_batch.py
+++ b/tests/unit/test_same_name_files_in_one_batch.py
@@ -30,6 +30,7 @@ def _processor(replace_duplicates: bool) -> DocumentFileProcessor:
jwt_token="mock-token",
replace_duplicates=replace_duplicates,
session_manager=MagicMock(),
+ allow_anonymous_delete=True,
)
# The index is empty as far as either file can tell: this is the window
# where both of them are told the name is free.
diff --git a/tests/unit/test_traditional_duplicate_handling.py b/tests/unit/test_traditional_duplicate_handling.py
index 5e378cc5c..bd964df35 100644
--- a/tests/unit/test_traditional_duplicate_handling.py
+++ b/tests/unit/test_traditional_duplicate_handling.py
@@ -48,6 +48,7 @@ async def test_traditional_processor_duplicate_exists_no_replace():
jwt_token="mock-token",
replace_duplicates=False,
session_manager=mock_session_manager,
+ allow_anonymous_delete=True,
)
# Assert that session_manager was set correctly on the processor
@@ -85,6 +86,7 @@ async def test_traditional_processor_duplicate_exists_with_replace():
jwt_token="mock-token",
replace_duplicates=True,
session_manager=mock_session_manager,
+ allow_anonymous_delete=True,
)
# Assert that session_manager was set correctly on the processor
@@ -133,6 +135,7 @@ async def test_langflow_processor_duplicate_exists_no_replace():
owner_user_id="user-123",
jwt_token="mock-token",
replace_duplicates=False,
+ allow_anonymous_delete=True,
)
processor.check_filename_exists = AsyncMock(return_value=True)
processor.delete_document_by_filename = AsyncMock()
@@ -165,6 +168,7 @@ async def test_resolve_duplicate_skips_when_delete_removes_nothing():
opensearch_client,
replace=True,
owner_user_id=None,
+ allow_anonymous_delete=True,
)
assert result == "skip"
@@ -198,6 +202,7 @@ async def test_delete_document_by_filename_shared_without_owner(monkeypatch):
opensearch_client,
owner_user_id=None,
shared=True,
+ allow_anonymous_delete=True,
)
assert deleted == 1
@@ -208,7 +213,8 @@ async def test_delete_document_by_filename_shared_without_owner(monkeypatch):
@pytest.mark.asyncio
async def test_delete_document_by_filename_replaces_owned_and_ownerless(monkeypatch):
- """With an owner in hand the replace scope is "mine OR ownerless", always.
+ """With an owner in hand, and the permission to delete shared documents,
+ the replace scope is "mine OR ownerless".
``shared`` says how the replacement will be written; it must not narrow what
gets deleted. An owner-only scope matched none of a shared document's
@@ -239,6 +245,7 @@ async def test_delete_document_by_filename_replaces_owned_and_ownerless(monkeypa
opensearch_client,
owner_user_id="user-123",
shared=False,
+ allow_anonymous_delete=True,
)
assert deleted == 1
@@ -266,6 +273,7 @@ def _build_s3_processor(replace_duplicates: bool) -> S3FileProcessor:
models_service=MagicMock(),
docling_service=MagicMock(),
replace_duplicates=replace_duplicates,
+ allow_anonymous_delete=True,
)
return processor
@@ -380,9 +388,12 @@ async def test_replace_without_anonymous_delete_permission_stays_owner_scoped(mo
@pytest.mark.asyncio
-async def test_shared_write_still_widens_without_the_flag(monkeypatch):
- """A shared write already required the permission upstream (connector_sync
- returns 403 without it), so the flag must not narrow that path."""
+async def test_shared_does_not_stand_in_for_the_permission(monkeypatch):
+ """``shared`` says how the replacement is written, and on the re-sync paths
+ it is inferred from what is indexed. Letting it widen the delete meant a
+ caller without knowledge:delete:anonymous could replace a shared document
+ by re-syncing it. An explicit shared sync loses nothing by this: it cannot
+ get here without the permission (connector_sync returns 403)."""
from models.processors import TaskProcessor
opensearch_client = _delete_scope_env(monkeypatch)
@@ -395,10 +406,10 @@ async def test_shared_write_still_widens_without_the_flag(monkeypatch):
allow_anonymous_delete=False,
)
- from utils.opensearch_queries import build_replace_filename_query
+ from utils.opensearch_queries import build_owned_filename_query
query = opensearch_client.search.await_args.kwargs["body"]["query"]
- assert query == build_replace_filename_query("report.pdf", "user-123")
+ assert query == build_owned_filename_query("report.pdf", "user-123")
@pytest.mark.asyncio
diff --git a/tests/unit/test_traditional_settings_handling.py b/tests/unit/test_traditional_settings_handling.py
index 966ada04c..25c0fb75f 100644
--- a/tests/unit/test_traditional_settings_handling.py
+++ b/tests/unit/test_traditional_settings_handling.py
@@ -31,6 +31,7 @@ async def test_traditional_processor_settings_propagation():
replace_duplicates=False,
session_manager=mock_session_manager,
settings=settings,
+ allow_anonymous_delete=True,
)
processor.check_filename_exists = AsyncMock(return_value=False)
@@ -78,6 +79,7 @@ async def test_traditional_processor_invalid_settings_fallback():
replace_duplicates=False,
session_manager=mock_session_manager,
settings=settings,
+ allow_anonymous_delete=True,
)
processor.check_filename_exists = AsyncMock(return_value=False)