Skip to content

Commit

Permalink
mime: correctly detect non-ASCII characters in FormatMediaType
Browse files Browse the repository at this point in the history
FormatMediaType used rune&0x80==0 to check if parameter values consisted
of valid ascii charaters. Comparing strings using their runes instead of
their bytes leads to some non-ascii strings to pass as valid.

E.g. the rune for 'Ą' is 0x104, 0x104 & 0x80 => 0. Its byte
representation is 0xc4 0x84, both of which result in non zero values
when masked with 0x80

Fixes #28849

Change-Id: Ib9fb4968bcbbec0197d81136f380d40a2a56c14b
Reviewed-on: https://go-review.googlesource.com/c/150417
Run-TryBot: Ian Lance Taylor <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
dddent authored and ianlancetaylor committed Nov 20, 2018
1 parent 399fec2 commit 2cc6d62
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/mime/mediatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func FormatMediaType(t string, param map[string]string) string {

b.WriteByte('"')
offset := 0
for index, character := range value {
for index, character := range []byte(value) {
if character == '"' || character == '\\' {
b.WriteString(value[offset:index])
offset = index
Expand Down
2 changes: 2 additions & 0 deletions src/mime/mediatype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ var formatTests = []formatTest{
{"noslash", map[string]string{"X": "Y"}, "noslash; x=Y"}, // e.g. Content-Disposition values (RFC 2183); issue 11289
{"foo bar/baz", nil, ""},
{"foo/bar baz", nil, ""},
{"attachment", map[string]string{"filename": "ĄĄŽŽČČŠŠ"}, ""},
{"attachment", map[string]string{"filename": "ÁÁÊÊÇÇÎÎ"}, ""},
{"foo/BAR", nil, "foo/bar"},
{"foo/BAR", map[string]string{"X": "Y"}, "foo/bar; x=Y"},
{"foo/BAR", map[string]string{"space": "With space"}, `foo/bar; space="With space"`},
Expand Down

0 comments on commit 2cc6d62

Please sign in to comment.