Skip to content

mime.ParseMediaType can't handle non-ASCII filename #1119

@gopherbot

Description

@gopherbot

by maddogfyg:

What steps will reproduce the problem?
1.Upload a file from <input type="file" name="someinput" ......
2.If the file name include non-ASCII character(such as "付云阁.jpg", my
chinese name), the post data is like:

Content-Disposition: form-data; name="someinput";
filename="���.jpg"

But mime.ParseMediaType just return "",nil


In mime package, two funcs cause this problem:

func consumeValue(v string) (value, rest string) {
    if !strings.HasPrefix(v, `"`) {
        return consumeToken(v)
    }

    // parse a quoted-string
    rest = v[1:] // consume the leading quote
    buffer := new(bytes.Buffer)
    var idx, rune int
    var nextIsLiteral bool
    for idx, rune = range rest {
        switch {
        case nextIsLiteral:
            if rune >= 0x80 {
                return "", v
            }
            buffer.WriteRune(rune)
            nextIsLiteral = false
        case rune == '"':
            return buffer.String(), rest[idx+1:]
        case IsQText(rune):
            buffer.WriteRune(rune)
        case rune == '\\':
            nextIsLiteral = true
        default:
            return "", v
        }
    }
    return "", v
}

// IsQText returns true if rune is in 'qtext' as defined by RFC 822.
func IsQText(rune int) bool {
    // CHAR        =  <any ASCII character>        ; (  0-177,  0.-127.)
    // qtext       =  <any CHAR excepting <">,     ; => may be folded
    //                "\" & CR, and including
    //                linear-white-space>
    switch rune {
    case '"', '\\', '\r':
        return false
    }
    //return rune < 0x80
    return true
}

The RFC 822 is published in 1982, it just designed for ASCII-base system. Now
"Go" can handle utf8 very well, the 0x80 limitation is unnecessary.
So the last sentence in func IsQText, "return rune < 0x80", should be
"return true".

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions