aboutsummaryrefslogtreecommitdiff
path: root/src/text
diff options
context:
space:
mode:
authorSamuel Tan <samueltan@google.com>2017-04-17 16:10:54 -0700
committerBrad Fitzpatrick <bradfitz@golang.org>2017-05-05 18:56:31 +0000
commit3a2fee0389e8459e482e987a98f226a71c2ad5fb (patch)
treeca3fd9da0570b53a7eb7e9c5d208c99382c76b9b /src/text
parent1acff5fe61013d4f5b1ed6602654abbbe73b1599 (diff)
downloadgo-3a2fee0389e8459e482e987a98f226a71c2ad5fb.tar.xz
html/template: allow safe usage of predefined escapers in pipelines
Allow the predefined escapers "html", "urlquery", and "js" to be used in pipelines when they have no potential to affect the correctness or safety of the escaped pipeline output. Specifically: - "urlquery" may be used if it is the last command in the pipeline. - "html" may be used if it is the last command in the pipeline, and the pipeline does not occur in an unquoted HTML attribute value context. - "js" may be used in any pipeline, since it does not affect the merging of contextual escapers. This change will loosens the restrictions on predefined escapers introduced in golang.org/cl/37880, which will hopefully ease the upgrade path for existing template users. This change brings back the escaper-merging logic, and associated unit tests, that were removed in golang.org/cl/37880. However, a few notable changes have been made: - "_html_template_nospaceescaper" is no longer considered equivalent to "html", since the former escapes spaces, while the latter does not (see #19345). This change should not silently break any templates, since pipelines where this substituion will happen will already trigger an explicit error. - An "_eval_args_" internal directive has been added to handle pipelines containing a single explicit call to a predefined escaper, e.g. {{html .X}} (see #19353). Also, the HTMLEscape function called by the predefined text/template "html" function now escapes the NULL character as well. This effectively makes it as secure as the internal html/template HTML escapers (see #19345). While this change is backward-incompatible, it will only affect illegitimate uses of this escaper, since the NULL character is always illegal in valid HTML. Fixes #19952 Change-Id: I9b5570a80a3ea284b53901e6a1f842fc59b33d3a Reviewed-on: https://go-review.googlesource.com/40936 Reviewed-by: Russ Cox <rsc@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/text')
-rw-r--r--src/text/template/doc.go5
-rw-r--r--src/text/template/funcs.go5
2 files changed, 8 insertions, 2 deletions
diff --git a/src/text/template/doc.go b/src/text/template/doc.go
index b35fe39ecc..23d58cf686 100644
--- a/src/text/template/doc.go
+++ b/src/text/template/doc.go
@@ -315,7 +315,8 @@ Predefined global functions are named as follows.
or the returned error value is non-nil, execution stops.
html
Returns the escaped HTML equivalent of the textual
- representation of its arguments.
+ representation of its arguments. This function is unavailable
+ in html/template, with a few exceptions.
index
Returns the result of indexing its first argument by the
following arguments. Thus "index x 1 2 3" is, in Go syntax,
@@ -341,6 +342,8 @@ Predefined global functions are named as follows.
urlquery
Returns the escaped value of the textual representation of
its arguments in a form suitable for embedding in a URL query.
+ This function is unavailable in html/template, with a few
+ exceptions.
The boolean functions take any zero value to be false and a non-zero
value to be true.
diff --git a/src/text/template/funcs.go b/src/text/template/funcs.go
index 3047b272e5..9107431037 100644
--- a/src/text/template/funcs.go
+++ b/src/text/template/funcs.go
@@ -489,6 +489,7 @@ var (
htmlAmp = []byte("&amp;")
htmlLt = []byte("&lt;")
htmlGt = []byte("&gt;")
+ htmlNull = []byte("\uFFFD")
)
// HTMLEscape writes to w the escaped HTML equivalent of the plain text data b.
@@ -497,6 +498,8 @@ func HTMLEscape(w io.Writer, b []byte) {
for i, c := range b {
var html []byte
switch c {
+ case '\000':
+ html = htmlNull
case '"':
html = htmlQuot
case '\'':
@@ -520,7 +523,7 @@ func HTMLEscape(w io.Writer, b []byte) {
// HTMLEscapeString returns the escaped HTML equivalent of the plain text data s.
func HTMLEscapeString(s string) string {
// Avoid allocation if we can.
- if !strings.ContainsAny(s, `'"&<>`) {
+ if !strings.ContainsAny(s, "'\"&<>\000") {
return s
}
var b bytes.Buffer