Skip to content

Commit 1f81870

Browse files
authored
Merge pull request #160 from nobu/digest_md5-bad-challenge
Fix for Digest MD5 bad challenges
2 parents 16dafde + 77ed8e8 commit 1f81870

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/net/imap/authenticators/digest_md5.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def process(challenge)
1515
@stage = STAGE_TWO
1616
sparams = {}
1717
c = StringScanner.new(challenge)
18-
while c.scan(/(?:\s*,)?\s*(\w+)=("(?:[^\\"]+|\\.)*"|[^,]+)\s*/)
18+
while c.scan(/(?:\s*,)?\s*(\w+)=("(?:[^\\"]|\\.)*"|[^,]+)\s*/)
1919
k, v = c[1], c[2]
2020
if v =~ /^"(.*)"$/
2121
v = $1
@@ -26,7 +26,7 @@ def process(challenge)
2626
sparams[k] = v
2727
end
2828

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

3232
response = {

test/net/imap/test_imap_authenticators.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,28 @@ def test_digest_md5_authenticator
139139
)
140140
)
141141
end
142+
143+
def test_digest_md5_authenticator_garbage
144+
auth = digest_md5("user", "pass")
145+
assert_raise(Net::IMAP::DataFormatError) do
146+
auth.process('.')
147+
end
148+
end
149+
150+
def test_digest_md5_authenticator_no_qop
151+
auth = digest_md5("user", "pass")
152+
assert_raise(Net::IMAP::DataFormatError) do
153+
auth.process('Qop=""')
154+
end
155+
end
156+
157+
def test_digest_md5_authenticator_illinear
158+
pre = ->(n) {'qop="a' + ',x'*n}
159+
assert_linear_performance([5, 10, 15, 20], pre: pre) do |challenge|
160+
auth = digest_md5("user", "pass")
161+
assert_raise(Net::IMAP::DataFormatError) do
162+
auth.process(challenge)
163+
end
164+
end
165+
end
142166
end

0 commit comments

Comments
 (0)