-
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: Paths in attributes not escaped correctly #38837
Comments
It seems what I am observing should only be happening if I would do:
|
/cc @mikesamuel @empijei per owners. |
Thanks for reporting this. The template package has in its threat model for URL only to make it impossible to inject exotic protocols like The escaping you are pointing out in your example is more of a semantic escaping that has hardly something to do with XSS: you are correct in stating that the path could be escaped by the package, but I would argue that this should not be done. URLs can be specified by user code to have dynamic paths safely and this is a feature of the contextual autoescaper. Patterns like Could you please provide an example of an attack that would rely on this issue to cause an XSS (or other attacks)? |
I too just faced and was surprised about this "lack" of escaping path elements. IMO it should only skip escaping if I ended up registering a custom
This will properly output (try it on the Go Playground):
|
Hi, thanks a lot for the answer. It's not about an XSS attack that would be possible. The problem is, illustrated using your example: What is impossible is to get the behavior where it percent-encodes it. So the current behavior is redundant and makes |
This package tries to protect its user from potential attacks. I don't understand what is the threat you are surfacing. If there is no threat, is your request to make it possible to percent-escape urls? Adding automatic escaping would break a lot of already existing code for a benefit that I fail to see and if we were to change the API as you suggest it would become quite hard to obtain the current behavior without introducing vulnerabilities. |
As you mentioned it is impossible to change the behavior of the package here as it would break existing use-cases, so I'll close the issue as it's impossible to do anything about it. Maybe a good idea to prevent any future confusion on this would be to show an example of this case in the documentation to make it clearer that no path escaping is happening. |
Do you have an idea on a precise change you'd apply to the documentation? I am generally in favor on disambiguation in the doc, but at the same time I don't want the doc to need a TL;DR. Let's keep this open and just change the title to reflect that this is now a discussion on doc changes. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
The html path escaper does not work properly, so paths are not properly escaped:
Playground
This snippet should do
url.PathEscape
on the variable used in the attribute,but it does some variation that does not properly escapes the
/
character,leading to incorrect URLs.
What did you expect to see?
This snippet should do
url.PathEscape
on the variable used in the attributes path,resulting in
Example%2FGenre
.What did you see instead?
it does some variation of a PathEscape, which does not properly escapes the
/
character,leading to incorrect URLs:
There is no easy workaround either I could find, that would lead to it correctly escaping this, as per the documentation it seems it assumes manual escaping is a security issue and should never be done? I could not find any function to escape the variable correctly in my template.
While I can wrap it in a different type to prevent escaping, there is no way to enforce escaping, except giving up on the html templating and do text templating instead which is a lot more dangerous as I would need to take care of all escaping myself. Maybe I am missing something obvious here? If so, clearly the documentation is lacking.
The text was updated successfully, but these errors were encountered: