Skip to content

strconv: add HTML quoting for string returned by strconv.ParseInt error #13127

@ianlancetaylor

Description

@ianlancetaylor

We have a report of a potential XSS vulnerability due to code that does, effectively, this:

num, err := strconv.Atoi(r.URL.Query().Get("field"))
    if err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
}

The issue is that strconv.Atoi and friends return the string they are trying to parse as part of the error value, and it is included in the err.Error string. http.Error adds headers that are intended to ensure that returned page is not interpreted as HTML, but reportedly some HTML readers ignore these options.

Perhaps this kind of code should be more careful, or perhaps strconv.ParseInt should not return the input in the error message, or perhaps HTML readers should not be buggy. But we are where we are. We don't necessarily have to change anything in the Go library, but it occurs to me that there is one relatively simple way to help code vulnerable to this sort of attack: quote all non-ASCII characters in the error message, and also quote any angle brackets. Since these characters can not reasonably appear in strings passed to strconv.Atoi and friends, this doesn't seem like a significant handicap, and should fit within the Go 1 compatibility guarantee.

I have a CL for this that I will send out. I've opened this issue for discussion in case people think this change might be unwise or unwarranted.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions