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

🔒️ Backport #160: Fix for Digest MD5 bad challenges #161

Merged
merged 4 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/net/imap/authenticators/digest_md5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def process(challenge)
@stage = STAGE_TWO
sparams = {}
c = StringScanner.new(challenge)
while c.scan(/(?:\s*,)?\s*(\w+)=("(?:[^\\"]+|\\.)*"|[^,]+)\s*/)
while c.scan(/(?:\s*,)?\s*(\w+)=("(?:[^\\"]|\\.)*"|[^,]+)\s*/)
k, v = c[1], c[2]
if v =~ /^"(.*)"$/
v = $1
Expand All @@ -26,7 +26,7 @@ def process(challenge)
sparams[k] = v
end

raise Net::IMAP::DataFormatError, "Bad Challenge: '#{challenge}'" unless c.eos?
raise Net::IMAP::DataFormatError, "Bad Challenge: '#{challenge}'" unless c.eos? and sparams['qop']
raise Net::IMAP::Error, "Server does not support auth (qop = #{sparams['qop'].join(',')})" unless sparams['qop'].include?("auth")

response = {
Expand Down
25 changes: 25 additions & 0 deletions test/net/imap/test_imap_authenticators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,29 @@ def test_digest_md5_authenticator
)
)
end

def test_digest_md5_authenticator_garbage
auth = digest_md5("user", "pass")
assert_raise(Net::IMAP::DataFormatError) do
auth.process('.')
end
end

def test_digest_md5_authenticator_no_qop
auth = digest_md5("user", "pass")
assert_raise(Net::IMAP::DataFormatError) do
auth.process('Qop=""')
end
end

def test_digest_md5_authenticator_illinear
pend "Need to backport #151 for assert_linear_performance"
pre = ->(n) {'qop="a' + ',x'*n}
assert_linear_performance([5, 10, 15, 20], pre: pre) do |challenge|
auth = digest_md5("user", "pass")
assert_raise(Net::IMAP::DataFormatError) do
auth.process(challenge)
end
end
end
end