-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
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
Closed
nogueira-raphael
wants to merge
4
commits into
python:main
from
nogueira-raphael:bugfix/136134-skip-cram-md5-on-md5-error
Closed
gh-136134: Fallback to next auth method when CRAM-MD5 fails due to unsupported hash (e.g. FIPS) #136188
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b379ce4
"gh-136134: Fallback to next auth method when CRAM-MD5 fails due to u…
nogueira-raphael 73014d5
gh-136134: Add code comment explaining CRAM-MD5 fallback and NEWS entry
nogueira-raphael d2cc372
bpo-136134: Raise SMTPAuthHashUnsupportedError when HMAC fails in CRA…
nogueira-raphael ba5c991
bpo-136134: Clarify comment on handling unsupported hash in CRAM-MD5
nogueira-raphael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
from test.support import threading_helper | ||
from test.support import asyncore | ||
from test.support import smtpd | ||
from unittest.mock import Mock | ||
from unittest.mock import Mock, patch | ||
|
||
|
||
support.requires_working_socket(module=True) | ||
|
@@ -1570,5 +1570,60 @@ def testAUTH_PLAIN_initial_response_auth(self): | |
self.assertEqual(code, 235) | ||
|
||
|
||
class TestSMTPLoginValueError(unittest.TestCase): | ||
def broken_hmac(*args, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make it an external function, and mock the entire class instead. |
||
raise ValueError("[digital envelope routines] unsupported") | ||
|
||
def test_login_raises_valueerror_when_cram_md5_fails(self): | ||
with patch("hmac.HMAC", self.broken_hmac): | ||
class FakeSMTP(smtplib.SMTP): | ||
def __init__(self): | ||
super().__init__(host='', port=0) | ||
self.esmtp_features = {"auth": "CRAM-MD5"} | ||
self._host = "localhost" | ||
|
||
def ehlo_or_helo_if_needed(self): | ||
pass | ||
|
||
def has_extn(self, ext): | ||
return ext.lower() == "auth" | ||
|
||
def docmd(self, *args, **kwargs): | ||
# Retorna uma challenge base64 válida | ||
return 334, b"Y2hhbGxlbmdl" | ||
|
||
smtp = FakeSMTP() | ||
with self.assertRaises(ValueError) as ctx: | ||
smtp.login("user", "pass") | ||
self.assertIn("unsupported", str(ctx.exception).lower()) | ||
|
||
def test_login_fallbacks_when_cram_md5_raises_valueerror(self): | ||
with patch("hmac.HMAC", self.broken_hmac): | ||
class FakeSMTP(smtplib.SMTP): | ||
def __init__(self): | ||
super().__init__(host='', port=0) | ||
self.esmtp_features = {"auth": "CRAM-MD5 LOGIN"} | ||
self._host = "localhost" | ||
|
||
def ehlo_or_helo_if_needed(self): | ||
pass | ||
|
||
def has_extn(self, ext): | ||
return ext.lower() == "auth" | ||
|
||
def docmd(self, *args, **kwargs): | ||
if args[0] == "AUTH" and args[1].startswith("CRAM-MD5"): | ||
return 334, b"Y2hhbGxlbmdl" # base64('challenge') | ||
return 235, b"Authentication successful" | ||
|
||
def auth_login(self, challenge=None): | ||
return "login response" | ||
|
||
smtp = FakeSMTP() | ||
code, resp = smtp.login("user", "pass") | ||
self.assertEqual(code, 235) | ||
self.assertEqual(resp, b"Authentication successful") | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.