Files
Daniel Eggert 9f61149d6d Validate IMAP URL components (#867)
* Validate percent-encoded IMAP URL components

`EncodedMailbox(mailbox: "x] x\r\n* BYE")` was written verbatim inside
the bare URL of `[REFERRAL …]`, injecting a response; likewise
`EncodedUser`, `EncodedAuthenticationType`, `EncodedSearch`,
`EncodedSection`.

RFC 5092: `enc-user`/`enc-auth-type = 1*achar`, `enc-mailbox`/
`enc-search`/`enc-section = 1*bchar`. Each type's public `var` and
labelled init become `init?(_:)`, literals trap, `String.init` reads.
One helper validates by character class and `pct-encoded` triplet, and
uppercases hex digits as the parser does, so values round-trip.

* Validate URLAUTH tokens

`EncodedAuthenticatedURL(data: "x\r\n* BYE")` was written verbatim at the
end of a URLAUTH URL, injecting a response.

RFC 4467: `enc-urlauth = 32*HEXDIG`. `init(data:)` and the public `var`
become `init?(_:)`; literals trap; `String.init` reads. The parser read
exactly 32 digits, so longer tokens didn't round-trip; it now reads all.

* Add IMAPServer.Host and keep IP-literal brackets

`IMAPServer(host: "h] x\r\n* BYE")` was written verbatim in the bare URL
of `[REFERRAL …]`, injecting a response. `imap://[::1]/` parsed as host
`::1` and re-encoded as `imap://::1/`. A negative port encoded as `:-1`.

RFC 3986: `host = IP-literal / IPv4address / reg-name`. New nested
`IMAPServer.Host` replaces the `String`: `init?(_:)` validates IPv6,
IPvFuture and reg-name (IPv4 is a subset), literals trap. The parser
keeps the brackets, accepts all of reg-name, and validates via `init?`;
its unreachable IPv4 branch is gone. `port` is now `UInt16?`.

* Don't parenthesize URLFETCH responses

`.urlFetch([.init(url: "url", data: nil)])` encoded as
`URLFETCH("url" NIL)`, which our parser rejects.

RFC 4467: `urlfetch-data = "URLFETCH" 1*(SP url-full SP nstring)`. Write
each pair after a space, unparenthesized. Fix fixtures that enshrined
the old output.

* Remove dead URL and date writers

`writeAuthIMAPURLRump`, `writeAuthIMAPURLFull`, `writeRelativeIMAPURL`,
`writeURLFetchType`, `writeInternetMessageDate` and
`writeListSelectBaseOptionQuoted` had no callers outside their own
encode tests, nor did the writers only they reached
(`writeAuthenticatedURL(NetworkMessagePath)`, `writeNetworkPath`,
`writeAbsoluteMessagePath`, `writeIUIDOnly`,
`writeURLMessageSectionOnly`, `writeMessagePathByteRangeOnly`). Delete
them and those tests; parse tests stay.

* Keep a section's trailing slash unless a partial follows

`imap://h/INBOX/;UID=1/;SECTION=1/` parsed with section `1`, so
`EncodedSection("1/")` didn't round-trip: the parser dropped the `/`
even when no `;PARTIAL=` followed it. Drop it only when one does.

* Reject `*` as a URL authentication type

`.type("*")` encoded as `;AUTH=*`, which parses as `.any`; `.type("*x")`
failed to parse, since `*` matched first.

RFC 5092: `iauth = ";AUTH=" ( "*" / enc-auth-type )`. `init?(_:)`
rejects `*`; the parser tries the longer `enc-auth-type` first.
2026-11-04 17:41:53 +00:00

73 lines
2.7 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
//
//===----------------------------------------------------------------------===//
/// A textual RFC 2822 date as it appears in email message headers.
///
/// Represents the date field value from an email message's `Date` header,
/// formatted according to RFC 2822 section 3.3. Unlike ``ServerMessageDate``, which records
/// when the server received the message, this date represents the date the message was
/// composed by its sender.
///
/// The value is stored as a string without parsing or validation. To use the string value,
/// construct a `String` from this type using the `init(_ date:)` initializer.
///
/// ### Example
///
/// ```
/// * 1 FETCH (BODY[HEADER.FIELDS (DATE)] “Date: Fri, 15 Mar 2026 10:30:45 +0100”)
/// ```
///
/// The date header value `Fri, 15 Mar 2026 10:30:45 +0100` can be represented as an
/// ``InternetMessageDate``. Use ``Swift/String/init(_:)-(InternetMessageDate)`` to retrieve the string value.
///
/// - SeeAlso: [RFC 2822 Section 3.3](https://datatracker.ietf.org/doc/html/rfc2822#section-3.3)
public struct InternetMessageDate: Hashable, Sendable {
var value: String
/// Creates a new ``InternetMessageDate`` from a given ``Swift/String``.
///
/// The string is stored as-is without parsing or validation. The caller is responsible
/// for ensuring the string represents a valid RFC 2822 date format.
///
/// - Parameter string: A `String` containing the RFC 2822 date value (for example, `”Fri, 15 Mar 2026 10:30:45 +0100”`).
public init(_ string: String) {
self.value = string
}
}
extension String {
/// Creates a new ``Swift/String`` from an ``InternetMessageDate``.
///
/// Extracts the underlying string value from the ``InternetMessageDate``.
///
/// - Parameter date: The ``InternetMessageDate`` to convert.
public init(_ date: InternetMessageDate) {
self = date.value
}
}
extension InternetMessageDate: ExpressibleByStringLiteral {
/// Creates a new `InternetMessageDate` from a string literal.
///
/// Allows writing RFC 2822 dates as string literals:
/// ```swift
/// let date: InternetMessageDate = “Fri, 15 Mar 2026 10:30:45 +0100”
/// ```
///
/// - Parameter value: A ``Swift/String`` containing the RFC 2822 date value.
public init(stringLiteral value: String) {
self.value = value
}
}