Skip to content

Commit 2cc6d62

Browse files
dddentianlancetaylor
authored andcommitted
mime: correctly detect non-ASCII characters in FormatMediaType
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]>
1 parent 399fec2 commit 2cc6d62

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

src/mime/mediatype.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func FormatMediaType(t string, param map[string]string) string {
5656

5757
b.WriteByte('"')
5858
offset := 0
59-
for index, character := range value {
59+
for index, character := range []byte(value) {
6060
if character == '"' || character == '\\' {
6161
b.WriteString(value[offset:index])
6262
offset = index

src/mime/mediatype_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,8 @@ var formatTests = []formatTest{
481481
{"noslash", map[string]string{"X": "Y"}, "noslash; x=Y"}, // e.g. Content-Disposition values (RFC 2183); issue 11289
482482
{"foo bar/baz", nil, ""},
483483
{"foo/bar baz", nil, ""},
484+
{"attachment", map[string]string{"filename": "ĄĄŽŽČČŠŠ"}, ""},
485+
{"attachment", map[string]string{"filename": "ÁÁÊÊÇÇÎÎ"}, ""},
484486
{"foo/BAR", nil, "foo/bar"},
485487
{"foo/BAR", map[string]string{"X": "Y"}, "foo/bar; x=Y"},
486488
{"foo/BAR", map[string]string{"space": "With space"}, `foo/bar; space="With space"`},

0 commit comments

Comments
 (0)