Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Fix: validate PDF output doc indexes in bulk edit (#14083)

shamoon529319455139
diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py
index a56eb014a..399282dc3 100644
--- a/src/documents/bulk_edit.py
+++ b/src/documents/bulk_edit.py
@@ -899,17 +899,26 @@ def edit_pdf(
pdf_docs: list[pikepdf.Pdf] = []
try:
+ if not operations:
+ raise ValueError("Output document index is out of bounds")
+
+ max_idx = max(op.get("doc", 0) for op in operations)
+ if update_document and max_idx > 0:
+ logger.error(
+ "Update requested but multiple output documents specified",
+ )
+ raise ValueError("Multiple output documents specified")
+
+ if any(
+ op.get("doc", 0) < 0 or op.get("doc", 0) >= len(operations)
+ for op in operations
+ ):
+ raise ValueError("Output document index is out of bounds")
+
with pikepdf.open(pair.source_doc.source_path) as src:
# prepare output documents
- max_idx = max(op.get("doc", 0) for op in operations)
pdf_docs = [pikepdf.new() for _ in range(max_idx + 1)]
- if update_document and len(pdf_docs) > 1:
- logger.error(
- "Update requested but multiple output documents specified",
- )
- raise ValueError("Multiple output documents specified")
-
for op in operations:
dst = pdf_docs[op.get("doc", 0)]
page = src.pages[op["page"] - 1]
diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py
index 8b817b3f2..9cd8d187a 100644
--- a/src/documents/serialisers.py
+++ b/src/documents/serialisers.py
@@ -1750,7 +1750,7 @@ class MergeDocumentsAsVersionsSerializer(DocumentListSerializer):
class EditPdfDocumentsSerializer(DocumentListSerializer, SourceModeValidationMixin):
- operations = serializers.ListField(required=True)
+ operations = serializers.ListField(required=True, allow_empty=False)
delete_original = serializers.BooleanField(required=False, default=False)
update_document = serializers.BooleanField(required=False, default=False)
include_metadata = serializers.BooleanField(required=False, default=True)
@@ -1788,6 +1788,12 @@ class EditPdfDocumentsSerializer(DocumentListSerializer, SourceModeValidationMix
"update_document only allowed with a single output document",
)
+ if any(
+ op.get("doc", 0) < 0 or op.get("doc", 0) >= len(operations)
+ for op in operations
+ ):
+ raise serializers.ValidationError("doc index is out of bounds")
+
doc = Document.objects.get(id=documents[0])
if doc.page_count:
for op in operations:
@@ -2124,6 +2130,8 @@ class BulkEditSerializer(
raise serializers.ValidationError("operations not specified")
if not isinstance(parameters["operations"], list):
raise serializers.ValidationError("operations must be a list")
+ if not parameters["operations"]:
+ raise serializers.ValidationError("operations must not be empty")
for op in parameters["operations"]:
if not isinstance(op, dict):
raise serializers.ValidationError("invalid operation entry")
@@ -2151,6 +2159,12 @@ class BulkEditSerializer(
"update_document only allowed with a single output document",
)
+ if any(
+ op.get("doc", 0) < 0 or op.get("doc", 0) >= len(parameters["operations"])
+ for op in parameters["operations"]
+ ):
+ raise serializers.ValidationError("doc index is out of bounds")
+
doc = Document.objects.get(id=document_id)
# doc existence is already validated
if doc.page_count:
diff --git a/src/documents/tests/test_api_bulk_edit.py b/src/documents/tests/test_api_bulk_edit.py
index 714e1bcfd..446f864d7 100644
--- a/src/documents/tests/test_api_bulk_edit.py
+++ b/src/documents/tests/test_api_bulk_edit.py
@@ -1649,6 +1649,40 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ def test_legacy_bulk_edit_rejects_out_of_bounds_pdf_doc_index(self) -> None:
+ response = self.client.post(
+ "/api/documents/bulk_edit/",
+ json.dumps(
+ {
+ "documents": [self.doc2.id],
+ "method": "edit_pdf",
+ "parameters": {
+ "operations": [{"page": 1, "doc": 2**32}],
+ },
+ },
+ ),
+ content_type="application/json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn(b"doc index is out of bounds", response.content)
+
+ def test_legacy_bulk_edit_rejects_empty_pdf_operations(self) -> None:
+ response = self.client.post(
+ "/api/documents/bulk_edit/",
+ json.dumps(
+ {
+ "documents": [self.doc2.id],
+ "method": "edit_pdf",
+ "parameters": {"operations": []},
+ },
+ ),
+ content_type="application/json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn(b"operations must not be empty", response.content)
+
@mock.patch("documents.views.bulk_edit.edit_pdf")
def test_edit_pdf(self, m) -> None:
self.setup_mock(m, "edit_pdf")
@@ -1699,6 +1733,13 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn(b"Expected a list of items", response.content)
+ response = self.client.post(
+ "/api/documents/edit_pdf/",
+ {"documents": [self.doc2.id], "operations": []},
+ format="json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+
response = self.client.post(
"/api/documents/edit_pdf/",
json.dumps(
@@ -1751,6 +1792,21 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn(b"doc must be an integer", response.content)
+ for doc_index in (-1, 2**32):
+ with self.subTest(doc_index=doc_index):
+ response = self.client.post(
+ "/api/documents/edit_pdf/",
+ json.dumps(
+ {
+ "documents": [self.doc2.id],
+ "operations": [{"page": 1, "doc": doc_index}],
+ },
+ ),
+ content_type="application/json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn(b"doc index is out of bounds", response.content)
+
response = self.client.post(
"/api/documents/edit_pdf/",
json.dumps(
diff --git a/src/documents/tests/test_bulk_edit.py b/src/documents/tests/test_bulk_edit.py
index 6df2a9c53..f9d4a5028 100644
--- a/src/documents/tests/test_bulk_edit.py
+++ b/src/documents/tests/test_bulk_edit.py
@@ -1642,6 +1642,16 @@ class TestPDFActions(DirectoriesMixin, TestCase):
mock_group.assert_not_called()
mock_consume_file.assert_not_called()
+ @mock.patch("pikepdf.open")
+ def test_edit_pdf_rejects_invalid_operations(self, mock_open) -> None:
+ for operations in ([], [{"page": 1, "doc": 2**32}]):
+ with self.subTest(operations=operations):
+ with self.assertLogs("paperless.bulk_edit", level="ERROR"):
+ with self.assertRaisesRegex(ValueError, "index is out of bounds"):
+ bulk_edit.edit_pdf([self.doc2.id], operations)
+
+ mock_open.assert_not_called()
+
@mock.patch("documents.bulk_edit.update_document_content_maybe_archive_file.delay")
@mock.patch("documents.tasks.consume_file.apply_async")
@mock.patch("documents.bulk_edit.tempfile.mkdtemp")