diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 3f4b49d68d..59f33d87db 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -86,6 +86,7 @@ from documents.permissions import get_document_count_filter_for_user from documents.permissions import get_permission_grantees from documents.permissions import has_perms_owner_aware from documents.permissions import permitted_document_ids +from documents.permissions import permitted_object_ids from documents.permissions import restrict_queryset_to_visible from documents.permissions import set_permissions_for_objects from documents.regex import validate_regex_pattern @@ -1802,11 +1803,23 @@ class ReprocessDocumentsSerializer(DocumentSelectionSerializer): class RunZoneOcrSerializer(serializers.Serializer): template = serializers.PrimaryKeyRelatedField( - queryset=OcrTemplate.objects.prefetch_related("zones", "zones__custom_field"), + queryset=OcrTemplate.objects.none(), required=False, allow_null=True, ) + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + request = self.context.get("request") + user = getattr(request, "user", None) if request else None + if user is not None and user.is_authenticated: + template_field = self.fields.get("template") + if not isinstance(template_field, serializers.PrimaryKeyRelatedField): + return + template_field.queryset = OcrTemplate.objects.filter( + id__in=permitted_object_ids(user, OcrTemplate, "view_ocrtemplate"), + ).prefetch_related("zones", "zones__custom_field") + class RunZoneOcrDocumentsSerializer(DocumentSelectionSerializer, RunZoneOcrSerializer): from_webui = serializers.BooleanField(required=False, default=False) @@ -3805,10 +3818,22 @@ class OcrTemplateZoneTestSerializer(OcrTemplateZoneSerializer): class OcrTemplateTestSerializer(serializers.Serializer): document = serializers.PrimaryKeyRelatedField( - queryset=Document.objects.select_related("root_document__owner"), + queryset=Document.objects.none(), ) zone = OcrTemplateZoneTestSerializer() + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + request = self.context.get("request") + user = getattr(request, "user", None) if request else None + if user is not None and user.is_authenticated: + document_field = self.fields.get("document") + if not isinstance(document_field, serializers.PrimaryKeyRelatedField): + return + document_field.queryset = Document.objects.filter( + id__in=permitted_document_ids(user), + ) + class OcrTemplateSerializer(MatchingModelSerializer, OwnedObjectSerializer): zones = OcrTemplateZoneSerializer(many=True, required=False) @@ -3821,6 +3846,22 @@ class OcrTemplateSerializer(MatchingModelSerializer, OwnedObjectSerializer): slug = None document_count = None + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + # Writes accept documents the user can view, plus the sample document + # the template already stores, so saving a template unchanged keeps it. + # Reads serialize the stored pk regardless of the queryset. + sample_field = self.fields.get("sample_document") + if not isinstance(sample_field, serializers.PrimaryKeyRelatedField): + return + if self.user is None: + sample_field.queryset = Document.objects.none() + return + allowed = Q(id__in=permitted_document_ids(self.user)) + if isinstance(self.instance, OcrTemplate) and self.instance.sample_document_id: + allowed |= Q(id=self.instance.sample_document_id) + sample_field.queryset = Document.objects.filter(allowed) + class Meta: model = OcrTemplate fields = [ diff --git a/src/documents/tests/test_api_bulk_edit.py b/src/documents/tests/test_api_bulk_edit.py index b877ee5fcf..8df7e5c3c1 100644 --- a/src/documents/tests/test_api_bulk_edit.py +++ b/src/documents/tests/test_api_bulk_edit.py @@ -2970,7 +2970,7 @@ class TestRunZoneOcrBulkEditAPI: WHEN: - API is called with that template THEN: - - The whole request is refused + - The whole request is rejected with 400 on the template field """ user = UserFactory(username="editor") grant_global(user, "view_document", "change_document") @@ -2986,5 +2986,6 @@ class TestRunZoneOcrBulkEditAPI: format="json", ) - assert response.status_code == status.HTTP_403_FORBIDDEN + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert "template" in response.json() mock_run_zone_ocr.assert_not_called() diff --git a/src/documents/tests/test_api_ocr_templates.py b/src/documents/tests/test_api_ocr_templates.py index af991535fe..fb439605dd 100644 --- a/src/documents/tests/test_api_ocr_templates.py +++ b/src/documents/tests/test_api_ocr_templates.py @@ -620,6 +620,191 @@ class TestOcrTemplatePermissions: assert template.owner == admin_user +@pytest.mark.django_db +@pytest.mark.usefixtures("paperless_dirs") +class TestOcrTemplateSampleDocument: + """The sample document of a template is limited to what the user can see.""" + + @pytest.fixture + def editor(self, regular_user: User) -> User: + grant_global( + regular_user, + "view_ocrtemplate", + "add_ocrtemplate", + "change_ocrtemplate", + "view_document", + ) + return regular_user + + @pytest.mark.usefixtures("editor") + def test_create_rejects_an_invisible_sample_document( + self, + user_client: APIClient, + ) -> None: + """ + GIVEN: + - A document owned by another user and not shared with the editor + WHEN: + - The editor creates a template using it as the sample document + THEN: + - 400 is returned on the sample_document field and no template is + created + """ + document = DocumentFactory(owner=UserFactory()) + + response = user_client.post( + ENDPOINT, + template_payload(sample_document=document.pk), + format="json", + ) + + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert "sample_document" in response.json() + assert not OcrTemplate.objects.exists() + + def test_update_rejects_an_invisible_sample_document( + self, + editor: User, + user_client: APIClient, + ) -> None: + """ + GIVEN: + - A template owned by the editor + - A document owned by another user and not shared with the editor + WHEN: + - The editor patches the template to use it as the sample document + THEN: + - 400 is returned on the sample_document field and the template + is unchanged + """ + document = DocumentFactory(owner=UserFactory()) + template = OcrTemplateFactory(owner=editor, match="Northwind") + + response = user_client.patch( + detail_url(template.pk), + {"sample_document": document.pk}, + format="json", + ) + + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert "sample_document" in response.json() + template.refresh_from_db() + assert template.sample_document is None + + def test_accepts_visible_sample_documents_and_clearing( + self, + editor: User, + user_client: APIClient, + ) -> None: + """ + GIVEN: + - A document owned by the editor and one shared with the editor + WHEN: + - The editor creates a template with the owned document, patches in + the shared document, then clears it + THEN: + - Each request succeeds and the template stores the value + """ + owned = DocumentFactory(owner=editor) + shared = DocumentFactory(owner=UserFactory()) + grant_object(editor, shared, "view_document") + + response = user_client.post( + ENDPOINT, + template_payload(sample_document=owned.pk), + format="json", + ) + assert response.status_code == status.HTTP_201_CREATED + assert response.json()["sample_document"] == owned.pk + url = detail_url(response.json()["id"]) + + for value in (shared.pk, None): + response = user_client.patch( + url, + {"sample_document": value}, + format="json", + ) + assert response.status_code == status.HTTP_200_OK + assert response.json()["sample_document"] == value + + def test_collaborator_can_save_a_template_keeping_its_sample_document( + self, + editor: User, + user_client: APIClient, + ) -> None: + """ + GIVEN: + - A template owned by another user and shared with the editor for + view and change, whose sample document the editor cannot see + WHEN: + - The editor saves the template as retrieved with a new name, and + then tries to switch to another document they cannot see + THEN: + - The save succeeds and keeps the stored sample document + - The switch is rejected with 400 + """ + owner = UserFactory() + sample = DocumentFactory(owner=owner) + template = OcrTemplateFactory( + owner=owner, + match="Northwind", + sample_document=sample, + ) + grant_object(editor, template, "view_ocrtemplate", "change_ocrtemplate") + + payload = user_client.get(detail_url(template.pk)).json() + payload["name"] = "Renamed" + response = user_client.put(detail_url(template.pk), payload, format="json") + + assert response.status_code == status.HTTP_200_OK + template.refresh_from_db() + assert template.name == "Renamed" + assert template.sample_document == sample + + other = DocumentFactory(owner=owner) + response = user_client.patch( + detail_url(template.pk), + {"sample_document": other.pk}, + format="json", + ) + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert "sample_document" in response.json() + template.refresh_from_db() + assert template.sample_document == sample + + def test_reading_shows_a_sample_document_the_viewer_cannot_see( + self, + regular_user: User, + user_client: APIClient, + ) -> None: + """ + GIVEN: + - A template visible to the user whose stored sample document + belongs to another user + WHEN: + - The user retrieves and lists templates + THEN: + - Both return 200 with the stored sample document pk + """ + grant_global(regular_user, "view_ocrtemplate") + document = DocumentFactory(owner=UserFactory()) + template = OcrTemplateFactory( + owner=regular_user, + match="Northwind", + sample_document=document, + ) + + response = user_client.get(detail_url(template.pk)) + assert response.status_code == status.HTTP_200_OK + assert response.json()["sample_document"] == document.pk + + response = user_client.get(ENDPOINT) + assert response.status_code == status.HTTP_200_OK + assert [t["sample_document"] for t in response.json()["results"]] == [ + document.pk, + ] + + @pytest.mark.django_db @pytest.mark.usefixtures("paperless_dirs") class TestOcrTemplateValidation: @@ -1598,7 +1783,8 @@ class TestRunZoneOcrPermissions: WHEN: - The editor runs zone OCR choosing that template THEN: - - 403 is returned and nothing is extracted + - 400 is returned, naming the template field, and nothing is + extracted """ grant_global(regular_user, "view_document", "change_document") document = DocumentFactory(content=CONTENT, owner=regular_user) @@ -1613,7 +1799,8 @@ class TestRunZoneOcrPermissions: format="json", ) - assert response.status_code == status.HTTP_403_FORBIDDEN + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert "template" in response.json() mock_extract.assert_not_called() def test_requires_change_permission_on_the_document( @@ -2269,7 +2456,8 @@ class TestOcrTemplatePreviewPermissions: WHEN: - The user requests a page image and a zone preview of the version THEN: - - Both are refused until the user is granted view on the root + - The page image is refused with 403 and the zone preview with 400 + until the user is granted view on the root - Afterwards both succeed """ other = UserFactory() @@ -2286,7 +2474,7 @@ class TestOcrTemplatePreviewPermissions: ) assert ( preview_zone(preview_client, version).status_code - == status.HTTP_403_FORBIDDEN + == status.HTTP_400_BAD_REQUEST ) mock_render.assert_not_called() mock_preview.assert_not_called() @@ -2339,18 +2527,20 @@ class TestOcrTemplatePreviewPermissions: WHEN: - The user previews a zone on the document THEN: - - The request is refused until the user is granted view on the document - - Afterwards it passes the permission check and reaches the missing file + - The request is rejected with 400 until the user is granted view + on the document + - Afterwards it passes validation and reaches the missing file - No preview extraction runs """ owner = UserFactory() 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 + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert "document" in response.json() grant_object(regular_user, document, "view_document") response = preview_zone(user_client, document, x=0, y=0, height=100, page=1) - # Past the permission check; the factory document has no file on disk. + # Past validation; the factory document has no file on disk. assert response.status_code == status.HTTP_404_NOT_FOUND mock_preview.assert_not_called() diff --git a/src/documents/views.py b/src/documents/views.py index 5012691a4c..37b1800ab6 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -2305,7 +2305,10 @@ class DocumentViewSet( Run zone-based OCR extraction on this document, with the given template or with every template that matches it. """ - serializer = RunZoneOcrSerializer(data=request.data) + serializer = RunZoneOcrSerializer( + data=request.data, + context={"request": request}, + ) serializer.is_valid(raise_exception=True) template = serializer.validated_data.get("template") @@ -2318,13 +2321,6 @@ class DocumentViewSet( ) or not has_perms_owner_aware(request.user, "change_document", document): return HttpResponseForbidden("Insufficient permissions") - if template is not None and not has_perms_owner_aware( - request.user, - "view_ocrtemplate", - template, - ): - return HttpResponseForbidden("Insufficient permissions") - from documents.zone_ocr import get_templates_for_document from documents.zone_ocr import run_zone_extraction @@ -3496,12 +3492,6 @@ class RunZoneOcrDocumentsView(DocumentOperationPermissionMixin): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) template = serializer.validated_data.get("template") - if template is not None and not has_perms_owner_aware( - request.user, - "view_ocrtemplate", - template, - ): - return HttpResponseForbidden("Insufficient permissions") if template is not None: serializer.validated_data["template"] = template.pk return self._execute_document_action( @@ -5908,13 +5898,13 @@ class OcrTemplateViewSet(BulkPermissionMixin, PassUserMixin, ModelViewSet[OcrTem """Extract an ad-hoc zone without changing the document.""" from documents.zone_ocr import extract_zone_preview - serializer = OcrTemplateTestSerializer(data=request.data) + serializer = OcrTemplateTestSerializer( + data=request.data, + context={"request": request}, + ) serializer.is_valid(raise_exception=True) document = serializer.validated_data["document"] - if not has_perms_owner_aware(request.user, "view_document", document): - return HttpResponseForbidden("Insufficient permissions") - doc_path = document.archive_path or document.source_path if not doc_path or not Path(doc_path).is_file(): return Response(