Skip to content

mediatype: dedup parameters by exact name, not substring - #406

Merged
jhillyerd merged 1 commit into
jhillyerd:mainfrom
binggao1230:fix-mediatype-substring-param-drop
Jul 30, 2026
Merged

mediatype: dedup parameters by exact name, not substring#406
jhillyerd merged 1 commit into
jhillyerd:mainfrom
binggao1230:fix-mediatype-substring-param-drop

Conversation

@binggao1230

Copy link
Copy Markdown
Contributor

fixMangledMediaType deduplicates media-type parameters (added for #162) with a substring test:

if strings.Contains(mtype, strings.TrimSpace(pair[0])) { // pair[0] == "name="
    continue
}

mtype is the parameter section accumulated so far, so a distinct parameter is dropped whenever its "<name>=" token appears in the earlier text — usually when the name is a suffix of an earlier one (name after filename, id after uuid, type after subtype), or when "<name>=" occurs inside an earlier value. Since filename and name co-occur in most attachment headers, application/octet-stream; filename="a"; name="b" loses name and the attachment filename is silently discarded.

These are all well-formed RFC 2045 headers that Go's mime.ParseMediaType returns intact, so it's a clean oracle for the intended result:

application/octet-stream; filename="a.txt"; name="b.txt"   -> name dropped
text/x; uuid="u"; id="7"                                    -> id dropped
text/plain; note="size=5"; size="10"                        -> size dropped

Fix: track seen parameter names in an exact, case-insensitive set (RFC 2045 §5.1 makes names case-insensitive) and skip only true duplicate keys. Exact repeats still collapse (the #162 behavior); distinct parameters are kept.

I diffed a broader well-formed battery (varied param order, quoted values containing ;/=, RFC 2231 extended params, encoded-words) against mime.ParseMediaType — the substring dedup was the only divergence; fixUnquotedSpecials, fixUnescapedQuotes and removeTrailingHTMLTags matched stdlib on every well-formed input. (#401 edits the same file but a different seam — RFC 2231 multi-segment reassembly — not this dedup.)

Added TestParseMediaType / TestFixMangledMediaType cases plus an end-to-end ReadParts test asserting the attachment FileName survives. go test ./... passes.

fixMangledMediaType dropped a distinct parameter whenever its "<name>="
token was a substring of the accumulated parameter string -- most often
when the name is a suffix of an earlier one (name after filename, id
after uuid, type after subtype) or appears inside an earlier value. This
silently discarded valid parameters that Go's mime.ParseMediaType keeps;
e.g. `filename="a"; name="b"` lost `name`, dropping attachment filenames.

Track seen parameter names in an exact, case-insensitive set (RFC 2045
section 5.1) so only true duplicate keys are removed, preserving the jhillyerd#162
behavior of collapsing exact repeats.
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 86.407% (+0.007%) from 86.4% — gaoflow:fix-mediatype-substring-param-drop into jhillyerd:main

@jhillyerd
jhillyerd merged commit f3d7490 into jhillyerd:main Jul 30, 2026
7 checks passed
@jhillyerd

Copy link
Copy Markdown
Owner

Nice, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants