Fix: bound email linkification work

This commit is contained in:
shamoon
2026-07-21 13:01:34 -07:00
parent 80210bd3bf
commit a686b1a7aa
2 changed files with 38 additions and 1 deletions
+10 -1
View File
@@ -58,6 +58,12 @@ _SUPPORTED_MIME_TYPES: dict[str, str] = {
"message/rfc822": ".eml",
}
# Bleach's email-address linkifier uses a superlinear regular expression. Keep
# email linkification for ordinary headers and short messages, but never run it
# over an unbounded attacker-controlled field. URL linkification remains enabled
# for longer text.
_MAX_EMAIL_LINKIFY_LENGTH = 2048
class MailDocumentParser:
"""Parse .eml email files for Paperless-ngx.
@@ -627,7 +633,10 @@ class MailDocumentParser:
text = str(text)
text = escape(text)
text = clean(text)
text = linkify(text, parse_email=True)
text = linkify(
text,
parse_email="@" in text and len(text) <= _MAX_EMAIL_LINKIFY_LENGTH,
)
text = text.replace("\n", "<br>")
return text
@@ -700,6 +700,34 @@ class TestParser:
assert expected_html == actual_html
def test_mail_to_html_bounds_email_linkification(
self,
mail_parser: MailDocumentParser,
) -> None:
mail = mock.Mock(
subject="sender@example.com",
from_values=None,
to_values=[],
cc_values=[],
bcc_values=[],
attachments=[],
date=timezone.now(),
text=("a." * 1500) + "@example.com",
)
with mock.patch(
"paperless.parsers.mail.linkify",
side_effect=lambda text, **kwargs: text,
) as mock_linkify:
mail_parser.mail_to_html(mail)
parse_email_by_text = {
call.args[0]: call.kwargs["parse_email"]
for call in mock_linkify.call_args_list
}
assert parse_email_by_text["sender@example.com"] is True
assert parse_email_by_text[mail.text] is False
def test_generate_pdf_from_mail(
self,
httpx_mock: HTTPXMock,