Compare commits

..
5 changed files with 48 additions and 22 deletions

No files matched your search

-5
View File
@@ -23,7 +23,6 @@ dependencies = [
"channels~=4.2",
"channels-redis~=4.2",
"concurrent-log-handler~=0.9.25",
"cryptography>=50",
"dateparser~=1.4",
# WARNING: django does not use semver.
# Only patch versions are guaranteed to not introduce breaking changes.
@@ -47,7 +46,6 @@ dependencies = [
"flower>=2.0.1,<2.3",
"gotenberg-client[httpx]~=1.0",
"httpx-oauth~=0.17",
"humanize>=4.15",
"ijson>=3.5.1",
"imap-tools>=1.14,<1.16",
"jinja2~=3.1.6",
@@ -58,13 +56,10 @@ dependencies = [
"llama-index-embeddings-openai-like>=0.2.2",
"llama-index-llms-ollama>=0.9.1",
"llama-index-llms-openai-like>=0.7.1",
"lxml>=6.1",
"ocrmypdf[heic]>=17.12,<17.13",
"ollama>=0.6.1",
"openai>=2.48",
"pathvalidate~=3.3.1",
"pdf2image~=1.17.0",
"pikepdf>=10.13",
"python-dateutil~=2.9.0",
"python-dotenv~=1.2.1",
"python-gnupg~=0.5.4",
+8 -3
View File
@@ -106,6 +106,10 @@ if TYPE_CHECKING:
logger = logging.getLogger("paperless.serializers")
# How much of an upload is read to detect its type. libmagic examines at most
# 7 MiB (its default bytes_max), so this is more than it will ever look at.
_MIME_SNIFF_BYTES = 8 * 1024 * 1024
# https://www.django-rest-framework.org/api-guide/serializers/#example
class DynamicFieldsModelSerializer(serializers.ModelSerializer[Any]):
@@ -2306,8 +2310,9 @@ class PostDocumentSerializer(serializers.Serializer[dict[str, Any]]):
)
def validate_document(self, document):
document_data = document.file.read()
mime_type = magic.from_buffer(document_data, mime=True)
# libmagic only examines a bounded prefix, so an upload of any size is
# never read into memory just to detect its type
mime_type = magic.from_buffer(document.read(_MIME_SNIFF_BYTES), mime=True)
if not is_mime_type_supported(mime_type):
if (
@@ -2323,7 +2328,7 @@ class PostDocumentSerializer(serializers.Serializer[dict[str, Any]]):
_("File type %(type)s not supported") % {"type": mime_type},
)
return document.name, document_data
return document.name, document
def validate_correspondent(self, correspondent):
if correspondent:
+32
View File
@@ -2089,6 +2089,38 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase):
self.assertIsNone(overrides.document_type_id)
self.assertIsNone(overrides.tag_ids)
def test_upload_type_is_detected_from_a_prefix(self) -> None:
"""
GIVEN:
- An upload larger than the prefix read to detect its type
WHEN:
- The document is posted
THEN:
- The type is detected from the prefix and the upload is accepted
- The file handed to the consumer holds the whole upload
"""
self.consume_file_mock.return_value = celery.result.AsyncResult(
id=str(uuid.uuid4()),
)
content = SIMPLE_DIGITAL_PDF.read_bytes()
with (
mock.patch("documents.serialisers._MIME_SNIFF_BYTES", 1024),
SimpleUploadedFile(
"simple.pdf",
content,
content_type="application/pdf",
) as f,
):
response = self.client.post(
"/api/documents/post_document/",
{"document": f},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
input_doc, _ = self.assert_queue_consumption_task_call_args()
self.assertEqual(input_doc.original_file.read_bytes(), content)
def test_upload_with_path_traversal_filename_is_reduced_to_basename(self) -> None:
self.consume_file_mock.return_value = celery.result.AsyncResult(
id=str(uuid.uuid4()),
+8 -4
View File
@@ -2189,7 +2189,7 @@ class DocumentViewSet(
raise Http404
try:
doc_name, doc_data = serializer.validated_data.get("document")
doc_name, doc_file = serializer.validated_data.get("document")
version_label = serializer.validated_data.get("version_label")
t = int(mktime(datetime.now().timetuple()))
@@ -2200,7 +2200,9 @@ class DocumentViewSet(
pathvalidate.sanitize_filename(doc_name),
)
temp_file_path.write_bytes(doc_data)
with temp_file_path.open("wb") as scratch_file:
for chunk in doc_file.chunks():
scratch_file.write(chunk)
os.utime(temp_file_path, times=(t, t))
@@ -3338,7 +3340,7 @@ class PostDocumentView(GenericAPIView[Any]):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
doc_name, doc_data = serializer.validated_data.get("document")
doc_name, doc_file = serializer.validated_data.get("document")
doc_name = normalize("NFC", doc_name)
correspondent_id = serializer.validated_data.get("correspondent")
document_type_id = serializer.validated_data.get("document_type")
@@ -3358,7 +3360,9 @@ class PostDocumentView(GenericAPIView[Any]):
pathvalidate.sanitize_filename(doc_name),
)
temp_file_path.write_bytes(doc_data)
with temp_file_path.open("wb") as scratch_file:
for chunk in doc_file.chunks():
scratch_file.write(chunk)
os.utime(temp_file_path, times=(t, t))
Generated
-10
View File
@@ -2516,7 +2516,6 @@ dependencies = [
{ name = "channels" },
{ name = "channels-redis" },
{ name = "concurrent-log-handler" },
{ name = "cryptography" },
{ name = "dateparser" },
{ name = "django" },
{ name = "django-allauth", extra = ["mfa", "socialaccount"] },
@@ -2538,7 +2537,6 @@ dependencies = [
{ name = "flower" },
{ name = "gotenberg-client", extra = ["httpx"] },
{ name = "httpx-oauth" },
{ name = "humanize" },
{ name = "ijson" },
{ name = "imap-tools" },
{ name = "jinja2" },
@@ -2549,13 +2547,10 @@ dependencies = [
{ name = "llama-index-embeddings-openai-like" },
{ name = "llama-index-llms-ollama" },
{ name = "llama-index-llms-openai-like" },
{ name = "lxml" },
{ name = "ocrmypdf", extra = ["heic"] },
{ name = "ollama" },
{ name = "openai" },
{ name = "pathvalidate" },
{ name = "pdf2image" },
{ name = "pikepdf" },
{ name = "python-dateutil" },
{ name = "python-dotenv" },
{ name = "python-gnupg" },
@@ -2673,7 +2668,6 @@ requires-dist = [
{ name = "channels", specifier = "~=4.2" },
{ name = "channels-redis", specifier = "~=4.2" },
{ name = "concurrent-log-handler", specifier = "~=0.9.25" },
{ name = "cryptography", specifier = ">=50.0.0" },
{ name = "dateparser", specifier = "~=1.4" },
{ name = "django", specifier = "~=5.2.18" },
{ name = "django-allauth", extras = ["mfa", "socialaccount"], specifier = "~=65.21.1" },
@@ -2696,7 +2690,6 @@ requires-dist = [
{ name = "gotenberg-client", extras = ["httpx"], specifier = "~=1.0" },
{ name = "granian", extras = ["uvloop"], marker = "extra == 'webserver'", specifier = ">=2.7,<2.9" },
{ name = "httpx-oauth", specifier = "~=0.17" },
{ name = "humanize", specifier = ">=4.15.0" },
{ name = "ijson", specifier = ">=3.5.1" },
{ name = "imap-tools", specifier = ">=1.14,<1.16" },
{ name = "jinja2", specifier = "~=3.1.6" },
@@ -2707,14 +2700,11 @@ requires-dist = [
{ name = "llama-index-embeddings-openai-like", specifier = ">=0.2.2" },
{ name = "llama-index-llms-ollama", specifier = ">=0.9.1" },
{ name = "llama-index-llms-openai-like", specifier = ">=0.7.1" },
{ name = "lxml", specifier = ">=6.1.0" },
{ name = "mysqlclient", marker = "extra == 'mariadb'", specifier = ">=2.2.7,<2.4" },
{ name = "ocrmypdf", extras = ["heic"], specifier = ">=17.12,<17.13" },
{ name = "ollama", specifier = ">=0.6.1" },
{ name = "openai", specifier = ">=2.48" },
{ name = "pathvalidate", specifier = "~=3.3.1" },
{ name = "pdf2image", specifier = "~=1.17.0" },
{ name = "pikepdf", specifier = ">=10.13" },
{ name = "psycopg", extras = ["c", "pool"], marker = "extra == 'postgres'", specifier = "==3.3.8" },
{ name = "psycopg-c", marker = "python_full_version == '3.12.*' and platform_machine == 'aarch64' and sys_platform == 'linux' and extra == 'postgres'", url = "https://github.com/paperless-ngx/builder/releases/download/psycopg-trixie-3.3.8/psycopg_c-3.3.8-cp312-cp312-manylinux_2_38_aarch64.whl" },
{ name = "psycopg-c", marker = "python_full_version == '3.14.*' and platform_machine == 'aarch64' and sys_platform == 'linux' and extra == 'postgres'", url = "https://github.com/paperless-ngx/builder/releases/download/psycopg-trixie-3.3.8/psycopg_c-3.3.8-cp314-cp314-manylinux_2_38_aarch64.whl" },