Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mime: RFC 2045 vs 2616 confusion in FormatMediaType #43128

Open
imirkin opened this issue Dec 11, 2020 · 4 comments · May be fixed by #44899
Open

mime: RFC 2045 vs 2616 confusion in FormatMediaType #43128

imirkin opened this issue Dec 11, 2020 · 4 comments · May be fixed by #44899
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@imirkin
Copy link

imirkin commented Dec 11, 2020

So ... this is like the definition of pedantic, but:

The list of characters that can't be in tokens in RFC 2045 [MIME] is (https://tools.ietf.org/html/rfc2045 section 5.1):

()<>@,;:\"/[]?=

as implemented in https://github.com/golang/go/blob/master/src/mime/grammar.go .

The list of characters that can't be in tokens in RFC 2616 [HTTP/1.1] is (https://tools.ietf.org/html/rfc2616 section 2.2):

()<>@,;:\"/[]?={}

Note the extra 2 characters. This impacts mime.FormatMediaType which is advertised as usable for either RFC in that it might try to stick a curly brace into a string which is otherwise considered a token.

My particular use-case for this is

mime.FormatMediaType("attachment", map[string]string{"filename": filename}))

which is used as the value of the Content-Disposition header. So for example

attachment; filename=foo

is valid, while

attachment; filename=foo{

is not -- that one would require quoting, at least per the spec. See sample code in https://play.golang.org/p/TbkEuMijPPb .

I have no idea if any web browsers care about this. I happened upon this when trying to figure out how filename characters need to be escaped properly before finding this function in the go standard library and then comparing it to the spec.

@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 11, 2020
@toothrot toothrot added this to the Backlog milestone Dec 11, 2020
@toothrot
Copy link
Contributor

/cc @bradfitz @minux

@andrius4669 andrius4669 linked a pull request Mar 9, 2021 that will close this issue
@gopherbot
Copy link

Change https://golang.org/cl/300249 mentions this issue: mime: fix FormatMediaType's RFC 2616 compliance

@andrius4669
Copy link
Contributor

andrius4669 commented Mar 9, 2021

I've made 28cbb62 which should fix this.
Note that in that commit I haven't touched RFC 2231 part of escaping, so that may still include {}, since it builds on top of RFC 2045 definitions, but I don't believe that RFC 2616 specifics would apply there, as escaping syntax is sufficiently different.

@andrius4669
Copy link
Contributor

So I've dug a bit deeper, and there is RFC 6266 (section 4.1), referring to RFC 5987 (section 3.2.1) which has been superseded and deprecated by RFC 8187 (section 3.2.1).
TL;DR of that RFC chain is that { and } should be excluded from RFC 2231 style escaping too (as in, we should escape them instead of including them as is), since our use generalizes for both mail and http use cases.
Additional observation is that these RFCs (and also original RFC 2231 (section 7)) further restrict attribute name definition to exclude *, ' and %. I do not believe anyone uses attribute names including these characters so having checks for these would probably make sense too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants