Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3.12] gh-144125: email: verify headers are sound in BytesGenerator#144188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
miss-islington wants to merge2 commits intopython:3.12
base:3.12
Choose a base branch
Loading
frommiss-islington:backport-052e55e-3.12
Open
Show file tree
Hide file tree
Changes fromall 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 deletionLib/email/generator.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@
NLCRE = re.compile(r'\r\n|\r|\n')
fcre = re.compile(r'^From ', re.MULTILINE)
NEWLINE_WITHOUT_FWSP = re.compile(r'\r\n[^ \t]|\r[^ \n\t]|\n[^ \t]')
NEWLINE_WITHOUT_FWSP_BYTES = re.compile(br'\r\n[^ \t]|\r[^ \n\t]|\n[^ \t]')


class Generator:
Expand DownExpand Up@@ -429,7 +430,16 @@ def _write_headers(self, msg):
# This is almost the same as the string version, except for handling
# strings with 8bit bytes.
for h, v in msg.raw_items():
self._fp.write(self.policy.fold_binary(h, v))
folded = self.policy.fold_binary(h, v)
if self.policy.verify_generated_headers:
linesep = self.policy.linesep.encode()
if not folded.endswith(linesep):
raise HeaderWriteError(
f'folded header does not end with {linesep!r}: {folded!r}')
if NEWLINE_WITHOUT_FWSP_BYTES.search(folded.removesuffix(linesep)):
raise HeaderWriteError(
f'folded header contains newline: {folded!r}')
self._fp.write(folded)
# A blank line always separates headers from body
self.write(self._NL)

Expand Down
4 changes: 3 additions & 1 deletionLib/test/test_email/test_generator.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -313,7 +313,7 @@ def test_flatten_unicode_linesep(self):
self.assertEqual(s.getvalue(),self.typ(expected))

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

withself.assertRaises(email.errors.HeaderWriteError):
message.as_string()
withself.assertRaises(email.errors.HeaderWriteError):
message.as_bytes()


classTestBytesGenerator(TestGeneratorBase,TestEmailBase):
Expand Down
6 changes: 5 additions & 1 deletionLib/test/test_email/test_policy.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -296,7 +296,7 @@ def test_short_maxlen_error(self):
policy.fold("Subject",subject)

deftest_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)
fortextin (
'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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
:mod:`~email.generator.BytesGenerator` will now refuse to serialize (write) headers
that are unsafely folded or delimited; see
:attr:`~email.policy.Policy.verify_generated_headers`. (Contributed by Bas
Bloemsaat and Petr Viktorin in:gh:`121650`).

[8]ページ先頭

©2009-2026 Movatter.jp