Skip to content

Commit ce18c9b

Browse files
committed
Address review comments
1 parent 8f6f6c3 commit ce18c9b

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

Lib/email/_policybase.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import abc
7+
import re
78
from email import header
89
from email import charset as _charset
910
from email.utils import _has_surrogates
@@ -14,6 +15,15 @@
1415
'compat32',
1516
]
1617

18+
header_re = re.compile(r'^[^\s:]+$')
19+
header_ascii_re = re.compile("[\041-\071\073-\176]+$")
20+
21+
def validate_header_name(name):
22+
# Validate header name according to RFC 5322
23+
if not header_re.match(name):
24+
raise ValueError(f"Invalid header field name {name!r}")
25+
if not header_ascii_re.match(name):
26+
raise ValueError(f"Header field name contains invalid characters: {name!r}")
1727

1828
class _PolicyBase:
1929

@@ -90,14 +100,6 @@ def __add__(self, other):
90100
"""
91101
return self.clone(**other.__dict__)
92102

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

102104
def _append_doc(doc, added_doc):
103105
doc = doc.rsplit('\n', 1)[0]
@@ -322,7 +324,7 @@ def header_store_parse(self, name, value):
322324
"""+
323325
The name and value are returned unmodified.
324326
"""
325-
validate_header(name)
327+
validate_header_name(name)
326328
return (name, value)
327329

328330
def header_fetch_parse(self, name, value):

Lib/email/policy.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
import re
66
import sys
7-
from email._policybase import Policy, Compat32, compat32, _extend_docstrings, validate_header
7+
from email._policybase import (
8+
_extend_docstrings,
9+
Compat32,
10+
compat32,
11+
Policy,
12+
validate_header
13+
)
814
from email.utils import _has_surrogates
915
from email.headerregistry import HeaderRegistry as HeaderRegistry
1016
from email.contentmanager import raw_data_manager

0 commit comments

Comments
 (0)