Skip to content

gh-136134: Fallback to next auth method when CRAM-MD5 fails due to unsupported hash (e.g. FIPS) #136188

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

nogueira-raphael
Copy link

This change improves the smtplib.SMTP.login() method to handle the case where CRAM-MD5 fails due to missing or unsupported MD5 digest (e.g. in FIPS-compliant environments). Instead of crashing with a ValueError, the client now skips CRAM-MD5 and tries the next available mechanism.

A regression test has been added to verify this fallback behavior.

Closes: gh-136134

@nogueira-raphael nogueira-raphael requested a review from a team as a code owner July 1, 2025 20:12
@python-cla-bot
Copy link

python-cla-bot bot commented Jul 1, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Jul 1, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Lib/smtplib.py Outdated
@@ -743,6 +743,10 @@ def login(self, user, password, *, initial_response_ok=True):
return (code, resp)
except SMTPAuthenticationError as e:
last_exception = e
except ValueError as e:
last_exception = e
if 'unsupported' in str(e).lower():

Choose a reason for hiding this comment

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

why do we care about this specific error? shouldn't the next method be tried regardless of the nature of the error?

Copy link
Author

Choose a reason for hiding this comment

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

In general, it makes sense to fallback regardless of the error. However, in this case, we handle this specific ValueError to deal with environments where FIPS mode is enabled, which disables certain hashing algorithms like MD5.

In FIPS mode, CRAM-MD5 will raise a ValueError from the underlying OpenSSL implementation (e.g. [digital envelope routines] unsupported). Since this isn't a misconfiguration or a generic programming error, but rather an expected, reproducible environment-specific issue, we explicitly catch it and fallback silently to the next available method (e.g. LOGIN).

Choose a reason for hiding this comment

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

I see your point. I had seen the error only on Linux container with a specific version of openssl. My worry is that the fix will be rendered ineffective if openssl changes the error string or returns a different error on a different OS/platform.

Copy link
Author

Choose a reason for hiding this comment

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

I don't think so, we are saving the error, and try another method, if none of them resolve, we will return the latest error.
However, I also don't like this kind of fix, I didn't found another better way to solve this.
And the error is not only related with that docker image.

Copy link
Member

Choose a reason for hiding this comment

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

We should do it differently. Relying on the content of an exception message is a bad idea as it can evolve in the future. Instead, locate where we call HMAC and wrap that call, then raise an appropriate exception and catch that one instead.

If the call is too deep in the stack we should find another way.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion @picnixz, I’ve applied the proposed approach by wrapping the HMAC call directly and raising a dedicated SMTPAuthHashUnsupportedError instead of relying on the exception message. I agree with this approach.

If there’s anything you’d like to adjust in naming or behavior, I’ll be happy to apply it.

…M-MD5

Wraps the HMAC call in auth_cram_md5 and raises a dedicated exception
to avoid relying on error message content. Also updates the relevant tests.
Lib/smtplib.py Outdated
@@ -743,13 +750,12 @@ def login(self, user, password, *, initial_response_ok=True):
return (code, resp)
except SMTPAuthenticationError as e:
last_exception = e
except ValueError as e:
except SMTPAuthHashUnsupportedError as e:
# Some environments (e.g., FIPS) disable certain hashing algorithms like MD5,
# which are required by CRAM-MD5. This raises a ValueError when trying to use HMAC.

Choose a reason for hiding this comment

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

name of the exception in the comment needs update

Copy link
Author

Choose a reason for hiding this comment

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

Good catch, tks
I’ve updated the comment to reflect the correct exception name

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

I would like to review it in 10 days when I am on my latpop. Meanwhile I can only offer simple comments

return self.user + " " + hmac.HMAC(
self.password.encode('ascii'), challenge, 'md5').hexdigest()
except ValueError as e:
raise SMTPAuthHashUnsupportedError(f'CRAM-MD5 failed: {e}') from e
Copy link
Member

Choose a reason for hiding this comment

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

Separate all calls and only wrap the hmac.HMAC call in a try-catch. TiA

@@ -1570,5 +1570,60 @@ def testAUTH_PLAIN_initial_response_auth(self):
self.assertEqual(code, 235)


class TestSMTPLoginValueError(unittest.TestCase):
def broken_hmac(*args, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

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

Make it an external function, and mock the entire class instead.

@@ -743,6 +750,12 @@ def login(self, user, password, *, initial_response_ok=True):
return (code, resp)
except SMTPAuthenticationError as e:
last_exception = e
except SMTPAuthHashUnsupportedError as e:
# Some environments (e.g., FIPS) disable certain hashing algorithms like MD5,
Copy link
Member

Choose a reason for hiding this comment

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

This comment is too verbose. Just write something like "skip unsupported HMAC-HASH algorithms"

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

Successfully merging this pull request may close these issues.

SMTP.login() method fails with ValueError in environments that do not support MD5
3 participants