|
1 | 1 | package message |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "mime" |
| 6 | + "strings" |
5 | 7 |
|
6 | 8 | "github.com/emersion/go-message/textproto" |
7 | 9 | ) |
8 | 10 |
|
| 11 | +// MalformedHeaderError is returned alongside recovered values when a header |
| 12 | +// field was malformed but could be partially recovered (e.g. duplicate |
| 13 | +// parameters). The accompanying return values are valid and safe to use. |
| 14 | +// The Err field holds the original underlying parse error. |
| 15 | +type MalformedHeaderError struct { |
| 16 | + Err error |
| 17 | +} |
| 18 | + |
| 19 | +func (e *MalformedHeaderError) Error() string { return e.Err.Error() } |
| 20 | +func (e *MalformedHeaderError) Unwrap() error { return e.Err } |
| 21 | + |
| 22 | +// IsMalformedHeader reports whether err signals a header that was malformed |
| 23 | +// but recovered. When true, the other return values from ContentType or |
| 24 | +// ContentDisposition are valid and should be used. |
| 25 | +func IsMalformedHeader(err error) bool { |
| 26 | + return errors.As(err, new(*MalformedHeaderError)) |
| 27 | +} |
| 28 | + |
| 29 | +// deduplicateContentTypeParams returns a copy of s with duplicate parameter |
| 30 | +// names removed (first occurrence wins). It handles quoted-string values that |
| 31 | +// may contain semicolons or backslash-escaped characters. |
| 32 | +// |
| 33 | +// Two-pass design: the first pass detects whether any duplicate exists using a |
| 34 | +// stack-allocated array and strings.EqualFold (no heap allocations). The second |
| 35 | +// pass only runs — and only allocates — when a duplicate is actually found. |
| 36 | +func deduplicateContentTypeParams(s string) string { |
| 37 | + idx := strings.IndexByte(s, ';') |
| 38 | + if idx < 0 { |
| 39 | + return s |
| 40 | + } |
| 41 | + |
| 42 | + // First pass: scan param names to detect any duplicate. |
| 43 | + // The fixed-size array covers the vast majority of real Content-Type headers; |
| 44 | + // if more than 8 params are present we conservatively trigger a rebuild. |
| 45 | + var names [8]string |
| 46 | + n := 0 |
| 47 | + hasDup := false |
| 48 | + |
| 49 | + rest := s[idx:] |
| 50 | + for len(rest) > 0 && !hasDup { |
| 51 | + if rest[0] != ';' { |
| 52 | + rest = rest[1:] |
| 53 | + continue |
| 54 | + } |
| 55 | + rest = rest[1:] |
| 56 | + rest = strings.TrimLeft(rest, " \t\r\n") |
| 57 | + |
| 58 | + eqIdx := strings.IndexByte(rest, '=') |
| 59 | + if eqIdx < 0 { |
| 60 | + break |
| 61 | + } |
| 62 | + if semiBeforeEq := strings.IndexByte(rest[:eqIdx], ';'); semiBeforeEq >= 0 { |
| 63 | + rest = rest[semiBeforeEq:] |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + name := strings.TrimRight(rest[:eqIdx], " \t") |
| 68 | + rest = rest[eqIdx+1:] |
| 69 | + |
| 70 | + // Check for a duplicate before scanning past the value — if we find one |
| 71 | + // we break immediately and skip the value-scanning work entirely. |
| 72 | + if n >= len(names) { |
| 73 | + hasDup = true // more params than our array — rebuild conservatively |
| 74 | + break |
| 75 | + } |
| 76 | + for i := range n { |
| 77 | + if strings.EqualFold(names[i], name) { |
| 78 | + hasDup = true |
| 79 | + break |
| 80 | + } |
| 81 | + } |
| 82 | + if hasDup { |
| 83 | + break |
| 84 | + } |
| 85 | + names[n] = name |
| 86 | + n++ |
| 87 | + |
| 88 | + // Skip past the value to advance to the next param. |
| 89 | + if len(rest) > 0 && rest[0] == '"' { |
| 90 | + end := 1 |
| 91 | + for end < len(rest) { |
| 92 | + if rest[end] == '\\' { |
| 93 | + end += 2 |
| 94 | + } else if rest[end] == '"' { |
| 95 | + end++ |
| 96 | + break |
| 97 | + } else { |
| 98 | + end++ |
| 99 | + } |
| 100 | + } |
| 101 | + rest = rest[end:] |
| 102 | + rest = strings.TrimLeft(rest, " \t\r\n") |
| 103 | + } else if semi := strings.IndexByte(rest, ';'); semi >= 0 { |
| 104 | + rest = rest[semi:] |
| 105 | + } else { |
| 106 | + rest = "" |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if !hasDup { |
| 111 | + return s // no duplicates — return the original string unchanged |
| 112 | + } |
| 113 | + |
| 114 | + // Second pass: rebuild the string with duplicates removed. |
| 115 | + seen := make(map[string]bool) |
| 116 | + var result strings.Builder |
| 117 | + result.WriteString(s[:idx]) |
| 118 | + |
| 119 | + rest = s[idx:] |
| 120 | + for len(rest) > 0 { |
| 121 | + if rest[0] != ';' { |
| 122 | + rest = rest[1:] |
| 123 | + continue |
| 124 | + } |
| 125 | + rest = rest[1:] |
| 126 | + rest = strings.TrimLeft(rest, " \t\r\n") |
| 127 | + |
| 128 | + eqIdx := strings.IndexByte(rest, '=') |
| 129 | + if eqIdx < 0 { |
| 130 | + break |
| 131 | + } |
| 132 | + if semiBeforeEq := strings.IndexByte(rest[:eqIdx], ';'); semiBeforeEq >= 0 { |
| 133 | + rest = rest[semiBeforeEq:] |
| 134 | + continue |
| 135 | + } |
| 136 | + |
| 137 | + name := strings.TrimRight(rest[:eqIdx], " \t") |
| 138 | + rest = rest[eqIdx+1:] |
| 139 | + |
| 140 | + var value string |
| 141 | + if len(rest) > 0 && rest[0] == '"' { |
| 142 | + end := 1 |
| 143 | + for end < len(rest) { |
| 144 | + if rest[end] == '\\' { |
| 145 | + end += 2 |
| 146 | + } else if rest[end] == '"' { |
| 147 | + end++ |
| 148 | + break |
| 149 | + } else { |
| 150 | + end++ |
| 151 | + } |
| 152 | + } |
| 153 | + value = rest[:end] |
| 154 | + rest = rest[end:] |
| 155 | + rest = strings.TrimLeft(rest, " \t\r\n") |
| 156 | + } else if semi := strings.IndexByte(rest, ';'); semi >= 0 { |
| 157 | + value = strings.TrimRight(rest[:semi], " \t\r\n") |
| 158 | + rest = rest[semi:] |
| 159 | + } else { |
| 160 | + value = strings.TrimRight(rest, " \t\r\n") |
| 161 | + rest = "" |
| 162 | + } |
| 163 | + |
| 164 | + lower := strings.ToLower(name) |
| 165 | + if name != "" && !seen[lower] { |
| 166 | + seen[lower] = true |
| 167 | + result.WriteString("; ") |
| 168 | + result.WriteString(name) |
| 169 | + result.WriteByte('=') |
| 170 | + result.WriteString(value) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return result.String() |
| 175 | +} |
| 176 | + |
9 | 177 | func parseHeaderWithParams(s string) (f string, params map[string]string, err error) { |
10 | 178 | f, params, err = mime.ParseMediaType(s) |
11 | 179 | if err != nil { |
12 | | - return s, nil, err |
| 180 | + // Try recovery by removing duplicate parameter names |
| 181 | + deduped := deduplicateContentTypeParams(s) |
| 182 | + var recoveredF string |
| 183 | + var recoveredParams map[string]string |
| 184 | + recoveredF, recoveredParams, _ = mime.ParseMediaType(deduped) |
| 185 | + if recoveredParams != nil { |
| 186 | + // Wrap the original error so callers can distinguish a recovered |
| 187 | + // malformed header (where the return values are valid) from a |
| 188 | + // genuinely unparseable one (where params is nil). |
| 189 | + f = recoveredF |
| 190 | + params = recoveredParams |
| 191 | + err = &MalformedHeaderError{Err: err} |
| 192 | + } else { |
| 193 | + return s, nil, err |
| 194 | + } |
13 | 195 | } |
14 | 196 | for k, v := range params { |
15 | 197 | params[k], _ = decodeHeader(v) |
|
0 commit comments