Compare commits

..
2 changed files with 109 additions and 1 deletions

No files matched your search

@@ -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()
+8 -1
View File
@@ -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(