Skip to content

mime: FormatMediaType fails to detect non-ASCII bytes #28849

@andrius4669

Description

@andrius4669

What version of Go are you using (go version)?

go version go1.11.2 linux/amd64

Does this issue reproduce with the latest release?

yeah.

What operating system and processor architecture are you using (go env)?

irrelevant.

What did you do?

https://play.golang.org/p/mkEHhue4cKm
Tried FormatMediaType with different unicode characters in parameter values.

What did you expect to see?

One of:

  1. FormatMediaType encoding parameters with any unicode characters as per RFC 2231 section 4.
  2. FormatMediaType consistently accepting all unicode characters. That's probably not really compliant with current standards, but works with few email clients I tried. I like RFC 6532, but it doesn't have any mention of Content-Type or Content-Disposition though, so this option can probably be ignored.
  3. FormatMediaType consistently rejecting all characters impossible to represent by US-ASCII.

What did you see instead?

Only characters covered by ISO-8859-1 are detected as unicode and cause encoding failure.
Characters whose rune values (actual unicode values, not UTF-8 encoded) aren't & 0x80 pass test and are included in output.

Additional info & reporter's opinion

I noticed this looking at source code of FormatMediaType and noticing that it contains if character&0x80 != 0 check, however it iterates thru string with range value (not range []byte(value) which would be correct).
I really don't like that it doesn't at least support RFC 2231 encoding, now I will either need to reimplement this function myself, or encode filename parameter using either mime.BEncoding or mime.QEncoding.
Adding proper check there and failing to encode could cause some issues if implementations relied on old broken behavior, I guess.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions