From 4788ac602bd1da12a404b904e450a7b9513ef5d8 Mon Sep 17 00:00:00 2001 From: stumpylog <797416+stumpylog@users.noreply.github.com> Date: Wed, 18 Nov 2026 07:43:32 -0800 Subject: [PATCH] Fix: limit manually run OCR templates to those the requester can view A user editing someone else's document could run the owner's private templates and see their names. Manual runs now use templates both can view. --- src/documents/bulk_edit.py | 3 +- src/documents/bulk_operations.py | 5 +- src/documents/tasks.py | 20 +++++-- src/documents/tests/test_api_bulk_edit.py | 4 +- src/documents/tests/test_api_ocr_templates.py | 60 +++++++++++++++++++ src/documents/tests/test_bulk_edit.py | 28 +++++++-- src/documents/tests/test_bulk_operations.py | 27 ++++++++- src/documents/tests/test_zone_ocr.py | 18 ++++++ src/documents/views.py | 2 +- src/documents/zone_ocr.py | 30 +++++++--- 10 files changed, 170 insertions(+), 27 deletions(-) diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py index 979c73a870..7b605a9f0e 100644 --- a/src/documents/bulk_edit.py +++ b/src/documents/bulk_edit.py @@ -1262,9 +1262,10 @@ def run_zone_ocr( doc_ids: list[int], *, template: int | None = None, + user: User | None = None, ) -> Literal["OK"]: """Run zone OCR within the tracked bulk operation task.""" for document_id in doc_ids: - extract_zones(document_id=document_id, template_id=template) + extract_zones(document_id=document_id, template_id=template, requester=user) return "OK" diff --git a/src/documents/bulk_operations.py b/src/documents/bulk_operations.py index e057056f4a..1bc4ba109e 100644 --- a/src/documents/bulk_operations.py +++ b/src/documents/bulk_operations.py @@ -55,10 +55,11 @@ USER_OPERATIONS = frozenset( "edit_pdf", "remove_password", "merge_as_versions", + "run_zone_ocr", }, ) -# merge_as_versions doesn't queue any consume tasks -TRIGGER_SOURCE_OPERATIONS = USER_OPERATIONS - {"merge_as_versions"} +# These don't queue any consume tasks +TRIGGER_SOURCE_OPERATIONS = USER_OPERATIONS - {"merge_as_versions", "run_zone_ocr"} def snapshot_field(doc_ids: list[int], field: str) -> dict[int, Any]: diff --git a/src/documents/tasks.py b/src/documents/tasks.py index 95b3703143..5bed784787 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -11,6 +11,7 @@ from tempfile import mkstemp from celery import Task from celery import shared_task from django.conf import settings +from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.contrib.sessions.models import Session from django.db import models @@ -73,6 +74,7 @@ from documents.utils import identity from documents.versioning import annotate_effective_content from documents.versioning import latest_version from documents.workflows.utils import get_workflows_for_trigger +from documents.zone_ocr import get_templates_for_document from documents.zone_ocr import run_zone_extraction from paperless.config import AIConfig from paperless.config import BarcodeConfig @@ -373,14 +375,20 @@ def _read_barcodes_for_reprocess(document: Document) -> list[StoredBarcode] | No @shared_task -def extract_zones(document_id: int, template_id: int | None = None) -> None: +def extract_zones( + document_id: int, + template_id: int | None = None, + requester: User | None = None, +) -> None: """ Run an OCR template against a stored document, or every template that - matches it when no template is given. + matches it when no template is given. Matched templates are limited to + those the requester can view. """ - document = Document.objects.get(id=document_id) - templates = None - if template_id is not None: + document = latest_version(Document.objects.get(id=document_id)) + if template_id is None: + templates = get_templates_for_document(document, requester=requester) + else: try: template = OcrTemplate.objects.prefetch_related( "zones", @@ -394,7 +402,7 @@ def extract_zones(document_id: int, template_id: int | None = None) -> None: return templates = [template] - run_zone_extraction(latest_version(document), templates=templates, reindex=True) + run_zone_extraction(document, templates=templates, reindex=True) @shared_task diff --git a/src/documents/tests/test_api_bulk_edit.py b/src/documents/tests/test_api_bulk_edit.py index 750ed75149..1262ca5b75 100644 --- a/src/documents/tests/test_api_bulk_edit.py +++ b/src/documents/tests/test_api_bulk_edit.py @@ -753,7 +753,7 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): m.assert_called_once() args, kwargs = m.call_args self.assertEqual(args[0], [self.doc1.id]) - self.assertEqual(kwargs, {}) + self.assertEqual(kwargs, {"user": self.user}) @mock.patch("documents.views.bulk_edit.run_zone_ocr") def test_run_zone_ocr_documents_endpoint_with_template(self, m) -> None: @@ -780,7 +780,7 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): m.assert_called_once() args, kwargs = m.call_args self.assertEqual(args[0], [self.doc1.id]) - self.assertEqual(kwargs, {"template": template.pk}) + self.assertEqual(kwargs, {"template": template.pk, "user": self.user}) @mock.patch("documents.views.bulk_edit.run_zone_ocr") def test_run_zone_ocr_documents_endpoint_rejects_private_template( diff --git a/src/documents/tests/test_api_ocr_templates.py b/src/documents/tests/test_api_ocr_templates.py index c92cc1eead..d20b9156ba 100644 --- a/src/documents/tests/test_api_ocr_templates.py +++ b/src/documents/tests/test_api_ocr_templates.py @@ -1134,6 +1134,66 @@ class TestOcrTemplatesAPI(DirectoriesMixin, APITestCase): ) self.assertEqual(run.call_count, 3) + @patch("documents.zone_ocr.subprocess.run") + def test_run_zone_ocr_hides_the_owners_private_templates_from_an_editor( + self, + run, + ): + """ + GIVEN: + - A document shared for editing with another user + - Templates private to the owner, unowned, and shared with both + WHEN: + - The editor runs zone OCR without choosing a template + THEN: + - Only templates the editor can also view run and are reported + """ + owner = User.objects.create_user(username="document_owner") + editor = User.objects.create_user(username="document_editor") + editor.user_permissions.add( + *Permission.objects.filter( + codename__in=["view_document", "change_document"], + ), + ) + document = DocumentFactory(content=CONTENT, owner=owner) + grant_object(editor, document, "view_document", "change_document") + with Image.new("RGB", (100, 100), "white") as image: + image.save(document.source_path, format="PNG") + run.return_value.stdout = "extracted" + for name, template_owner in [ + ("Owner private", owner), + ("Unowned", None), + ("Shared", owner), + ]: + template = OcrTemplate.objects.create( + name=name, + owner=template_owner, + match="Northwind", + source_width=100, + source_height=100, + ) + OcrTemplateZone.objects.create( + template=template, + name=name, + target="title", + x=0, + y=0, + width=20, + height=20, + ) + if name == "Shared": + grant_object(editor, template, "view_ocrtemplate") + self.client.force_authenticate(editor) + + response = self.client.post(f"/api/documents/{document.pk}/run-zone-ocr/") + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertCountEqual( + [result["template"] for result in response.json()["results"]], + ["Unowned", "Shared"], + ) + self.assertEqual(run.call_count, 2) + def test_shared_editor_cannot_transfer_ownership_or_change_sharing(self): editor = User.objects.create_user(username="shared_editor") editor.user_permissions.add( diff --git a/src/documents/tests/test_bulk_edit.py b/src/documents/tests/test_bulk_edit.py index b96f6019ea..b88f9a9dc2 100644 --- a/src/documents/tests/test_bulk_edit.py +++ b/src/documents/tests/test_bulk_edit.py @@ -30,6 +30,7 @@ from documents.permissions import set_permissions_for_objects from documents.tasks import extract_zones from paperless_testing.dirs import DirectoriesMixin from paperless_testing.factories import DocumentFactory +from paperless_testing.factories import UserFactory from paperless_testing.permissions import grant_object from paperless_testing.samples import MULTI_PAGE_DIGITAL_PDF from paperless_testing.samples import SIMPLE_DIGITAL_PDF @@ -1948,11 +1949,14 @@ class TestBulkEditRunZoneOcr: ) -> None: docs = DocumentFactory.create_batch(2) - result = bulk_edit.run_zone_ocr([doc.id for doc in docs]) + user = UserFactory() + + result = bulk_edit.run_zone_ocr([doc.id for doc in docs], user=user) assert result == "OK" assert mock_task.call_args_list == [ - mock.call(document_id=doc.id, template_id=None) for doc in docs + mock.call(document_id=doc.id, template_id=None, requester=user) + for doc in docs ] mock_task.apply_async.assert_not_called() @@ -1966,7 +1970,11 @@ class TestBulkEditRunZoneOcr: bulk_edit.run_zone_ocr([doc.id], template=template.pk) - mock_task.assert_called_once_with(document_id=doc.id, template_id=template.pk) + mock_task.assert_called_once_with( + document_id=doc.id, + template_id=template.pk, + requester=None, + ) mock_task.apply_async.assert_not_called() def test_task_runs_extraction(self, mocker: MockerFixture) -> None: @@ -1976,14 +1984,22 @@ class TestBulkEditRunZoneOcr: WHEN: - The task runs THEN: - - Zone extraction runs on the stored document + - Templates are selected for the requester and extraction runs on + the stored document """ extract = mocker.patch("documents.tasks.run_zone_extraction") + select = mocker.patch("documents.tasks.get_templates_for_document") doc = DocumentFactory() + user = UserFactory() - extract_zones(doc.id) + extract_zones(doc.id, requester=user) - extract.assert_called_once_with(doc, templates=None, reindex=True) + select.assert_called_once_with(doc, requester=user) + extract.assert_called_once_with( + doc, + templates=select.return_value, + reindex=True, + ) def test_task_runs_the_chosen_template(self, mocker: MockerFixture) -> None: """ diff --git a/src/documents/tests/test_bulk_operations.py b/src/documents/tests/test_bulk_operations.py index b647aa0298..b299b74ef5 100644 --- a/src/documents/tests/test_bulk_operations.py +++ b/src/documents/tests/test_bulk_operations.py @@ -115,11 +115,36 @@ def test_zone_ocr_finishes_extraction_within_the_tracked_task(bulk_setup): {"template": template.pk}, user.pk, ) - extract.assert_called_once_with(document_id=doc.pk, template_id=template.pk) + extract.assert_called_once_with( + document_id=doc.pk, + template_id=template.pk, + requester=user, + ) extract.apply_async.assert_not_called() assert result == {"operation": "run_zone_ocr", "document_count": 1} +@pytest.mark.django_db +def test_zone_ocr_without_template_selects_for_the_requester(bulk_setup): + """ + GIVEN: + - A queued zone OCR run without a chosen template + WHEN: + - The tracked task executes it + THEN: + - Extraction receives the requesting user so templates are limited to + those they can view + """ + user, _, doc, _ = bulk_setup + with mock.patch("documents.bulk_edit.extract_zones") as extract: + execute_bulk_operation("run_zone_ocr", [doc.pk], {}, user.pk) + extract.assert_called_once_with( + document_id=doc.pk, + template_id=None, + requester=user, + ) + + @pytest.mark.django_db @pytest.mark.parametrize("revoked", ["document", "template", "user"]) def test_zone_ocr_rechecks_access_before_extraction(bulk_setup, revoked): diff --git a/src/documents/tests/test_zone_ocr.py b/src/documents/tests/test_zone_ocr.py index 988e99b70b..472df6a4dd 100644 --- a/src/documents/tests/test_zone_ocr.py +++ b/src/documents/tests/test_zone_ocr.py @@ -654,6 +654,24 @@ class TestTemplateVisibility(DirectoriesMixin, TestCase): [self.mine, self.unowned, self.private], ) + def test_requester_limits_the_owners_templates(self): + """ + GIVEN: + - Templates owned by the document owner, unowned, private to + another user, and owned by the owner but shared with the requester + WHEN: + - Templates are selected for a manual run by the requester + THEN: + - Only templates both the owner and the requester can view are used + """ + shared = self.template("Shared", owner=self.owner) + grant_object(self.other, shared, "view_ocrtemplate") + + self.assertCountEqual( + get_templates_for_document(self.document, requester=self.other), + [self.unowned, shared], + ) + def test_inactive_owner_has_no_templates(self): self.owner.is_active = False self.assertEqual(get_templates_for_document(self.document), []) diff --git a/src/documents/views.py b/src/documents/views.py index d8cc48d62c..34c04cb457 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -2336,7 +2336,7 @@ class DocumentViewSet( templates = ( [template] if template is not None - else get_templates_for_document(file_document) + else get_templates_for_document(file_document, requester=request.user) ) if not templates: return Response( diff --git a/src/documents/zone_ocr.py b/src/documents/zone_ocr.py index 5244e6ca79..a594fca3aa 100644 --- a/src/documents/zone_ocr.py +++ b/src/documents/zone_ocr.py @@ -13,6 +13,7 @@ from decimal import Decimal from decimal import InvalidOperation from functools import partial from pathlib import Path +from typing import TYPE_CHECKING import dateparser import magic @@ -34,6 +35,9 @@ from documents.regex import safe_regex_fullmatch from documents.signals.handlers import add_to_index from documents.versioning import get_root_document +if TYPE_CHECKING: + from django.contrib.auth.models import User + logger = logging.getLogger("paperless.zone_ocr") @@ -73,19 +77,29 @@ def _zone_result( return ZoneOcrResult(template.name, zone.name, label, value, reason) -def get_templates_for_document(document: Document) -> list[OcrTemplate]: +def get_templates_for_document( + document: Document, + *, + requester: User | None = None, +) -> list[OcrTemplate]: """ The enabled templates that match the selected file's content and that the - root document's owner can view. + root document's owner can view. For a manual run, the templates are further + limited to those the requester can view, so a user editing someone else's + document never runs or sees that owner's private templates. """ owner = get_root_document(document).owner - templates = ( - OcrTemplate.objects.filter( - enabled=True, - id__in=permitted_object_ids(owner, OcrTemplate, "view_ocrtemplate"), + templates = OcrTemplate.objects.filter( + enabled=True, + id__in=permitted_object_ids(owner, OcrTemplate, "view_ocrtemplate"), + ) + if requester is not None: + templates = templates.filter( + id__in=permitted_object_ids(requester, OcrTemplate, "view_ocrtemplate"), ) - .order_by("name", "pk") - .prefetch_related("zones", "zones__custom_field") + templates = templates.order_by("name", "pk").prefetch_related( + "zones", + "zones__custom_field", ) if document.root_document_id is None and templates: # The original file can be selected even when newer versions exist.