mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-07-29 23:34:56 +00:00
Fix: bound email linkification work
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user