Skip to content

Commit b8c1025

Browse files
committed
🥅 Cancel AUTHENTICATE if process raises an error [🚧 server error]
The exception will be re-raised after the protocol cancel response has been sent.
1 parent 4099dfb commit b8c1025

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

lib/net/imap/sasl/authentication_exchange.rb

+17-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module SASL
77
# AuthenticationExchange is used internally by Net::IMAP#authenticate.
88
# But the API is still *experimental*, and may change.
99
#
10-
# TODO: catch exceptions in #process and send #cancel_response.
11-
# TODO: raise an error if the command succeeds after being canceled.
1210
# TODO: use with more clients, to verify the API can accommodate them.
1311
# TODO: pass ClientAdapter#service to SASL.authenticator
1412
#
@@ -81,6 +79,9 @@ def self.build(client, mechanism, *args, sasl_ir: true, **kwargs, &block)
8179

8280
attr_reader :mechanism, :authenticator
8381

82+
# An exception that has been raised by <tt>authenticator.process</tt>.
83+
attr_reader :process_error
84+
8485
def initialize(client, mechanism, authenticator, sasl_ir: true)
8586
@client = client
8687
@mechanism = Authenticators.normalize_name(mechanism)
@@ -93,8 +94,17 @@ def initialize(client, mechanism, authenticator, sasl_ir: true)
9394
# using #authenticator. Authentication failures will raise an
9495
# exception. Any exceptions other than AuthenticationCanceled or those
9596
# in <tt>client.response_errors</tt> will drop the connection.
97+
#
98+
# When <tt>authenticator.process</tt> raises any StandardError
99+
# (including AuthenticationCanceled), the authentication exchange will
100+
# be canceled before re-raising the exception. The server will usually
101+
# respond with an error response, and the client will most likely raise
102+
# that error. This client error will supercede the original error.
103+
# Unfortunately, the original error will not be the +#cause+ for the
104+
# client error. But it will be available on #process_error.
96105
def authenticate
97106
client.run_command(mechanism, initial_response) { process _1 }
107+
.tap { raise process_error if process_error }
98108
.tap { raise AuthenticationIncomplete, _1 unless done? }
99109
rescue AuthenticationCanceled, *client.response_errors
100110
raise # but don't drop the connection
@@ -128,9 +138,12 @@ def initial_response
128138
end
129139

130140
def process(challenge)
131-
client.encode authenticator.process client.decode challenge
132-
ensure
133141
@processed = true
142+
return client.cancel_response if process_error
143+
client.encode authenticator.process client.decode challenge
144+
rescue => process_error
145+
@process_error = process_error
146+
client.cancel_response
134147
end
135148

136149
end

test/net/imap/fake_server/command_router.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ def handler_for(command)
9797
response_b64 = resp.request_continuation("") || ""
9898
state.commands << {continuation: response_b64}
9999
end
100-
response = Base64.decode64(response_b64)
101-
response.empty? and return resp.fail_bad "canceled"
100+
response_b64.strip == ?* and return resp.fail_bad "canceled"
101+
response = Base64.decode64(response_b64) rescue :decode64_failed
102+
response == :decode64_failed and return resp.fail_bad "invalid b64"
102103
# TODO: support mechanisms other than PLAIN.
103104
parts = response.split("\0")
104105
parts.length == 3 or return resp.fail_bad "invalid"

test/net/imap/test_imap.rb

+19
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,25 @@ def test_id
10731073
end
10741074
end
10751075

1076+
test "#authenticate can be canceled with SASL::AuthenticationCanceled" do
1077+
with_fake_server(
1078+
preauth: false, cleartext_auth: true,
1079+
sasl_ir: false, sasl_mechanisms: %i[PLAIN]
1080+
) do |server, imap|
1081+
registry = Net::IMAP::SASL::Authenticators.new(use_defaults: false)
1082+
registry.add_authenticator :plain, ->(*a, **kw, &b) {
1083+
->(challenge) {
1084+
raise(Net::IMAP::SASL::AuthenticationCanceled,
1085+
"a: %p, kw: %p, b: %p" % [a, kw, b])
1086+
}
1087+
}
1088+
assert_raise_with_message(Net::IMAP::BadResponseError, "canceled") do
1089+
imap.authenticate(:plain, hello: :world, registry: registry)
1090+
end
1091+
refute imap.disconnected?
1092+
end
1093+
end
1094+
10761095
test "#uid_expunge with EXPUNGE responses" do
10771096
with_fake_server(select: "INBOX",
10781097
extensions: %i[UIDPLUS]) do |server, imap|

0 commit comments

Comments
 (0)