Commit Graph
546 Commits
Author SHA1 Message Date
Daniel Eggert fcab771ec0 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-03 14:59:38 +01:00
Daniel Eggert 4195d3986c 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-11-03 14:59:38 +01:00
Daniel Eggert 8b8d972682 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.
2026-11-03 14:59:38 +01:00
Daniel Eggert 62f62a1708 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.
2026-11-03 14:59:38 +01:00
Daniel Eggert 75483e332e 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-11-03 14:59:38 +01:00
Daniel Eggert b85bf2bee8 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-11-03 14:59:37 +01:00
Daniel Eggert 59a7e1b552 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-11-03 14:59:37 +01:00
Daniel Eggert dc32f9085e 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-11-03 14:59:37 +01:00
Daniel Eggert dd881edcce 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-11-03 14:59:37 +01:00
Daniel Eggert fa9c9da70a 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.
2026-11-03 14:59:37 +01:00
Daniel Eggert be08e0dce0 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-11-03 14:59:35 +01:00
Daniel Eggert 39e602fc52 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.
2026-11-03 11:30:26 +01:00
Daniel Eggert 8fddeca15e 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-11-03 11:30:26 +01:00
Daniel Eggert 960821c044 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-11-03 11:30:26 +01:00
Daniel Eggert 0cc9a59e55 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-11-03 11:30:26 +01:00
Daniel Eggert b03742e4ae 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-11-03 11:30:26 +01:00
Daniel Eggert 740fe66c92 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-11-03 11:30:26 +01:00
Daniel Eggert 5f4ad4135f 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-11-03 11:30:25 +01:00
Daniel Eggert 6b32236b33 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-11-03 11:30:25 +01:00
Daniel Eggert 0c03851fc5 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-11-03 11:30:25 +01:00
Daniel Eggert a7d63b2890 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-11-03 11:30:25 +01:00
Daniel Eggert b218ac7a82 Base64-encode URLMECH mechanism data on write
The parser base64-decodes `mechanism-base64`, but the encoder wrote the
decoded bytes raw: parse `INTERNAL=YQ==` → "a" → `INTERNAL=a`. A proxy
re-encoding `[URLMECH INTERNAL X=<base64 of CRLF…>]` emitted raw CRLF.

RFC 4467: `mechanism-base64 = mechanism ["=" base64]`. Encode on write;
fix the fixtures that locked in the mismatch.
2026-11-03 11:30:25 +01:00
Daniel Eggert f3abd28c09 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-11-03 11:30:25 +01:00
Daniel Eggert da5bb624ac 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-11-03 11:30:24 +01:00
Daniel Eggert 8497bc298c Write media type and subtype as string
`Media.Subtype("x\"\r\n* 1 EXPUNGE")` was spliced into `"…"` unescaped;
a MIME `Content-Type` header could inject an untagged response.

`media-basic = ((DQUOTE (…) DQUOTE) / string) SP media-subtype`,
`media-subtype = string`. Use `writeIMAPString` (still uppercased).
2026-11-03 11:30:24 +01:00
Daniel Eggert 77cbd03bfb Write body encoding as string
`Encoding("x\" 0 0) UID 9 (")` was spliced into `"…"` unescaped, so a
MIME `Content-Transfer-Encoding` header spoofs FETCH items. The parser
accepts any `string` (incl. literals with CRLF), so proxies re-emitted it.

`body-fld-enc = (DQUOTE ("7BIT"/…) DQUOTE) / string`. Use
`writeIMAPString`; standard values are unchanged on the wire.
2026-11-03 11:30:24 +01:00
Daniel Eggert da0eb28b27 Write URLFETCH URLs as astring
`.urlFetch([ByteBuffer(string: "x\r\nA2 DELETE INBOX")])` was written
verbatim, injecting a command.

RFC 4467: `url-full = astring`. Write bare if `1*ASTRING-CHAR` (keeps
long URLAUTH URLs out of literals), else `writeIMAPString` (`%`, `*`).
2026-11-03 11:30:24 +01:00
Daniel Eggert 01d461cae1 Write GmailLabel bare only if it is a system label
`GmailLabel("\\x)\r\nA2 DELETE INBOX\r\nA3 NOOP (")` was written raw
because it starts with `\`, injecting a command into STORE X-GM-LABELS.
Reachable from server data via `init(mailboxName:)`.

Write bare only for `"\" atom`; otherwise `writeIMAPString`. Also fixes
round-trip of astring labels such as `"\\Foo bar"`.
2026-11-03 11:30:24 +01:00
Daniel Eggert d7018634d2 Explicit “invalid” case for invalid mailbox IDs (#876) 2026-11-02 21:53:00 +01:00
Matt Diephouse f5fa7d3691 Implement OBJECTID+ extension (#840) 2026-10-30 13:47:17 +00:00
Daniel Eggert fbec780de3 Normalize malformed ENVELOPE address groups (#872) 2026-10-27 15:06:24 +01:00
Matt Diephouse 17929ed44f Handle invalid mailbox IDs (#873) 2026-10-16 09:07:48 -04:00
AnthonyandDaniel Eggert d456d32908 Accept the responses a server may send during IDLE (#855)
Co-authored-by: Daniel Eggert <deggert@apple.com>
2026-10-13 17:08:26 +02: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
Daniel Eggert 088b6eb105 Fix race in ClientBootstrap (#870) 2026-10-12 15:32:49 +02: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 6c8cfaddcc Add a imperative IMAP client and an imap-tool CLI (#841) 2026-09-25 16:16:41 +01:00
Daniel Eggert 8fd34eaff7 Validate flag and quota resource names as atoms (#854) 2026-09-14 21:16:50 +00:00
Christian 1c7076fab6 Fix framing of zero-length literals (#851) 2026-09-14 14:13:19 -07:00
Eirik Botten Nicolaysen 2e4eee1f31 Treat DEL (0x7F) as an atom-special (#848) 2026-09-13 10:40:42 -07:00
K Gopal Krishnaandkaygdotorg 26a69caf84 Allow configuring IMAP client line buffer size (#849)
Co-authored-by: kaygdotorg <106914721+kaygdotorg@users.noreply.github.com>
2026-09-09 08:51:12 +02:00
Daniel Eggert e536be599e Reject invalid AuthenticationMechanism (#846) 2026-09-01 10:11:11 +02:00
Daniel Eggert c4d4d23e29 Drop pre-Swift 6.2 (#845) 2026-08-29 09:23:34 +02:00
Daniel Eggert 273b5602db Update UIDBATCHES RFC number (#842) 2026-08-10 14:46:51 -04: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
Daniel Eggert 1057994e93 Move examples into sub-dir (#834) 2026-07-18 12:00:06 +02:00
Daniel Eggert 880366a417 Add macOS CI job (#836) 2026-07-07 11:37:57 +02:00
Léon Simmons 79c4237b5c docs: fix typo in AuthenticationStateMachine comment (#832)
authentiction → authentication
2026-07-03 16:33:52 +02:00