Skip to content

Commit 81bc4d3

Browse files
committed
🥅 Cancel AUTHENTICATE if process raises an error
The exception will be re-raised after the protocol cancel response has been sent.
1 parent 4ba5f94 commit 81bc4d3

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

lib/net/imap/sasl/authentication_exchange.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ module SASL
66

77
# This API is *experimental*, and may change.
88
#
9-
# TODO: catch exceptions in #process and send #cancel_response.
10-
# TODO: raise an error if the command succeeds after being canceled.
119
# TODO: use with more clients, to verify the API can accommodate them.
1210
#
1311
# An AuthenticationExchange represents a single attempt to authenticate
@@ -79,6 +77,9 @@ def self.build(client, mechanism, *args, sasl_ir: true, **kwargs, &block)
7977

8078
attr_reader :mechanism, :authenticator
8179

80+
# An exception that has been raised by <tt>authenticator.process</tt>.
81+
attr_reader :process_error
82+
8283
def initialize(client, mechanism, authenticator, sasl_ir: true)
8384
client => SASL::ClientAdapter
8485
@client = client
@@ -94,6 +95,7 @@ def initialize(client, mechanism, authenticator, sasl_ir: true)
9495
# in <tt>client.response_errors</tt> will drop the connection.
9596
def authenticate
9697
client.run_command(mechanism, initial_response) { process _1 }
98+
.tap { raise process_error if process_error }
9799
.tap { raise AuthenticationIncomplete, _1 unless done? }
98100
rescue AuthenticationCanceled, *client.response_errors
99101
raise # but don't drop the connection
@@ -127,9 +129,12 @@ def initial_response
127129
end
128130

129131
def process(challenge)
130-
client.encode authenticator.process client.decode challenge
131-
ensure
132132
@processed = true
133+
return client.cancel_response if process_error
134+
client.encode authenticator.process client.decode challenge
135+
rescue => process_error
136+
@process_error = process_error
137+
client.cancel_response
133138
end
134139

135140
end

0 commit comments

Comments
 (0)