8000 gh-121650 : detect newlines in headers by basbloemsaat · Pull Request #121812 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-121650 : detect newlines in headers #121812

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions Lib/email/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ def header_store_parse(self, name, value):
"""
if hasattr(value, 'name') and value.name.lower() == name.lower():
return (name, value)
if isinstance(value, str) and len(value.splitlines())>1:
# XXX this error message isn't quite right when we use splitlines
# (see issue 22233), but I'm not sure what should happen here.
if (isinstance(value, str) and
linesep_splitter.search(self.header_factory(name, value))
):
raise ValueError("Header values may not contain linefeed "
"or carriage return characters")
"or carriage return characters")
return (name, self.header_factory(name, value))

def header_fetch_parse(self, name, value):
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_email/test_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ def test_header_store_parse_rejects_newlines(self):
instance.header_store_parse,
'From', 'spam\negg@foo.py')

self.assertRaises(ValueError,
instance.header_store_parse,
'Subject', 'te\nst')

self.assertRaises(ValueError,
instance.header_store_parse,
'Subject', 'A 💩 subject=?UTF-8?Q?=0A?=Bcc: injected@example.com')

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes newlines (and carriage returns) in email headers, both at the end of
the header, and encoded entities that are resolved to newlines and returns
Loading
0