From ccf80328f0923d5802f88100defcf2cf0fb69b12 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Sun, 29 Nov 2026 09:02:02 -0800 Subject: [PATCH] Fix: ignore follow_formatting=false when downloading and strip control characters from the download filename --- .../tests/test_api_download_filename.py | 101 ++++++++++++++++++ src/documents/views.py | 9 +- 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/documents/tests/test_api_download_filename.py diff --git a/src/documents/tests/test_api_download_filename.py b/src/documents/tests/test_api_download_filename.py new file mode 100644 index 000000000..191a09965 --- /dev/null +++ b/src/documents/tests/test_api_download_filename.py @@ -0,0 +1,101 @@ +import pytest +from rest_framework import status +from rest_framework.test import APIClient + +from documents.models import Document +from paperless_testing.dirs import PaperlessDirs +from paperless_testing.factories import DocumentFactory + + +@pytest.fixture +def archived_document(paperless_dirs: PaperlessDirs) -> Document: + document = DocumentFactory.create( + title="title", + filename="source.pdf", + archive_filename="archived.pdf", + mime_type="application/pdf", + ) + document.source_path.write_bytes(b"source") + document.archive_path.write_bytes(b"archive") + return document + + +class TestDownloadFollowFormatting: + @pytest.mark.parametrize("value", ["false", "0", "no", "off", ""]) + def test_false_values_use_the_public_filename( + self, + admin_client: APIClient, + archived_document: Document, + value: str, + ) -> None: + """ + GIVEN: + - A document whose name on disk differs from its public name + WHEN: + - It is downloaded with follow_formatting set to a false value + THEN: + - The public name is used, the string "false" is not true + """ + response = admin_client.get( + f"/api/documents/{archived_document.pk}/download/?follow_formatting={value}", + ) + + assert response.status_code == status.HTTP_200_OK + assert "title.pdf" in response["Content-Disposition"] + response.close() + + @pytest.mark.parametrize("value", ["true", "True", "1"]) + def test_true_values_use_the_name_on_disk( + self, + admin_client: APIClient, + archived_document: Document, + value: str, + ) -> None: + """ + GIVEN: + - A document whose name on disk differs from its public name + WHEN: + - It is downloaded with follow_formatting set to a true value + THEN: + - The name on disk is used + """ + response = admin_client.get( + f"/api/documents/{archived_document.pk}/download/?follow_formatting={value}", + ) + + assert "archived.pdf" in response["Content-Disposition"] + response.close() + + +class TestDownloadFilenameControlCharacters: + def test_control_characters_are_not_sent_in_the_header( + self, + admin_client: APIClient, + paperless_dirs: PaperlessDirs, + ) -> None: + """ + GIVEN: + - A document whose name on disk holds control characters, a title can + WHEN: + - It is downloaded following the name on disk + THEN: + - The header has the printable part only, a server rejects the header + otherwise and the response is a 500 + """ + document = DocumentFactory.create( + filename="source.pdf", + archive_filename="ar\x12chi\x14ved.pdf", + mime_type="application/pdf", + ) + document.source_path.write_bytes(b"source") + document.archive_path.parent.mkdir(parents=True, exist_ok=True) + document.archive_path.write_bytes(b"archive") + + response = admin_client.get( + f"/api/documents/{document.pk}/download/?follow_formatting=true", + ) + + disposition = response["Content-Disposition"] + assert 'filename="archived.pdf"' in disposition + assert not any(ord(char) < 32 for char in disposition) + response.close() diff --git a/src/documents/views.py b/src/documents/views.py index d3f6de8bc..ec158fb6e 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -1456,7 +1456,12 @@ class DocumentViewSet( use_archive=not self.original_requested(request) and file_doc.has_archive_version, disposition=disposition, - follow_formatting=request.query_params.get("follow_formatting", False), + # The value is a string, "false" must not count as true + follow_formatting=request.query_params.get( + "follow_formatting", + "", + ).lower() + in {"true", "1", "yes", "on"}, ) def get_metadata(self, file, mime_type): @@ -5328,6 +5333,8 @@ def serve_file( # RFC 5987 addresses this issue # see https://datatracker.ietf.org/doc/html/rfc5987#section-4.2 # Chromium cannot handle commas in the filename + # The name on disk can hold control characters (a title can), a header cannot + filename = "".join(char for char in filename if char.isprintable()) filename_normalized = ( normalize("NFKD", filename.replace(",", "_")) .encode(