carlos / platform Public

mailer: the * parameter rule reads unquoted parameter names only

Task 6 review P3: a quoted value (boundary="a*=b") is not an RFC 2231
parameter and is admitted; name*="..." is still refused. Quoted strings,
backslash escapes included, are taken out before the rule reads the
Content-Type or Content-Disposition value.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev a0e07a86beeea05b50fe001b8c8a86dd03fea9d4 parent 0655efe
2 files changed, +27 −1
  • internal/mailer/encoded.go +24 −1
  • internal/mailer/hygiene_test.go +3 −0
diff --git a/internal/mailer/encoded.go b/internal/mailer/encoded.go
index 76c1f06b0..6b9bd6065 100644
--- a/internal/mailer/encoded.go
+++ b/internal/mailer/encoded.go
@@ -36,6 +36,29 @@ var encodedWordShape = regexp.MustCompile(`=\?[^?]*\?[^?]*\?.*?\?=`)
// the first parameter with no ";" before it included (Task 6 review P3).
var rfc2231Param = regexp.MustCompile(`\*[\s0-9*]*=`)
+// unquoted is v with its quoted strings (RFC 5322 quoted-string, a
+// backslash escaping the next character) taken out, so the parameter rule
+// reads parameter names, never a quoted value: boundary="a*=b" is a
+// boundary, not an RFC 2231 parameter (Task 6 review P3). An unclosed
+// quote runs to the end.
+func unquoted(v string) string {
+ var b strings.Builder
+ in := false
+ for i := 0; i < len(v); i++ {
+ c := v[i]
+ switch {
+ case in && c == '\\':
+ i++
+ case c == '"':
+ in = !in
+ b.WriteByte(' ')
+ case !in:
+ b.WriteByte(c)
+ }
+ }
+ return b.String()
+}
+
// 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.
@@ -53,7 +76,7 @@ var mimeParamFields = map[string]bool{"content-type": true, "content-disposition
// 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(name, v string) bool {
- if mimeParamFields[strings.ToLower(name)] && rfc2231Param.MatchString(v) {
+ if mimeParamFields[strings.ToLower(name)] && rfc2231Param.MatchString(unquoted(v)) {
return false
}
if !strings.Contains(v, "=?") {
diff --git a/internal/mailer/hygiene_test.go b/internal/mailer/hygiene_test.go
index dc1c2760b..b39021f6d 100644
--- a/internal/mailer/hygiene_test.go
+++ b/internal/mailer/hygiene_test.go
@@ -176,6 +176,7 @@ func TestHeaderBlockRefusesEncodedControls(t *testing.T) {
"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 with a quoted value": msg("From: "+from, `Content-Type: text/plain; name*="a*=b"`),
"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?="),
@@ -205,6 +206,8 @@ func TestHeaderBlockRefusesEncodedControls(t *testing.T) {
"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 quoted boundary holding *=": msg("From: "+from, `Content-Type: multipart/mixed; boundary="a*=b"`),
+ "an escaped quote in a quoted value": msg("From: "+from, `Content-Type: text/plain; name="a\"*=b"`),
"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"),