Files
Daniel Eggert e7729053c0 Fix writers that emit untrusted bytes unquoted (#863)
* Test basic list-mailbox parsing

`parseListMailbox` was only tested against DEL. Cover wildcards,
resp-specials, the quoted-string branch, and an empty input.

* 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"`.

* 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` (`%`, `*`).

* 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.

* 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).

* 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.

* 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.

* 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-04 17:41:51 +00:00
..