-
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: <script> tags with type "text/template" now escapes EJS templates #18569
Comments
This appears to have been intentionally implemented in ffd1c78 (https://golang.org/cl/14336) by @rsc |
One possible workaround is to insert a template data key to insert the script type at evaluation time (tested on 1.8rc1) https://play.golang.org/p/HbcDfwbXwq this outputs the html you want without escaping on 1.8rc1.
|
@kennygrant that might be acceptable for smaller projects, but I have a number of these templates and also rely on some amount of template interpolation within them too. Consider:
|
Assigning to @rsc to decide whether this is an acceptable change/regression for Go 1.8. |
Wait, is the claim that "text/template" is the script type for EJS templates? When I saw that before I thought people were using that for Go templates. |
This bug is filed with respect to the string "text/template" as in |
From what I understand, marking script tags that you intend to use HTML templates as Not sure if that answers your question - let me know if I can clarify anything! Thanks @rsc |
It is a common practice to store HTML fragments inside |
OK, so what was happening before is that the <script> body was being treated as JavaScript, preserving things like < > symbols. Now it's being treated like HTML, and html/template doesn't recognize <%= %> and <% %> tags, so it de-tags them. This happens both inside and outside <script>s. The only workaround would be to tag your script javascript explicitly (or use the odd .Type hack, which probably shouldn't work).
The implementation of ffd1c78 makes scripts with unrecognized (non-JS) types treated like any other tag, meaning the body is HTML like the rest of the page. That seems about right, actually, but not here. I wonder if html/template should further understand that <% %> and <%= %> contain Javascript, or if that's too specialized. It sounds like otherwise we have to look for the </script> tag and leave everything uninterpreted until then. |
I think the new behavior is the correct one. |
Mostly I'm confused by the intention of the this statement from the html/template documentation:
Escaping content that is actually in the raw template file itself seems like it presumes that the author is also not trusted and that this package is attempting to correct something the author has done wrong. If that statement were true, then an injected Agreed that treating |
html/template must understand the meaning of the html tags in the input in order to understand which places need which escaping (for example <script> vs <div> or onclick= vs href=). My educated guess (without going searching) is that <% %> is escaped because html/template does not see it as a tag and does not want to emit something it doesn't understand that invalidates the security assertions it is making about the overall output. |
I wonder if |
I think for this release the fix is going to be to use template.HTML (or the type indirection) to insert <% ArbitraryJS %> into your output. |
Same problem for I'm switching to use template.HTML to fix this. Would really like a "just stick this text in the template output without escaping it" option that didn't care what the context was. |
Looks like possible workaround is to temporary use mimetype from the list Lines 383 to 399 in 37dbc7b
Example: <script type="application/json"
|
Go 1.8 introduced checking of script type and templates are no longer loading, as the script type is not allowed by Go 1.8 golang/go#18569 captures the details. Change template script type to one of the allowed types, it doesn't matter for clientside-haml-js, it only needs to match the script id
Based on the discussion above and on the fact that there's a known workaround for non-JS, it seems like we should accept the new behavior, namely that only javascript is escaped as javascript. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?Go 1.8
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build705350648=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
What did you do?
When emitting an EJS template (interpolation happens through the
<%=
directive) inside a Golang HTML template, the<%=
is escaped to<%=
. This does not happen on versions of Go < 1.8. The following play.golang.org link works as expected.https://play.golang.org/p/BXzy9OWSSq
What did you expect to see?
What did you see instead?
I'm content with this being a desired security addition to Golang HTML templates, but I wanted to raise this as an issue for existing users who embed Javascript templates into their Golang templates.
The text was updated successfully, but these errors were encountered: