-
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: user-supplied escapers can undermine contextual auto-escaping #19336
Comments
One way to fix this behavior is to do away with the escaper-merging behavior, and delete existing escapers from the pipeline if they are equivalent to any of the inserted escapers. For example, the following pipeline:
should be rewritten as
Since Caveat: This change will break existing html/template users who rely on this implied escaper-merging behavior. However, this security issue probably justifies this compatibility breakage. This change will only affect potentially insecure templates (i.e. templates whose rewritten pipelines do not end with the inserted escapers). |
After finding several other bugs associated with this escaper-merging logic, I think the best and simplest way to solve this would be to remove all user-supplied escapers (i.e. |
CL https://golang.org/cl/37880 mentions this issue. |
version: go version devel +4cce27a3fa Sat Jan 21 03:20:55 2017 +0000 linux/amd64
env: linux amd64
html/template rewrites pipelines and adds calls to the escaping functions necessary to correctly and safely embed the output of these pipelines in their HTML contexts. For example, the following pipeline:
is rewritten as
by the html/template escaper, where
formatAsLink
is the name of a user-defined function, and the rest of the_html_*
names are names of internally-defined escapers.However, if the pipeline already contains any of the predefined escaper functions (i.e.
html
orurlquery
), and some of the escapers-to-be-inserted are considered equivalent, the html/template escaper will attempt to merge the inserted escapers with the existing predefined escapers, in order. For example,is rewritten as
since
_html_template_nospaceescaper
andhtml
are considered equivalent, and the escaper preserves the order of the escaping operations (i.e. URL escaping first, then HTML escaping). However, in this example, the user-definedformatAsLink
function determines the final output of the pipeline. IfformatAsLink
contains a bug that causes it to emit an unsafe URL, the rendered HTML will be vulnerable to Cross-Site Scripting (XSS).In general, users can shoot themselves in the foot if they manually escape their pipelines with
html
orurlquery
because of this escaper-merging behavior. This behavior should either be explicitly documented or changed.The text was updated successfully, but these errors were encountered: