Skip to content

Commit

Permalink
Fix for receiving untagged response during SASL auth
Browse files Browse the repository at this point in the history
This fixes #729 and adds tests.
  • Loading branch information
danieleggert committed Jun 27, 2023
1 parent 9716083 commit ab98f30
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
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

0 comments on commit ab98f30

Please sign in to comment.