-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
html/template: XSS risk with external JSON #15399
Comments
Means that you have verified that it is safe, not that Go makes sure that it is safe. |
Then "safe" should be defined properly. A JSON value is "JS code that is safe to run without causing any harm", but apparently not "safe to parse in an HTML context". I think right now this is a pitfall. |
@OneOfOne I still think that when it comes to security, one should err on the side of caution. |
@neelance, it looks like this XSS may be fixed by substituting |
The document says "known safe" for a reason. (Known to the
developer of the code/template to be safe, i.e. not controlled
by the outside attacker.)
In the validExternalJSON example, it's obviously not safe,
so using template.JS is not appropriate.
Because it's known safe, I don't think the template package should
do any validation or transformation on the string.
|
I always read "known to be safe" as in known to be valid JavaScript, which does not purposefully execute anything malicious. I found the fact that "it cannot contain unescaped string sequences From what I understand, I agree there is indeed a risk of unintended self-XSS and I maybe a fix is to clarify what is meant by "safe". Prior to this, I would've thought the following is "safe" JavaScript: validJS := template.JS(`var msg = "This string contains a harmless </script>, right?";`) |
The purpose of the We had this XSS vulnerability in our product. I wasn't the author, but I have to admit that if I would have reviewed this code, I would not have spotted this vulnerability. Right now I would, but that is because now I know about the "string contents can be dangerous" fact. Still, I think this fact is not intuitive and thus has a high risk of human error if the same situation has not been experienced in the past. It would be in the spirit of the |
CL https://golang.org/cl/22824 mentions this issue. |
@neelance said on the CL:
To include JSON you should parse the JSON with By its very nature, We could further document, in the case of |
I have seen this issue in production. I have a very high opinion of my coworker who wrote that code, still he did this mistake. A more inexperienced programmer might even be more likely to do this mistake. My goal is to find a way to make this more unlikely in the future and thus make this world's software a little bit more secure. I know that strict checks are not in the spirit of Go. If you really want to shoot yourself in the foot, Go allows you to do so. Still, I think that one of the goals of Go and especially of the Maybe a good solution here would be to mention the |
Where should this be mentioned? On the docs for |
English is not my first language, but maybe something like this: |
Thanks. I have updated the CL.
|
Cool, thanks! |
Go version:
go version go1.6 darwin/amd64
The following program highlights an XSS risk when injecting externally obtained JSON via
template.JS
:This is somewhat unexpected, since the documentation of
template.JS
says "JS encapsulates a known safe EcmaScript5 Expression", which intuitively any valid JSON should fulfill. The documentation should at least include a warning about this use case. Even better for avoiding this situation would be atemplate.JSON
type which automatically gets sanitized viajson.HTMLEscape
before rendering.The text was updated successfully, but these errors were encountered: