Commit Graph
311 Commits
Author SHA1 Message Date
Daniel Eggert dacf1aa00a Make RESETKEY mechanisms require a mailbox
`.resetKey(mailbox: nil, mechanisms: [.internal])` encoded as bare
`RESETKEY`, silently dropping the mechanisms and resetting every key.

RFC 4467: `resetkey = "RESETKEY" [SP mailbox *(SP mechanism)]`. The
payload becomes `Command.ResetKeyScope`: `.allMailboxes` or
`.mailbox(_:mechanisms:)`.
2026-10-29 15:51:59 +01:00
Daniel Eggert b6f3bc0339 Validate JMAPACCESS URLs
`.jmapAccess(URL(string: "jmap", relativeTo: base))` encoded as
`JMAPACCESS "jmap -- https://example.com/"` (`URL.description`); `http`
URLs were written though the parser rejects them.

The payload becomes `ResponsePayload.JMAPAccessURL`: `init?(_:)` resolves
against the base and requires `https`, a host, and no escaping inside
`quoted`; `URL.init` reads. The parser uses the same check.
2026-10-29 15:51:59 +01:00
Daniel Eggert 774faa510e Round-trip multi-value LIST option values
`.array(["a", "b"])` encoded as `(("a" "b"))`; the parser rejected both
that and `("a" "b")`: it took the `astring` branch first and could not
backtrack.

RFC 5258: `option-value = "(" option-val-comp ")"`, `option-val-comp =
astring / option-val-comp *(SP option-val-comp) / "(" option-val-comp
")"`. The parser reads a sequence of elements; the encoder writes
several values inside the `option-value` parentheses.
2026-10-29 15:51:59 +01:00
Daniel Eggert 58479e707b Model the RFC 3339 time offset
`;EXPIRE=2025-12-31T23:59:59Z` failed to parse inside a URL: the
parser stopped before `Z`. The encoder never wrote an offset.

RFC 4467: `expire = ";EXPIRE=" date-time`; RFC 3339: `full-time =
partial-time time-offset`, `time-offset = "Z" / time-numoffset`. Adds
the required `FullTime.offset`.
2026-10-29 15:51:58 +01:00
Daniel Eggert cd90191ee1 Keep leading zeros in fractional seconds
`12:34:56.05` parsed as `fraction: 5` and re-encoded as `12:34:56.5`,
changing the value.

RFC 3339: `time-secfrac = "." 1*DIGIT`. `FullTime.fraction` becomes
`FullTime.Fraction`, which stores the digits; `init?(_:)` validates,
literals trap, `String.init` reads.
2026-10-29 15:51:58 +01:00
Daniel Eggert 6a292a31ee 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-10-29 15:51:56 +01:00
Daniel Eggert dfea793f7b 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.
2026-10-29 15:51:55 +01:00
Daniel Eggert a937ce84c6 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?`.
2026-10-29 15:51:55 +01:00
Daniel Eggert 6341c18827 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.
2026-10-29 15:51:52 +01:00
Daniel Eggert 94eb86e0b4 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.
2026-10-29 15:51:50 +01:00
Daniel Eggert 6fe7d5ce4b 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.
2026-10-29 15:51:41 +01:00
Daniel Eggert 0a9e8af8ec 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.
2026-10-29 15:51:41 +01:00
Daniel Eggert f1dd8b2cba Add ResponseText.Text for response text
`.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)`. New
`ResponseText.Text` replaces the `String`; `init?(_:)` rejects NUL, CR,
LF and a leading space (the parser skips it), literals trap. Empty stays
valid. The parser now accepts UTF-8 text.
2026-10-29 15:51:38 +01:00
Daniel Eggert 20e5b9d6f0 Add ExtensionKey for tagged-extension parameters
Generic over the extended type, so a STORE key can't be used where a FETCH key is expected.
2026-10-29 15:47:10 +01:00
Daniel Eggert 3abb3b169b 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.
2026-10-24 11:36:52 +02:00
Daniel Eggert a037a11159 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`.
2026-10-24 11:36:52 +02:00
Daniel Eggert 12c2beecd4 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.
2026-10-24 11:36:52 +02:00
Daniel Eggert 08da79e5b1 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(_:)`.
2026-10-24 11:36:52 +02:00
Daniel Eggert b49361d9fa 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.
2026-10-24 11:36:52 +02:00
Daniel Eggert f522d8497f 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.
2026-10-24 11:36:51 +02:00
Daniel Eggert 4ae20e6c93 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.
2026-10-24 11:36:51 +02:00
Daniel Eggert de1fc01ccc 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.
2026-10-24 11:36:51 +02:00
Daniel Eggert d92510a699 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.
2026-10-24 11:36:51 +02:00
Daniel Eggert a6e10ee295 Fix escaping of mailbox path separator
The quoted-special cases were swapped: `\` encoded as `"\"`
(unterminated), `"` as `"\\"` (a backslash). `pathSeparator: "\n"` was
written as a raw LF inside `"…"`, and NUL silently became NIL.

`(DQUOTE QUOTED-CHAR DQUOTE / nil)`, `QUOTED-CHAR = <TEXT-CHAR except
quoted-specials> / "\" quoted-specials`. Swap the cases; parse the
escaped forms; `MailboxPath.init` throws for CR, LF, CRLF, NUL.
2026-10-24 11:36:50 +02:00
Daniel Eggert 057ee8e5a0 Write search correlator tag as string
`SearchCorrelator(tag: "a\") * BYE")` was spliced into `"…"` unescaped,
so ESEARCH / UIDBATCHES responses could carry injected data.

RFC 4466: `tag-string = string`. Use `writeIMAPString`, and parse a
`string` rather than `"` tag `"` so every tag round-trips.
2026-10-24 11:36:50 +02:00
Matt Diephouse 17929ed44f Handle invalid mailbox IDs (#873) 2026-10-16 09:07:48 -04:00
Matt Diephouse b9e08e13cf Fix some issues with RFC 2087 (QUOTA extension) (#871)
* Separate resources in QUOTA responses with spaces

RFC 2087 defines quota_list as "(" #quota_resource ")", with a single
space as the delimiter. writeQuotaResources concatenated resources with
no separator, producing e.g. (STORAGE 10 512MESSAGE 1 100).

* Support zero or more quota roots in QUOTAROOT responses

RFC 2087 defines quotaroot_response as "QUOTAROOT" SP astring
*(SP astring): a mailbox has zero or more quota roots. The parser
required exactly one, so `* QUOTAROOT comp.mail.mime` (the RFC's own
example) failed to parse and any roots after the first were dropped.

ResponsePayload.quotaRoot now carries [QuotaRoot]. This is a source-
breaking change to the public API.
2026-10-13 10:51:38 -04:00
Ross Cooperman 544677dbaa Add Gmail X-GM-MSGID, X-GM-THRID, and X-GM-RAW search keys (#853) 2026-10-10 18:48:53 +02:00
Daniel Eggert 8fd34eaff7 Validate flag and quota resource names as atoms (#854) 2026-09-14 21:16:50 +00:00
Eirik Botten Nicolaysen 2e4eee1f31 Treat DEL (0x7F) as an atom-special (#848) 2026-09-13 10:40:42 -07:00
Daniel Eggert e536be599e Reject invalid AuthenticationMechanism (#846) 2026-09-01 10:11:11 +02:00
Matt Diephouse 4b83b70e04 Enable MemberImportVisibility (#844)
https://docs.swift.org/compiler/documentation/diagnostics/member-import-visibility/
2026-07-30 11:03:01 -04:00
Oliver Drobnik d67f1243f7 Replace C ctype calls with pure-Swift ASCII checks in UInt8+ParseTypeMembership (#838) 2026-07-20 13:28:36 +02:00
Daniel Eggert e67fc89e63 RFC 9979 (#833) 2026-07-18 20:57:21 +02:00
Oliver DrobnikandClaude Opus 4.8 0da5049bc9 NIOIMAPCore: import the Android overlay module instead of Bionic (#829)
* NIOIMAPCore: import the Android overlay module, not Bionic

#826 added a `canImport(Bionic)` / `import Bionic` arm to NIOIMAPCore's
per-file libc-import guards. Switch it to the `Android` umbrella module,
which re-exports Bionic plus the Swift overlay, so the import is the more
complete/idiomatic libc surface on the Swift Android SDK. No functional
change — both resolve the libc symbols (isalnum/isalpha) used here.

* CI: enable InlineAlways experimental feature for Android nightly build

swift-collections 1.6.0 guards `@inline(always)` with `#if compiler(>=6.3)`,
but that attribute is still behind the experimental `InlineAlways` feature in
the nightly `main` toolchain the Android Swift SDK job builds with, so the build
fails with "'@inline(always)' is an experimental feature". Pass
`-enable-experimental-feature InlineAlways` to swift build until the upstream
guard is fixed (cf. apple/swift-collections #580/#584).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 09:04:30 +01:00
Oliver DrobnikandClaude Opus 4.8 bcf875610c Add Android to NIOIMAPCore's libc imports (#826)
### Motivation:

NIOIMAPCore's parser guards the libc import per file with
Darwin/Glibc/Musl/Windows and otherwise falls into
`#else let badOS = { fatalError("unsupported OS") }()`. On the Swift
Android SDK none of those arms match, so every guarded file takes the
`#else` branch and the module fails to build with "invalid redeclaration
of 'badOS'" (the symbol is declared at file scope in ~17 sources).

### Modifications:

Add an `#elseif canImport(Android)` arm importing `Android` to the per-file
libc-import guards in NIOIMAPCore. For `UInt8+ParseTypeMembership.swift`,
which imports specific ctype functions, add `import func Android.isalnum` /
`import func Android.isalpha`.

### Result:

NIOIMAPCore compiles for Android. No change on any other platform.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-02 16:06:29 +00:00
Daniel Eggert abfc170ff1 Only parse continuation responses while an AUTHENTICATE is in progress (#827) 2026-06-01 14:13:22 -04:00
Daniel Eggert 01de1f9a29 Allow () in place of NIL for empty addr-list fields. Fixes #821 (#823) 2026-04-30 08:12:53 +02:00
Daniel Eggert 34883bcba6 Update style in documentation (#822) 2026-04-28 18:06:43 +02:00
Oliver Drobnik 9c56edfa19 Fix parsing of untagged SORT responses (#819) (#820) 2026-04-26 10:11:20 +02:00
Oliver Drobnik b132d4c4bb Refactor: Roll MailboxData.searchSort into .search (#727) (#817) 2026-04-23 19:09:04 +02:00
Oliver Drobnik 2f7d3b73a8 Refactor: Remove FetchModificationResponse, use ModificationSequenceValue directly (#818) 2026-04-23 19:03:14 +02:00
Daniel Eggert 871d0122c9 Better docc comments (#815) 2026-04-16 09:20:29 -07:00
Oliver Drobnik 5207e4f8e9 Add SORT command support (RFC 5256) (#814) 2026-04-13 13:53:57 +02:00
Daniel Eggert 9491d31e2a Additional Tests (#811) 2026-03-09 09:58:21 -04:00
Daniel Eggert e466da1b68 Inbound buffer split (#812) 2026-03-09 08:50:04 -04:00
Daniel Eggert af9dbf1d92 Cleanup and improve tests (#809) 2026-03-03 13:01:01 -05:00
Brian Suda 7697574894 Add visionOS platform support (#810) 2026-03-03 18:58:29 +01:00
Matt Diephouse 0a471f59b2 Add support for RFC9698 JMAPACCESS (#807) 2025-11-17 22:30:34 +01:00
Arnt Gulbrandsen 0d286ae155 Add support for RFC 9755 (#805) 2025-11-17 22:29:15 +01:00