diff options
Diffstat (limited to 'Lib/email/_policybase.py')
-rw-r--r-- | Lib/email/_policybase.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/email/_policybase.py b/Lib/email/_policybase.py index 4b63b97..95e79b8 100644 --- a/Lib/email/_policybase.py +++ b/Lib/email/_policybase.py @@ -4,6 +4,7 @@ Allows fine grained feature control of how the package parses and emits data. """ import abc +import re from email import header from email import charset as _charset from email.utils import _has_surrogates @@ -14,6 +15,14 @@ __all__ = [ 'compat32', ] +# validation regex from RFC 5322, equivalent to pattern re.compile("[!-9;-~]+$") +valid_header_name_re = re.compile("[\041-\071\073-\176]+$") + +def validate_header_name(name): + # Validate header name according to RFC 5322 + if not valid_header_name_re.match(name): + raise ValueError( + f"Header field name contains invalid characters: {name!r}") class _PolicyBase: @@ -314,6 +323,7 @@ class Compat32(Policy): """+ The name and value are returned unmodified. """ + validate_header_name(name) return (name, value) def header_fetch_parse(self, name, value): |