mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-24 04:38:04 +00:00
* Reject invalid response text when writing `.no(.init(text: "No mailbox x\r\n* BYE pwned"))` was written verbatim, injecting an untagged response; likewise tagged, BYE, PREAUTH and continuation responses. RFC 9051: `text = 1*(TEXT-CHAR / UTF8-2 / UTF8-3 / UTF8-4)`. `writeText` now asserts no NUL, CR or LF. Empty stays valid. The parser now accepts UTF-8 text. * Add Command.Tag for command and response tags `TaggedCommand(tag: "A1 NOOP\r\nA2 DELETE INBOX\r\nA3", command: .noop)` was written verbatim, injecting a command; likewise `APPEND` start. `TaggedResponse(tag: "* BYE x\r\nA1", …)` injected a response. RFC 3501: `tag = 1*<any ASTRING-CHAR except "+">`. New `Command.Tag` replaces the `String` in `TaggedCommand`, `TaggedResponse`, `Response`, `CommandStreamPart`, `AppendCommand`, `BadCommand` and NIOIMAP's state machine errors; `init?(_:)` validates, literals trap. The parser already enforces the production. IMAPCommands converts its numeric tags. * Validate and escape namespace delimiters `NamespaceDescription(string: "x", char: "\r\n", …)` wrote a raw CRLF inside `"…"`, injecting a response. `"\""` encoded as `"""`, and `"\\"` as `"\"` (unterminated); non-ASCII was accepted. RFC 2342: `DQUOTE QUOTED-CHAR DQUOTE / nil`. `init(string:delimiter: responseExtensions:)` (was `char:`) throws `InvalidDelimiterError` for non-ASCII, CR, LF, NUL; the delimiter is stored as a byte, read-only. Quoted-specials are escaped on write and parsed when escaped. * Gate a capturing exit test on Swift 6.3
215 lines
9.1 KiB
Swift
215 lines
9.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2021 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import NIOIMAPCore
|
|
|
|
extension ClientStateMachine {
|
|
struct Append: Hashable {
|
|
enum State: Hashable {
|
|
// TODO: Find a better name than "started".
|
|
/// We've sent the append command to the server, and will now send data
|
|
/// Can move to `.waitingForAppend...` or `catenating`.
|
|
/// `canFinish` is true iff at least one "standard" append, or one catenate
|
|
/// has completed.
|
|
case started(canFinish: Bool)
|
|
|
|
/// We want to send a literal to the server, but first need the server to confirm it's ready.
|
|
/// When we receive the continuation request, we'll move to `.sendingMessageBytes`.
|
|
case waitingForAppendContinuationRequest
|
|
|
|
/// We're streaming a message to the server.
|
|
/// Once the message has been sent, we return to `.started`.
|
|
case sendingMessageBytes
|
|
|
|
/// We're catenating messages or URLS. No state transformation is required for URLs, as they're
|
|
/// done in one shot, but to catenate a message we need to move to `.waitingForCatenate...`
|
|
case catenating(sentFirstObject: Bool)
|
|
|
|
/// We want to send a literal to the server, but first need the server to confirm it's ready.
|
|
/// Once we've received the continuation, we'll move to `.sendingCatenateBytes`.
|
|
case waitingForCatenateContinuationRequest
|
|
|
|
/// We're streaming a catenate message to the server. Once the
|
|
/// message is sent we return to `.started`.
|
|
case sendingCatenateBytes
|
|
|
|
/// The client has sent the "finish" command, we just need the server to confirm.
|
|
/// From here we move to `finished`.
|
|
case waitingForTaggedResponse
|
|
|
|
/// The server has returned a tagged response to finish the append command. No
|
|
/// valid state transformations at this point.
|
|
case finished
|
|
}
|
|
|
|
// The tag of the APPEND command
|
|
var tag: Command.Tag
|
|
var state: State = .started(canFinish: false)
|
|
|
|
var isWaitingForContinuationRequest: Bool {
|
|
switch state {
|
|
case .waitingForAppendContinuationRequest, .waitingForCatenateContinuationRequest:
|
|
true
|
|
case .started, .sendingMessageBytes, .catenating, .sendingCatenateBytes, .waitingForTaggedResponse,
|
|
.finished:
|
|
false
|
|
}
|
|
}
|
|
|
|
var hasCatenatedAtLeastOneObject: Bool {
|
|
switch self.state {
|
|
case .started, .waitingForAppendContinuationRequest, .sendingMessageBytes,
|
|
.waitingForCatenateContinuationRequest, .sendingCatenateBytes, .waitingForTaggedResponse, .finished:
|
|
return false
|
|
case .catenating(sentFirstObject: let sentFirstObject):
|
|
return sentFirstObject
|
|
}
|
|
}
|
|
|
|
enum ReceiveResponseResult {
|
|
case continueAppending
|
|
case doneAppending
|
|
}
|
|
|
|
// We don't expect any responses while appending other than
|
|
// 1. Untagged responses (including untagged fetch responses)
|
|
// 2. The final tagged response.
|
|
mutating func receiveResponse(_ response: Response) throws -> ReceiveResponseResult {
|
|
switch response {
|
|
case .untagged, .fetch:
|
|
return .continueAppending
|
|
case .tagged(let tagged) where tagged.tag != self.tag:
|
|
// This is the completion for another (previous) command. Ignore.
|
|
return .continueAppending
|
|
case .tagged, .fatal, .authenticationChallenge, .idleStarted:
|
|
break
|
|
}
|
|
|
|
switch self.state {
|
|
case .started, .waitingForAppendContinuationRequest, .sendingMessageBytes, .catenating,
|
|
.waitingForCatenateContinuationRequest, .sendingCatenateBytes, .finished:
|
|
throw UnexpectedResponse(kind: .appendNotWaitingForTaggedResponse)
|
|
case .waitingForTaggedResponse:
|
|
break
|
|
}
|
|
|
|
switch response {
|
|
case .untagged, .fetch, .fatal, .authenticationChallenge, .idleStarted:
|
|
throw UnexpectedResponse(kind: .appendWaitingForTaggedResponse)
|
|
case .tagged:
|
|
self.state = .finished
|
|
return .doneAppending
|
|
}
|
|
}
|
|
|
|
mutating func receiveContinuationRequest(_: ContinuationRequest) throws {
|
|
switch self.state {
|
|
case .started, .sendingMessageBytes, .catenating,
|
|
.sendingCatenateBytes, .finished, .waitingForTaggedResponse:
|
|
throw UnexpectedContinuationRequest(kind: .append)
|
|
case .waitingForAppendContinuationRequest:
|
|
self.state = .sendingMessageBytes
|
|
case .waitingForCatenateContinuationRequest:
|
|
self.state = .sendingCatenateBytes
|
|
}
|
|
}
|
|
|
|
/// - returns: `true` if a continuation is required after this command is sent, otherwise `false`.
|
|
mutating func sendCommand(_ command: CommandStreamPart) {
|
|
// we only care about append commands, obviously
|
|
let appendCommand: AppendCommand
|
|
switch command {
|
|
case .idleDone, .tagged, .continuationResponse:
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
case .append(let _appendCommand):
|
|
appendCommand = _appendCommand
|
|
}
|
|
|
|
switch self.state {
|
|
case .waitingForTaggedResponse, .waitingForAppendContinuationRequest,
|
|
.waitingForCatenateContinuationRequest, .finished:
|
|
preconditionFailure("Invalid state: \(self.state)")
|
|
case .started:
|
|
self.sendCommand_startedState(appendCommand)
|
|
case .sendingMessageBytes:
|
|
self.sendCommand_sendingMessageBytesState(appendCommand)
|
|
case .catenating:
|
|
return self.sendCommand_catenatingState(appendCommand)
|
|
case .sendingCatenateBytes:
|
|
self.sendCommand_sendingCatenateBytesState(appendCommand)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Send
|
|
|
|
extension ClientStateMachine.Append {
|
|
private mutating func sendCommand_startedState(_ command: AppendCommand) {
|
|
switch command {
|
|
case .start, .messageBytes, .endMessage, .catenateURL, .catenateData, .endCatenate:
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
case .beginMessage:
|
|
self.state = .waitingForAppendContinuationRequest
|
|
case .beginCatenate:
|
|
self.state = .catenating(sentFirstObject: false)
|
|
case .finish:
|
|
guard
|
|
self.state == .started(canFinish: true)
|
|
else {
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
}
|
|
self.state = .waitingForTaggedResponse
|
|
}
|
|
}
|
|
|
|
private mutating func sendCommand_sendingMessageBytesState(_ command: AppendCommand) {
|
|
switch command {
|
|
case .start, .beginMessage, .beginCatenate, .catenateURL, .catenateData, .endCatenate, .finish:
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
case .endMessage:
|
|
self.state = .started(canFinish: true)
|
|
case .messageBytes:
|
|
self.state = .sendingMessageBytes // continue sending bytes until we're told to stop
|
|
}
|
|
}
|
|
|
|
private mutating func sendCommand_catenatingState(_ command: AppendCommand) {
|
|
switch command {
|
|
case .start, .beginMessage, .beginCatenate, .messageBytes, .endMessage, .finish, .catenateData(.bytes),
|
|
.catenateData(.end):
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
case .catenateURL:
|
|
self.state = .catenating(sentFirstObject: true)
|
|
case .catenateData(.begin):
|
|
self.state = .waitingForCatenateContinuationRequest
|
|
case .endCatenate:
|
|
self.state = .started(canFinish: true)
|
|
}
|
|
}
|
|
|
|
/// `true` if a continuation is required, otherwise `false`
|
|
private mutating func sendCommand_sendingCatenateBytesState(_ command: AppendCommand) {
|
|
switch command {
|
|
case .start, .beginMessage, .beginCatenate, .endMessage, .catenateURL, .messageBytes, .finish, .endCatenate,
|
|
.catenateData(.begin):
|
|
preconditionFailure("Invalid command for state: \(self.state)")
|
|
case .catenateData(.end):
|
|
self.state = .catenating(sentFirstObject: true)
|
|
case .catenateData(.bytes):
|
|
self.state = .sendingCatenateBytes // continue sending bytes until we're told to stop
|
|
}
|
|
}
|
|
}
|