Skip to content

The check_dmarc method can fail to find a From: domain #32

@msapiro

Description

@msapiro

A message with a From: header like

From: "Txxxxxx Mxxxxxxx de Vxxxxxx, Fxxxxxxxx" <fxxxxxxxx.txxxxxx@subd.example.net>

when flattened as_bytes and passed to check_dmark will have the header folded as

From:  "Txxxxxx Mxxxxxxx de Vxxxxxx,
  Fxxxxxxxx" <fxxxxxxxx.txxxxxx@subd.example.net>

While this is reported as a bug at python/cpython#114362, it is compliant with RFC 5322 which allows FWS within a quoted string.
In any case the code acting on this does the following:

headers, _ = rfc822_parse(msg)

which returns one of the elements in the headers list as

[b'From', b' "Txxxxxx Mxxxxxxx de Vxxxxxx,\r\n Fxxxxxxxx" <fxxxxxxxx.txxxxxx@subd.example.net>\r\n']

and then

from_headers = [a[1] for a in getaddresses(x[1].decode(errors='ignore').strip() for x in headers if x[0].lower() == b"from")]

yields

['Txxxxxx Mxxxxxxx de Vxxxxxx,']

as from_headers.

One way to fix this is to change

from_headers = [a[1] for a in getaddresses(x[1].decode(errors='ignore').strip() for x in headers if x[0].lower() == b"from")]

to

from_headers = [a[1] for a in getaddresses(x[1].replace(b'\r\n', b'')
    .decode(errors='ignore').strip() for x in headers
    if x[0].lower() == b"from")]

to unfold the header value before passing it to getaddresses. Normally getaddresses handles folded headers, but I think folding with \r\n is an issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions