Source file src/html/template/escape_test.go

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

View as plain text