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

Fix for receiving untagged response during SASL auth #743

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 6 additions & 8 deletions Sources/NIOIMAP/Client/AuthenticationStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension ClientStateMachine {
case finished
}

private var state: State = .waitingForServer
private(set) var state: State = .waitingForServer

mutating func receiveResponse(_ response: Response) throws {
switch self.state {
Expand Down Expand Up @@ -62,10 +62,13 @@ extension ClientStateMachine {
// the function below.

switch response {
case .untagged, .fetch, .fatal, .idleStarted, .authenticationChallenge:
case .fetch, .fatal, .idleStarted, .authenticationChallenge:
throw UnexpectedResponse(activePromise: nil)
case .untagged:
// Ignore
break
case .tagged:
try self.handleTaggedResponse()
self.state = .finished
}
}

Expand All @@ -80,11 +83,6 @@ extension ClientStateMachine {
self.state = .waitingForChallengeResponse
}

// we don't care about the specific response
private mutating func handleTaggedResponse() throws {
self.state = .finished
}

mutating func sendCommand(_ command: CommandStreamPart) {
switch self.state {
case .finished, .waitingForServer:
Expand Down
16 changes: 16 additions & 0 deletions Tests/NIOIMAPTests/Client/AuthenticationStateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,25 @@ class AuthenticationStateMachineTests: XCTestCase {
stateMachine.sendCommand(.continuationResponse("response2"))
XCTAssertNoThrow(try stateMachine.receiveContinuationRequest(.data("challenge3")))
stateMachine.sendCommand(.continuationResponse("response3"))
XCTAssertEqual(stateMachine.state, .waitingForServer)

// finish
XCTAssertNoThrow(try stateMachine.receiveResponse(.tagged(.init(tag: "A1", state: .ok(.init(code: nil, text: "OK"))))))
XCTAssertEqual(stateMachine.state, .finished)
}

func testReceivingUntaggedDuringAuthentication() {
var stateMachine = ClientStateMachine.Authentication()

// send and respond to a couple of challenges
XCTAssertNoThrow(try stateMachine.receiveContinuationRequest(.data("challenge1")))
stateMachine.sendCommand(.continuationResponse("response1"))
XCTAssertNoThrow(try stateMachine.receiveResponse(.untagged(.capabilityData([.imap4rev1]))))
XCTAssertEqual(stateMachine.state, .waitingForServer)

// finish
XCTAssertNoThrow(try stateMachine.receiveResponse(.tagged(.init(tag: "A1", state: .ok(.init(code: nil, text: "OK"))))))
XCTAssertEqual(stateMachine.state, .finished)
}

func testDuplicateChallengeThrows() {
Expand Down