Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion airflow-core/src/airflow/utils/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def build_mime_message(
basename = os.path.basename(fname)
with open(fname, "rb") as file:
part = MIMEApplication(file.read(), Name=basename)
part["Content-Disposition"] = f'attachment; filename="{basename}"'
part.add_header("Content-Disposition", "attachment", filename=basename)
part["Content-ID"] = f"<{basename}>"
msg.attach(part)

Expand Down
19 changes: 19 additions & 0 deletions airflow-core/tests/unit/utils/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,25 @@ def test_build_mime_message(self):
assert [mail_to] == recipients
assert msg["To"] == ",".join(recipients)

def test_build_mime_message_escapes_attachment_filename(self, tmp_path):
# A quote in the filename must not break out of the quoted
# Content-Disposition value and inject extra parameters.
malicious = 'report.txt"; x-evil="1'
attachment = tmp_path / malicious
attachment.write_bytes(b"data")

msg, _ = email.build_mime_message(
mail_from="from@example.com",
to="to@example.com",
subject="subject",
html_content="<html></html>",
files=[os.fspath(attachment)],
)

part = msg.get_payload()[-1]
assert part.get_filename() == malicious
assert "x-evil" not in dict(part.get_params(header="Content-Disposition"))


@pytest.mark.db_test
class TestEmailSmtp:
Expand Down
2 changes: 1 addition & 1 deletion providers/smtp/src/airflow/providers/smtp/hooks/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def _build_mime_message(
basename = os.path.basename(fname)
with open(fname, "rb") as file:
part = MIMEApplication(file.read(), Name=basename)
part["Content-Disposition"] = f'attachment; filename="{basename}"'
part.add_header("Content-Disposition", "attachment", filename=basename)
part["Content-ID"] = f"<{basename}>"
msg.attach(part)

Expand Down
20 changes: 20 additions & 0 deletions providers/smtp/tests/unit/smtp/hooks/test_smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,26 @@ def test_build_mime_message(self, mock_smtplib):
assert [mail_to] == recipients
assert msg["To"] == ",".join(recipients)

@patch(smtplib_string)
def test_build_mime_message_escapes_attachment_filename(self, mock_smtplib, tmp_path):
# A quote in the filename must not break out of the quoted
# Content-Disposition value and inject extra parameters.
malicious = 'report.txt"; x-evil="1'
attachment = tmp_path / malicious
attachment.write_bytes(b"data")
with SmtpHook() as smtp_hook:
msg, _ = smtp_hook._build_mime_message(
mail_from=FROM_EMAIL,
to=TO_EMAIL,
subject=TEST_SUBJECT,
html_content=TEST_BODY,
files=[os.fspath(attachment)],
)

part = msg.get_payload()[-1]
assert part.get_filename() == malicious
assert "x-evil" not in dict(part.get_params(header="Content-Disposition"))

@patch(smtplib_string)
def test_send_smtp(self, mock_smtplib):
mock_send_mime = mock_smtplib.SMTP_SSL().sendmail
Expand Down
Loading