During fuzzing some custom HTTP request handling of my own I discovered a Content-Type #;*0="" which mime.ParseMediaType is happy to parse more or less as I would expect without error, but for which mime.FormatMediaType returns an empty string indicating some part of it is invalid.

I'm not familiar enough with the details of the relevant standards to say which of the two is correct - I seriously doubt anyone needs it to be valid - but these two functions should probably agree on whether it's valid or not.

It seems possibly related to, but the impact is distinct from, #43128.

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

$ go version
go version go1.19.3 linux/amd64

But also reproducible on go.dev.

What did you do?

https://go.dev/play/p/oeqtYtq72MY

    t, param, err := mime.ParseMediaType(`#;*0=""`)
    fmt.Printf("%q, %+v, %+v\n", t, param, err)
    fmt.Printf("%q", mime.FormatMediaType(t, param))

What did you expect to see?

An error during parsing, or some non-empty equivalent media type when reformatted:

"#", map[:], <nil>
"#;*0=\"\""

What did you see instead?

"#", map[:], <nil>
""

Comment From: mknyszek

CC @neild

Comment From: seankhliao

My understanding is that the current behaviour is correct. *0 is RFC 2231 continuation and has no semantic meaning, and an empty key-value pair encodes to nothing as well.