mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-28 05:14:27 +00:00
* Reject invalid URLAUTH mechanisms
`.resetKey(mailbox: .inbox, mechanisms: [.init("X\r\nA2 DELETE INBOX")])`
was written verbatim, injecting a command into RESETKEY / GENURLAUTH.
RFC 4467: `mechanism = "INTERNAL" / 1*(ALPHA / DIGIT / "-" / ".")`.
`init(_:)` becomes `init?(_:)`; literals trap on invalid input. The
parser already enforces the production.
* Reject invalid mailbox attributes
`MailboxInfo.Attribute("\\X)\r\n* BYE")` was written verbatim, injecting
an untagged response into LIST. It also feeds CREATE … USE via
`UseAttribute(_:)`.
RFC 3501: `mbx-list-flag` is `"\" atom` in every branch. `init(_:)`
becomes `init?(_:)`; literals trap on invalid input. Constants become
`static let` literals; storage is `let`. Fix the `String.init` doc,
which claimed lowercase storage.
* Reject invalid use attributes
`UseAttribute("\\All))\r\nA2 DELETE INBOX\r\nA3 NOOP ((")` was written
verbatim, injecting a command into CREATE … (USE (…)). It also feeds
`GmailLabel(useAttribute:)`.
RFC 6154: `use-attr-ext = "\" atom`. `init(_:)` becomes `init?(_:)`;
literals trap on invalid input. `init(_: MailboxInfo.Attribute)` stays
non-failable: same production. Fix docs that claimed lowercase storage.
* Reject invalid attribute flags
`AttributeFlag("x\" all 1\r\nA2 DELETE INBOX\r\nA3 NOOP \"")` was
written verbatim inside SEARCH MODSEQ "/flags/…", injecting a command.
RFC 7162: `attr-flag = "\\" atom / atom`, where `"\\"` is two literal
backslashes (the stored form is already escaped). `init(_:)` becomes
`init?(_:)`, validating after lowercasing; literals trap on invalid
input. Fix docs that claimed casing is preserved.
* Add FilterName for SEARCH FILTER
`.filter("x\r\nA2 DELETE INBOX")` was written verbatim, injecting a
command into SEARCH.
RFC 5466: `filter-name = 1*<any ATOM-CHAR except "/">`. New `FilterName`
type replaces the `String` payload of `SearchKey.filter`; `init?(_:)`
validates, literals trap on invalid input.
* Reject invalid compression kinds
`.compress(.init("DEFLATE\r\nA2 DELETE INBOX"))` was written verbatim,
injecting a command after COMPRESS.
RFC 4978: `algorithm = "DEFLATE"`, an atom. `init(_:)` becomes
`init?(_:)`, validating after uppercasing; literals trap on invalid
input. Add `String.init(_:)`.
* Reject invalid capabilities
`Capability("X\r\nA2 DELETE INBOX")` was written verbatim, injecting a
command into ENABLE, or (`"X\r\n* OK [ALERT] hi"`) a response into
CAPABILITY / ENABLED. Same for the kinds a capability is built from,
e.g. `.context(.init("X\r\n…"))`.
RFC 3501: `capability = ("AUTH=" auth-type) / atom`. `init(_:)` becomes
`init?(_:)` for `Capability` and each kind (kinds validate after
uppercasing); literals trap on invalid input. Storage is `let`. Add
`String.init(_:)` for the kinds.
* Add validated LIST option tag types
`.standard("X)\r\nA2 DELETE INBOX\r\nA3 LIST (")` was written verbatim,
injecting a command into LIST select/return options. Same for
`.vendor(KeyValue<String, String>)`.
RFC 5258: `option-standard-tag = atom`, `option-vendor-tag =
vendor-token "-" atom`. New `OptionExtensionKind.Standard` and
`.Vendor` replace the payloads; `init?(_:)` validates, literals trap.
`Vendor` stores `ACME-SORT` and derives `token` / `name`.
* Add CharacterSetName for SEARCH, SORT, and BADCHARSET
`.sort(…, charset: "UTF-8 ALL\r\nA2 DELETE INBOX\r\nA3 SEARCH", …)` was
written verbatim, injecting a command into SORT / SEARCH / ESEARCH.
`.badCharset(["a)] x\r\n* BYE"])` injected a response. Quoted names
also didn't round-trip: `"a b"` parsed, then encoded bare.
RFC 3501: `charset = atom / quoted`. New `CharacterSetName` replaces
`String`; valid = non-empty `TEXT-CHAR`. Written as an atom where it is
one (`CHARSET UTF-8` unchanged), quoted otherwise, never a literal. The
parser rejects quoted names it couldn't write back.
* Assert keyword validity when writing a flag keyword
`writeFlagKeyword` wrote verbatim with no check. `writeAtom` would be
wrong: Gmail keywords contain `]`, which no atom does. Assert the
keyword's own production instead; test that a `]` keyword encodes.
* Hoist a #require the compiler flagged as redundant
110 lines
4.3 KiB
Swift
110 lines
4.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2026 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// The name of a character set, such as `UTF-8`, as used by `SEARCH`, `SORT`, and the
|
|
/// `BADCHARSET` response code.
|
|
///
|
|
/// ## Validity
|
|
///
|
|
/// [RFC 3501](https://datatracker.ietf.org/doc/html/rfc3501#section-9) defines
|
|
/// `charset = atom / quoted`. A name is written as an atom where it is one, and quoted
|
|
/// otherwise — never as a literal, which `charset` doesn't allow. So anything that can't be
|
|
/// quoted is rejected:
|
|
///
|
|
/// - ``init(_:)`` returns `nil`. Use it for a name derived from input.
|
|
/// - ``init(stringLiteral:)`` traps. A literal is written by the programmer, so an invalid one is a
|
|
/// bug to be caught on first run rather than handled.
|
|
///
|
|
/// Names compare case-insensitively, so `"utf-8" == "UTF-8"`
|
|
/// ([RFC 2978 Section 2.3](https://datatracker.ietf.org/doc/html/rfc2978#section-2.3); RFC 3501
|
|
/// uses those names). The original case is kept and written to the wire.
|
|
public struct CharacterSetName: Hashable, Sendable {
|
|
let stringValue: String
|
|
|
|
/// Creates a character set name from a string, if the string is a valid `charset`.
|
|
///
|
|
/// A string literal resolves to ``init(stringLiteral:)`` instead, which traps rather than
|
|
/// returning `nil`.
|
|
///
|
|
/// - parameter stringValue: The name, for example `UTF-8`.
|
|
/// - returns: A new name, or `nil` if `stringValue` is not a valid `charset`.
|
|
public init?(_ stringValue: String) {
|
|
guard Self.isValidName(stringValue) else { return nil }
|
|
self.stringValue = stringValue
|
|
}
|
|
|
|
init(unchecked stringValue: String) {
|
|
assert(Self.isValidName(stringValue))
|
|
self.stringValue = stringValue
|
|
}
|
|
|
|
/// Whether the given string can be written as a `charset`:
|
|
/// ```
|
|
/// charset = atom / quoted
|
|
/// quoted = DQUOTE *QUOTED-CHAR DQUOTE
|
|
/// QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
|
|
/// "\" quoted-specials
|
|
/// ```
|
|
/// Every `atom` is also a non-empty run of `TEXT-CHAR`, so that is the whole check. The empty
|
|
/// string is rejected: it's no character set.
|
|
static func isValidName(_ string: String) -> Bool {
|
|
!string.isEmpty && string.utf8.allSatisfy(\.isTextChar)
|
|
}
|
|
|
|
/// Compares case-insensitively.
|
|
///
|
|
/// Valid values are ASCII, so `uppercased()` is ASCII case-folding.
|
|
public static func == (lhs: CharacterSetName, rhs: CharacterSetName) -> Bool {
|
|
lhs.stringValue.uppercased() == rhs.stringValue.uppercased()
|
|
}
|
|
|
|
/// Hashes the case-folded value, consistent with `==`.
|
|
public func hash(into hasher: inout Hasher) {
|
|
self.stringValue.uppercased().hash(into: &hasher)
|
|
}
|
|
}
|
|
|
|
extension String {
|
|
/// Creates a `String` from a ``CharacterSetName``, unquoted.
|
|
///
|
|
/// - parameter other: The name to convert.
|
|
public init(_ other: CharacterSetName) {
|
|
self = other.stringValue
|
|
}
|
|
}
|
|
|
|
extension CharacterSetName: ExpressibleByStringLiteral {
|
|
/// Creates a character set name from a string literal.
|
|
///
|
|
/// A literal is written by the programmer, so an invalid one is a bug to be caught on first
|
|
/// run rather than handled. Use ``init(_:)`` for anything else.
|
|
///
|
|
/// - parameter value: The string literal.
|
|
/// - Precondition: `value` is a valid `charset`; this traps if it isn't.
|
|
public init(stringLiteral value: String) {
|
|
precondition(Self.isValidName(value), "Invalid character set name: \(String(reflecting: value))")
|
|
self.init(unchecked: value)
|
|
}
|
|
}
|
|
|
|
// MARK: - Encoding
|
|
|
|
extension EncodeBuffer {
|
|
/// Writes an atom where possible, so `UTF-8` stays `UTF-8` on the wire; quoted otherwise.
|
|
@discardableResult mutating func writeCharacterSetName(_ name: CharacterSetName) -> Int {
|
|
guard !name.stringValue.isIMAPAtom else { return self.writeString(name.stringValue) }
|
|
return self.writeQuotedIMAPString(name.stringValue.utf8)
|
|
}
|
|
}
|