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

Emails sent multiple times to the same address can be rejected #4543

Closed
TheWitness opened this issue Feb 4, 2022 · 10 comments
Closed

Emails sent multiple times to the same address can be rejected #4543

TheWitness opened this issue Feb 4, 2022 · 10 comments
Labels
bug Undesired behaviour resolved A fixed issue
Milestone

Comments

@TheWitness
Copy link
Member

Describe the bug

We recently got into a mailer issue the other day where we had the same Email in a Notification list, and the same Email in a notification_extra using an upper case email. When calling the mailer with this condition, the upstream mail services rejected the Email since it had duplicated Email addresses.

EMAIL@domain.com,email@domain.com

That example was rejected upstream.

Expected behavior

Cacti should per-process the Email list, convert the Emails to lower case, and de-duplicate them prior to sending to the upstream mail service.

@TheWitness TheWitness added the bug Undesired behaviour label Feb 4, 2022
@TheWitness TheWitness added this to the v1.2.20 milestone Feb 4, 2022
@netniV
Copy link
Member

netniV commented Feb 4, 2022

Technically, we can't de-dupe that situation. Both are valid independent email addresses according to the RFC.

@TheWitness
Copy link
Member Author

Ouch. Okay, maybe we issue a warning then? I was able to cleanup the issue (In thold), and I plan on disabling legacy notifications.

strtolower($mail) == strtolower($other_email)

@netniV
Copy link
Member

netniV commented Feb 4, 2022

Taken from https://stackoverflow.com/a/9808332/697159

From RFC 5321, section 2.3.11:

The standard mailbox naming convention is defined to be
"local-part@domain"; contemporary usage permits a much broader set of
applications than simple "user names". Consequently, and due to a
long history of problems when intermediate hosts have attempted to
optimize transport by modifying them, the local-part MUST be
interpreted and assigned semantics only by the host specified in the
domain part of the address.

So yes, the part before the "@" could be case-sensitive, since it is entirely under the control of the host system. In practice though, no widely used mail systems distinguish different addresses based on case.

The part after the @ sign however is the domain and according to RFC 1035, section 3.1,

"Name servers and resolvers must compare [domains] in a case-insensitive manner"

In short, you are safe to treat email addresses as case-insensitive.

@netniV
Copy link
Member

netniV commented Feb 4, 2022

Maybe we ignore the RFC on that one since most systems do these days. But we need to make sure it's documented.

@TheWitness
Copy link
Member Author

I think we should. It's a minor thing. Looking at the code parse_email_details(), could de-duplicate there, just have to be careful. Maybe make the strlower($email) the index of the array.

@TheWitness
Copy link
Member Author

How does this look?

diff --git a/lib/functions.php b/lib/functions.php
index 08b383488..447020ccd 100644
--- a/lib/functions.php
+++ b/lib/functions.php
@@ -4728,7 +4728,7 @@ function parse_email_details($emails, $max_records = 0, $details = array()) {

                                foreach($emails as $email) {
                                        $email_array = split_emaildetail($email);
-                                       $details[] = $email_array;
+                                       $details[$email_array['email']] = $email_array;
                                }
                        } else {
                                $has_name  = array_key_exists('name', $check_email);
@@ -4742,13 +4742,14 @@ function parse_email_details($emails, $max_records = 0, $details = array()) {
                                        $email = array_key_exists(0, $check_email) ? $check_email[0] : '';
                                }

-                               $details[] = array('name' => trim($name), 'email' => trim($email));
+                               $details[trim(strtolower($email))] = array('name' => trim($name), 'email' => trim(strtolower($email)));
                        }
                }
        }

        if ($max_records == 1) {
-               $results = count($details) ? $details[0] : array();
+               $index = array_keys($details)[0];
+               $results = count($details) ? $details[$index] : array();
        } elseif ($max_records != 0 && $max_records < count($details)) {
                $results = array();
                foreach ($details as $d) {
@@ -4787,7 +4788,7 @@ function split_emaildetail($email) {
                $rname = $email[1];
        }

-       return array('name' => $rname, 'email' => $rmail);
+       return array('name' => $rname, 'email' => strtolower($rmail));
 }

 function create_emailtext($e) {

@netniV
Copy link
Member

netniV commented Feb 4, 2022

I would personally just use reset($details) to get the first item. Saves messing around getting the keys to get the first element.

@netniV
Copy link
Member

netniV commented Feb 4, 2022

Also, I would only set the element once, but report duplicates if there is a difference in the values? For example, if you have the same email but different names....

@TheWitness
Copy link
Member Author

Interesting. Let me think about it.

TheWitness added a commit that referenced this issue Feb 5, 2022
- Some Email systems reject sending email if emails are duplicated in a send request. Cacti should deduplicate them
- I did not fix the duplicate name issue.  We can put that into the backlog as a new issue.
- Used reset() to shortcut getting the first value
- All Emails converted to lower case regardless of RFC as there is some debate there.
@TheWitness TheWitness added the resolved A fixed issue label Feb 5, 2022
@TheWitness
Copy link
Member Author

Okay, used reset, but not detecting the duplicate name for now. We can put that into the backlog.

@netniV netniV changed the title Some Email systems reject sending email if emails are duplicated in a send request. Cacti should deduplicate them Emails sent multiple times to the same address can be rejected Apr 3, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Dec 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Undesired behaviour resolved A fixed issue
Projects
None yet
Development

No branches or pull requests

2 participants