...

Source file src/html/template/escape_test.go

Documentation: html/template

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package template
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/json"
    10  	"fmt"
    11  	"os"
    12  	"strings"
    13  	"testing"
    14  	"text/template"
    15  	"text/template/parse"
    16  )
    17  
    18  type badMarshaler struct{}
    19  
    20  func (x *badMarshaler) MarshalJSON() ([]byte, error) {
    21  	// Keys in valid JSON must be double quoted as must all strings.
    22  	return []byte("{ foo: 'not quite valid JSON' }"), nil
    23  }
    24  
    25  type goodMarshaler struct{}
    26  
    27  func (x *goodMarshaler) MarshalJSON() ([]byte, error) {
    28  	return []byte(`{ "<foo>": "O'Reilly" }`), nil
    29  }
    30  
    31  func TestEscape(t *testing.T) {
    32  	data := struct {
    33  		F, T       bool
    34  		C, G, H, I string
    35  		A, E       []string
    36  		B, M       json.Marshaler
    37  		N          int
    38  		U          any  // untyped nil
    39  		Z          *int // typed nil
    40  		W          HTML
    41  	}{
    42  		F: false,
    43  		T: true,
    44  		C: "<Cincinnati>",
    45  		G: "<Goodbye>",
    46  		H: "<Hello>",
    47  		A: []string{"<a>", "<b>"},
    48  		E: []string{},
    49  		N: 42,
    50  		B: &badMarshaler{},
    51  		M: &goodMarshaler{},
    52  		U: nil,
    53  		Z: nil,
    54  		W: HTML(`&iexcl;<b class="foo">Hello</b>, <textarea>O'World</textarea>!`),
    55  		I: "${ asd `` }",
    56  	}
    57  	pdata := &data
    58  
    59  	tests := []struct {
    60  		name   string
    61  		input  string
    62  		output string
    63  	}{
    64  		{
    65  			"if",
    66  			"{{if .T}}Hello{{end}}, {{.C}}!",
    67  			"Hello, &lt;Cincinnati&gt;!",
    68  		},
    69  		{
    70  			"else",
    71  			"{{if .F}}{{.H}}{{else}}{{.G}}{{end}}!",
    72  			"&lt;Goodbye&gt;!",
    73  		},
    74  		{
    75  			"overescaping1",
    76  			"Hello, {{.C | html}}!",
    77  			"Hello, &lt;Cincinnati&gt;!",
    78  		},
    79  		{
    80  			"overescaping2",
    81  			"Hello, {{html .C}}!",
    82  			"Hello, &lt;Cincinnati&gt;!",
    83  		},
    84  		{
    85  			"overescaping3",
    86  			"{{with .C}}{{$msg := .}}Hello, {{$msg}}!{{end}}",
    87  			"Hello, &lt;Cincinnati&gt;!",
    88  		},
    89  		{
    90  			"assignment",
    91  			"{{if $x := .H}}{{$x}}{{end}}",
    92  			"&lt;Hello&gt;",
    93  		},
    94  		{
    95  			"withBody",
    96  			"{{with .H}}{{.}}{{end}}",
    97  			"&lt;Hello&gt;",
    98  		},
    99  		{
   100  			"withElse",
   101  			"{{with .E}}{{.}}{{else}}{{.H}}{{end}}",
   102  			"&lt;Hello&gt;",
   103  		},
   104  		{
   105  			"rangeBody",
   106  			"{{range .A}}{{.}}{{end}}",
   107  			"&lt;a&gt;&lt;b&gt;",
   108  		},
   109  		{
   110  			"rangeElse",
   111  			"{{range .E}}{{.}}{{else}}{{.H}}{{end}}",
   112  			"&lt;Hello&gt;",
   113  		},
   114  		{
   115  			"nonStringValue",
   116  			"{{.T}}",
   117  			"true",
   118  		},
   119  		{
   120  			"untypedNilValue",
   121  			"{{.U}}",
   122  			"",
   123  		},
   124  		{
   125  			"typedNilValue",
   126  			"{{.Z}}",
   127  			"&lt;nil&gt;",
   128  		},
   129  		{
   130  			"constant",
   131  			`<a href="/search?q={{"'a<b'"}}">`,
   132  			`<a href="/search?q=%27a%3cb%27">`,
   133  		},
   134  		{
   135  			"multipleAttrs",
   136  			"<a b=1 c={{.H}}>",
   137  			"<a b=1 c=&lt;Hello&gt;>",
   138  		},
   139  		{
   140  			"urlStartRel",
   141  			`<a href='{{"/foo/bar?a=b&c=d"}}'>`,
   142  			`<a href='/foo/bar?a=b&amp;c=d'>`,
   143  		},
   144  		{
   145  			"urlStartAbsOk",
   146  			`<a href='{{"http://example.com/foo/bar?a=b&c=d"}}'>`,
   147  			`<a href='http://example.com/foo/bar?a=b&amp;c=d'>`,
   148  		},
   149  		{
   150  			"protocolRelativeURLStart",
   151  			`<a href='{{"//example.com:8000/foo/bar?a=b&c=d"}}'>`,
   152  			`<a href='//example.com:8000/foo/bar?a=b&amp;c=d'>`,
   153  		},
   154  		{
   155  			"pathRelativeURLStart",
   156  			`<a href="{{"/javascript:80/foo/bar"}}">`,
   157  			`<a href="/javascript:80/foo/bar">`,
   158  		},
   159  		{
   160  			"dangerousURLStart",
   161  			`<a href='{{"javascript:alert(%22pwned%22)"}}'>`,
   162  			`<a href='#ZgotmplZ'>`,
   163  		},
   164  		{
   165  			"dangerousURLStart2",
   166  			`<a href='  {{"javascript:alert(%22pwned%22)"}}'>`,
   167  			`<a href='  #ZgotmplZ'>`,
   168  		},
   169  		{
   170  			"nonHierURL",
   171  			`<a href={{"mailto:Muhammed \"The Greatest\" Ali <m.ali@example.com>"}}>`,
   172  			`<a href=mailto:Muhammed%20%22The%20Greatest%22%20Ali%20%3cm.ali@example.com%3e>`,
   173  		},
   174  		{
   175  			"urlPath",
   176  			`<a href='http://{{"javascript:80"}}/foo'>`,
   177  			`<a href='http://javascript:80/foo'>`,
   178  		},
   179  		{
   180  			"urlQuery",
   181  			`<a href='/search?q={{.H}}'>`,
   182  			`<a href='/search?q=%3cHello%3e'>`,
   183  		},
   184  		{
   185  			"urlFragment",
   186  			`<a href='/faq#{{.H}}'>`,
   187  			`<a href='/faq#%3cHello%3e'>`,
   188  		},
   189  		{
   190  			"urlBranch",
   191  			`<a href="{{if .F}}/foo?a=b{{else}}/bar{{end}}">`,
   192  			`<a href="/bar">`,
   193  		},
   194  		{
   195  			"urlBranchConflictMoot",
   196  			`<a href="{{if .T}}/foo?a={{else}}/bar#{{end}}{{.C}}">`,
   197  			`<a href="/foo?a=%3cCincinnati%3e">`,
   198  		},
   199  		{
   200  			"jsStrValue",
   201  			"<button onclick='alert({{.H}})'>",
   202  			`<button onclick='alert(&#34;\u003cHello\u003e&#34;)'>`,
   203  		},
   204  		{
   205  			"jsNumericValue",
   206  			"<button onclick='alert({{.N}})'>",
   207  			`<button onclick='alert( 42 )'>`,
   208  		},
   209  		{
   210  			"jsBoolValue",
   211  			"<button onclick='alert({{.T}})'>",
   212  			`<button onclick='alert( true )'>`,
   213  		},
   214  		{
   215  			"jsNilValueTyped",
   216  			"<button onclick='alert(typeof{{.Z}})'>",
   217  			`<button onclick='alert(typeof null )'>`,
   218  		},
   219  		{
   220  			"jsNilValueUntyped",
   221  			"<button onclick='alert(typeof{{.U}})'>",
   222  			`<button onclick='alert(typeof null )'>`,
   223  		},
   224  		{
   225  			"jsObjValue",
   226  			"<button onclick='alert({{.A}})'>",
   227  			`<button onclick='alert([&#34;\u003ca\u003e&#34;,&#34;\u003cb\u003e&#34;])'>`,
   228  		},
   229  		{
   230  			"jsObjValueScript",
   231  			"<script>alert({{.A}})</script>",
   232  			`<script>alert(["\u003ca\u003e","\u003cb\u003e"])</script>`,
   233  		},
   234  		{
   235  			"scriptTypeSpace",
   236  			"<script type=\" \">{{.H}}</script>",
   237  			"<script type=\" \">\"\\u003cHello\\u003e\"</script>",
   238  		},
   239  		{
   240  			"scriptTypeTab",
   241  			"<script type=\"\t\">{{.H}}</script>",
   242  			"<script type=\"\t\">\"\\u003cHello\\u003e\"</script>",
   243  		},
   244  		{
   245  			"scriptTypeEmpty",
   246  			"<script type=\"\">{{.H}}</script>",
   247  			"<script type=\"\">\"\\u003cHello\\u003e\"</script>",
   248  		},
   249  		{
   250  			"jsObjValueNotOverEscaped",
   251  			"<button onclick='alert({{.A | html}})'>",
   252  			`<button onclick='alert([&#34;\u003ca\u003e&#34;,&#34;\u003cb\u003e&#34;])'>`,
   253  		},
   254  		{
   255  			"jsStr",
   256  			"<button onclick='alert(&quot;{{.H}}&quot;)'>",
   257  			`<button onclick='alert(&quot;\u003cHello\u003e&quot;)'>`,
   258  		},
   259  		{
   260  			"badMarshaler",
   261  			`<button onclick='alert(1/{{.B}}in numbers)'>`,
   262  			`<button onclick='alert(1/ /* json: error calling MarshalJSON for type *template.badMarshaler: invalid character &#39;f&#39; looking for beginning of object key string */null in numbers)'>`,
   263  		},
   264  		{
   265  			"jsMarshaler",
   266  			`<button onclick='alert({{.M}})'>`,
   267  			`<button onclick='alert({&#34;\u003cfoo\u003e&#34;:&#34;O&#39;Reilly&#34;})'>`,
   268  		},
   269  		{
   270  			"jsStrNotUnderEscaped",
   271  			"<button onclick='alert({{.C | urlquery}})'>",
   272  			// URL escaped, then quoted for JS.
   273  			`<button onclick='alert(&#34;%3CCincinnati%3E&#34;)'>`,
   274  		},
   275  		{
   276  			"jsRe",
   277  			`<button onclick='alert(/{{"foo+bar"}}/.test(""))'>`,
   278  			`<button onclick='alert(/foo\u002bbar/.test(""))'>`,
   279  		},
   280  		{
   281  			"jsReBlank",
   282  			`<script>alert(/{{""}}/.test(""));</script>`,
   283  			`<script>alert(/(?:)/.test(""));</script>`,
   284  		},
   285  		{
   286  			"jsReAmbigOk",
   287  			`<script>{{if true}}var x = 1{{end}}</script>`,
   288  			// The {if} ends in an ambiguous jsCtx but there is
   289  			// no slash following so we shouldn't care.
   290  			`<script>var x = 1</script>`,
   291  		},
   292  		{
   293  			"styleBidiKeywordPassed",
   294  			`<p style="dir: {{"ltr"}}">`,
   295  			`<p style="dir: ltr">`,
   296  		},
   297  		{
   298  			"styleBidiPropNamePassed",
   299  			`<p style="border-{{"left"}}: 0; border-{{"right"}}: 1in">`,
   300  			`<p style="border-left: 0; border-right: 1in">`,
   301  		},
   302  		{
   303  			"styleExpressionBlocked",
   304  			`<p style="width: {{"expression(alert(1337))"}}">`,
   305  			`<p style="width: ZgotmplZ">`,
   306  		},
   307  		{
   308  			"styleTagSelectorPassed",
   309  			`<style>{{"p"}} { color: pink }</style>`,
   310  			`<style>p { color: pink }</style>`,
   311  		},
   312  		{
   313  			"styleIDPassed",
   314  			`<style>p{{"#my-ID"}} { font: Arial }</style>`,
   315  			`<style>p#my-ID { font: Arial }</style>`,
   316  		},
   317  		{
   318  			"styleClassPassed",
   319  			`<style>p{{".my_class"}} { font: Arial }</style>`,
   320  			`<style>p.my_class { font: Arial }</style>`,
   321  		},
   322  		{
   323  			"styleQuantityPassed",
   324  			`<a style="left: {{"2em"}}; top: {{0}}">`,
   325  			`<a style="left: 2em; top: 0">`,
   326  		},
   327  		{
   328  			"stylePctPassed",
   329  			`<table style=width:{{"100%"}}>`,
   330  			`<table style=width:100%>`,
   331  		},
   332  		{
   333  			"styleColorPassed",
   334  			`<p style="color: {{"#8ff"}}; background: {{"#000"}}">`,
   335  			`<p style="color: #8ff; background: #000">`,
   336  		},
   337  		{
   338  			"styleObfuscatedExpressionBlocked",
   339  			`<p style="width: {{"  e\\78preS\x00Sio/**/n(alert(1337))"}}">`,
   340  			`<p style="width: ZgotmplZ">`,
   341  		},
   342  		{
   343  			"styleMozBindingBlocked",
   344  			`<p style="{{"-moz-binding(alert(1337))"}}: ...">`,
   345  			`<p style="ZgotmplZ: ...">`,
   346  		},
   347  		{
   348  			"styleObfuscatedMozBindingBlocked",
   349  			`<p style="{{"  -mo\\7a-B\x00I/**/nding(alert(1337))"}}: ...">`,
   350  			`<p style="ZgotmplZ: ...">`,
   351  		},
   352  		{
   353  			"styleFontNameString",
   354  			`<p style='font-family: "{{"Times New Roman"}}"'>`,
   355  			`<p style='font-family: "Times New Roman"'>`,
   356  		},
   357  		{
   358  			"styleFontNameString",
   359  			`<p style='font-family: "{{"Times New Roman"}}", "{{"sans-serif"}}"'>`,
   360  			`<p style='font-family: "Times New Roman", "sans-serif"'>`,
   361  		},
   362  		{
   363  			"styleFontNameUnquoted",
   364  			`<p style='font-family: {{"Times New Roman"}}'>`,
   365  			`<p style='font-family: Times New Roman'>`,
   366  		},
   367  		{
   368  			"styleURLQueryEncoded",
   369  			`<p style="background: url(/img?name={{"O'Reilly Animal(1)<2>.png"}})">`,
   370  			`<p style="background: url(/img?name=O%27Reilly%20Animal%281%29%3c2%3e.png)">`,
   371  		},
   372  		{
   373  			"styleQuotedURLQueryEncoded",
   374  			`<p style="background: url('/img?name={{"O'Reilly Animal(1)<2>.png"}}')">`,
   375  			`<p style="background: url('/img?name=O%27Reilly%20Animal%281%29%3c2%3e.png')">`,
   376  		},
   377  		{
   378  			"styleStrQueryEncoded",
   379  			`<p style="background: '/img?name={{"O'Reilly Animal(1)<2>.png"}}'">`,
   380  			`<p style="background: '/img?name=O%27Reilly%20Animal%281%29%3c2%3e.png'">`,
   381  		},
   382  		{
   383  			"styleURLBadProtocolBlocked",
   384  			`<a style="background: url('{{"javascript:alert(1337)"}}')">`,
   385  			`<a style="background: url('#ZgotmplZ')">`,
   386  		},
   387  		{
   388  			"styleStrBadProtocolBlocked",
   389  			`<a style="background: '{{"vbscript:alert(1337)"}}'">`,
   390  			`<a style="background: '#ZgotmplZ'">`,
   391  		},
   392  		{
   393  			"styleStrEncodedProtocolEncoded",
   394  			`<a style="background: '{{"javascript\\3a alert(1337)"}}'">`,
   395  			// The CSS string 'javascript\\3a alert(1337)' does not contain a colon.
   396  			`<a style="background: 'javascript\\3a alert\28 1337\29 '">`,
   397  		},
   398  		{
   399  			"styleURLGoodProtocolPassed",
   400  			`<a style="background: url('{{"http://oreilly.com/O'Reilly Animals(1)<2>;{}.html"}}')">`,
   401  			`<a style="background: url('http://oreilly.com/O%27Reilly%20Animals%281%29%3c2%3e;%7b%7d.html')">`,
   402  		},
   403  		{
   404  			"styleStrGoodProtocolPassed",
   405  			`<a style="background: '{{"http://oreilly.com/O'Reilly Animals(1)<2>;{}.html"}}'">`,
   406  			`<a style="background: 'http\3a\2f\2foreilly.com\2fO\27Reilly Animals\28 1\29\3c 2\3e\3b\7b\7d.html'">`,
   407  		},
   408  		{
   409  			"styleURLEncodedForHTMLInAttr",
   410  			`<a style="background: url('{{"/search?img=foo&size=icon"}}')">`,
   411  			`<a style="background: url('/search?img=foo&amp;size=icon')">`,
   412  		},
   413  		{
   414  			"styleURLNotEncodedForHTMLInCdata",
   415  			`<style>body { background: url('{{"/search?img=foo&size=icon"}}') }</style>`,
   416  			`<style>body { background: url('/search?img=foo&size=icon') }</style>`,
   417  		},
   418  		{
   419  			"styleURLMixedCase",
   420  			`<p style="background: URL(#{{.H}})">`,
   421  			`<p style="background: URL(#%3cHello%3e)">`,
   422  		},
   423  		{
   424  			"stylePropertyPairPassed",
   425  			`<a style='{{"color: red"}}'>`,
   426  			`<a style='color: red'>`,
   427  		},
   428  		{
   429  			"styleStrSpecialsEncoded",
   430  			`<a style="font-family: '{{"/**/'\";:// \\"}}', &quot;{{"/**/'\";:// \\"}}&quot;">`,
   431  			`<a style="font-family: '\2f**\2f\27\22\3b\3a\2f\2f  \\', &quot;\2f**\2f\27\22\3b\3a\2f\2f  \\&quot;">`,
   432  		},
   433  		{
   434  			"styleURLSpecialsEncoded",
   435  			`<a style="border-image: url({{"/**/'\";:// \\"}}), url(&quot;{{"/**/'\";:// \\"}}&quot;), url('{{"/**/'\";:// \\"}}'), 'http://www.example.com/?q={{"/**/'\";:// \\"}}''">`,
   436  			`<a style="border-image: url(/**/%27%22;://%20%5c), url(&quot;/**/%27%22;://%20%5c&quot;), url('/**/%27%22;://%20%5c'), 'http://www.example.com/?q=%2f%2a%2a%2f%27%22%3b%3a%2f%2f%20%5c''">`,
   437  		},
   438  		{
   439  			"HTML comment",
   440  			"<b>Hello, <!-- name of world -->{{.C}}</b>",
   441  			"<b>Hello, &lt;Cincinnati&gt;</b>",
   442  		},
   443  		{
   444  			"HTML comment not first < in text node.",
   445  			"<<!-- -->!--",
   446  			"&lt;!--",
   447  		},
   448  		{
   449  			"HTML normalization 1",
   450  			"a < b",
   451  			"a &lt; b",
   452  		},
   453  		{
   454  			"HTML normalization 2",
   455  			"a << b",
   456  			"a &lt;&lt; b",
   457  		},
   458  		{
   459  			"HTML normalization 3",
   460  			"a<<!-- --><!-- -->b",
   461  			"a&lt;b",
   462  		},
   463  		{
   464  			"HTML doctype not normalized",
   465  			"<!DOCTYPE html>Hello, World!",
   466  			"<!DOCTYPE html>Hello, World!",
   467  		},
   468  		{
   469  			"HTML doctype not case-insensitive",
   470  			"<!doCtYPE htMl>Hello, World!",
   471  			"<!doCtYPE htMl>Hello, World!",
   472  		},
   473  		{
   474  			"No doctype injection",
   475  			`<!{{"DOCTYPE"}}`,
   476  			"&lt;!DOCTYPE",
   477  		},
   478  		{
   479  			"Split HTML comment",
   480  			"<b>Hello, <!-- name of {{if .T}}city -->{{.C}}{{else}}world -->{{.W}}{{end}}</b>",
   481  			"<b>Hello, &lt;Cincinnati&gt;</b>",
   482  		},
   483  		{
   484  			"JS line comment",
   485  			"<script>for (;;) { if (c()) break// foo not a label\n" +
   486  				"foo({{.T}});}</script>",
   487  			"<script>for (;;) { if (c()) break\n" +
   488  				"foo( true );}</script>",
   489  		},
   490  		{
   491  			"JS multiline block comment",
   492  			"<script>for (;;) { if (c()) break/* foo not a label\n" +
   493  				" */foo({{.T}});}</script>",
   494  			// Newline separates break from call. If newline
   495  			// removed, then break will consume label leaving
   496  			// code invalid.
   497  			"<script>for (;;) { if (c()) break\n" +
   498  				"foo( true );}</script>",
   499  		},
   500  		{
   501  			"JS single-line block comment",
   502  			"<script>for (;;) {\n" +
   503  				"if (c()) break/* foo a label */foo;" +
   504  				"x({{.T}});}</script>",
   505  			// Newline separates break from call. If newline
   506  			// removed, then break will consume label leaving
   507  			// code invalid.
   508  			"<script>for (;;) {\n" +
   509  				"if (c()) break foo;" +
   510  				"x( true );}</script>",
   511  		},
   512  		{
   513  			"JS block comment flush with mathematical division",
   514  			"<script>var a/*b*//c\nd</script>",
   515  			"<script>var a /c\nd</script>",
   516  		},
   517  		{
   518  			"JS mixed comments",
   519  			"<script>var a/*b*///c\nd</script>",
   520  			"<script>var a \nd</script>",
   521  		},
   522  		{
   523  			"JS HTML-like comments",
   524  			"<script>before <!-- beep\nbetween\nbefore-->boop\n</script>",
   525  			"<script>before \nbetween\nbefore\n</script>",
   526  		},
   527  		{
   528  			"JS hashbang comment",
   529  			"<script>#! beep\n</script>",
   530  			"<script>\n</script>",
   531  		},
   532  		{
   533  			"Special tags in <script> string literals",
   534  			`<script>var a = "asd < 123 <!-- 456 < fgh <script jkl < 789 </script"</script>`,
   535  			`<script>var a = "asd < 123 \x3C!-- 456 < fgh \x3Cscript jkl < 789 \x3C/script"</script>`,
   536  		},
   537  		{
   538  			"Special tags in <script> string literals (mixed case)",
   539  			`<script>var a = "<!-- <ScripT </ScripT"</script>`,
   540  			`<script>var a = "\x3C!-- \x3CScripT \x3C/ScripT"</script>`,
   541  		},
   542  		{
   543  			"Special tags in <script> regex literals (mixed case)",
   544  			`<script>var a = /<!-- <ScripT </ScripT/</script>`,
   545  			`<script>var a = /\x3C!-- \x3CScripT \x3C/ScripT/</script>`,
   546  		},
   547  		{
   548  			"CSS comments",
   549  			"<style>p// paragraph\n" +
   550  				`{border: 1px/* color */{{"#00f"}}}</style>`,
   551  			"<style>p\n" +
   552  				"{border: 1px #00f}</style>",
   553  		},
   554  		{
   555  			"JS attr block comment",
   556  			`<a onclick="f(&quot;&quot;); /* alert({{.H}}) */">`,
   557  			// Attribute comment tests should pass if the comments
   558  			// are successfully elided.
   559  			`<a onclick="f(&quot;&quot;); /* alert() */">`,
   560  		},
   561  		{
   562  			"JS attr line comment",
   563  			`<a onclick="// alert({{.G}})">`,
   564  			`<a onclick="// alert()">`,
   565  		},
   566  		{
   567  			"CSS attr block comment",
   568  			`<a style="/* color: {{.H}} */">`,
   569  			`<a style="/* color:  */">`,
   570  		},
   571  		{
   572  			"CSS attr line comment",
   573  			`<a style="// color: {{.G}}">`,
   574  			`<a style="// color: ">`,
   575  		},
   576  		{
   577  			"HTML substitution commented out",
   578  			"<p><!-- {{.H}} --></p>",
   579  			"<p></p>",
   580  		},
   581  		{
   582  			"Comment ends flush with start",
   583  			"<!--{{.}}--><script>/*{{.}}*///{{.}}\n</script><style>/*{{.}}*///{{.}}\n</style><a onclick='/*{{.}}*///{{.}}' style='/*{{.}}*///{{.}}'>",
   584  			"<script> \n</script><style> \n</style><a onclick='/**///' style='/**///'>",
   585  		},
   586  		{
   587  			"typed HTML in text",
   588  			`{{.W}}`,
   589  			`&iexcl;<b class="foo">Hello</b>, <textarea>O'World</textarea>!`,
   590  		},
   591  		{
   592  			"typed HTML in attribute",
   593  			`<div title="{{.W}}">`,
   594  			`<div title="&iexcl;Hello, O&#39;World!">`,
   595  		},
   596  		{
   597  			"typed HTML in script",
   598  			`<button onclick="alert({{.W}})">`,
   599  			`<button onclick="alert(&#34;\u0026iexcl;\u003cb class=\&#34;foo\&#34;\u003eHello\u003c/b\u003e, \u003ctextarea\u003eO&#39;World\u003c/textarea\u003e!&#34;)">`,
   600  		},
   601  		{
   602  			"typed HTML in RCDATA",
   603  			`<textarea>{{.W}}</textarea>`,
   604  			`<textarea>&iexcl;&lt;b class=&#34;foo&#34;&gt;Hello&lt;/b&gt;, &lt;textarea&gt;O&#39;World&lt;/textarea&gt;!</textarea>`,
   605  		},
   606  		{
   607  			"range in textarea",
   608  			"<textarea>{{range .A}}{{.}}{{end}}</textarea>",
   609  			"<textarea>&lt;a&gt;&lt;b&gt;</textarea>",
   610  		},
   611  		{
   612  			"No tag injection",
   613  			`{{"10$"}}<{{"script src,evil.org/pwnd.js"}}...`,
   614  			`10$&lt;script src,evil.org/pwnd.js...`,
   615  		},
   616  		{
   617  			"No comment injection",
   618  			`<{{"!--"}}`,
   619  			`&lt;!--`,
   620  		},
   621  		{
   622  			"No RCDATA end tag injection",
   623  			`<textarea><{{"/textarea "}}...</textarea>`,
   624  			`<textarea>&lt;/textarea ...</textarea>`,
   625  		},
   626  		{
   627  			"optional attrs",
   628  			`<img class="{{"iconClass"}}"` +
   629  				`{{if .T}} id="{{"<iconId>"}}"{{end}}` +
   630  				// Double quotes inside if/else.
   631  				` src=` +
   632  				`{{if .T}}"?{{"<iconPath>"}}"` +
   633  				`{{else}}"images/cleardot.gif"{{end}}` +
   634  				// Missing space before title, but it is not a
   635  				// part of the src attribute.
   636  				`{{if .T}}title="{{"<title>"}}"{{end}}` +
   637  				// Quotes outside if/else.
   638  				` alt="` +
   639  				`{{if .T}}{{"<alt>"}}` +
   640  				`{{else}}{{if .F}}{{"<title>"}}{{end}}` +
   641  				`{{end}}"` +
   642  				`>`,
   643  			`<img class="iconClass" id="&lt;iconId&gt;" src="?%3ciconPath%3e"title="&lt;title&gt;" alt="&lt;alt&gt;">`,
   644  		},
   645  		{
   646  			"conditional valueless attr name",
   647  			`<input{{if .T}} checked{{end}} name=n>`,
   648  			`<input checked name=n>`,
   649  		},
   650  		{
   651  			"conditional dynamic valueless attr name 1",
   652  			`<input{{if .T}} {{"checked"}}{{end}} name=n>`,
   653  			`<input checked name=n>`,
   654  		},
   655  		{
   656  			"conditional dynamic valueless attr name 2",
   657  			`<input {{if .T}}{{"checked"}} {{end}}name=n>`,
   658  			`<input checked name=n>`,
   659  		},
   660  		{
   661  			"dynamic attribute name",
   662  			`<img on{{"load"}}="alert({{"loaded"}})">`,
   663  			// Treated as JS since quotes are inserted.
   664  			`<img onload="alert(&#34;loaded&#34;)">`,
   665  		},
   666  		{
   667  			"bad dynamic attribute name 1",
   668  			// Allow checked, selected, disabled, but not JS or
   669  			// CSS attributes.
   670  			`<input {{"onchange"}}="{{"doEvil()"}}">`,
   671  			`<input ZgotmplZ="doEvil()">`,
   672  		},
   673  		{
   674  			"bad dynamic attribute name 2",
   675  			`<div {{"sTyle"}}="{{"color: expression(alert(1337))"}}">`,
   676  			`<div ZgotmplZ="color: expression(alert(1337))">`,
   677  		},
   678  		{
   679  			"bad dynamic attribute name 3",
   680  			// Allow title or alt, but not a URL.
   681  			`<img {{"src"}}="{{"javascript:doEvil()"}}">`,
   682  			`<img ZgotmplZ="javascript:doEvil()">`,
   683  		},
   684  		{
   685  			"bad dynamic attribute name 4",
   686  			// Structure preservation requires values to associate
   687  			// with a consistent attribute.
   688  			`<input checked {{""}}="Whose value am I?">`,
   689  			`<input checked ZgotmplZ="Whose value am I?">`,
   690  		},
   691  		{
   692  			"dynamic element name",
   693  			`<h{{3}}><table><t{{"head"}}>...</h{{3}}>`,
   694  			`<h3><table><thead>...</h3>`,
   695  		},
   696  		{
   697  			"bad dynamic element name",
   698  			// Dynamic element names are typically used to switch
   699  			// between (thead, tfoot, tbody), (ul, ol), (th, td),
   700  			// and other replaceable sets.
   701  			// We do not currently easily support (ul, ol).
   702  			// If we do change to support that, this test should
   703  			// catch failures to filter out special tag names which
   704  			// would violate the structure preservation property --
   705  			// if any special tag name could be substituted, then
   706  			// the content could be raw text/RCDATA for some inputs
   707  			// and regular HTML content for others.
   708  			`<{{"script"}}>{{"doEvil()"}}</{{"script"}}>`,
   709  			`&lt;script>doEvil()&lt;/script>`,
   710  		},
   711  		{
   712  			"srcset bad URL in second position",
   713  			`<img srcset="{{"/not-an-image#,javascript:alert(1)"}}">`,
   714  			// The second URL is also filtered.
   715  			`<img srcset="/not-an-image#,#ZgotmplZ">`,
   716  		},
   717  		{
   718  			"srcset buffer growth",
   719  			`<img srcset={{",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"}}>`,
   720  			`<img srcset=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>`,
   721  		},
   722  		{
   723  			"unquoted empty attribute value (plaintext)",
   724  			"<p name={{.U}}>",
   725  			"<p name=ZgotmplZ>",
   726  		},
   727  		{
   728  			"unquoted empty attribute value (url)",
   729  			"<p href={{.U}}>",
   730  			"<p href=ZgotmplZ>",
   731  		},
   732  		{
   733  			"quoted empty attribute value",
   734  			"<p name=\"{{.U}}\">",
   735  			"<p name=\"\">",
   736  		},
   737  		{
   738  			"JS template lit special characters",
   739  			"<script>var a = `{{.I}}`</script>",
   740  			"<script>var a = `\\u0024\\u007b asd \\u0060\\u0060 \\u007d`</script>",
   741  		},
   742  		{
   743  			"JS template lit special characters, nested lit",
   744  			"<script>var a = `${ `{{.I}}` }`</script>",
   745  			"<script>var a = `${ `\\u0024\\u007b asd \\u0060\\u0060 \\u007d` }`</script>",
   746  		},
   747  		{
   748  			"JS template lit, nested JS",
   749  			"<script>var a = `${ var a = \"{{\"a \\\" d\"}}\" }`</script>",
   750  			"<script>var a = `${ var a = \"a \\u0022 d\" }`</script>",
   751  		},
   752  		{
   753  			"meta content attribute url",
   754  			`<meta http-equiv="refresh" content="asd; url={{"javascript:alert(1)"}}; asd; url={{"vbscript:alert(1)"}}; asd">`,
   755  			`<meta http-equiv="refresh" content="asd; url=#ZgotmplZ; asd; url=#ZgotmplZ; asd">`,
   756  		},
   757  		{
   758  			"meta content string",
   759  			`<meta http-equiv="refresh" content="{{"asd: 123"}}">`,
   760  			`<meta http-equiv="refresh" content="asd: 123">`,
   761  		},
   762  		{
   763  			"meta content url with whitespace before equals",
   764  			`<meta http-equiv="refresh" content="0;url ={{"javascript:alert(1)"}}">`,
   765  			`<meta http-equiv="refresh" content="0;url =#ZgotmplZ">`,
   766  		},
   767  		{
   768  			"meta content url with tab before equals",
   769  			"<meta http-equiv=\"refresh\" content=\"0;url\t={{\"javascript:alert(1)\"}}\">",
   770  			"<meta http-equiv=\"refresh\" content=\"0;url\t=#ZgotmplZ\">",
   771  		},
   772  		{
   773  			"meta content url with space after equals",
   774  			`<meta http-equiv="refresh" content="0;url= {{"javascript:alert(1)"}}">`,
   775  			`<meta http-equiv="refresh" content="0;url= #ZgotmplZ">`,
   776  		},
   777  		{
   778  			"meta content url with whitespace both sides of equals",
   779  			"<meta http-equiv=\"refresh\" content=\"0;url \t= {{\"javascript:alert(1)\"}}\">",
   780  			"<meta http-equiv=\"refresh\" content=\"0;url \t= #ZgotmplZ\">",
   781  		},
   782  	}
   783  
   784  	for _, test := range tests {
   785  		t.Run(test.name, func(t *testing.T) {
   786  			tmpl := New(test.name)
   787  			tmpl = Must(tmpl.Parse(test.input))
   788  			// Check for bug 6459: Tree field was not set in Parse.
   789  			if tmpl.Tree != tmpl.text.Tree {
   790  				t.Fatalf("%s: tree not set properly", test.name)
   791  			}
   792  			b := new(strings.Builder)
   793  			if err := tmpl.Execute(b, data); err != nil {
   794  				t.Fatalf("%s: template execution failed: %s", test.name, err)
   795  			}
   796  			if w, g := test.output, b.String(); w != g {
   797  				t.Fatalf("%s: escaped output: want\n\t%q\ngot\n\t%q", test.name, w, g)
   798  			}
   799  			b.Reset()
   800  			if err := tmpl.Execute(b, pdata); err != nil {
   801  				t.Fatalf("%s: template execution failed for pointer: %s", test.name, err)
   802  			}
   803  			if w, g := test.output, b.String(); w != g {
   804  				t.Fatalf("%s: escaped output for pointer: want\n\t%q\ngot\n\t%q", test.name, w, g)
   805  			}
   806  			if tmpl.Tree != tmpl.text.Tree {
   807  				t.Fatalf("%s: tree mismatch", test.name)
   808  			}
   809  		})
   810  	}
   811  }
   812  
   813  func TestEscapeMap(t *testing.T) {
   814  	data := map[string]string{
   815  		"html":     `<h1>Hi!</h1>`,
   816  		"urlquery": `http://www.foo.com/index.html?title=main`,
   817  	}
   818  	for _, test := range [...]struct {
   819  		desc, input, output string
   820  	}{
   821  		// covering issue 20323
   822  		{
   823  			"field with predefined escaper name 1",
   824  			`{{.html | print}}`,
   825  			`&lt;h1&gt;Hi!&lt;/h1&gt;`,
   826  		},
   827  		// covering issue 20323
   828  		{
   829  			"field with predefined escaper name 2",
   830  			`{{.urlquery | print}}`,
   831  			`http://www.foo.com/index.html?title=main`,
   832  		},
   833  	} {
   834  		tmpl := Must(New("").Parse(test.input))
   835  		b := new(strings.Builder)
   836  		if err := tmpl.Execute(b, data); err != nil {
   837  			t.Errorf("%s: template execution failed: %s", test.desc, err)
   838  			continue
   839  		}
   840  		if w, g := test.output, b.String(); w != g {
   841  			t.Errorf("%s: escaped output: want\n\t%q\ngot\n\t%q", test.desc, w, g)
   842  			continue
   843  		}
   844  	}
   845  }
   846  
   847  func TestEscapeSet(t *testing.T) {
   848  	type dataItem struct {
   849  		Children []*dataItem
   850  		X        string
   851  	}
   852  
   853  	data := dataItem{
   854  		Children: []*dataItem{
   855  			{X: "foo"},
   856  			{X: "<bar>"},
   857  			{
   858  				Children: []*dataItem{
   859  					{X: "baz"},
   860  				},
   861  			},
   862  		},
   863  	}
   864  
   865  	tests := []struct {
   866  		inputs map[string]string
   867  		want   string
   868  	}{
   869  		// The trivial set.
   870  		{
   871  			map[string]string{
   872  				"main": ``,
   873  			},
   874  			``,
   875  		},
   876  		// A template called in the start context.
   877  		{
   878  			map[string]string{
   879  				"main": `Hello, {{template "helper"}}!`,
   880  				// Not a valid top level HTML template.
   881  				// "<b" is not a full tag.
   882  				"helper": `{{"<World>"}}`,
   883  			},
   884  			`Hello, &lt;World&gt;!`,
   885  		},
   886  		// A template called in a context other than the start.
   887  		{
   888  			map[string]string{
   889  				"main": `<a onclick='a = {{template "helper"}};'>`,
   890  				// Not a valid top level HTML template.
   891  				// "<b" is not a full tag.
   892  				"helper": `{{"<a>"}}<b`,
   893  			},
   894  			`<a onclick='a = &#34;\u003ca\u003e&#34;<b;'>`,
   895  		},
   896  		// A recursive template that ends in its start context.
   897  		{
   898  			map[string]string{
   899  				"main": `{{range .Children}}{{template "main" .}}{{else}}{{.X}} {{end}}`,
   900  			},
   901  			`foo &lt;bar&gt; baz `,
   902  		},
   903  		// A recursive helper template that ends in its start context.
   904  		{
   905  			map[string]string{
   906  				"main":   `{{template "helper" .}}`,
   907  				"helper": `{{if .Children}}<ul>{{range .Children}}<li>{{template "main" .}}</li>{{end}}</ul>{{else}}{{.X}}{{end}}`,
   908  			},
   909  			`<ul><li>foo</li><li>&lt;bar&gt;</li><li><ul><li>baz</li></ul></li></ul>`,
   910  		},
   911  		// Co-recursive templates that end in its start context.
   912  		{
   913  			map[string]string{
   914  				"main":   `<blockquote>{{range .Children}}{{template "helper" .}}{{end}}</blockquote>`,
   915  				"helper": `{{if .Children}}{{template "main" .}}{{else}}{{.X}}<br>{{end}}`,
   916  			},
   917  			`<blockquote>foo<br>&lt;bar&gt;<br><blockquote>baz<br></blockquote></blockquote>`,
   918  		},
   919  		// A template that is called in two different contexts.
   920  		{
   921  			map[string]string{
   922  				"main":   `<button onclick="title='{{template "helper"}}'; ...">{{template "helper"}}</button>`,
   923  				"helper": `{{11}} of {{"<100>"}}`,
   924  			},
   925  			`<button onclick="title='11 of \u003c100\u003e'; ...">11 of &lt;100&gt;</button>`,
   926  		},
   927  		// A non-recursive template that ends in a different context.
   928  		// helper starts in jsCtxRegexp and ends in jsCtxDivOp.
   929  		{
   930  			map[string]string{
   931  				"main":   `<script>var x={{template "helper"}}/{{"42"}};</script>`,
   932  				"helper": "{{126}}",
   933  			},
   934  			`<script>var x= 126 /"42";</script>`,
   935  		},
   936  		// A recursive template that ends in a similar context.
   937  		{
   938  			map[string]string{
   939  				"main":      `<script>var x=[{{template "countdown" 4}}];</script>`,
   940  				"countdown": `{{.}}{{if .}},{{template "countdown" . | pred}}{{end}}`,
   941  			},
   942  			`<script>var x=[ 4 , 3 , 2 , 1 , 0 ];</script>`,
   943  		},
   944  		// A recursive template that ends in a different context.
   945  		/*
   946  			{
   947  				map[string]string{
   948  					"main":   `<a href="/foo{{template "helper" .}}">`,
   949  					"helper": `{{if .Children}}{{range .Children}}{{template "helper" .}}{{end}}{{else}}?x={{.X}}{{end}}`,
   950  				},
   951  				`<a href="/foo?x=foo?x=%3cbar%3e?x=baz">`,
   952  			},
   953  		*/
   954  	}
   955  
   956  	// pred is a template function that returns the predecessor of a
   957  	// natural number for testing recursive templates.
   958  	fns := FuncMap{"pred": func(a ...any) (any, error) {
   959  		if len(a) == 1 {
   960  			if i, _ := a[0].(int); i > 0 {
   961  				return i - 1, nil
   962  			}
   963  		}
   964  		return nil, fmt.Errorf("undefined pred(%v)", a)
   965  	}}
   966  
   967  	for _, test := range tests {
   968  		source := ""
   969  		for name, body := range test.inputs {
   970  			source += fmt.Sprintf("{{define %q}}%s{{end}} ", name, body)
   971  		}
   972  		tmpl, err := New("root").Funcs(fns).Parse(source)
   973  		if err != nil {
   974  			t.Errorf("error parsing %q: %v", source, err)
   975  			continue
   976  		}
   977  		var b strings.Builder
   978  
   979  		if err := tmpl.ExecuteTemplate(&b, "main", data); err != nil {
   980  			t.Errorf("%q executing %v", err.Error(), tmpl.Lookup("main"))
   981  			continue
   982  		}
   983  		if got := b.String(); test.want != got {
   984  			t.Errorf("want\n\t%q\ngot\n\t%q", test.want, got)
   985  		}
   986  	}
   987  
   988  }
   989  
   990  func TestErrors(t *testing.T) {
   991  	tests := []struct {
   992  		input string
   993  		err   string
   994  	}{
   995  		// Non-error cases.
   996  		{
   997  			"{{if .Cond}}<a>{{else}}<b>{{end}}",
   998  			"",
   999  		},
  1000  		{
  1001  			"{{if .Cond}}<a>{{end}}",
  1002  			"",
  1003  		},
  1004  		{
  1005  			"{{if .Cond}}{{else}}<b>{{end}}",
  1006  			"",
  1007  		},
  1008  		{
  1009  			"{{with .Cond}}<div>{{end}}",
  1010  			"",
  1011  		},
  1012  		{
  1013  			"{{range .Items}}<a>{{end}}",
  1014  			"",
  1015  		},
  1016  		{
  1017  			"<a href='/foo?{{range .Items}}&{{.K}}={{.V}}{{end}}'>",
  1018  			"",
  1019  		},
  1020  		{
  1021  			"{{range .Items}}<a{{if .X}}{{end}}>{{end}}",
  1022  			"",
  1023  		},
  1024  		{
  1025  			"{{range .Items}}<a{{if .X}}{{end}}>{{continue}}{{end}}",
  1026  			"",
  1027  		},
  1028  		{
  1029  			"{{range .Items}}<a{{if .X}}{{end}}>{{break}}{{end}}",
  1030  			"",
  1031  		},
  1032  		{
  1033  			"{{range .Items}}<a{{if .X}}{{end}}>{{if .X}}{{break}}{{end}}{{end}}",
  1034  			"",
  1035  		},
  1036  		{
  1037  			"<script>var a = `${a+b}`</script>`",
  1038  			"",
  1039  		},
  1040  		{
  1041  			"<script>var tmpl = `asd`;</script>",
  1042  			``,
  1043  		},
  1044  		{
  1045  			"<script>var tmpl = `${1}`;</script>",
  1046  			``,
  1047  		},
  1048  		{
  1049  			"<script>var tmpl = `${return ``}`;</script>",
  1050  			``,
  1051  		},
  1052  		{
  1053  			"<script>var tmpl = `${return {{.}} }`;</script>",
  1054  			``,
  1055  		},
  1056  		{
  1057  			"<script>var tmpl = `${ let a = {1:1} {{.}} }`;</script>",
  1058  			``,
  1059  		},
  1060  		{
  1061  			"<script>var tmpl = `asd ${return \"{\"}`;</script>",
  1062  			``,
  1063  		},
  1064  		{
  1065  			`{{if eq "" ""}}<meta>{{end}}`,
  1066  			``,
  1067  		},
  1068  		{
  1069  			`{{if eq "" ""}}<meta content="url={{"asd"}}">{{end}}`,
  1070  			``,
  1071  		},
  1072  
  1073  		// Error cases.
  1074  		{
  1075  			"{{if .Cond}}<a{{end}}",
  1076  			"z:1:5: {{if}} branches",
  1077  		},
  1078  		{
  1079  			"{{if .Cond}}\n{{else}}\n<a{{end}}",
  1080  			"z:1:5: {{if}} branches",
  1081  		},
  1082  		{
  1083  			// Missing quote in the else branch.
  1084  			`{{if .Cond}}<a href="foo">{{else}}<a href="bar>{{end}}`,
  1085  			"z:1:5: {{if}} branches",
  1086  		},
  1087  		{
  1088  			// Different kind of attribute: href implies a URL.
  1089  			"<a {{if .Cond}}href='{{else}}title='{{end}}{{.X}}'>",
  1090  			"z:1:8: {{if}} branches",
  1091  		},
  1092  		{
  1093  			"\n{{with .X}}<a{{end}}",
  1094  			"z:2:7: {{with}} branches",
  1095  		},
  1096  		{
  1097  			"\n{{with .X}}<a>{{else}}<a{{end}}",
  1098  			"z:2:7: {{with}} branches",
  1099  		},
  1100  		{
  1101  			"{{range .Items}}<a{{end}}",
  1102  			`z:1: on range loop re-entry: "<" in attribute name: "<a"`,
  1103  		},
  1104  		{
  1105  			"\n{{range .Items}} x='<a{{end}}",
  1106  			"z:2:8: on range loop re-entry: {{range}} branches",
  1107  		},
  1108  		{
  1109  			"{{range .Items}}<a{{if .X}}{{break}}{{end}}>{{end}}",
  1110  			"z:1:29: at range loop break: {{range}} branches end in different contexts",
  1111  		},
  1112  		{
  1113  			"{{range .Items}}<a{{if .X}}{{continue}}{{end}}>{{end}}",
  1114  			"z:1:29: at range loop continue: {{range}} branches end in different contexts",
  1115  		},
  1116  		{
  1117  			"{{range .Items}}{{if .X}}{{break}}{{end}}<a{{if .Y}}{{continue}}{{end}}>{{if .Z}}{{continue}}{{end}}{{end}}",
  1118  			"z:1:54: at range loop continue: {{range}} branches end in different contexts",
  1119  		},
  1120  		{
  1121  			"<a b=1 c={{.H}}",
  1122  			"z: ends in a non-text context: {stateAttr delimSpaceOrTagEnd",
  1123  		},
  1124  		{
  1125  			"<script>foo();",
  1126  			"z: ends in a non-text context: {stateJS",
  1127  		},
  1128  		{
  1129  			`<a href="{{if .F}}/foo?a={{else}}/bar/{{end}}{{.H}}">`,
  1130  			"z:1:47: {{.H}} appears in an ambiguous context within a URL",
  1131  		},
  1132  		{
  1133  			`<a onclick="alert('Hello \`,
  1134  			`unfinished escape sequence in JS string: "Hello \\"`,
  1135  		},
  1136  		{
  1137  			`<a onclick='alert("Hello\, World\`,
  1138  			`unfinished escape sequence in JS string: "Hello\\, World\\"`,
  1139  		},
  1140  		{
  1141  			`<a onclick='alert(/x+\`,
  1142  			`unfinished escape sequence in JS string: "x+\\"`,
  1143  		},
  1144  		{
  1145  			`<a onclick="/foo[\]/`,
  1146  			`unfinished JS regexp charset: "foo[\\]/"`,
  1147  		},
  1148  		{
  1149  			// It is ambiguous whether 1.5 should be 1\.5 or 1.5.
  1150  			// Either `var x = 1/- 1.5 /i.test(x)`
  1151  			// where `i.test(x)` is a method call of reference i,
  1152  			// or `/-1\.5/i.test(x)` which is a method call on a
  1153  			// case insensitive regular expression.
  1154  			`<script>{{if false}}var x = 1{{end}}/-{{"1.5"}}/i.test(x)</script>`,
  1155  			`'/' could start a division or regexp: "/-"`,
  1156  		},
  1157  		{
  1158  			`{{template "foo"}}`,
  1159  			"z:1:11: no such template \"foo\"",
  1160  		},
  1161  		{
  1162  			`<div{{template "y"}}>` +
  1163  				// Illegal starting in stateTag but not in stateText.
  1164  				`{{define "y"}} foo<b{{end}}`,
  1165  			`"<" in attribute name: " foo<b"`,
  1166  		},
  1167  		{
  1168  			`<script>reverseList = [{{template "t"}}]</script>` +
  1169  				// Missing " after recursive call.
  1170  				`{{define "t"}}{{if .Tail}}{{template "t" .Tail}}{{end}}{{.Head}}",{{end}}`,
  1171  			`: cannot compute output context for template t$htmltemplate_stateJS_elementScript`,
  1172  		},
  1173  		{
  1174  			`<input type=button value=onclick=>`,
  1175  			`html/template:z: "=" in unquoted attr: "onclick="`,
  1176  		},
  1177  		{
  1178  			`<input type=button value= onclick=>`,
  1179  			`html/template:z: "=" in unquoted attr: "onclick="`,
  1180  		},
  1181  		{
  1182  			`<input type=button value= 1+1=2>`,
  1183  			`html/template:z: "=" in unquoted attr: "1+1=2"`,
  1184  		},
  1185  		{
  1186  			"<a class=`foo>",
  1187  			"html/template:z: \"`\" in unquoted attr: \"`foo\"",
  1188  		},
  1189  		{
  1190  			`<a style=font:'Arial'>`,
  1191  			`html/template:z: "'" in unquoted attr: "font:'Arial'"`,
  1192  		},
  1193  		{
  1194  			`<a=foo>`,
  1195  			`: expected space, attr name, or end of tag, but got "=foo>"`,
  1196  		},
  1197  		{
  1198  			`Hello, {{. | urlquery | print}}!`,
  1199  			// urlquery is disallowed if it is not the last command in the pipeline.
  1200  			`predefined escaper "urlquery" disallowed in template`,
  1201  		},
  1202  		{
  1203  			`Hello, {{. | html | print}}!`,
  1204  			// html is disallowed if it is not the last command in the pipeline.
  1205  			`predefined escaper "html" disallowed in template`,
  1206  		},
  1207  		{
  1208  			`Hello, {{html . | print}}!`,
  1209  			// A direct call to html is disallowed if it is not the last command in the pipeline.
  1210  			`predefined escaper "html" disallowed in template`,
  1211  		},
  1212  		{
  1213  			`<div class={{. | html}}>Hello<div>`,
  1214  			// html is disallowed in a pipeline that is in an unquoted attribute context,
  1215  			// even if it is the last command in the pipeline.
  1216  			`predefined escaper "html" disallowed in template`,
  1217  		},
  1218  		{
  1219  			`Hello, {{. | urlquery | html}}!`,
  1220  			// html is allowed since it is the last command in the pipeline, but urlquery is not.
  1221  			`predefined escaper "urlquery" disallowed in template`,
  1222  		},
  1223  		{
  1224  			"<script>var a = `{{if .X}}`{{end}}",
  1225  			`{{if}} branches end in different contexts`,
  1226  		},
  1227  		{
  1228  			"<script>var a = `{{if .X}}a{{else}}`{{end}}",
  1229  			`{{if}} branches end in different contexts`,
  1230  		},
  1231  		{
  1232  			"<script>var a = `{{if .X}}a{{else}}b{{end}}`</script>",
  1233  			``,
  1234  		},
  1235  	}
  1236  	for _, test := range tests {
  1237  		buf := new(bytes.Buffer)
  1238  		tmpl, err := New("z").Parse(test.input)
  1239  		if err != nil {
  1240  			t.Errorf("input=%q: unexpected parse error %s\n", test.input, err)
  1241  			continue
  1242  		}
  1243  		err = tmpl.Execute(buf, nil)
  1244  		var got string
  1245  		if err != nil {
  1246  			got = err.Error()
  1247  		}
  1248  		if test.err == "" {
  1249  			if got != "" {
  1250  				t.Errorf("input=%q: unexpected error %q", test.input, got)
  1251  			}
  1252  			continue
  1253  		}
  1254  		if !strings.Contains(got, test.err) {
  1255  			t.Errorf("input=%q: error\n\t%q\ndoes not contain expected string\n\t%q", test.input, got, test.err)
  1256  			continue
  1257  		}
  1258  		// Check that we get the same error if we call Execute again.
  1259  		if err := tmpl.Execute(buf, nil); err == nil || err.Error() != got {
  1260  			t.Errorf("input=%q: unexpected error on second call %q", test.input, err)
  1261  
  1262  		}
  1263  	}
  1264  }
  1265  
  1266  func TestEscapeText(t *testing.T) {
  1267  	tests := []struct {
  1268  		input  string
  1269  		output context
  1270  	}{
  1271  		{
  1272  			``,
  1273  			context{},
  1274  		},
  1275  		{
  1276  			`Hello, World!`,
  1277  			context{},
  1278  		},
  1279  		{
  1280  			// An orphaned "<" is OK.
  1281  			`I <3 Ponies!`,
  1282  			context{},
  1283  		},
  1284  		{
  1285  			`<a`,
  1286  			context{state: stateTag},
  1287  		},
  1288  		{
  1289  			`<a `,
  1290  			context{state: stateTag},
  1291  		},
  1292  		{
  1293  			`<a>`,
  1294  			context{state: stateText},
  1295  		},
  1296  		{
  1297  			`<a href`,
  1298  			context{state: stateAttrName, attr: attrURL},
  1299  		},
  1300  		{
  1301  			`<a on`,
  1302  			context{state: stateAttrName, attr: attrScript},
  1303  		},
  1304  		{
  1305  			`<a href `,
  1306  			context{state: stateAfterName, attr: attrURL},
  1307  		},
  1308  		{
  1309  			`<a style  =  `,
  1310  			context{state: stateBeforeValue, attr: attrStyle},
  1311  		},
  1312  		{
  1313  			`<a href=`,
  1314  			context{state: stateBeforeValue, attr: attrURL},
  1315  		},
  1316  		{
  1317  			`<a href=x`,
  1318  			context{state: stateURL, delim: delimSpaceOrTagEnd, urlPart: urlPartPreQuery, attr: attrURL},
  1319  		},
  1320  		{
  1321  			`<a href=x `,
  1322  			context{state: stateTag},
  1323  		},
  1324  		{
  1325  			`<a href=>`,
  1326  			context{state: stateText},
  1327  		},
  1328  		{
  1329  			`<a href=x>`,
  1330  			context{state: stateText},
  1331  		},
  1332  		{
  1333  			`<a href ='`,
  1334  			context{state: stateURL, delim: delimSingleQuote, attr: attrURL},
  1335  		},
  1336  		{
  1337  			`<a href=''`,
  1338  			context{state: stateTag},
  1339  		},
  1340  		{
  1341  			`<a href= "`,
  1342  			context{state: stateURL, delim: delimDoubleQuote, attr: attrURL},
  1343  		},
  1344  		{
  1345  			`<a href=""`,
  1346  			context{state: stateTag},
  1347  		},
  1348  		{
  1349  			`<a title="`,
  1350  			context{state: stateAttr, delim: delimDoubleQuote},
  1351  		},
  1352  		{
  1353  			`<a HREF='http:`,
  1354  			context{state: stateURL, delim: delimSingleQuote, urlPart: urlPartPreQuery, attr: attrURL},
  1355  		},
  1356  		{
  1357  			`<a Href='/`,
  1358  			context{state: stateURL, delim: delimSingleQuote, urlPart: urlPartPreQuery, attr: attrURL},
  1359  		},
  1360  		{
  1361  			`<a href='"`,
  1362  			context{state: stateURL, delim: delimSingleQuote, urlPart: urlPartPreQuery, attr: attrURL},
  1363  		},
  1364  		{
  1365  			`<a href="'`,
  1366  			context{state: stateURL, delim: delimDoubleQuote, urlPart: urlPartPreQuery, attr: attrURL},
  1367  		},
  1368  		{
  1369  			`<a href='&apos;`,
  1370  			context{state: stateURL, delim: delimSingleQuote, urlPart: urlPartPreQuery, attr: attrURL},
  1371  		},
  1372  		{
  1373  			`<a href="&quot;`,
  1374  			context{state: stateURL, delim: delimDoubleQuote, urlPart: urlPartPreQuery, attr: attrURL},
  1375  		},
  1376  		{
  1377  			`<a href="&#34;`,
  1378  			context{state: stateURL, delim: delimDoubleQuote, urlPart: urlPartPreQuery, attr: attrURL},
  1379  		},
  1380  		{
  1381  			`<a href=&quot;`,
  1382  			context{state: stateURL, delim: delimSpaceOrTagEnd, urlPart: urlPartPreQuery, attr: attrURL},
  1383  		},
  1384  		{
  1385  			`<img alt="1">`,
  1386  			context{state: stateText},
  1387  		},
  1388  		{
  1389  			`<img alt="1>"`,
  1390  			context{state: stateTag},
  1391  		},
  1392  		{
  1393  			`<img alt="1>">`,
  1394  			context{state: stateText},
  1395  		},
  1396  		{
  1397  			`<input checked type="checkbox"`,
  1398  			context{state: stateTag},
  1399  		},
  1400  		{
  1401  			`<a onclick="`,
  1402  			context{state: stateJS, delim: delimDoubleQuote, attr: attrScript},
  1403  		},
  1404  		{
  1405  			`<a onclick="//foo`,
  1406  			context{state: stateJSLineCmt, delim: delimDoubleQuote, attr: attrScript},
  1407  		},
  1408  		{
  1409  			"<a onclick='//\n",
  1410  			context{state: stateJS, delim: delimSingleQuote, attr: attrScript},
  1411  		},
  1412  		{
  1413  			"<a onclick='//\r\n",
  1414  			context{state: stateJS, delim: delimSingleQuote, attr: attrScript},
  1415  		},
  1416  		{
  1417  			"<a onclick='//\u2028",
  1418  			context{state: stateJS, delim: delimSingleQuote, attr: attrScript},
  1419  		},
  1420  		{
  1421  			`<a onclick="/*`,
  1422  			context{state: stateJSBlockCmt, delim: delimDoubleQuote, attr: attrScript},
  1423  		},
  1424  		{
  1425  			`<a onclick="/*/`,
  1426  			context{state: stateJSBlockCmt, delim: delimDoubleQuote, attr: attrScript},
  1427  		},
  1428  		{
  1429  			`<a onclick="/**/`,
  1430  			context{state: stateJS, delim: delimDoubleQuote, attr: attrScript},
  1431  		},
  1432  		{
  1433  			`<a onkeypress="&quot;`,
  1434  			context{state: stateJSDqStr, delim: delimDoubleQuote, attr: attrScript},
  1435  		},
  1436  		{
  1437  			`<a onclick='&quot;foo&quot;`,
  1438  			context{state: stateJS, delim: delimSingleQuote, jsCtx: jsCtxDivOp, attr: attrScript},
  1439  		},
  1440  		{
  1441  			`<a onclick=&#39;foo&#39;`,
  1442  			context{state: stateJS, delim: delimSpaceOrTagEnd, jsCtx: jsCtxDivOp, attr: attrScript},
  1443  		},
  1444  		{
  1445  			`<a onclick=&#39;foo`,
  1446  			context{state: stateJSSqStr, delim: delimSpaceOrTagEnd, attr: attrScript},
  1447  		},
  1448  		{
  1449  			`<a onclick="&quot;foo'`,
  1450  			context{state: stateJSDqStr, delim: delimDoubleQuote, attr: attrScript},
  1451  		},
  1452  		{
  1453  			`<a onclick="'foo&quot;`,
  1454  			context{state: stateJSSqStr, delim: delimDoubleQuote, attr: attrScript},
  1455  		},
  1456  		{
  1457  			"<a onclick=\"`foo",
  1458  			context{state: stateJSTmplLit, delim: delimDoubleQuote, attr: attrScript},
  1459  		},
  1460  		{
  1461  			`<A ONCLICK="'`,
  1462  			context{state: stateJSSqStr, delim: delimDoubleQuote, attr: attrScript},
  1463  		},
  1464  		{
  1465  			`<a onclick="/`,
  1466  			context{state: stateJSRegexp, delim: delimDoubleQuote, attr: attrScript},
  1467  		},
  1468  		{
  1469  			`<a onclick="'foo'`,
  1470  			context{state: stateJS, delim: delimDoubleQuote, jsCtx: jsCtxDivOp, attr: attrScript},
  1471  		},
  1472  		{
  1473  			`<a onclick="'foo\'`,
  1474  			context{state: stateJSSqStr, delim: delimDoubleQuote, attr: attrScript},
  1475  		},
  1476  		{
  1477  			`<a onclick="'foo\'`,
  1478  			context{state: stateJSSqStr, delim: delimDoubleQuote, attr: attrScript},
  1479  		},
  1480  		{
  1481  			`<a onclick="/foo/`,
  1482  			context{state: stateJS, delim: delimDoubleQuote, jsCtx: jsCtxDivOp, attr: attrScript},
  1483  		},
  1484  		{
  1485  			`<script>/foo/ /=`,
  1486  			context{state: stateJS, element: elementScript},
  1487  		},
  1488  		{
  1489  			`<a onclick="1 /foo`,
  1490  			context{state: stateJS, delim: delimDoubleQuote, jsCtx: jsCtxDivOp, attr: attrScript},
  1491  		},
  1492  		{
  1493  			`<a onclick="1 /*c*/ /foo`,
  1494  			context{state: stateJS, delim: delimDoubleQuote, jsCtx: jsCtxDivOp, attr: attrScript},
  1495  		},
  1496  		{
  1497  			`<a onclick="/foo[/]`,
  1498  			context{state: stateJSRegexp, delim: delimDoubleQuote, attr: attrScript},
  1499  		},
  1500  		{
  1501  			`<a onclick="/foo\/`,
  1502  			context{state: stateJSRegexp, delim: delimDoubleQuote, attr: attrScript},
  1503  		},
  1504  		{
  1505  			`<a onclick="/foo/`,
  1506  			context{state: stateJS, delim: delimDoubleQuote, jsCtx: jsCtxDivOp, attr: attrScript},
  1507  		},
  1508  		{
  1509  			`<input checked style="`,
  1510  			context{state: stateCSS, delim: delimDoubleQuote, attr: attrStyle},
  1511  		},
  1512  		{
  1513  			`<a style="//`,
  1514  			context{state: stateCSSLineCmt, delim: delimDoubleQuote, attr: attrStyle},
  1515  		},
  1516  		{
  1517  			`<a style="//</script>`,
  1518  			context{state: stateCSSLineCmt, delim: delimDoubleQuote, attr: attrStyle},
  1519  		},
  1520  		{
  1521  			"<a style='//\n",
  1522  			context{state: stateCSS, delim: delimSingleQuote, attr: attrStyle},
  1523  		},
  1524  		{
  1525  			"<a style='//\r",
  1526  			context{state: stateCSS, delim: delimSingleQuote, attr: attrStyle},
  1527  		},
  1528  		{
  1529  			`<a style="/*`,
  1530  			context{state: stateCSSBlockCmt, delim: delimDoubleQuote, attr: attrStyle},
  1531  		},
  1532  		{
  1533  			`<a style="/*/`,
  1534  			context{state: stateCSSBlockCmt, delim: delimDoubleQuote, attr: attrStyle},
  1535  		},
  1536  		{
  1537  			`<a style="/**/`,
  1538  			context{state: stateCSS, delim: delimDoubleQuote, attr: attrStyle},
  1539  		},
  1540  		{
  1541  			`<a style="background: '`,
  1542  			context{state: stateCSSSqStr, delim: delimDoubleQuote, attr: attrStyle},
  1543  		},
  1544  		{
  1545  			`<a style="background: &quot;`,
  1546  			context{state: stateCSSDqStr, delim: delimDoubleQuote, attr: attrStyle},
  1547  		},
  1548  		{
  1549  			`<a style="background: '/foo?img=`,
  1550  			context{state: stateCSSSqStr, delim: delimDoubleQuote, urlPart: urlPartQueryOrFrag, attr: attrStyle},
  1551  		},
  1552  		{
  1553  			`<a style="background: '/`,
  1554  			context{state: stateCSSSqStr, delim: delimDoubleQuote, urlPart: urlPartPreQuery, attr: attrStyle},
  1555  		},
  1556  		{
  1557  			`<a style="background: url(&#x22;/`,
  1558  			context{state: stateCSSDqURL, delim: delimDoubleQuote, urlPart: urlPartPreQuery, attr: attrStyle},
  1559  		},
  1560  		{
  1561  			`<a style="background: url('/`,
  1562  			context{state: stateCSSSqURL, delim: delimDoubleQuote, urlPart: urlPartPreQuery, attr: attrStyle},
  1563  		},
  1564  		{
  1565  			`<a style="background: url('/)`,
  1566  			context{state: stateCSSSqURL, delim: delimDoubleQuote, urlPart: urlPartPreQuery, attr: attrStyle},
  1567  		},
  1568  		{
  1569  			`<a style="background: url('/ `,
  1570  			context{state: stateCSSSqURL, delim: delimDoubleQuote, urlPart: urlPartPreQuery, attr: attrStyle},
  1571  		},
  1572  		{
  1573  			`<a style="background: url(/`,
  1574  			context{state: stateCSSURL, delim: delimDoubleQuote, urlPart: urlPartPreQuery, attr: attrStyle},
  1575  		},
  1576  		{
  1577  			`<a style="background: url( `,
  1578  			context{state: stateCSSURL, delim: delimDoubleQuote, attr: attrStyle},
  1579  		},
  1580  		{
  1581  			`<a style="background: url( /image?name=`,
  1582  			context{state: stateCSSURL, delim: delimDoubleQuote, urlPart: urlPartQueryOrFrag, attr: attrStyle},
  1583  		},
  1584  		{
  1585  			`<a style="background: url(x)`,
  1586  			context{state: stateCSS, delim: delimDoubleQuote, attr: attrStyle},
  1587  		},
  1588  		{
  1589  			`<a style="background: url('x'`,
  1590  			context{state: stateCSS, delim: delimDoubleQuote, attr: attrStyle},
  1591  		},
  1592  		{
  1593  			`<a style="background: url( x `,
  1594  			context{state: stateCSS, delim: delimDoubleQuote, attr: attrStyle},
  1595  		},
  1596  		{
  1597  			`<!-- foo`,
  1598  			context{state: stateHTMLCmt},
  1599  		},
  1600  		{
  1601  			`<!-->`,
  1602  			context{state: stateHTMLCmt},
  1603  		},
  1604  		{
  1605  			`<!--->`,
  1606  			context{state: stateHTMLCmt},
  1607  		},
  1608  		{
  1609  			`<!-- foo -->`,
  1610  			context{state: stateText},
  1611  		},
  1612  		{
  1613  			`<script`,
  1614  			context{state: stateTag, element: elementScript},
  1615  		},
  1616  		{
  1617  			`<script `,
  1618  			context{state: stateTag, element: elementScript},
  1619  		},
  1620  		{
  1621  			`<script src="foo.js" `,
  1622  			context{state: stateTag, element: elementScript},
  1623  		},
  1624  		{
  1625  			`<script src='foo.js' `,
  1626  			context{state: stateTag, element: elementScript},
  1627  		},
  1628  		{
  1629  			`<script type=text/javascript `,
  1630  			context{state: stateTag, element: elementScript},
  1631  		},
  1632  		{
  1633  			`<script>`,
  1634  			context{state: stateJS, jsCtx: jsCtxRegexp, element: elementScript},
  1635  		},
  1636  		{
  1637  			`<script>foo`,
  1638  			context{state: stateJS, jsCtx: jsCtxDivOp, element: elementScript},
  1639  		},
  1640  		{
  1641  			`<script>foo</script>`,
  1642  			context{state: stateText},
  1643  		},
  1644  		{
  1645  			`<script>foo</script><!--`,
  1646  			context{state: stateHTMLCmt},
  1647  		},
  1648  		{
  1649  			`<script>document.write("<p>foo</p>");`,
  1650  			context{state: stateJS, element: elementScript},
  1651  		},
  1652  		{
  1653  			`<script>document.write("<p>foo<\/script>");`,
  1654  			context{state: stateJS, element: elementScript},
  1655  		},
  1656  		{
  1657  			// <script and </script tags are escaped, so </script> should not
  1658  			// cause us to exit the JS state.
  1659  			`<script>document.write("<script>alert(1)</script>");`,
  1660  			context{state: stateJS, element: elementScript},
  1661  		},
  1662  		{
  1663  			`<script>document.write("<script>`,
  1664  			context{state: stateJSDqStr, element: elementScript},
  1665  		},
  1666  		{
  1667  			`<script>document.write("<script>alert(1)</script>`,
  1668  			context{state: stateJSDqStr, element: elementScript},
  1669  		},
  1670  		{
  1671  			`<script>document.write("<script>alert(1)<!--`,
  1672  			context{state: stateJSDqStr, element: elementScript},
  1673  		},
  1674  		{
  1675  			`<script>document.write("<script>alert(1)</Script>");`,
  1676  			context{state: stateJS, element: elementScript},
  1677  		},
  1678  		{
  1679  			`<script>document.write("<!--");`,
  1680  			context{state: stateJS, element: elementScript},
  1681  		},
  1682  		{
  1683  			`<script>let a = /</script`,
  1684  			context{state: stateJSRegexp, element: elementScript},
  1685  		},
  1686  		{
  1687  			`<script>let a = /</script/`,
  1688  			context{state: stateJS, element: elementScript, jsCtx: jsCtxDivOp},
  1689  		},
  1690  		{
  1691  			`<script type="text/template">`,
  1692  			context{state: stateText},
  1693  		},
  1694  		// covering issue 19968
  1695  		{
  1696  			`<script type="TEXT/JAVASCRIPT">`,
  1697  			context{state: stateJS, element: elementScript},
  1698  		},
  1699  		// covering issue 19965
  1700  		{
  1701  			`<script TYPE="text/template">`,
  1702  			context{state: stateText},
  1703  		},
  1704  		{
  1705  			`<script type="notjs">`,
  1706  			context{state: stateText},
  1707  		},
  1708  		{
  1709  			`<Script>`,
  1710  			context{state: stateJS, element: elementScript},
  1711  		},
  1712  		{
  1713  			`<SCRIPT>foo`,
  1714  			context{state: stateJS, jsCtx: jsCtxDivOp, element: elementScript},
  1715  		},
  1716  		{
  1717  			`<textarea>value`,
  1718  			context{state: stateRCDATA, element: elementTextarea},
  1719  		},
  1720  		{
  1721  			`<textarea>value</TEXTAREA>`,
  1722  			context{state: stateText},
  1723  		},
  1724  		{
  1725  			`<textarea name=html><b`,
  1726  			context{state: stateRCDATA, element: elementTextarea},
  1727  		},
  1728  		{
  1729  			`<title>value`,
  1730  			context{state: stateRCDATA, element: elementTitle},
  1731  		},
  1732  		{
  1733  			`<style>value`,
  1734  			context{state: stateCSS, element: elementStyle},
  1735  		},
  1736  		{
  1737  			`<a xlink:href`,
  1738  			context{state: stateAttrName, attr: attrURL},
  1739  		},
  1740  		{
  1741  			`<a xmlns`,
  1742  			context{state: stateAttrName, attr: attrURL},
  1743  		},
  1744  		{
  1745  			`<a xmlns:foo`,
  1746  			context{state: stateAttrName, attr: attrURL},
  1747  		},
  1748  		{
  1749  			`<a xmlnsxyz`,
  1750  			context{state: stateAttrName},
  1751  		},
  1752  		{
  1753  			`<a data-url`,
  1754  			context{state: stateAttrName, attr: attrURL},
  1755  		},
  1756  		{
  1757  			`<a data-iconUri`,
  1758  			context{state: stateAttrName, attr: attrURL},
  1759  		},
  1760  		{
  1761  			`<a data-urlItem`,
  1762  			context{state: stateAttrName, attr: attrURL},
  1763  		},
  1764  		{
  1765  			`<a g:`,
  1766  			context{state: stateAttrName},
  1767  		},
  1768  		{
  1769  			`<a g:url`,
  1770  			context{state: stateAttrName, attr: attrURL},
  1771  		},
  1772  		{
  1773  			`<a g:iconUri`,
  1774  			context{state: stateAttrName, attr: attrURL},
  1775  		},
  1776  		{
  1777  			`<a g:urlItem`,
  1778  			context{state: stateAttrName, attr: attrURL},
  1779  		},
  1780  		{
  1781  			`<a g:value`,
  1782  			context{state: stateAttrName},
  1783  		},
  1784  		{
  1785  			`<a svg:style='`,
  1786  			context{state: stateCSS, delim: delimSingleQuote, attr: attrStyle},
  1787  		},
  1788  		{
  1789  			`<svg:font-face`,
  1790  			context{state: stateTag},
  1791  		},
  1792  		{
  1793  			`<svg:a svg:onclick="`,
  1794  			context{state: stateJS, delim: delimDoubleQuote, attr: attrScript},
  1795  		},
  1796  		{
  1797  			`<svg:a svg:onclick="x()">`,
  1798  			context{},
  1799  		},
  1800  		{
  1801  			"<script>var a = `",
  1802  			context{state: stateJSTmplLit, element: elementScript},
  1803  		},
  1804  		{
  1805  			"<script>var a = `${",
  1806  			context{state: stateJS, element: elementScript, jsBraceDepth: []int{0}},
  1807  		},
  1808  		{
  1809  			"<script>var a = `${}",
  1810  			context{state: stateJSTmplLit, element: elementScript},
  1811  		},
  1812  		{
  1813  			"<script>var a = `${`",
  1814  			context{state: stateJSTmplLit, element: elementScript, jsBraceDepth: []int{0}},
  1815  		},
  1816  		{
  1817  			"<script>var a = `${var a = \"",
  1818  			context{state: stateJSDqStr, element: elementScript, jsBraceDepth: []int{0}},
  1819  		},
  1820  		{
  1821  			"<script>var a = `${var a = \"`",
  1822  			context{state: stateJSDqStr, element: elementScript, jsBraceDepth: []int{0}},
  1823  		},
  1824  		{
  1825  			"<script>var a = `${var a = \"}",
  1826  			context{state: stateJSDqStr, element: elementScript, jsBraceDepth: []int{0}},
  1827  		},
  1828  		{
  1829  			"<script>var a = `${``",
  1830  			context{state: stateJS, element: elementScript, jsBraceDepth: []int{0}},
  1831  		},
  1832  		{
  1833  			"<script>var a = `${`}",
  1834  			context{state: stateJSTmplLit, element: elementScript, jsBraceDepth: []int{0}},
  1835  		},
  1836  		{
  1837  			"<script>`${ {} } asd`</script><script>`${ {} }",
  1838  			context{state: stateJSTmplLit, element: elementScript},
  1839  		},
  1840  		{
  1841  			"<script>var foo = `${ (_ => { return \"x\" })() + \"${",
  1842  			context{state: stateJSDqStr, element: elementScript, jsBraceDepth: []int{0}},
  1843  		},
  1844  		{
  1845  			"<script>var a = `${ {</script><script>var b = `${ x }",
  1846  			context{state: stateJSTmplLit, element: elementScript, jsCtx: jsCtxDivOp},
  1847  		},
  1848  		{
  1849  			"<script>var foo = `x` + \"${",
  1850  			context{state: stateJSDqStr, element: elementScript},
  1851  		},
  1852  		{
  1853  			"<script>function f() { var a = `${}`; }",
  1854  			context{state: stateJS, element: elementScript},
  1855  		},
  1856  		{
  1857  			"<script>{`${}`}",
  1858  			context{state: stateJS, element: elementScript},
  1859  		},
  1860  		{
  1861  			"<script>`${ function f() { return `${1}` }() }`",
  1862  			context{state: stateJS, element: elementScript, jsCtx: jsCtxDivOp},
  1863  		},
  1864  		{
  1865  			"<script>function f() {`${ function f() { `${1}` } }`}",
  1866  			context{state: stateJS, element: elementScript, jsCtx: jsCtxDivOp},
  1867  		},
  1868  		{
  1869  			"<script>`${ { `` }",
  1870  			context{state: stateJS, element: elementScript, jsBraceDepth: []int{0}},
  1871  		},
  1872  		{
  1873  			"<script>`${ { }`",
  1874  			context{state: stateJSTmplLit, element: elementScript, jsBraceDepth: []int{0}},
  1875  		},
  1876  		{
  1877  			"<script>var foo = `${ foo({ a: { c: `${",
  1878  			context{state: stateJS, element: elementScript, jsBraceDepth: []int{2, 0}},
  1879  		},
  1880  		{
  1881  			"<script>var foo = `${ foo({ a: { c: `${ {{.}} }` }, b: ",
  1882  			context{state: stateJS, element: elementScript, jsBraceDepth: []int{1}},
  1883  		},
  1884  		{
  1885  			"<script>`${ `}",
  1886  			context{state: stateJSTmplLit, element: elementScript, jsBraceDepth: []int{0}},
  1887  		},
  1888  	}
  1889  
  1890  	for _, test := range tests {
  1891  		b, e := []byte(test.input), makeEscaper(nil)
  1892  		c := e.escapeText(context{}, &parse.TextNode{NodeType: parse.NodeText, Text: b})
  1893  		if !test.output.eq(c) {
  1894  			t.Errorf("input %q: want context\n\t%v\ngot\n\t%v", test.input, test.output, c)
  1895  			continue
  1896  		}
  1897  		if test.input != string(b) {
  1898  			t.Errorf("input %q: text node was modified: want %q got %q", test.input, test.input, b)
  1899  			continue
  1900  		}
  1901  	}
  1902  }
  1903  
  1904  func TestEnsurePipelineContains(t *testing.T) {
  1905  	tests := []struct {
  1906  		input, output string
  1907  		ids           []string
  1908  	}{
  1909  		{
  1910  			"{{.X}}",
  1911  			".X",
  1912  			[]string{},
  1913  		},
  1914  		{
  1915  			"{{.X | html}}",
  1916  			".X | html",
  1917  			[]string{},
  1918  		},
  1919  		{
  1920  			"{{.X}}",
  1921  			".X | html",
  1922  			[]string{"html"},
  1923  		},
  1924  		{
  1925  			"{{html .X}}",
  1926  			"_eval_args_ .X | html | urlquery",
  1927  			[]string{"html", "urlquery"},
  1928  		},
  1929  		{
  1930  			"{{html .X .Y .Z}}",
  1931  			"_eval_args_ .X .Y .Z | html | urlquery",
  1932  			[]string{"html", "urlquery"},
  1933  		},
  1934  		{
  1935  			"{{.X | print}}",
  1936  			".X | print | urlquery",
  1937  			[]string{"urlquery"},
  1938  		},
  1939  		{
  1940  			"{{.X | print | urlquery}}",
  1941  			".X | print | urlquery",
  1942  			[]string{"urlquery"},
  1943  		},
  1944  		{
  1945  			"{{.X | urlquery}}",
  1946  			".X | html | urlquery",
  1947  			[]string{"html", "urlquery"},
  1948  		},
  1949  		{
  1950  			"{{.X | print 2 | .f 3}}",
  1951  			".X | print 2 | .f 3 | urlquery | html",
  1952  			[]string{"urlquery", "html"},
  1953  		},
  1954  		{
  1955  			// covering issue 10801
  1956  			"{{.X | println.x }}",
  1957  			".X | println.x | urlquery | html",
  1958  			[]string{"urlquery", "html"},
  1959  		},
  1960  		{
  1961  			// covering issue 10801
  1962  			"{{.X | (print 12 | println).x }}",
  1963  			".X | (print 12 | println).x | urlquery | html",
  1964  			[]string{"urlquery", "html"},
  1965  		},
  1966  		// The following test cases ensure that the merging of internal escapers
  1967  		// with the predefined "html" and "urlquery" escapers is correct.
  1968  		{
  1969  			"{{.X | urlquery}}",
  1970  			".X | _html_template_urlfilter | urlquery",
  1971  			[]string{"_html_template_urlfilter", "_html_template_urlnormalizer"},
  1972  		},
  1973  		{
  1974  			"{{.X | urlquery}}",
  1975  			".X | urlquery | _html_template_urlfilter | _html_template_cssescaper",
  1976  			[]string{"_html_template_urlfilter", "_html_template_cssescaper"},
  1977  		},
  1978  		{
  1979  			"{{.X | urlquery}}",
  1980  			".X | urlquery",
  1981  			[]string{"_html_template_urlnormalizer"},
  1982  		},
  1983  		{
  1984  			"{{.X | urlquery}}",
  1985  			".X | urlquery",
  1986  			[]string{"_html_template_urlescaper"},
  1987  		},
  1988  		{
  1989  			"{{.X | html}}",
  1990  			".X | html",
  1991  			[]string{"_html_template_htmlescaper"},
  1992  		},
  1993  		{
  1994  			"{{.X | html}}",
  1995  			".X | html",
  1996  			[]string{"_html_template_rcdataescaper"},
  1997  		},
  1998  	}
  1999  	for i, test := range tests {
  2000  		tmpl := template.Must(template.New("test").Parse(test.input))
  2001  		action, ok := (tmpl.Tree.Root.Nodes[0].(*parse.ActionNode))
  2002  		if !ok {
  2003  			t.Errorf("First node is not an action: %s", test.input)
  2004  			continue
  2005  		}
  2006  		pipe := action.Pipe
  2007  		originalIDs := make([]string, len(test.ids))
  2008  		copy(originalIDs, test.ids)
  2009  		ensurePipelineContains(pipe, test.ids)
  2010  		got := pipe.String()
  2011  		if got != test.output {
  2012  			t.Errorf("#%d: %s, %v: want\n\t%s\ngot\n\t%s", i, test.input, originalIDs, test.output, got)
  2013  		}
  2014  	}
  2015  }
  2016  
  2017  func TestEscapeMalformedPipelines(t *testing.T) {
  2018  	tests := []string{
  2019  		"{{ 0 | $ }}",
  2020  		"{{ 0 | $ | urlquery }}",
  2021  		"{{ 0 | (nil) }}",
  2022  		"{{ 0 | (nil) | html }}",
  2023  	}
  2024  	for _, test := range tests {
  2025  		var b bytes.Buffer
  2026  		tmpl, err := New("test").Parse(test)
  2027  		if err != nil {
  2028  			t.Errorf("failed to parse set: %q", err)
  2029  		}
  2030  		err = tmpl.Execute(&b, nil)
  2031  		if err == nil {
  2032  			t.Errorf("Expected error for %q", test)
  2033  		}
  2034  	}
  2035  }
  2036  
  2037  func TestEscapeErrorsNotIgnorable(t *testing.T) {
  2038  	var b bytes.Buffer
  2039  	tmpl, _ := New("dangerous").Parse("<a")
  2040  	err := tmpl.Execute(&b, nil)
  2041  	if err == nil {
  2042  		t.Errorf("Expected error")
  2043  	} else if b.Len() != 0 {
  2044  		t.Errorf("Emitted output despite escaping failure")
  2045  	}
  2046  }
  2047  
  2048  func TestEscapeSetErrorsNotIgnorable(t *testing.T) {
  2049  	var b bytes.Buffer
  2050  	tmpl, err := New("root").Parse(`{{define "t"}}<a{{end}}`)
  2051  	if err != nil {
  2052  		t.Errorf("failed to parse set: %q", err)
  2053  	}
  2054  	err = tmpl.ExecuteTemplate(&b, "t", nil)
  2055  	if err == nil {
  2056  		t.Errorf("Expected error")
  2057  	} else if b.Len() != 0 {
  2058  		t.Errorf("Emitted output despite escaping failure")
  2059  	}
  2060  }
  2061  
  2062  func TestRedundantFuncs(t *testing.T) {
  2063  	inputs := []any{
  2064  		"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f" +
  2065  			"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
  2066  			` !"#$%&'()*+,-./` +
  2067  			`0123456789:;<=>?` +
  2068  			`@ABCDEFGHIJKLMNO` +
  2069  			`PQRSTUVWXYZ[\]^_` +
  2070  			"`abcdefghijklmno" +
  2071  			"pqrstuvwxyz{|}~\x7f" +
  2072  			"\u00A0\u0100\u2028\u2029\ufeff\ufdec\ufffd\uffff\U0001D11E" +
  2073  			"&amp;%22\\",
  2074  		CSS(`a[href =~ "//example.com"]#foo`),
  2075  		HTML(`Hello, <b>World</b> &amp;tc!`),
  2076  		HTMLAttr(` dir="ltr"`),
  2077  		JS(`c && alert("Hello, World!");`),
  2078  		JSStr(`Hello, World & O'Reilly\x21`),
  2079  		URL(`greeting=H%69&addressee=(World)`),
  2080  	}
  2081  
  2082  	for n0, m := range redundantFuncs {
  2083  		f0 := funcMap[n0].(func(...any) string)
  2084  		for n1 := range m {
  2085  			f1 := funcMap[n1].(func(...any) string)
  2086  			for _, input := range inputs {
  2087  				want := f0(input)
  2088  				if got := f1(want); want != got {
  2089  					t.Errorf("%s %s with %T %q: want\n\t%q,\ngot\n\t%q", n0, n1, input, input, want, got)
  2090  				}
  2091  			}
  2092  		}
  2093  	}
  2094  }
  2095  
  2096  func TestIndirectPrint(t *testing.T) {
  2097  	a := 3
  2098  	ap := &a
  2099  	b := "hello"
  2100  	bp := &b
  2101  	bpp := &bp
  2102  	tmpl := Must(New("t").Parse(`{{.}}`))
  2103  	var buf strings.Builder
  2104  	err := tmpl.Execute(&buf, ap)
  2105  	if err != nil {
  2106  		t.Errorf("Unexpected error: %s", err)
  2107  	} else if buf.String() != "3" {
  2108  		t.Errorf(`Expected "3"; got %q`, buf.String())
  2109  	}
  2110  	buf.Reset()
  2111  	err = tmpl.Execute(&buf, bpp)
  2112  	if err != nil {
  2113  		t.Errorf("Unexpected error: %s", err)
  2114  	} else if buf.String() != "hello" {
  2115  		t.Errorf(`Expected "hello"; got %q`, buf.String())
  2116  	}
  2117  }
  2118  
  2119  // This is a test for issue 3272.
  2120  func TestEmptyTemplateHTML(t *testing.T) {
  2121  	page := Must(New("page").ParseFiles(os.DevNull))
  2122  	if err := page.ExecuteTemplate(os.Stdout, "page", "nothing"); err == nil {
  2123  		t.Fatal("expected error")
  2124  	}
  2125  }
  2126  
  2127  type Issue7379 int
  2128  
  2129  func (Issue7379) SomeMethod(x int) string {
  2130  	return fmt.Sprintf("<%d>", x)
  2131  }
  2132  
  2133  // This is a test for issue 7379: type assertion error caused panic, and then
  2134  // the code to handle the panic breaks escaping. It's hard to see the second
  2135  // problem once the first is fixed, but its fix is trivial so we let that go. See
  2136  // the discussion for issue 7379.
  2137  func TestPipeToMethodIsEscaped(t *testing.T) {
  2138  	tmpl := Must(New("x").Parse("<html>{{0 | .SomeMethod}}</html>\n"))
  2139  	tryExec := func() string {
  2140  		defer func() {
  2141  			panicValue := recover()
  2142  			if panicValue != nil {
  2143  				t.Errorf("panicked: %v\n", panicValue)
  2144  			}
  2145  		}()
  2146  		var b strings.Builder
  2147  		tmpl.Execute(&b, Issue7379(0))
  2148  		return b.String()
  2149  	}
  2150  	for i := 0; i < 3; i++ {
  2151  		str := tryExec()
  2152  		const expect = "<html>&lt;0&gt;</html>\n"
  2153  		if str != expect {
  2154  			t.Errorf("expected %q got %q", expect, str)
  2155  		}
  2156  	}
  2157  }
  2158  
  2159  // Unlike text/template, html/template crashed if given an incomplete
  2160  // template, that is, a template that had been named but not given any content.
  2161  // This is issue #10204.
  2162  func TestErrorOnUndefined(t *testing.T) {
  2163  	tmpl := New("undefined")
  2164  
  2165  	err := tmpl.Execute(nil, nil)
  2166  	if err == nil {
  2167  		t.Error("expected error")
  2168  	} else if !strings.Contains(err.Error(), "incomplete") {
  2169  		t.Errorf("expected error about incomplete template; got %s", err)
  2170  	}
  2171  }
  2172  
  2173  // This covers issue #20842.
  2174  func TestIdempotentExecute(t *testing.T) {
  2175  	tmpl := Must(New("").
  2176  		Parse(`{{define "main"}}<body>{{template "hello"}}</body>{{end}}`))
  2177  	Must(tmpl.
  2178  		Parse(`{{define "hello"}}Hello, {{"Ladies & Gentlemen!"}}{{end}}`))
  2179  	got := new(strings.Builder)
  2180  	var err error
  2181  	// Ensure that "hello" produces the same output when executed twice.
  2182  	want := "Hello, Ladies &amp; Gentlemen!"
  2183  	for i := 0; i < 2; i++ {
  2184  		err = tmpl.ExecuteTemplate(got, "hello", nil)
  2185  		if err != nil {
  2186  			t.Errorf("unexpected error: %s", err)
  2187  		}
  2188  		if got.String() != want {
  2189  			t.Errorf("after executing template \"hello\", got:\n\t%q\nwant:\n\t%q\n", got.String(), want)
  2190  		}
  2191  		got.Reset()
  2192  	}
  2193  	// Ensure that the implicit re-execution of "hello" during the execution of
  2194  	// "main" does not cause the output of "hello" to change.
  2195  	err = tmpl.ExecuteTemplate(got, "main", nil)
  2196  	if err != nil {
  2197  		t.Errorf("unexpected error: %s", err)
  2198  	}
  2199  	// If the HTML escaper is added again to the action {{"Ladies & Gentlemen!"}},
  2200  	// we would expected to see the ampersand overescaped to "&amp;amp;".
  2201  	want = "<body>Hello, Ladies &amp; Gentlemen!</body>"
  2202  	if got.String() != want {
  2203  		t.Errorf("after executing template \"main\", got:\n\t%q\nwant:\n\t%q\n", got.String(), want)
  2204  	}
  2205  }
  2206  
  2207  func BenchmarkEscapedExecute(b *testing.B) {
  2208  	tmpl := Must(New("t").Parse(`<a onclick="alert('{{.}}')">{{.}}</a>`))
  2209  	var buf bytes.Buffer
  2210  	b.ResetTimer()
  2211  	for i := 0; i < b.N; i++ {
  2212  		tmpl.Execute(&buf, "foo & 'bar' & baz")
  2213  		buf.Reset()
  2214  	}
  2215  }
  2216  
  2217  // Covers issue 22780.
  2218  func TestOrphanedTemplate(t *testing.T) {
  2219  	t1 := Must(New("foo").Parse(`<a href="{{.}}">link1</a>`))
  2220  	t2 := Must(t1.New("foo").Parse(`bar`))
  2221  
  2222  	var b strings.Builder
  2223  	const wantError = `template: "foo" is an incomplete or empty template`
  2224  	if err := t1.Execute(&b, "javascript:alert(1)"); err == nil {
  2225  		t.Fatal("expected error executing t1")
  2226  	} else if gotError := err.Error(); gotError != wantError {
  2227  		t.Fatalf("got t1 execution error:\n\t%s\nwant:\n\t%s", gotError, wantError)
  2228  	}
  2229  	b.Reset()
  2230  	if err := t2.Execute(&b, nil); err != nil {
  2231  		t.Fatalf("error executing t2: %s", err)
  2232  	}
  2233  	const want = "bar"
  2234  	if got := b.String(); got != want {
  2235  		t.Fatalf("t2 rendered %q, want %q", got, want)
  2236  	}
  2237  }
  2238  
  2239  // Covers issue 21844.
  2240  func TestAliasedParseTreeDoesNotOverescape(t *testing.T) {
  2241  	const (
  2242  		tmplText = `{{.}}`
  2243  		data     = `<baz>`
  2244  		want     = `&lt;baz&gt;`
  2245  	)
  2246  	// Templates "foo" and "bar" both alias the same underlying parse tree.
  2247  	tpl := Must(New("foo").Parse(tmplText))
  2248  	if _, err := tpl.AddParseTree("bar", tpl.Tree); err != nil {
  2249  		t.Fatalf("AddParseTree error: %v", err)
  2250  	}
  2251  	var b1, b2 strings.Builder
  2252  	if err := tpl.ExecuteTemplate(&b1, "foo", data); err != nil {
  2253  		t.Fatalf(`ExecuteTemplate failed for "foo": %v`, err)
  2254  	}
  2255  	if err := tpl.ExecuteTemplate(&b2, "bar", data); err != nil {
  2256  		t.Fatalf(`ExecuteTemplate failed for "foo": %v`, err)
  2257  	}
  2258  	got1, got2 := b1.String(), b2.String()
  2259  	if got1 != want {
  2260  		t.Fatalf(`Template "foo" rendered %q, want %q`, got1, want)
  2261  	}
  2262  	if got1 != got2 {
  2263  		t.Fatalf(`Template "foo" and "bar" rendered %q and %q respectively, expected equal values`, got1, got2)
  2264  	}
  2265  }
  2266  
  2267  func TestMetaContentEscapeGODEBUG(t *testing.T) {
  2268  	savedGODEBUG := os.Getenv("GODEBUG")
  2269  	os.Setenv("GODEBUG", savedGODEBUG+",htmlmetacontenturlescape=0")
  2270  	defer func() { os.Setenv("GODEBUG", savedGODEBUG) }()
  2271  
  2272  	tmpl := Must(New("").Parse(`<meta http-equiv="refresh" content="asd; url={{"javascript:alert(1)"}}; asd; url={{"vbscript:alert(1)"}}; asd">`))
  2273  	var b strings.Builder
  2274  	if err := tmpl.Execute(&b, nil); err != nil {
  2275  		t.Fatalf("unexpected error: %s", err)
  2276  	}
  2277  	want := `<meta http-equiv="refresh" content="asd; url=javascript:alert(1); asd; url=vbscript:alert(1); asd">`
  2278  	if got := b.String(); got != want {
  2279  		t.Fatalf("got %q, want %q", got, want)
  2280  	}
  2281  }
  2282  

View as plain text