Skip to contentSkip to conversation

smtp_auth_maybe: improve smtp authentication error handling logic

PATCH Superseded Zheng Yuting 2 participants 4 messages Mar 7, 2025 – Mar 11, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover smtp_auth_maybe: improve smtp authentication error handling logic

This patch enhances error handling in the smtp_auth_maybe() function by distinguishing between temporary errors and permanent authentication failures.

- For temporary errors (e.g., timeout, network issues, etc.), the function logs a warning and returns 1, allowing retries.

- For permanent errors (e.g., invalid credentials), it returns 0.

Additionally, it separates handling for SASL and plain authentication to prevent rejecting valid credentials due to non-credential errors, improving robustness.

Zheng Yuting (1):
improve smtp authentication error handling logic

 git-send-email.perl | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

--
2.49.0.rc0.57.gdb91954e18

#1 lore raw

PATCH 1/1 Improve SMTP authentication error handling logic

---
 git-send-email.perl | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
git-send-email.perl +22 -8
index 798d59b84f..a012d61abb 100755
@@ -1419,19 +1419,19 @@ sub smtp_auth_maybe {
 		die "invalid smtp auth: '${smtp_auth}'";
 	}
 
-	# TODO: Authentication may fail not because credentials were
+	# Authentication may fail not because credentials were
 	# invalid but due to other reasons, in which we should not
 	# reject credentials.
 	$auth = Git::credential({
 		'protocol' => 'smtp',
 		'host' => smtp_host_string(),
 		'username' => $smtp_authuser,
-		# if there's no password, "git credential fill" will
-		# give us one, otherwise it'll just pass this one.
 		'password' => $smtp_authpass
+
 	}, sub {
 		my $cred = shift;
-
+		my $result;
+		my $error;
 		if ($smtp_auth) {
 			my $sasl = Authen::SASL->new(
 				mechanism => $smtp_auth,
@@ -1441,13 +1441,27 @@ sub smtp_auth_maybe {
 					authname => $cred->{'username'},
 				}
 			);
-
 			return !!$smtp->auth($sasl);
+		} else {
+			# Handle plain authentication errors
+			eval {
+				$result = $smtp->auth($cred->{'username'}, $cred->{'password'});
+				1; # Ensure true value is returned
+			} or do {
+				$error = $@ || 'Unknown error';
+			};
 		}
-
-		return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
+		# Unified error handling logic
+		if ($error) {
+			# Match temporary errors
+			if ($error =~ /timeout|temporary|greylist|throttled|quota\s+exceeded|queue|overload|try\s+again|connection\s+lost|network\s+error/i) {
+				warn "SMTP temporary error: $error";
+				return 1;
+			}
+			return 0;
+		}
+		return !!$result;
 	});
-
 	return $auth;
 }
 
Signature · 1 line
2.49.0.rc0.57.gdb91954e18
#2 lore raw

Re: [PATCH 1/1] Improve SMTP authentication error handling logic

Zheng Yuting <[email protected]> writes:

I see that you've added a cover message detailing the patch, but individual patches should include the relevant commit description and message. This is also outlined in 'Documentation/SubmittingPatches'.

That said my perl knowledge is limited, but I'll ask some questions anyways.

Quoted 12 lines
---
 git-send-email.perl | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
git-send-email.perl +0 -0
index 798d59b84f..a012d61abb 100755
@@ -1419,19 +1419,19 @@ sub/ smtp_auth_maybe {
 		die "invalid smtp auth: '${smtp_auth}'";
 	}
 

git-send-email.perl sub/ smtp_auth_maybe { · line 1430

Quoted 10 lines
git-send-email.perl +1 -3
-	# TODO: Authentication may fail not because credentials were
+	# Authentication may fail not because credentials were
 	# invalid but due to other reasons, in which we should not
 	# reject credentials.
 	$auth = Git::credential({
 		'protocol' => 'smtp',
 		'host' => smtp_host_string(),
 		'username' => $smtp_authuser,
-		# if there's no password, "git credential fill" will
-		# give us one, otherwise it'll just pass this one.

I didn't understand why these comments were removed.

git-send-email.perl sub smtp_auth_maybe { · line 1430

Quoted 2 lines
git-send-email.perl +1 -0
 		'password' => $smtp_authpass
+

Nit: unnecessary newline.

git-send-email.perl sub smtp_auth_maybe { · line 1434

Quoted 5 lines
git-send-email.perl +2 -1
 	}, sub {
 		my $cred = shift;
-
+		my $result;
+		my $error;

Nit: It is easier to read with a newline separating the variable declaration with the rest of the code.

git-send-email.perl sub smtp_auth_maybe { · line 1444

Quoted 8 lines
git-send-email.perl +0 -1
 		if ($smtp_auth) {
 			my $sasl = Authen::SASL->new(
 				mechanism => $smtp_auth,
@@ -1441,13 +1441,27 @@ sub smtp_auth_maybe {
 					authname => $cred->{'username'},
 				}
 			);
-

Nit: unnecessary newline removal

git-send-email.perl sub smtp_auth_maybe { · line 1444

Quoted 1 line
git-send-email.perl +0 -0
 			return !!$smtp->auth($sasl);

Shouldn't we do the same kind of changes for this smpt->auth() as the other below?

git-send-email.perl sub smtp_auth_maybe { · line 1452

Quoted 8 lines
git-send-email.perl +8 -0
+		} else {
+			# Handle plain authentication errors
+			eval {
+				$result = $smtp->auth($cred->{'username'}, $cred->{'password'});
+				1; # Ensure true value is returned
+			} or do {
+				$error = $@ || 'Unknown error';
+			};

Okay, so here we try to do the authentication and if there aren't any exceptions we return 1.

git-send-email.perl sub smtp_auth_maybe { · line 1457

Quoted 7 lines
git-send-email.perl +4 -2
 		}
-
-		return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
+		# Unified error handling logic
+		if ($error) {
+			# Match temporary errors
+			if ($error =~ /timeout|temporary|greylist|throttled|quota\s+exceeded|queue|overload|try\s+again|connection\s+lost|network\s+error/i) {

Where do we get this list from? Regex matching errors for the type doesn't seem like the best way to go about this.

git-send-email.perl sub smtp_auth_maybe { · line 1450

Quoted 8 lines
git-send-email.perl +6 -1
+				warn "SMTP temporary error: $error";
+				return 1;
+			}
+			return 0;
+		}
+		return !!$result;
 	});
-

Nit: unnecessary newline removal

Quoted 5 lines
git-send-email.perl +0 -1
 	return $auth;
 }
 
--

2.49.0.rc0.57.gdb91954e18

#3 lore raw

Re: [PATCH 1/1] Improve SMTP authentication error handling logic

Quoted 5 lines · Karthik Nayak

Zheng Yuting <[email protected]> writes:

I see that you've added a cover message detailing the patch, but individual patches should include the relevant commit description and message. This is also outlined in 'Documentation/SubmittingPatches'.

Thanks for pointing out my mistakes, I'm sorry to ignore the individual patch commits. I will add them in my next version.

Quoted 4 lines

That said my perl knowledge is limited, but I'll ask some questions anyways.

I am grateful for your support. As a new contributor, I’m still learning the best practices and will improve my process based on your feedback.

Quoted 5 lines
Quoted 2 lines

->--->---# if there's no password, "git credential fill" will ->--->---# give us one, otherwise it'll just pass this one.

I didn't understand why these comments were removed.

I removed those comments because I intended to use the password directly at that point. I understand the confusion this caused and will restore or clarify the comments in my next revision.

Quoted 5 lines
Quoted 2 lines
Quoted 1 line

--->---'password' => $smtp_authpass

+

Nit: unnecessary newline.

I’ll remove it.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

---}, sub {
--->---my $cred = shift;

-
+>--->---my $result;
+>--->---my $error;

Nit: It is easier to read with a newline separating the variable declaration with the rest of the code.

Quoted 7 lines

Review sub smtp_auth_maybe { · line 1444

Quoted 5 lines
(quoted diff) +0 -1
@@ -1441,13 +1441,27 @@ sub smtp_auth_maybe {
 >--->--->--->--->---authname => $cred->{'username'},
 >--->--->--->---}
 >--->--->---);
-

Nit: unnecessary newline removal

I will adjust these suggestions in the next version.

Quoted 6 lines

Review sub smtp_auth_maybe { · line 1444

Quoted 2 lines
(quoted diff) +0 -0
 >--->--->---return !!$smtp->auth($sasl);

Shouldn't we do the same kind of changes for this smpt->auth() as the other below?

I’ll review both instances of the smtp->auth() call and update them so they follow a consistent approach in error handling and formatting.

Quoted 7 lines

Review sub smtp_auth_maybe { · line 1447

Quoted 3 lines
(quoted diff) +3 -0
+>--->---if ($error) {
+>--->--->---# Match temporary errors
+>--->--->---if ($error =~ /timeout|temporary|greylist|throttled|quota\s+exceeded|queue|overload|try\s+again|connection\s+lost|network\s+error/i) {

Where do we get this list from? Regex matching errors for the type doesn't seem like the best way to go about this.

I agree with your assessment. Initially, I aimed to interpret the SMTP server's responses directly. But after considering your suggestion, I realized that I should use SMTP servers return standardized status codes for command outcomes according to RFC 5321 (4.2.2 Reply Codes by Function Groups). I will improve it.

Quoted 6 lines

Review sub smtp_auth_maybe { · line 1447

Quoted 3 lines
(quoted diff) +1 -1
+>--->---return !!$result;
 >---});
-

Nit: unnecessary newline removal

I will adjust these suggestions in the next version.

Quoted 104 lines · Karthik Nayak

Zheng Yuting <[email protected]> writes:

I see that you've added a cover message detailing the patch, but individual patches should include the relevant commit description and message. This is also outlined in 'Documentation/SubmittingPatches'.

That said my perl knowledge is limited, but I'll ask some questions anyways.

Quoted 12 lines
(quoted diff) +0 -1
---
 git-send-email.perl | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
 
git-send-email.perl +0 -0
index 798d59b84f..a012d61abb 100755
@@ -1419,19 +1419,19 @@ sub/ smtp_auth_maybe {
              die "invalid smtp auth: '${smtp_auth}'";
      }
 

git-send-email.perl sub/ smtp_auth_maybe { · line 1430

Quoted 10 lines
git-send-email.perl +1 -3
-     # TODO: Authentication may fail not because credentials were
+     # Authentication may fail not because credentials were
      # invalid but due to other reasons, in which we should not
      # reject credentials.
      $auth = Git::credential({
              'protocol' => 'smtp',
              'host' => smtp_host_string(),
              'username' => $smtp_authuser,
-             # if there's no password, "git credential fill" will
-             # give us one, otherwise it'll just pass this one.

I didn't understand why these comments were removed.

git-send-email.perl sub smtp_auth_maybe { · line 1430

Quoted 2 lines
git-send-email.perl +1 -0
              'password' => $smtp_authpass
+

Nit: unnecessary newline.

git-send-email.perl sub smtp_auth_maybe { · line 1434

Quoted 5 lines
git-send-email.perl +2 -1
      }, sub {
              my $cred = shift;
-
+             my $result;
+             my $error;

Nit: It is easier to read with a newline separating the variable declaration with the rest of the code.

git-send-email.perl sub smtp_auth_maybe { · line 1444

Quoted 8 lines
git-send-email.perl +0 -1
              if ($smtp_auth) {
                      my $sasl = Authen::SASL->new(
                              mechanism => $smtp_auth,
@@ -1441,13 +1441,27 @@ sub smtp_auth_maybe {
                                      authname => $cred->{'username'},
                              }
                      );
-

Nit: unnecessary newline removal

git-send-email.perl sub smtp_auth_maybe { · line 1444

Quoted 1 line
git-send-email.perl +0 -0
                      return !!$smtp->auth($sasl);

Shouldn't we do the same kind of changes for this smpt->auth() as the other below?

git-send-email.perl sub smtp_auth_maybe { · line 1452

Quoted 8 lines
git-send-email.perl +8 -0
+             } else {
+                     # Handle plain authentication errors
+                     eval {
+                             $result = $smtp->auth($cred->{'username'}, $cred->{'password'});
+                             1; # Ensure true value is returned
+                     } or do {
+                             $error = $@ || 'Unknown error';
+                     };

Okay, so here we try to do the authentication and if there aren't any exceptions we return 1.

git-send-email.perl sub smtp_auth_maybe { · line 1457

Quoted 7 lines
git-send-email.perl +4 -2
              }
-
-             return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
+             # Unified error handling logic
+             if ($error) {
+                     # Match temporary errors
+                     if ($error =~ /timeout|temporary|greylist|throttled|quota\s+exceeded|queue|overload|try\s+again|connection\s+lost|network\s+error/i) {

Where do we get this list from? Regex matching errors for the type doesn't seem like the best way to go about this.

git-send-email.perl sub smtp_auth_maybe { · line 1450

Quoted 8 lines
git-send-email.perl +6 -1
+                             warn "SMTP temporary error: $error";
+                             return 1;
+                     }
+                     return 0;
+             }
+             return !!$result;
      });
-

Nit: unnecessary newline removal

Quoted 5 lines
git-send-email.perl +0 -1
      return $auth;
 }
 
--

2.49.0.rc0.57.gdb91954e18