Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasreddy committed Dec 17, 2024
1 parent 8f6f6c3 commit ce18c9b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 11 additions & 9 deletions Lib/email/_policybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import abc
import re
from email import header
from email import charset as _charset
from email.utils import _has_surrogates
Expand All @@ -14,6 +15,15 @@
'compat32',
]

header_re = re.compile(r'^[^\s:]+$')
header_ascii_re = re.compile("[\041-\071\073-\176]+$")

def validate_header_name(name):
# Validate header name according to RFC 5322
if not header_re.match(name):
raise ValueError(f"Invalid header field name {name!r}")
if not header_ascii_re.match(name):
raise ValueError(f"Header field name contains invalid characters: {name!r}")

class _PolicyBase:

Expand Down Expand Up @@ -90,14 +100,6 @@ def __add__(self, other):
"""
return self.clone(**other.__dict__)

def validate_header(name):
# Validate header name according to RFC 5322
import re
if not re.match(r'^[^\s:]+$', name):
raise ValueError(f"Invalid header field name {name!r}")
# Only allow printable ASCII characters
if any(ord(c) < 33 or ord(c) > 126 for c in name):
raise ValueError(f"Invalid header field name {name!r}")

def _append_doc(doc, added_doc):
doc = doc.rsplit('\n', 1)[0]
Expand Down Expand Up @@ -322,7 +324,7 @@ def header_store_parse(self, name, value):
"""+
The name and value are returned unmodified.
"""
validate_header(name)
validate_header_name(name)
return (name, value)

def header_fetch_parse(self, name, value):
Expand Down
8 changes: 7 additions & 1 deletion Lib/email/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

import re
import sys
from email._policybase import Policy, Compat32, compat32, _extend_docstrings, validate_header
from email._policybase import (
_extend_docstrings,
Compat32,
compat32,
Policy,
validate_header
)
from email.utils import _has_surrogates
from email.headerregistry import HeaderRegistry as HeaderRegistry
from email.contentmanager import raw_data_manager
Expand Down

0 comments on commit ce18c9b

Please sign in to comment.