fix: feature-detect getaddresses(strict=) so address parsing works on Python < 3.13#161
Open
apoorvdarshan wants to merge 1 commit into
Open
Conversation
… 3.13
email.utils.getaddresses only gained the ``strict`` keyword in Python 3.13
(backported to later 3.9-3.12 security patch releases). mail-parser targets
``requires-python >=3.9,<3.15``, so on an earlier patch release (e.g. CPython
3.11.3) get_addresses raised:
TypeError: getaddresses() got an unexpected keyword argument 'strict'
which made downstream callers (e.g. parsedmarc) treat valid messages as
invalid. Detect whether ``strict`` is supported via inspect.signature and only
pass it when available, falling back to the default call otherwise.
Adds a regression test that simulates a pre-3.13 getaddresses and asserts the
address is parsed instead of crashing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mailparser.utils.get_addressesunconditionally callsemail.utils.getaddresses([raw], strict=True). Thestrictkeyword onlyexists on Python 3.13+ (and later 3.9-3.12 security patch releases). Since
mail-parser targets
requires-python >=3.9,<3.15, on an earlier patch releasethis raises a
TypeErrorand breaks address parsing for every message.Reported downstream in domainaware/parsedmarc#808 ("getmessages fails with
invalid parameter 'strict'"), where valid messages were consequently moved to
an
archived/invalidfolder instead of being processed.Root cause
src/mailparser/utils.py(previously line 137):strictwas added toemail.utils.getaddressesin CPython 3.13 and backportedonly to later 3.9-3.12 security patch releases (e.g. 3.11.10). On an earlier
interpreter such as CPython 3.11.3 the parameter does not exist, so passing it
raises:
Reproduction
On an interpreter whose
email.utils.getaddressespredatesstrict(simulatedhere by patching in the pre-3.13 signature):
Actual (before the fix):
Corrected (after the fix):
Fix
Feature-detect the
strictparameter before passing it, in a small helper_getaddresses:The change is localized to
get_addresses: it preserves strict, CVE-2023-27043hardened parsing on interpreters that support it (the existing empty-address
regex fallback is unchanged) and only omits the keyword on older interpreters
where it is unavailable. No behavioral change on Python 3.13+.
Tests
Added
tests/test_utils.py::TestUtilsEdgeCases::test_get_addresses_without_strict_parameter,which patches in a pre-3.13
getaddresses(a real function withoutstrict,so the feature detection observes the older signature) and asserts the address
is parsed rather than crashing.
TypeErroratsrc/mailparser/utils.py:137.211 passed(python -m pytest tests/).ruff checkandruff format --checkare clean on the changed files.Disclosure: prepared with AI assistance; reviewed and verified locally.