Skip to content

Commit 5ef3cd7

Browse files
committed
security(mail): encode attachment filenames, reject header chars
Content-Disposition interpolated basename(file_out) as raw text. A recipient or badge name with CR/LF or quotes could inject MIME headers on the signed-badge email path. Reject those characters in output filenames and set the attachment filename via add_header (RFC 2231). Fixes #316 Verified: flake8, mypy, pytest --cov=openbadgeslib (1736 passed, 93.29%).
1 parent 298526d commit 5ef3cd7

5 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎Changelog.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Newest first. Dates are ISO 8601 (YYYY-MM-DD).
2121
that would write Type Metadata outside ``-o`` is refused.
2222
- security(ob1): an empty revocation-list response fails closed instead
2323
of treating the badge as not revoked.
24+
- security(mail): attachment filenames are RFC 2231 encoded, and CR/LF
25+
or quotes in a badge/recipient name are rejected before they can
26+
inject MIME headers.
2427

2528

2629
* v4.4.2 - 2026-08-13

‎openbadgeslib/issue.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ def _safe_filename_component(value: str, field_name: str) -> str:
116116
raise ValueError('%s is not safe for use in an output filename' % field_name)
117117
if os.path.basename(value) != value or ntpath.basename(value) != value:
118118
raise ValueError('%s must not contain path separators' % field_name)
119+
# CR/LF/quotes/semicolons would inject MIME headers when this name is
120+
# used as Content-Disposition filename (#316).
121+
if any(ch in value for ch in '\r\n"\';'):
122+
raise ValueError(
123+
'%s contains characters that are not safe in a filename'
124+
% field_name)
119125
return value
120126

121127

‎openbadgeslib/mail.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ def send(self, badge: Any) -> None:
6767
raise BadgeImgFormatUnsupported(
6868
'Unsupported image type: %r' % (badge.source.image_type,))
6969

70-
image = MIMEImage(badge.source.image,
71-
Content_Disposition='attachment; filename=%s' % basename(badge.file_out),
72-
Content_Description='Signed OpenBadge',
73-
_subtype=mime_type)
70+
image = MIMEImage(badge.source.image, _subtype=mime_type)
71+
# Keyword form so the filename is RFC 2231 encoded, never interpolated
72+
# into the header value as raw bytes (#316).
73+
image.add_header('Content-Disposition', 'attachment',
74+
filename=basename(badge.file_out))
75+
image.add_header('Content-Description', 'Signed OpenBadge')
7476
msg.attach(image)
7577

7678
try:

‎tests/test_mail.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ def test_plain_smtp_sends_and_quits(self):
4343
smtp.login.assert_not_called()
4444
smtp.sendmail.assert_called_once()
4545
smtp.quit.assert_called_once()
46-
frm, to, _body = smtp.sendmail.call_args.args
46+
frm, to, body = smtp.sendmail.call_args.args
4747
assert frm == 'issuer@example.com' and to == 'r@example.com'
48+
assert 'Content-Disposition: attachment' in body
49+
assert 'filename="badge_1_r.svg"' in body or "filename=badge_1_r.svg" in body
50+
assert '\nBcc:' not in body
4851

4952
def test_ssl_with_auth_logs_in(self):
5053
mail = _ready_mail(use_ssl=True, username='u', password='p')

‎tests/test_ob1_signer.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ def test_safe_filename_component_accepts_plain_values(self, value):
376376
r'recipient\outside',
377377
'C:recipient',
378378
'recipient\x00@example.com',
379+
'foo\r\nBcc: attacker@evil',
380+
'foo";\r\nBcc: x',
381+
"foo'; header",
379382
])
380383
def test_safe_filename_component_rejects_path_values(self, value):
381384
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)