Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReceivedHeader: incorrectly detects the IP address #152

Closed
mariuszkrzaczkowski opened this issue Nov 30, 2020 · 8 comments
Closed

ReceivedHeader: incorrectly detects the IP address #152

mariuszkrzaczkowski opened this issue Nov 30, 2020 · 8 comments

Comments

@mariuszkrzaczkowski
Copy link
Contributor

IP discovery only works when the address is at the end, not as it is at the beginning
an example for which it works

Received: from web.live.com ([124.236.173.16] helo=mail.live.com)

an example for which it doesn't work

Received: from web.live.com (helo=mail.live.com [124.236.173.16])

the problem is in regex

private function matchHostPart($value, &$hostname, &$address) {
$matches = [];
$pattern = '~^(?P<name>[a-z0-9\-]+\.[a-z0-9\-\.]+)?\s*(\[(IPv[64])?(?P<addr>[a-f\d\.\:]+)\])?$~i';
if (preg_match($pattern, $value, $matches)) {
if (!empty($matches['name'])) {
$hostname = $matches['name'];
}
if (!empty($matches['addr'])) {
$address = $matches['addr'];
}
return true;
}
return false;

mariuszkrzaczkowski added a commit to mariuszkrzaczkowski/mail-mime-parser that referenced this issue Nov 30, 2020
ReceivedHeader: incorrectly detects the IP address zbateson#152
@mariuszkrzaczkowski
Copy link
Contributor Author

mariuszkrzaczkowski commented Nov 30, 2020

I proposed a solution to this problem in mariuszkrzaczkowski@882f89b

#153

@zbateson
Copy link
Owner

zbateson commented Nov 30, 2020

Hi @mariuszkrzaczkowski --

Unfortunately with the Received header it's a losing battle trying to parse every possibility. Please read through my thoughts on this issue: #78

The trouble is implementations for this header are all over the place, and besides covering the very basics of what the header is defined as in RFC 5321 section 4.4, I'm not convinced this library should attempt to parse much outside of that definition. In this case, the relevant part is:

From-domain    = "FROM" FWS Extended-Domain
Extended-Domain  = Domain /
                    ( Domain FWS "(" TCP-info ")" ) /
                    ( address-literal FWS "(" TCP-info ")" )

The trouble with your example is the 'helo=' part in the address, which is not valid according to that.

For example this test is testing exactly that, the domain part in front of the address:

$header = new ReceivedHeader($this->consumerService, 'Received', 'FROM JonSnow (domain.com [1.2.3.4]) (Crow Crow)');

@mariuszkrzaczkowski
Copy link
Contributor Author

Do you have any idea to handle it universally? if others are doing it, it may be worth fighting. I can help

@mariuszkrzaczkowski
Copy link
Contributor Author

we can add experimental or compliance mode or not and then try to handle such different cases in an additional mode

@mariuszkrzaczkowski
Copy link
Contributor Author

I checked the last few messages and a lot is from IP at the beginning
image

it's from big postal providers google, outlook

@zbateson
Copy link
Owner

zbateson commented Dec 1, 2020

My feeling on this is that I don't want mail-mime-parser chasing unstandardized formats -- think this or something like finding the 'reply' part of an email. There's just too much variation for a library like this, and it's not what I think it should be.

I'd encourage this to be outside of mail-mime-parser as an add-on for instance or something. Unfortunately at the moment mmp isn't very extensible, but I'm working on fixing that in a 2.0 so something like this could be created on the side and plugged in if someone wanted it... it's very slow going as I'm going through a busy period (fatherhood) and so haven't had much time to dedicate unfortunately.

@mariuszkrzaczkowski
Copy link
Contributor Author

only how many servers are standards compliant ??? this is meant to be used and must be able to deal with every e-mail present only then the library can be used everywhere, this is my opinion

@zbateson
Copy link
Owner

zbateson commented Dec 1, 2020

The variation on Received headers is to the point that the format is 'unstandardized' imo, not that there are widely-used bad implementations. I'm happy to support mistakes in implementation so long as they're widely used and can't be fixed at the source.

Implementing support for Received (seems to me) to be an exercise in how much to support -- you can always find a new variation and need additional code added for it. I don't think that belongs here.

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

No branches or pull requests

2 participants