Skip to content

Fix crash in parsing received hops chain#37

Open
ulidtko wants to merge 2 commits into
mgeeky:mainfrom
ulidtko:fix/received-hops-crash
Open

Fix crash in parsing received hops chain#37
ulidtko wants to merge 2 commits into
mgeeky:mainfrom
ulidtko:fix/received-hops-crash

Conversation

@ulidtko

@ulidtko ulidtko commented Dec 16, 2025

Copy link
Copy Markdown

Stumbled into a case where packaging.version.parse gets fed with "ffacd0b85a97d-42f7cbd8c20si11690432f8f.269.2025.12.09.06.21.40" and so crashes with InvalidVersion exception.

That ID is clearly not a version string. I also don't see why it ever would be. 🤔

o The ID clause MAY contain an "@" as suggested in RFC 822, but this is not required.

-- https://www.rfc-editor.org/rfc/rfc5321#section-4.4

The ID clause in Received: header -- is supposed to be mta-local "message id"; it's not intended to contain versions. In practice, I see it taking values such as:

  • ls17csp2524806jab,
  • 6a1803df08f44-888284b1477si197598466d6.457.2025.12.09.06.21.46,
  • A0F173EFD3,
  • ffacd0b85a97d-42f89f48092mr11828940f8f.31.1765290101044,
  • etc.

No idea why the code hopes to find a version string in there; I just stubbed it out so that a None is returned instead of exception, and the report then actually shows the received server hops chain.

Example crash:

> Traceback (most recent call last):
>   File "/tmp/decode-spam-headers.py", line 6925, in <module>
>     main(sys.argv)
>     ~~~~^^^^^^^^^^
>   File "/tmp/decode-spam-headers.py", line 6905, in main
>     out = an.parse(text)
>   File "/tmp/decode-spam-headers.py", line 2285, in parse
>     self.results[testName] = testFunc()
>                              ~~~~~~~~^^
>   File "/tmp/decode-spam-headers.py", line 5209, in testReceived
>     vers = SMTPHeadersAnalysis.parseExchangeVersion(obj['ver'])
>   File "/tmp/decode-spam-headers.py", line 2107, in parseExchangeVersion
>     lookupparsed = packaging.version.parse(lookup)
>   File "/Users/maksym.ivanov/Library/Python/3.14/lib/python/site-packages/packaging/version.py", line 56, in parse
>     return Version(version)
>   File "/Users/maksym.ivanov/Library/Python/3.14/lib/python/site-packages/packaging/version.py", line 202, in __init__
>     raise InvalidVersion(f"Invalid version: {version!r}")
> packaging.version.InvalidVersion: Invalid version: 'ffacd0b85a97d-42f7cbd8c20si11690432f8f.269.2025.12.09.06.21.40'

In Recieved: headers, value under `id` is not necessarily formatted as a version string.
ulidtko added a commit to ulidtko/decode-spam-headers that referenced this pull request Dec 16, 2025
Cherry-pick of open PR mgeeky#37
(squashed)
Comment thread decode-spam-headers.py
Comment on lines +2109 to +2111
try:
lookupparsed = packaging.version.parse(lookup)
except packaging.version.InvalidVersion:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the crux of the fix. 👀 Everything else -- drive-by cleanups / refactors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant