[3.14] gh-144125: email: verify headers are sound in BytesGenerator by miss-islington · Pull Request #144182 · python/cpython · GitHub
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
12 changes: 11 additions & 1 deletion Lib/email/generator.py
4 changes: 3 additions & 1 deletion Lib/test/test_email/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def test_flatten_unicode_linesep(self):
self.assertEqual(s.getvalue(), self.typ(expected))

def test_verify_generated_headers(self):
"""gh-121650: by default the generator prevents header injection"""
# gh-121650: by default the generator prevents header injection
class LiteralHeader(str):
name = 'Header'
def fold(self, **kwargs):
Expand All @@ -334,6 +334,8 @@ def fold(self, **kwargs):

with self.assertRaises(email.errors.HeaderWriteError):
message.as_string()
with self.assertRaises(email.errors.HeaderWriteError):
message.as_bytes()


class TestBytesGenerator(TestGeneratorBase, TestEmailBase):
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_email/test_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def test_short_maxlen_error(self):
policy.fold("Subject", subject)

def test_verify_generated_headers(self):
"""Turning protection off allows header injection"""
# Turning protection off allows header injection
policy = email.policy.default.clone(verify_generated_headers=False)
for text in (
'Header: Value\r\nBad: Injection\r\n',
Expand All @@ -319,6 +319,10 @@ def fold(self, **kwargs):
message.as_string(),
f"{text}\nBody",
)
self.assertEqual(
message.as_bytes(),
f"{text}\nBody".encode(),
)

# XXX: Need subclassing tests.
# For adding subclassed objects, make sure the usual rules apply (subclass
Expand Down
Loading