carlos / platform Public

mailer: RFC 2231 parameters refused in MIME fields only, anywhere in them

Task 6 review P3s (the family is approved):

- In Content-Type and Content-Disposition, a parameter whose name holds
  "*" (name*=, filename*=, name*0=) is refused anywhere in the value,
  the first parameter with no ";" before it included.
- The rule applies to those structured MIME fields only, so
  "Subject: note; x*=2 works" is admitted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 3300bffd40a955be7d01495c8de19b6eda23c914 parent 85cadce
3 files changed, +47 −34
  • internal/mailer/encoded.go +14 −8
  • internal/mailer/hygiene.go +3 −1
  • internal/mailer/hygiene_test.go +30 −25
diff --git a/internal/mailer/encoded.go b/internal/mailer/encoded.go
index 8de87241d..76c1f06b0 100644
--- a/internal/mailer/encoded.go
+++ b/internal/mailer/encoded.go
@@ -30,14 +30,20 @@ import (
// decoding failing here.
var encodedWordShape = regexp.MustCompile(`=\?[^?]*\?[^?]*\?.*?\?=`)
-// rfc2231Param is a header parameter whose name holds "*": after a ";",
-// a name (any run of characters but "=" and ";", so "name *" counts too)
-// holding a "*", then "=". Only a parameter position is matched, so
-// "Subject: a*=b" is not one.
-var rfc2231Param = regexp.MustCompile(`;[^=;]*\*[^=;]*=`)
+// rfc2231Param is an RFC 2231 parameter in a structured MIME field (its
+// name holds "*": name*=, name*0=, name*1*=, spaced or not): a "*" then
+// "=" with only digits, "*" and whitespace between, anywhere in the value,
+// the first parameter with no ";" before it included (Task 6 review P3).
+var rfc2231Param = regexp.MustCompile(`\*[\s0-9*]*=`)
+
+// mimeParamFields are the structured MIME fields whose parameters a MIME
+// reader decodes (RFC 2231), lowercased: the parameter rule applies to
+// them alone, so "Subject: note; x*=2 works" is admitted.
+var mimeParamFields = map[string]bool{"content-type": true, "content-disposition": true}
// cleanFieldValue reports whether an unfolded header field value carries
-// only clean encoded text: no RFC 2231 parameter, and everything shaped
+// only clean encoded text: no RFC 2231 parameter in a structured MIME
+// field (mimeParamFields), and everything shaped
// like an encoded word (=?x?x?…?=) decodes cleanly as one
// (decodeEncodedWord, cleanDecoded). Adjacent words, which a reader joins
// (RFC 2047 §6.2), need no joined check: a C0, DEL or ISO-8859 C1 byte is
@@ -46,8 +52,8 @@ var rfc2231Param = regexp.MustCompile(`;[^=;]*\*[^=;]*=`)
// Text with "=?" and "?=" that is not shaped
// like a word ("is a=?b then c?=d") is not one, and is admitted (ruled
// 2026-11-14): the leftmost-first match leaves nothing word-shaped behind.
-func cleanFieldValue(v string) bool {
- if rfc2231Param.MatchString(v) {
+func cleanFieldValue(name, v string) bool {
+ if mimeParamFields[strings.ToLower(name)] && rfc2231Param.MatchString(v) {
return false
}
if !strings.Contains(v, "=?") {
diff --git a/internal/mailer/hygiene.go b/internal/mailer/hygiene.go
index eb50ec2ea..0a291cea7 100644
--- a/internal/mailer/hygiene.go
+++ b/internal/mailer/hygiene.go
@@ -65,8 +65,9 @@ func headerBlock(raw []byte) ([]byte, error) {
// fold's whitespace kept (RFC 5322's unfolding), and with it removed,
// as a reader that drops the fold whole would read it. Both are
// checked (ruled 2026-11-14).
+ var name string
var value, joined []byte
- field := func() bool { return cleanFieldValue(string(value)) && cleanFieldValue(string(joined)) }
+ field := func() bool { return cleanFieldValue(name, string(value)) && cleanFieldValue(name, string(joined)) }
for i := 0; i < len(header); {
j := bytes.IndexByte(header[i:], '\n')
line := bytes.TrimSuffix(header[i:i+j], []byte{'\r'})
@@ -95,6 +96,7 @@ func headerBlock(raw []byte) ([]byte, error) {
return nil, errHeaderSyntax
}
rest = line[c+1:]
+ name = string(line[:c])
value = append([]byte(nil), rest...)
joined = append([]byte(nil), rest...)
}
diff --git a/internal/mailer/hygiene_test.go b/internal/mailer/hygiene_test.go
index ed4435f6c..dc1c2760b 100644
--- a/internal/mailer/hygiene_test.go
+++ b/internal/mailer/hygiene_test.go
@@ -164,16 +164,19 @@ func TestHeaderBlockRefusesEncodedControls(t *testing.T) {
"List-Id": msg("From: "+from, "List-Id: =?utf-8?q?x=0Ay?= <list.example.org>"),
"Content-Type name=": msg("From: "+from, `Content-Type: application/pdf; name="=?utf-8?q?x=0Ay.pdf?="`),
// Ruled 2026-11-14: every RFC 2231 parameter is refused up front,
- // whatever it decodes to. Six spellings: extended, extended and
- // continued, continued plain, an empty charset, spaced and upper
- // case, and in a field that is not MIME's.
- "RFC 2231 extended": msg("From: "+from, "Content-Disposition: attachment; filename*=utf-8''x%0D%0ABcc%3A%20v"),
- "RFC 2231 extended, continued": msg("From: "+from, "Content-Disposition: attachment;", " filename*0*=utf-8''a; filename*1*=%0Ab"),
- "RFC 2231 continued, plain": msg("From: "+from, `Content-Type: application/pdf; name*0="a"; name*1="b.pdf"`),
- "RFC 2231 empty charset": msg("From: "+from, "Content-Type: text/plain; name*=''%0A"),
- "RFC 2231 spaced, upper case": msg("From: "+from, "Content-Type: text/plain; NAME *= UTF-7''%2BAA0ACg-"),
- "RFC 2231 in another field": msg("From: "+from, "X-Note: a; title*=utf-8''caf%C3%A9"),
- "RFC 2231, clean": msg("From: "+from, "Content-Disposition: attachment; filename*=utf-8'en'caf%C3%A9.pdf"),
+ // whatever it decodes to, in a structured MIME field (Content-Type,
+ // Content-Disposition; Task 6 review P3: anywhere in it, the first
+ // parameter included). Spellings: extended, extended and continued,
+ // continued plain, an empty charset, spaced and upper case, with no
+ // ";" before it, name*0=.
+ "RFC 2231 extended": msg("From: "+from, "Content-Disposition: attachment; filename*=utf-8''x%0D%0ABcc%3A%20v"),
+ "RFC 2231 extended, continued": msg("From: "+from, "Content-Disposition: attachment;", " filename*0*=utf-8''a; filename*1*=%0Ab"),
+ "RFC 2231 continued, plain": msg("From: "+from, `Content-Type: application/pdf; name*0="a"; name*1="b.pdf"`),
+ "RFC 2231 empty charset": msg("From: "+from, "Content-Type: text/plain; name*=''%0A"),
+ "RFC 2231 spaced, upper case": msg("From: "+from, "Content-Type: text/plain; NAME *= UTF-7''%2BAA0ACg-"),
+ "RFC 2231, the first parameter with no ;": msg("From: "+from, "Content-Disposition: attachment filename*=utf-8''x%0D%0ABcc%3A%20v"),
+ "RFC 2231 name*0= in Content-Type": msg("From: "+from, "Content-Type: text/plain; name*0=a"),
+ "RFC 2231, clean": msg("From: "+from, "Content-Disposition: attachment; filename*=utf-8'en'caf%C3%A9.pdf"),
// Ruled 2026-11-14, the family's P3s.
"invalid UTF-8 in a utf-8 word": msg("From: "+from, "Subject: =?utf-8?q?a=E2=80?="),
"U+2028 split across two words": msg("From: "+from, "Subject: =?utf-8?q?a=E2=80?= =?utf-8?q?=A8b?="),
@@ -193,21 +196,23 @@ func TestHeaderBlockRefusesEncodedControls(t *testing.T) {
}
}
for name, raw := range map[string][]byte{
- "B, a UTF-8 name": msg("From: =?utf-8?b?Y2Fmw6k=?= <" + from + ">"),
- "Q, a UTF-8 name": msg("From: =?utf-8?q?caf=C3=A9?= <" + from + ">"),
- "Q, a tab": msg("From: "+from, "Subject: =?utf-8?q?a=09b?="),
- "ISO-2022-JP's ESC": msg("From: "+from, "Subject: =?iso-2022-jp?b?GyRCJEokcyQrGyhC?="),
- "no encoded word": msg("From: Docs <"+from+">", "Subject: =? not a word"),
- "a folded subject": msg("From: "+from, "Subject: =?utf-8?q?caf=C3=A9?=", " =?utf-8?q?_au_lait?="),
- "windows-1252": msg("From: "+from, "Subject: =?windows-1252?q?=93quoted=94?="),
- "Shift_JIS": msg("From: "+from, "Subject: =?Shift_JIS?B?g26DjYFb?="),
- "a language suffix": msg("From: "+from, "Subject: =?utf-8*en?q?hello?="),
- "a star, not in a parameter": msg("From: "+from, "Subject: a*=b"),
- "=? and ?= that are not a word": msg("From: "+from, "Subject: is a=?b then c?=d"),
- "a URL holding =? and ?=": msg("From: "+from, "List-Unsubscribe: <https://example.org/u?x=?1&y=2?=3>"),
- "=? with too few ?": msg("From: "+from, "Subject: =?utf-8?=0A?="),
- "a plain parameter": msg("From: "+from, `Content-Type: application/pdf; name="report.pdf"`),
- "a URL with ?=": msg("From: "+from, "List-Unsubscribe: <https://example.org/u?id=1&t=2>"),
+ "B, a UTF-8 name": msg("From: =?utf-8?b?Y2Fmw6k=?= <" + from + ">"),
+ "Q, a UTF-8 name": msg("From: =?utf-8?q?caf=C3=A9?= <" + from + ">"),
+ "Q, a tab": msg("From: "+from, "Subject: =?utf-8?q?a=09b?="),
+ "ISO-2022-JP's ESC": msg("From: "+from, "Subject: =?iso-2022-jp?b?GyRCJEokcyQrGyhC?="),
+ "no encoded word": msg("From: Docs <"+from+">", "Subject: =? not a word"),
+ "a folded subject": msg("From: "+from, "Subject: =?utf-8?q?caf=C3=A9?=", " =?utf-8?q?_au_lait?="),
+ "windows-1252": msg("From: "+from, "Subject: =?windows-1252?q?=93quoted=94?="),
+ "Shift_JIS": msg("From: "+from, "Subject: =?Shift_JIS?B?g26DjYFb?="),
+ "a language suffix": msg("From: "+from, "Subject: =?utf-8*en?q?hello?="),
+ "a parameter star outside a MIME field": msg("From: "+from, "Subject: note; x*=2 works"),
+ "RFC 2231 in a field that is not MIME's": msg("From: "+from, "X-Note: a; title*=utf-8''caf%C3%A9"),
+ "a star, not in a parameter": msg("From: "+from, "Subject: a*=b"),
+ "=? and ?= that are not a word": msg("From: "+from, "Subject: is a=?b then c?=d"),
+ "a URL holding =? and ?=": msg("From: "+from, "List-Unsubscribe: <https://example.org/u?x=?1&y=2?=3>"),
+ "=? with too few ?": msg("From: "+from, "Subject: =?utf-8?=0A?="),
+ "a plain parameter": msg("From: "+from, `Content-Type: application/pdf; name="report.pdf"`),
+ "a URL with ?=": msg("From: "+from, "List-Unsubscribe: <https://example.org/u?id=1&t=2>"),
} {
if _, err := headerBlock(raw); err != nil {
t.Errorf("%s: headerBlock refused %q: %v", name, raw, err)