]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Validate page bounds
authorshamoon <4887959+shamoon@users.noreply.github.com>
Sat, 2 Aug 2025 13:23:04 +0000 (09:23 -0400)
committershamoon <4887959+shamoon@users.noreply.github.com>
Sat, 2 Aug 2025 13:23:51 +0000 (09:23 -0400)
src/documents/serialisers.py
src/documents/tests/test_api_bulk_edit.py

index 5c06232efff1715d932bcc3767cf1f05259bd3b1..5a9c089f39ef53c367937e98706e745445f67e32 100644 (file)
@@ -1524,7 +1524,7 @@ class BulkEditSerializer(
         else:
             parameters["archive_fallback"] = False
 
-    def _validate_parameters_edit_pdf(self, parameters):
+    def _validate_parameters_edit_pdf(self, parameters, document_id):
         if "operations" not in parameters:
             raise serializers.ValidationError("operations not specified")
         if not isinstance(parameters["operations"], list):
@@ -1556,6 +1556,15 @@ class BulkEditSerializer(
                     "update_document only allowed with a single output document",
                 )
 
+        doc = Document.objects.get(id=document_id)
+        # doc existence is already validated
+        if doc.page_count:
+            for op in parameters["operations"]:
+                if op["page"] < 1 or op["page"] > doc.page_count:
+                    raise serializers.ValidationError(
+                        f"Page {op['page']} is out of bounds for document with {doc.page_count} pages.",
+                    )
+
     def validate(self, attrs):
         method = attrs["method"]
         parameters = attrs["parameters"]
@@ -1595,7 +1604,7 @@ class BulkEditSerializer(
                 raise serializers.ValidationError(
                     "Edit PDF method only supports one document",
                 )
-            self._validate_parameters_edit_pdf(parameters)
+            self._validate_parameters_edit_pdf(parameters, attrs["documents"][0])
 
         return attrs
 
index 7e636b0c78109a124faac7f5f84969344f7384ff..31aaff946622f18d1398ba4e49354f9395e4eb35 100644 (file)
@@ -41,6 +41,7 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
             title="B",
             correspondent=self.c1,
             document_type=self.dt1,
+            page_count=5,
         )
         self.doc3 = Document.objects.create(
             checksum="C",
@@ -1555,6 +1556,32 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
             response.content,
         )
 
+    @mock.patch("documents.serialisers.bulk_edit.edit_pdf")
+    def test_edit_pdf_page_out_of_bounds(self, m):
+        """
+        GIVEN:
+            - API data for editing PDF is called
+            - The page number is out of bounds
+        WHEN:
+            - API is called
+        THEN:
+            - The API fails with a correct error code
+        """
+        self.setup_mock(m, "edit_pdf")
+        response = self.client.post(
+            "/api/documents/bulk_edit/",
+            json.dumps(
+                {
+                    "documents": [self.doc2.id],
+                    "method": "edit_pdf",
+                    "parameters": {"operations": [{"page": 99}]},
+                },
+            ),
+            content_type="application/json",
+        )
+        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+        self.assertIn(b"out of bounds", response.content)
+
     @override_settings(AUDIT_LOG_ENABLED=True)
     def test_bulk_edit_audit_log_enabled_simple_field(self):
         """