From f401267c4bdd473b5a5a0b2dd3c478392cb9dee1 Mon Sep 17 00:00:00 2001 From: stumpylog <797416+stumpylog@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:27:51 -0800 Subject: [PATCH] Fix: let template viewers preview zones The zone preview is a POST, so it required permission to add templates. Both preview endpoints now require viewing templates and documents. --- src/documents/permissions.py | 15 +++++ src/documents/tests/test_api_ocr_templates.py | 58 ++++++++++++++++++- src/documents/views.py | 11 +++- 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/documents/permissions.py b/src/documents/permissions.py index 1b7537061b..bf23055054 100644 --- a/src/documents/permissions.py +++ b/src/documents/permissions.py @@ -699,6 +699,21 @@ class ViewDocumentsPermissions(GlobalPermissionsByMethod): } +class ViewOcrTemplatesPermissions(GlobalPermissionsByMethod): + """ + Requires the global permission to view OCR templates for OPTIONS, GET and + POST, for the template editor's read-only preview endpoints. + """ + + perms_map = { + "OPTIONS": ["documents.view_ocrtemplate"], + "GET": ["documents.view_ocrtemplate"], + # The zone preview is a POST only to carry the zone in the body; it + # creates and changes nothing, so viewing templates is enough. + "POST": ["documents.view_ocrtemplate"], + } + + class TrashPermissions(GlobalPermissionsByMethod): """Check the global document permission for each trash operation.""" diff --git a/src/documents/tests/test_api_ocr_templates.py b/src/documents/tests/test_api_ocr_templates.py index e10d0b585b..af991535fe 100644 --- a/src/documents/tests/test_api_ocr_templates.py +++ b/src/documents/tests/test_api_ocr_templates.py @@ -2201,6 +2201,60 @@ class TestOcrTemplatePreviewPermissions: assert response.status_code == status.HTTP_403_FORBIDDEN mock_preview.assert_not_called() + @pytest.mark.parametrize( + "action", + [ + pytest.param("document-page-image", id="document-page-image"), + pytest.param("test-zone", id="test-zone"), + ], + ) + @pytest.mark.parametrize( + ("template_perms", "expected"), + [ + pytest.param( + ("view_ocrtemplate",), + status.HTTP_200_OK, + id="view-template-is-enough", + ), + pytest.param( + ("add_ocrtemplate", "change_ocrtemplate"), + status.HTTP_403_FORBIDDEN, + id="no-view-template", + ), + ], + ) + def test_previews_require_global_template_view_permission( + self, + regular_user: User, + user_client: APIClient, + mock_preview: MagicMock, + action: str, + template_perms: tuple[str, ...], + expected: int, + ) -> None: + """ + GIVEN: + - A user with global view document permission and either only view + template permission, or add and change but not view + - An unowned image document + WHEN: + - The user requests a page image or a zone preview + THEN: + - Viewing templates is enough for both, and without it both are + refused + """ + grant_global(regular_user, "view_document", *template_perms) + document = DocumentFactory(owner=None, mime_type="image/png") + document.source_path.write_bytes(b"document contents") + mock_preview.return_value = {"raw_text": None, "value": None} + + if action == "document-page-image": + response = user_client.get(preview_url(document)) + else: + response = preview_zone(user_client, document) + + assert response.status_code == expected + def test_versions_are_authorized_through_the_root_document( self, preview_client: APIClient, @@ -2281,7 +2335,7 @@ class TestOcrTemplatePreviewPermissions: """ GIVEN: - A document owned by another user - - A user with global add template and view document permissions + - A user with global view template and view document permissions WHEN: - The user previews a zone on the document THEN: @@ -2290,7 +2344,7 @@ class TestOcrTemplatePreviewPermissions: - No preview extraction runs """ owner = UserFactory() - grant_global(regular_user, "add_ocrtemplate", "view_document") + grant_global(regular_user, "view_ocrtemplate", "view_document") document = DocumentFactory(owner=owner) response = preview_zone(user_client, document, x=0, y=0, height=100, page=1) assert response.status_code == status.HTTP_403_FORBIDDEN diff --git a/src/documents/views.py b/src/documents/views.py index 56bbc61df6..5012691a4c 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -183,6 +183,7 @@ from documents.permissions import PaperlessNotePermissions from documents.permissions import PaperlessObjectPermissions from documents.permissions import TrashPermissions from documents.permissions import ViewDocumentsPermissions +from documents.permissions import ViewOcrTemplatesPermissions from documents.permissions import annotate_document_count_for_related_queryset from documents.permissions import get_document_count_filter_for_user from documents.permissions import get_permission_grantees @@ -5836,10 +5837,14 @@ class OcrTemplateViewSet(BulkPermissionMixin, PassUserMixin, ModelViewSet[OcrTem ordering_fields = ("name",) def get_permissions(self): - permissions = super().get_permissions() if self.action in {"document_page_image", "test_zone"}: - permissions.append(ViewDocumentsPermissions()) - return permissions + # Read-only previews: viewing templates is enough, even for the POST. + return [ + IsAuthenticated(), + ViewOcrTemplatesPermissions(), + ViewDocumentsPermissions(), + ] + return super().get_permissions() @action( detail=False,