Source file src/html/template/escape.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  	"fmt"
    10  	"html"
    11  	"internal/godebug"
    12  	"io"
    13  	"maps"
    14  	"regexp"
    15  	"text/template"
    16  	"text/template/parse"
    17  )
    18  
    19  // escapeTemplate rewrites the named template, which must be
    20  // associated with t, to guarantee that the output of any of the named
    21  // templates is properly escaped. If no error is returned, then the named templates have
    22  // been modified. Otherwise the named templates have been rendered
    23  // unusable.
    24  func escapeTemplate(tmpl *Template, node parse.Node, name string) error {
    25  	c, _ := tmpl.esc.escapeTree(context{}, node, name, 0)
    26  	var err error
    27  	if c.err != nil {
    28  		err, c.err.Name = c.err, name
    29  	} else if c.state != stateText {
    30  		err = &Error{ErrEndContext, nil, name, 0, fmt.Sprintf("ends in a non-text context: %v", c)}
    31  	}
    32  	if err != nil {
    33  		// Prevent execution of unsafe templates.
    34  		if t := tmpl.set[name]; t != nil {
    35  			t.escapeErr = err
    36  			t.text.Tree = nil
    37  			t.Tree = nil
    38  		}
    39  		return err
    40  	}
    41  	tmpl.esc.commit()
    42  	if t := tmpl.set[name]; t != nil {
    43  		t.escapeErr = escapeOK
    44  		t.Tree = t.text.Tree
    45  	}
    46  	return nil
    47  }
    48  
    49  // evalArgs formats the list of arguments into a string. It is equivalent to
    50  // fmt.Sprint(args...), except that it dereferences all pointers.
    51  func evalArgs(args ...any) string {
    52  	// Optimization for simple common case of a single string argument.
    53  	if len(args) == 1 {
    54  		if s, ok := args[0].(string); ok {
    55  			return s
    56  		}
    57  	}
    58  	for i, arg := range args {
    59  		args[i] = indirectToStringerOrError(arg)
    60  	}
    61  	return fmt.Sprint(args...)
    62  }
    63  
    64  // funcMap maps command names to functions that render their inputs safe.
    65  var funcMap = template.FuncMap{
    66  	"_html_template_attrescaper":      attrEscaper,
    67  	"_html_template_commentescaper":   commentEscaper,
    68  	"_html_template_cssescaper":       cssEscaper,
    69  	"_html_template_cssvaluefilter":   cssValueFilter,
    70  	"_html_template_htmlnamefilter":   htmlNameFilter,
    71  	"_html_template_htmlescaper":      htmlEscaper,
    72  	"_html_template_jsregexpescaper":  jsRegexpEscaper,
    73  	"_html_template_jsstrescaper":     jsStrEscaper,
    74  	"_html_template_jstmpllitescaper": jsTmplLitEscaper,
    75  	"_html_template_jsvalescaper":     jsValEscaper,
    76  	"_html_template_nospaceescaper":   htmlNospaceEscaper,
    77  	"_html_template_rcdataescaper":    rcdataEscaper,
    78  	"_html_template_srcsetescaper":    srcsetFilterAndEscaper,
    79  	"_html_template_urlescaper":       urlEscaper,
    80  	"_html_template_urlfilter":        urlFilter,
    81  	"_html_template_urlnormalizer":    urlNormalizer,
    82  	"_eval_args_":                     evalArgs,
    83  }
    84  
    85  // escaper collects type inferences about templates and changes needed to make
    86  // templates injection safe.
    87  type escaper struct {
    88  	// ns is the nameSpace that this escaper is associated with.
    89  	ns *nameSpace
    90  	// output[templateName] is the output context for a templateName that
    91  	// has been mangled to include its input context.
    92  	output map[string]context
    93  	// derived[c.mangle(name)] maps to a template derived from the template
    94  	// named name templateName for the start context c.
    95  	derived map[string]*template.Template
    96  	// called[templateName] is a set of called mangled template names.
    97  	called map[string]bool
    98  	// xxxNodeEdits are the accumulated edits to apply during commit.
    99  	// Such edits are not applied immediately in case a template set
   100  	// executes a given template in different escaping contexts.
   101  	actionNodeEdits   map[*parse.ActionNode][]string
   102  	templateNodeEdits map[*parse.TemplateNode]string
   103  	textNodeEdits     map[*parse.TextNode][]byte
   104  	// rangeContext holds context about the current range loop.
   105  	rangeContext *rangeContext
   106  }
   107  
   108  // rangeContext holds information about the current range loop.
   109  type rangeContext struct {
   110  	outer     *rangeContext // outer loop
   111  	breaks    []context     // context at each break action
   112  	continues []context     // context at each continue action
   113  }
   114  
   115  // makeEscaper creates a blank escaper for the given set.
   116  func makeEscaper(n *nameSpace) escaper {
   117  	return escaper{
   118  		n,
   119  		map[string]context{},
   120  		map[string]*template.Template{},
   121  		map[string]bool{},
   122  		map[*parse.ActionNode][]string{},
   123  		map[*parse.TemplateNode]string{},
   124  		map[*parse.TextNode][]byte{},
   125  		nil,
   126  	}
   127  }
   128  
   129  // filterFailsafe is an innocuous word that is emitted in place of unsafe values
   130  // by sanitizer functions. It is not a keyword in any programming language,
   131  // contains no special characters, is not empty, and when it appears in output
   132  // it is distinct enough that a developer can find the source of the problem
   133  // via a search engine.
   134  const filterFailsafe = "ZgotmplZ"
   135  
   136  // escape escapes a template node.
   137  func (e *escaper) escape(c context, n parse.Node) context {
   138  	switch n := n.(type) {
   139  	case *parse.ActionNode:
   140  		return e.escapeAction(c, n)
   141  	case *parse.BreakNode:
   142  		c.n = n
   143  		e.rangeContext.breaks = append(e.rangeContext.breaks, c)
   144  		return context{state: stateDead}
   145  	case *parse.CommentNode:
   146  		return c
   147  	case *parse.ContinueNode:
   148  		c.n = n
   149  		e.rangeContext.continues = append(e.rangeContext.continues, c)
   150  		return context{state: stateDead}
   151  	case *parse.IfNode:
   152  		return e.escapeBranch(c, &n.BranchNode, "if")
   153  	case *parse.ListNode:
   154  		return e.escapeList(c, n)
   155  	case *parse.RangeNode:
   156  		return e.escapeBranch(c, &n.BranchNode, "range")
   157  	case *parse.TemplateNode:
   158  		return e.escapeTemplate(c, n)
   159  	case *parse.TextNode:
   160  		return e.escapeText(c, n)
   161  	case *parse.WithNode:
   162  		return e.escapeBranch(c, &n.BranchNode, "with")
   163  	}
   164  	panic("escaping " + n.String() + " is unimplemented")
   165  }
   166  
   167  var debugAllowActionJSTmpl = godebug.New("jstmpllitinterp")
   168  
   169  // escapeAction escapes an action template node.
   170  func (e *escaper) escapeAction(c context, n *parse.ActionNode) context {
   171  	if len(n.Pipe.Decl) != 0 {
   172  		// A local variable assignment, not an interpolation.
   173  		return c
   174  	}
   175  	c = nudge(c)
   176  	// Check for disallowed use of predefined escapers in the pipeline.
   177  	for pos, idNode := range n.Pipe.Cmds {
   178  		node, ok := idNode.Args[0].(*parse.IdentifierNode)
   179  		if !ok {
   180  			// A predefined escaper "esc" will never be found as an identifier in a
   181  			// Chain or Field node, since:
   182  			// - "esc.x ..." is invalid, since predefined escapers return strings, and
   183  			//   strings do not have methods, keys or fields.
   184  			// - "... .esc" is invalid, since predefined escapers are global functions,
   185  			//   not methods or fields of any types.
   186  			// Therefore, it is safe to ignore these two node types.
   187  			continue
   188  		}
   189  		ident := node.Ident
   190  		if _, ok := predefinedEscapers[ident]; ok {
   191  			if pos < len(n.Pipe.Cmds)-1 ||
   192  				c.state == stateAttr && c.delim == delimSpaceOrTagEnd && ident == "html" {
   193  				return context{
   194  					state: stateError,
   195  					err:   errorf(ErrPredefinedEscaper, n, n.Line, "predefined escaper %q disallowed in template", ident),
   196  				}
   197  			}
   198  		}
   199  	}
   200  	s := make([]string, 0, 3)
   201  	switch c.state {
   202  	case stateError:
   203  		return c
   204  	case stateURL, stateCSSDqStr, stateCSSSqStr, stateCSSDqURL, stateCSSSqURL, stateCSSURL:
   205  		switch c.urlPart {
   206  		case urlPartNone:
   207  			s = append(s, "_html_template_urlfilter")
   208  			fallthrough
   209  		case urlPartPreQuery:
   210  			switch c.state {
   211  			case stateCSSDqStr, stateCSSSqStr:
   212  				s = append(s, "_html_template_cssescaper")
   213  			default:
   214  				s = append(s, "_html_template_urlnormalizer")
   215  			}
   216  		case urlPartQueryOrFrag:
   217  			s = append(s, "_html_template_urlescaper")
   218  		case urlPartUnknown:
   219  			return context{
   220  				state: stateError,
   221  				err:   errorf(ErrAmbigContext, n, n.Line, "%s appears in an ambiguous context within a URL", n),
   222  			}
   223  		default:
   224  			panic(c.urlPart.String())
   225  		}
   226  	case stateJS:
   227  		s = append(s, "_html_template_jsvalescaper")
   228  		// A slash after a value starts a div operator.
   229  		c.jsCtx = jsCtxDivOp
   230  	case stateJSDqStr, stateJSSqStr:
   231  		s = append(s, "_html_template_jsstrescaper")
   232  	case stateJSTmplLit:
   233  		s = append(s, "_html_template_jstmpllitescaper")
   234  	case stateJSRegexp:
   235  		s = append(s, "_html_template_jsregexpescaper")
   236  	case stateCSS:
   237  		s = append(s, "_html_template_cssvaluefilter")
   238  	case stateText:
   239  		s = append(s, "_html_template_htmlescaper")
   240  	case stateRCDATA:
   241  		s = append(s, "_html_template_rcdataescaper")
   242  	case stateAttr:
   243  		// Handled below in delim check.
   244  	case stateAttrName, stateTag:
   245  		c.state = stateAttrName
   246  		s = append(s, "_html_template_htmlnamefilter")
   247  	case stateSrcset:
   248  		s = append(s, "_html_template_srcsetescaper")
   249  	default:
   250  		if isComment(c.state) {
   251  			s = append(s, "_html_template_commentescaper")
   252  		} else {
   253  			panic("unexpected state " + c.state.String())
   254  		}
   255  	}
   256  	switch c.delim {
   257  	case delimNone:
   258  		// No extra-escaping needed for raw text content.
   259  	case delimSpaceOrTagEnd:
   260  		s = append(s, "_html_template_nospaceescaper")
   261  	default:
   262  		s = append(s, "_html_template_attrescaper")
   263  	}
   264  	e.editActionNode(n, s)
   265  	return c
   266  }
   267  
   268  // ensurePipelineContains ensures that the pipeline ends with the commands with
   269  // the identifiers in s in order. If the pipeline ends with a predefined escaper
   270  // (i.e. "html" or "urlquery"), merge it with the identifiers in s.
   271  func ensurePipelineContains(p *parse.PipeNode, s []string) {
   272  	if len(s) == 0 {
   273  		// Do not rewrite pipeline if we have no escapers to insert.
   274  		return
   275  	}
   276  	// Precondition: p.Cmds contains at most one predefined escaper and the
   277  	// escaper will be present at p.Cmds[len(p.Cmds)-1]. This precondition is
   278  	// always true because of the checks in escapeAction.
   279  	pipelineLen := len(p.Cmds)
   280  	if pipelineLen > 0 {
   281  		lastCmd := p.Cmds[pipelineLen-1]
   282  		if idNode, ok := lastCmd.Args[0].(*parse.IdentifierNode); ok {
   283  			if esc := idNode.Ident; predefinedEscapers[esc] {
   284  				// Pipeline ends with a predefined escaper.
   285  				if len(p.Cmds) == 1 && len(lastCmd.Args) > 1 {
   286  					// Special case: pipeline is of the form {{ esc arg1 arg2 ... argN }},
   287  					// where esc is the predefined escaper, and arg1...argN are its arguments.
   288  					// Convert this into the equivalent form
   289  					// {{ _eval_args_ arg1 arg2 ... argN | esc }}, so that esc can be easily
   290  					// merged with the escapers in s.
   291  					lastCmd.Args[0] = parse.NewIdentifier("_eval_args_").SetTree(nil).SetPos(lastCmd.Args[0].Position())
   292  					p.Cmds = appendCmd(p.Cmds, newIdentCmd(esc, p.Position()))
   293  					pipelineLen++
   294  				}
   295  				// If any of the commands in s that we are about to insert is equivalent
   296  				// to the predefined escaper, use the predefined escaper instead.
   297  				dup := false
   298  				for i, escaper := range s {
   299  					if escFnsEq(esc, escaper) {
   300  						s[i] = idNode.Ident
   301  						dup = true
   302  					}
   303  				}
   304  				if dup {
   305  					// The predefined escaper will already be inserted along with the
   306  					// escapers in s, so do not copy it to the rewritten pipeline.
   307  					pipelineLen--
   308  				}
   309  			}
   310  		}
   311  	}
   312  	// Rewrite the pipeline, creating the escapers in s at the end of the pipeline.
   313  	newCmds := make([]*parse.CommandNode, pipelineLen, pipelineLen+len(s))
   314  	insertedIdents := make(map[string]bool)
   315  	for i := 0; i < pipelineLen; i++ {
   316  		cmd := p.Cmds[i]
   317  		newCmds[i] = cmd
   318  		if idNode, ok := cmd.Args[0].(*parse.IdentifierNode); ok {
   319  			insertedIdents[normalizeEscFn(idNode.Ident)] = true
   320  		}
   321  	}
   322  	for _, name := range s {
   323  		if !insertedIdents[normalizeEscFn(name)] {
   324  			// When two templates share an underlying parse tree via the use of
   325  			// AddParseTree and one template is executed after the other, this check
   326  			// ensures that escapers that were already inserted into the pipeline on
   327  			// the first escaping pass do not get inserted again.
   328  			newCmds = appendCmd(newCmds, newIdentCmd(name, p.Position()))
   329  		}
   330  	}
   331  	p.Cmds = newCmds
   332  }
   333  
   334  // predefinedEscapers contains template predefined escapers that are equivalent
   335  // to some contextual escapers. Keep in sync with equivEscapers.
   336  var predefinedEscapers = map[string]bool{
   337  	"html":     true,
   338  	"urlquery": true,
   339  }
   340  
   341  // equivEscapers matches contextual escapers to equivalent predefined
   342  // template escapers.
   343  var equivEscapers = map[string]string{
   344  	// The following pairs of HTML escapers provide equivalent security
   345  	// guarantees, since they all escape '\000', '\'', '"', '&', '<', and '>'.
   346  	"_html_template_attrescaper":   "html",
   347  	"_html_template_htmlescaper":   "html",
   348  	"_html_template_rcdataescaper": "html",
   349  	// These two URL escapers produce URLs safe for embedding in a URL query by
   350  	// percent-encoding all the reserved characters specified in RFC 3986 Section
   351  	// 2.2
   352  	"_html_template_urlescaper": "urlquery",
   353  	// These two functions are not actually equivalent; urlquery is stricter as it
   354  	// escapes reserved characters (e.g. '#'), while _html_template_urlnormalizer
   355  	// does not. It is therefore only safe to replace _html_template_urlnormalizer
   356  	// with urlquery (this happens in ensurePipelineContains), but not the otherI've
   357  	// way around. We keep this entry around to preserve the behavior of templates
   358  	// written before Go 1.9, which might depend on this substitution taking place.
   359  	"_html_template_urlnormalizer": "urlquery",
   360  }
   361  
   362  // escFnsEq reports whether the two escaping functions are equivalent.
   363  func escFnsEq(a, b string) bool {
   364  	return normalizeEscFn(a) == normalizeEscFn(b)
   365  }
   366  
   367  // normalizeEscFn(a) is equal to normalizeEscFn(b) for any pair of names of
   368  // escaper functions a and b that are equivalent.
   369  func normalizeEscFn(e string) string {
   370  	if norm := equivEscapers[e]; norm != "" {
   371  		return norm
   372  	}
   373  	return e
   374  }
   375  
   376  // redundantFuncs[a][b] implies that funcMap[b](funcMap[a](x)) == funcMap[a](x)
   377  // for all x.
   378  var redundantFuncs = map[string]map[string]bool{
   379  	"_html_template_commentescaper": {
   380  		"_html_template_attrescaper": true,
   381  		"_html_template_htmlescaper": true,
   382  	},
   383  	"_html_template_cssescaper": {
   384  		"_html_template_attrescaper": true,
   385  	},
   386  	"_html_template_jsregexpescaper": {
   387  		"_html_template_attrescaper": true,
   388  	},
   389  	"_html_template_jsstrescaper": {
   390  		"_html_template_attrescaper": true,
   391  	},
   392  	"_html_template_jstmpllitescaper": {
   393  		"_html_template_attrescaper": true,
   394  	},
   395  	"_html_template_urlescaper": {
   396  		"_html_template_urlnormalizer": true,
   397  	},
   398  }
   399  
   400  // appendCmd appends the given command to the end of the command pipeline
   401  // unless it is redundant with the last command.
   402  func appendCmd(cmds []*parse.CommandNode, cmd *parse.CommandNode) []*parse.CommandNode {
   403  	if n := len(cmds); n != 0 {
   404  		last, okLast := cmds[n-1].Args[0].(*parse.IdentifierNode)
   405  		next, okNext := cmd.Args[0].(*parse.IdentifierNode)
   406  		if okLast && okNext && redundantFuncs[last.Ident][next.Ident] {
   407  			return cmds
   408  		}
   409  	}
   410  	return append(cmds, cmd)
   411  }
   412  
   413  // newIdentCmd produces a command containing a single identifier node.
   414  func newIdentCmd(identifier string, pos parse.Pos) *parse.CommandNode {
   415  	return &parse.CommandNode{
   416  		NodeType: parse.NodeCommand,
   417  		Args:     []parse.Node{parse.NewIdentifier(identifier).SetTree(nil).SetPos(pos)}, // TODO: SetTree.
   418  	}
   419  }
   420  
   421  // nudge returns the context that would result from following empty string
   422  // transitions from the input context.
   423  // For example, parsing:
   424  //
   425  //	`<a href=`
   426  //
   427  // will end in context{stateBeforeValue, attrURL}, but parsing one extra rune:
   428  //
   429  //	`<a href=x`
   430  //
   431  // will end in context{stateURL, delimSpaceOrTagEnd, ...}.
   432  // There are two transitions that happen when the 'x' is seen:
   433  // (1) Transition from a before-value state to a start-of-value state without
   434  //
   435  //	consuming any character.
   436  //
   437  // (2) Consume 'x' and transition past the first value character.
   438  // In this case, nudging produces the context after (1) happens.
   439  func nudge(c context) context {
   440  	switch c.state {
   441  	case stateTag:
   442  		// In `<foo {{.}}`, the action should emit an attribute.
   443  		c.state = stateAttrName
   444  	case stateBeforeValue:
   445  		// In `<foo bar={{.}}`, the action is an undelimited value.
   446  		c.state, c.delim, c.attr = attrStartStates[c.attr], delimSpaceOrTagEnd, attrNone
   447  	case stateAfterName:
   448  		// In `<foo bar {{.}}`, the action is an attribute name.
   449  		c.state, c.attr = stateAttrName, attrNone
   450  	}
   451  	return c
   452  }
   453  
   454  // join joins the two contexts of a branch template node. The result is an
   455  // error context if either of the input contexts are error contexts, or if the
   456  // input contexts differ.
   457  func join(a, b context, node parse.Node, nodeName string) context {
   458  	if a.state == stateError {
   459  		return a
   460  	}
   461  	if b.state == stateError {
   462  		return b
   463  	}
   464  	if a.state == stateDead {
   465  		return b
   466  	}
   467  	if b.state == stateDead {
   468  		return a
   469  	}
   470  	if a.eq(b) {
   471  		return a
   472  	}
   473  
   474  	c := a
   475  	c.urlPart = b.urlPart
   476  	if c.eq(b) {
   477  		// The contexts differ only by urlPart.
   478  		c.urlPart = urlPartUnknown
   479  		return c
   480  	}
   481  
   482  	c = a
   483  	c.jsCtx = b.jsCtx
   484  	if c.eq(b) {
   485  		// The contexts differ only by jsCtx.
   486  		c.jsCtx = jsCtxUnknown
   487  		return c
   488  	}
   489  
   490  	// Allow a nudged context to join with an unnudged one.
   491  	// This means that
   492  	//   <p title={{if .C}}{{.}}{{end}}
   493  	// ends in an unquoted value state even though the else branch
   494  	// ends in stateBeforeValue.
   495  	if c, d := nudge(a), nudge(b); !(c.eq(a) && d.eq(b)) {
   496  		if e := join(c, d, node, nodeName); e.state != stateError {
   497  			return e
   498  		}
   499  	}
   500  
   501  	return context{
   502  		state: stateError,
   503  		err:   errorf(ErrBranchEnd, node, 0, "{{%s}} branches end in different contexts: %v, %v", nodeName, a, b),
   504  	}
   505  }
   506  
   507  // escapeBranch escapes a branch template node: "if", "range" and "with".
   508  func (e *escaper) escapeBranch(c context, n *parse.BranchNode, nodeName string) context {
   509  	if nodeName == "range" {
   510  		e.rangeContext = &rangeContext{outer: e.rangeContext}
   511  	}
   512  	c0 := e.escapeList(c, n.List)
   513  	if nodeName == "range" {
   514  		if c0.state != stateError {
   515  			c0 = joinRange(c0, e.rangeContext)
   516  		}
   517  		e.rangeContext = e.rangeContext.outer
   518  		if c0.state == stateError {
   519  			return c0
   520  		}
   521  
   522  		// The "true" branch of a "range" node can execute multiple times.
   523  		// We check that executing n.List once results in the same context
   524  		// as executing n.List twice.
   525  		e.rangeContext = &rangeContext{outer: e.rangeContext}
   526  		c1, _ := e.escapeListConditionally(c0, n.List, nil)
   527  		c0 = join(c0, c1, n, nodeName)
   528  		if c0.state == stateError {
   529  			e.rangeContext = e.rangeContext.outer
   530  			// Make clear that this is a problem on loop re-entry
   531  			// since developers tend to overlook that branch when
   532  			// debugging templates.
   533  			c0.err.Line = n.Line
   534  			c0.err.Description = "on range loop re-entry: " + c0.err.Description
   535  			return c0
   536  		}
   537  		c0 = joinRange(c0, e.rangeContext)
   538  		e.rangeContext = e.rangeContext.outer
   539  		if c0.state == stateError {
   540  			return c0
   541  		}
   542  	}
   543  	c1 := e.escapeList(c, n.ElseList)
   544  	return join(c0, c1, n, nodeName)
   545  }
   546  
   547  func joinRange(c0 context, rc *rangeContext) context {
   548  	// Merge contexts at break and continue statements into overall body context.
   549  	// In theory we could treat breaks differently from continues, but for now it is
   550  	// enough to treat them both as going back to the start of the loop (which may then stop).
   551  	for _, c := range rc.breaks {
   552  		c0 = join(c0, c, c.n, "range")
   553  		if c0.state == stateError {
   554  			c0.err.Line = c.n.(*parse.BreakNode).Line
   555  			c0.err.Description = "at range loop break: " + c0.err.Description
   556  			return c0
   557  		}
   558  	}
   559  	for _, c := range rc.continues {
   560  		c0 = join(c0, c, c.n, "range")
   561  		if c0.state == stateError {
   562  			c0.err.Line = c.n.(*parse.ContinueNode).Line
   563  			c0.err.Description = "at range loop continue: " + c0.err.Description
   564  			return c0
   565  		}
   566  	}
   567  	return c0
   568  }
   569  
   570  // escapeList escapes a list template node.
   571  func (e *escaper) escapeList(c context, n *parse.ListNode) context {
   572  	if n == nil {
   573  		return c
   574  	}
   575  	for _, m := range n.Nodes {
   576  		c = e.escape(c, m)
   577  		if c.state == stateDead {
   578  			break
   579  		}
   580  	}
   581  	return c
   582  }
   583  
   584  // escapeListConditionally escapes a list node but only preserves edits and
   585  // inferences in e if the inferences and output context satisfy filter.
   586  // It returns the best guess at an output context, and the result of the filter
   587  // which is the same as whether e was updated.
   588  func (e *escaper) escapeListConditionally(c context, n *parse.ListNode, filter func(*escaper, context) bool) (context, bool) {
   589  	e1 := makeEscaper(e.ns)
   590  	e1.rangeContext = e.rangeContext
   591  	// Make type inferences available to f.
   592  	maps.Copy(e1.output, e.output)
   593  	c = e1.escapeList(c, n)
   594  	ok := filter != nil && filter(&e1, c)
   595  	if ok {
   596  		// Copy inferences and edits from e1 back into e.
   597  		maps.Copy(e.output, e1.output)
   598  		maps.Copy(e.derived, e1.derived)
   599  		maps.Copy(e.called, e1.called)
   600  		for k, v := range e1.actionNodeEdits {
   601  			e.editActionNode(k, v)
   602  		}
   603  		for k, v := range e1.templateNodeEdits {
   604  			e.editTemplateNode(k, v)
   605  		}
   606  		for k, v := range e1.textNodeEdits {
   607  			e.editTextNode(k, v)
   608  		}
   609  	}
   610  	return c, ok
   611  }
   612  
   613  // escapeTemplate escapes a {{template}} call node.
   614  func (e *escaper) escapeTemplate(c context, n *parse.TemplateNode) context {
   615  	c, name := e.escapeTree(c, n, n.Name, n.Line)
   616  	if name != n.Name {
   617  		e.editTemplateNode(n, name)
   618  	}
   619  	return c
   620  }
   621  
   622  // escapeTree escapes the named template starting in the given context as
   623  // necessary and returns its output context.
   624  func (e *escaper) escapeTree(c context, node parse.Node, name string, line int) (context, string) {
   625  	// Mangle the template name with the input context to produce a reliable
   626  	// identifier.
   627  	dname := c.mangle(name)
   628  	e.called[dname] = true
   629  	if out, ok := e.output[dname]; ok {
   630  		// Already escaped.
   631  		return out, dname
   632  	}
   633  	t := e.template(name)
   634  	if t == nil {
   635  		// Two cases: The template exists but is empty, or has never been mentioned at
   636  		// all. Distinguish the cases in the error messages.
   637  		if e.ns.set[name] != nil {
   638  			return context{
   639  				state: stateError,
   640  				err:   errorf(ErrNoSuchTemplate, node, line, "%q is an incomplete or empty template", name),
   641  			}, dname
   642  		}
   643  		return context{
   644  			state: stateError,
   645  			err:   errorf(ErrNoSuchTemplate, node, line, "no such template %q", name),
   646  		}, dname
   647  	}
   648  	if dname != name {
   649  		// Use any template derived during an earlier call to escapeTemplate
   650  		// with different top level templates, or clone if necessary.
   651  		dt := e.template(dname)
   652  		if dt == nil {
   653  			dt = template.New(dname)
   654  			dt.Tree = &parse.Tree{Name: dname, Root: t.Root.CopyList()}
   655  			e.derived[dname] = dt
   656  		}
   657  		t = dt
   658  	}
   659  	return e.computeOutCtx(c, t), dname
   660  }
   661  
   662  // computeOutCtx takes a template and its start context and computes the output
   663  // context while storing any inferences in e.
   664  func (e *escaper) computeOutCtx(c context, t *template.Template) context {
   665  	// Propagate context over the body.
   666  	c1, ok := e.escapeTemplateBody(c, t)
   667  	if !ok {
   668  		// Look for a fixed point by assuming c1 as the output context.
   669  		if c2, ok2 := e.escapeTemplateBody(c1, t); ok2 {
   670  			c1, ok = c2, true
   671  		}
   672  		// Use c1 as the error context if neither assumption worked.
   673  	}
   674  	if !ok && c1.state != stateError {
   675  		return context{
   676  			state: stateError,
   677  			err:   errorf(ErrOutputContext, t.Tree.Root, 0, "cannot compute output context for template %s", t.Name()),
   678  		}
   679  	}
   680  	return c1
   681  }
   682  
   683  // escapeTemplateBody escapes the given template assuming the given output
   684  // context, and returns the best guess at the output context and whether the
   685  // assumption was correct.
   686  func (e *escaper) escapeTemplateBody(c context, t *template.Template) (context, bool) {
   687  	filter := func(e1 *escaper, c1 context) bool {
   688  		if c1.state == stateError {
   689  			// Do not update the input escaper, e.
   690  			return false
   691  		}
   692  		if !e1.called[t.Name()] {
   693  			// If t is not recursively called, then c1 is an
   694  			// accurate output context.
   695  			return true
   696  		}
   697  		// c1 is accurate if it matches our assumed output context.
   698  		return c.eq(c1)
   699  	}
   700  	// We need to assume an output context so that recursive template calls
   701  	// take the fast path out of escapeTree instead of infinitely recurring.
   702  	// Naively assuming that the input context is the same as the output
   703  	// works >90% of the time.
   704  	e.output[t.Name()] = c
   705  	return e.escapeListConditionally(c, t.Tree.Root, filter)
   706  }
   707  
   708  // delimEnds maps each delim to a string of characters that terminate it.
   709  var delimEnds = [...]string{
   710  	delimDoubleQuote: `"`,
   711  	delimSingleQuote: "'",
   712  	// Determined empirically by running the below in various browsers.
   713  	// var div = document.createElement("DIV");
   714  	// for (var i = 0; i < 0x10000; ++i) {
   715  	//   div.innerHTML = "<span title=x" + String.fromCharCode(i) + "-bar>";
   716  	//   if (div.getElementsByTagName("SPAN")[0].title.indexOf("bar") < 0)
   717  	//     document.write("<p>U+" + i.toString(16));
   718  	// }
   719  	delimSpaceOrTagEnd: " \t\n\f\r>",
   720  }
   721  
   722  var (
   723  	// Per WHATWG HTML specification, section 4.12.1.3, there are extremely
   724  	// complicated rules for how to handle the set of opening tags <!--,
   725  	// <script, and </script when they appear in JS literals (i.e. strings,
   726  	// regexs, and comments). The specification suggests a simple solution,
   727  	// rather than implementing the arcane ABNF, which involves simply escaping
   728  	// the opening bracket with \x3C. We use the below regex for this, since it
   729  	// makes doing the case-insensitive find-replace much simpler.
   730  	specialScriptTagRE          = regexp.MustCompile("(?i)<(script|/script|!--)")
   731  	specialScriptTagReplacement = []byte("\\x3C$1")
   732  )
   733  
   734  func containsSpecialScriptTag(s []byte) bool {
   735  	return specialScriptTagRE.Match(s)
   736  }
   737  
   738  func escapeSpecialScriptTags(s []byte) []byte {
   739  	return specialScriptTagRE.ReplaceAll(s, specialScriptTagReplacement)
   740  }
   741  
   742  var doctypeBytes = []byte("<!DOCTYPE")
   743  
   744  // escapeText escapes a text template node.
   745  func (e *escaper) escapeText(c context, n *parse.TextNode) context {
   746  	s, written, i, b := n.Text, 0, 0, new(bytes.Buffer)
   747  	for i != len(s) {
   748  		c1, nread := contextAfterText(c, s[i:])
   749  		i1 := i + nread
   750  		if c.state == stateText || c.state == stateRCDATA {
   751  			end := i1
   752  			if c1.state != c.state {
   753  				for j := end - 1; j >= i; j-- {
   754  					if s[j] == '<' {
   755  						end = j
   756  						break
   757  					}
   758  				}
   759  			}
   760  			for j := i; j < end; j++ {
   761  				if s[j] == '<' && !bytes.HasPrefix(bytes.ToUpper(s[j:]), doctypeBytes) {
   762  					b.Write(s[written:j])
   763  					b.WriteString("&lt;")
   764  					written = j + 1
   765  				}
   766  			}
   767  		} else if isComment(c.state) && c.delim == delimNone {
   768  			switch c.state {
   769  			case stateJSBlockCmt:
   770  				// https://es5.github.io/#x7.4:
   771  				// "Comments behave like white space and are
   772  				// discarded except that, if a MultiLineComment
   773  				// contains a line terminator character, then
   774  				// the entire comment is considered to be a
   775  				// LineTerminator for purposes of parsing by
   776  				// the syntactic grammar."
   777  				if bytes.ContainsAny(s[written:i1], "\n\r\u2028\u2029") {
   778  					b.WriteByte('\n')
   779  				} else {
   780  					b.WriteByte(' ')
   781  				}
   782  			case stateCSSBlockCmt:
   783  				b.WriteByte(' ')
   784  			}
   785  			written = i1
   786  		}
   787  		if c.state != c1.state && isComment(c1.state) && c1.delim == delimNone {
   788  			// Preserve the portion between written and the comment start.
   789  			cs := i1 - 2
   790  			if c1.state == stateHTMLCmt || c1.state == stateJSHTMLOpenCmt {
   791  				// "<!--" instead of "/*" or "//"
   792  				cs -= 2
   793  			} else if c1.state == stateJSHTMLCloseCmt {
   794  				// "-->" instead of "/*" or "//"
   795  				cs -= 1
   796  			}
   797  			b.Write(s[written:cs])
   798  			written = i1
   799  		}
   800  		if isInScriptLiteral(c.state) && containsSpecialScriptTag(s[i:i1]) {
   801  			b.Write(s[written:i])
   802  			b.Write(escapeSpecialScriptTags(s[i:i1]))
   803  			written = i1
   804  		}
   805  		if i == i1 && c.state == c1.state {
   806  			panic(fmt.Sprintf("infinite loop from %v to %v on %q..%q", c, c1, s[:i], s[i:]))
   807  		}
   808  		c, i = c1, i1
   809  	}
   810  
   811  	if written != 0 && c.state != stateError {
   812  		if !isComment(c.state) || c.delim != delimNone {
   813  			b.Write(n.Text[written:])
   814  		}
   815  		e.editTextNode(n, b.Bytes())
   816  	}
   817  	return c
   818  }
   819  
   820  // contextAfterText starts in context c, consumes some tokens from the front of
   821  // s, then returns the context after those tokens and the unprocessed suffix.
   822  func contextAfterText(c context, s []byte) (context, int) {
   823  	if c.delim == delimNone {
   824  		c1, i := tSpecialTagEnd(c, s)
   825  		if i == 0 {
   826  			// A special end tag (`</script>`) has been seen and
   827  			// all content preceding it has been consumed.
   828  			return c1, 0
   829  		}
   830  		// Consider all content up to any end tag.
   831  		return transitionFunc[c.state](c, s[:i])
   832  	}
   833  
   834  	// We are at the beginning of an attribute value.
   835  
   836  	i := bytes.IndexAny(s, delimEnds[c.delim])
   837  	if i == -1 {
   838  		i = len(s)
   839  	}
   840  	if c.delim == delimSpaceOrTagEnd {
   841  		// https://www.w3.org/TR/html5/syntax.html#attribute-value-(unquoted)-state
   842  		// lists the runes below as error characters.
   843  		// Error out because HTML parsers may differ on whether
   844  		// "<a id= onclick=f("     ends inside id's or onclick's value,
   845  		// "<a class=`foo "        ends inside a value,
   846  		// "<a style=font:'Arial'" needs open-quote fixup.
   847  		// IE treats '`' as a quotation character.
   848  		if j := bytes.IndexAny(s[:i], "\"'<=`"); j >= 0 {
   849  			return context{
   850  				state: stateError,
   851  				err:   errorf(ErrBadHTML, nil, 0, "%q in unquoted attr: %q", s[j:j+1], s[:i]),
   852  			}, len(s)
   853  		}
   854  	}
   855  	if i == len(s) {
   856  		// Remain inside the attribute.
   857  		// Decode the value so non-HTML rules can easily handle
   858  		//     <button onclick="alert(&quot;Hi!&quot;)">
   859  		// without having to entity decode token boundaries.
   860  		for u := []byte(html.UnescapeString(string(s))); len(u) != 0; {
   861  			c1, i1 := transitionFunc[c.state](c, u)
   862  			c, u = c1, u[i1:]
   863  		}
   864  		return c, len(s)
   865  	}
   866  
   867  	element := c.element
   868  
   869  	// If this is a non-JS "type" attribute inside "script" tag, do not treat the contents as JS.
   870  	if c.state == stateAttr && c.element == elementScript && c.attr == attrScriptType && !isJSType(string(s[:i])) {
   871  		element = elementNone
   872  	}
   873  
   874  	if c.delim != delimSpaceOrTagEnd {
   875  		// Consume any quote.
   876  		i++
   877  	}
   878  	// On exiting an attribute, we discard all state information
   879  	// except the state and element.
   880  	return context{state: stateTag, element: element}, i
   881  }
   882  
   883  // editActionNode records a change to an action pipeline for later commit.
   884  func (e *escaper) editActionNode(n *parse.ActionNode, cmds []string) {
   885  	if _, ok := e.actionNodeEdits[n]; ok {
   886  		panic(fmt.Sprintf("node %s shared between templates", n))
   887  	}
   888  	e.actionNodeEdits[n] = cmds
   889  }
   890  
   891  // editTemplateNode records a change to a {{template}} callee for later commit.
   892  func (e *escaper) editTemplateNode(n *parse.TemplateNode, callee string) {
   893  	if _, ok := e.templateNodeEdits[n]; ok {
   894  		panic(fmt.Sprintf("node %s shared between templates", n))
   895  	}
   896  	e.templateNodeEdits[n] = callee
   897  }
   898  
   899  // editTextNode records a change to a text node for later commit.
   900  func (e *escaper) editTextNode(n *parse.TextNode, text []byte) {
   901  	if _, ok := e.textNodeEdits[n]; ok {
   902  		panic(fmt.Sprintf("node %s shared between templates", n))
   903  	}
   904  	e.textNodeEdits[n] = text
   905  }
   906  
   907  // commit applies changes to actions and template calls needed to contextually
   908  // autoescape content and adds any derived templates to the set.
   909  func (e *escaper) commit() {
   910  	for name := range e.output {
   911  		e.template(name).Funcs(funcMap)
   912  	}
   913  	// Any template from the name space associated with this escaper can be used
   914  	// to add derived templates to the underlying text/template name space.
   915  	tmpl := e.arbitraryTemplate()
   916  	for _, t := range e.derived {
   917  		if _, err := tmpl.text.AddParseTree(t.Name(), t.Tree); err != nil {
   918  			panic("error adding derived template")
   919  		}
   920  	}
   921  	for n, s := range e.actionNodeEdits {
   922  		ensurePipelineContains(n.Pipe, s)
   923  	}
   924  	for n, name := range e.templateNodeEdits {
   925  		n.Name = name
   926  	}
   927  	for n, s := range e.textNodeEdits {
   928  		n.Text = s
   929  	}
   930  	// Reset state that is specific to this commit so that the same changes are
   931  	// not re-applied to the template on subsequent calls to commit.
   932  	e.called = make(map[string]bool)
   933  	e.actionNodeEdits = make(map[*parse.ActionNode][]string)
   934  	e.templateNodeEdits = make(map[*parse.TemplateNode]string)
   935  	e.textNodeEdits = make(map[*parse.TextNode][]byte)
   936  }
   937  
   938  // template returns the named template given a mangled template name.
   939  func (e *escaper) template(name string) *template.Template {
   940  	// Any template from the name space associated with this escaper can be used
   941  	// to look up templates in the underlying text/template name space.
   942  	t := e.arbitraryTemplate().text.Lookup(name)
   943  	if t == nil {
   944  		t = e.derived[name]
   945  	}
   946  	return t
   947  }
   948  
   949  // arbitraryTemplate returns an arbitrary template from the name space
   950  // associated with e and panics if no templates are found.
   951  func (e *escaper) arbitraryTemplate() *Template {
   952  	for _, t := range e.ns.set {
   953  		return t
   954  	}
   955  	panic("no templates in name space")
   956  }
   957  
   958  // Forwarding functions so that clients need only import this package
   959  // to reach the general escaping functions of text/template.
   960  
   961  // HTMLEscape writes to w the escaped HTML equivalent of the plain text data b.
   962  func HTMLEscape(w io.Writer, b []byte) {
   963  	template.HTMLEscape(w, b)
   964  }
   965  
   966  // HTMLEscapeString returns the escaped HTML equivalent of the plain text data s.
   967  func HTMLEscapeString(s string) string {
   968  	return template.HTMLEscapeString(s)
   969  }
   970  
   971  // HTMLEscaper returns the escaped HTML equivalent of the textual
   972  // representation of its arguments.
   973  func HTMLEscaper(args ...any) string {
   974  	return template.HTMLEscaper(args...)
   975  }
   976  
   977  // JSEscape writes to w the escaped JavaScript equivalent of the plain text data b.
   978  func JSEscape(w io.Writer, b []byte) {
   979  	template.JSEscape(w, b)
   980  }
   981  
   982  // JSEscapeString returns the escaped JavaScript equivalent of the plain text data s.
   983  func JSEscapeString(s string) string {
   984  	return template.JSEscapeString(s)
   985  }
   986  
   987  // JSEscaper returns the escaped JavaScript equivalent of the textual
   988  // representation of its arguments.
   989  func JSEscaper(args ...any) string {
   990  	return template.JSEscaper(args...)
   991  }
   992  
   993  // URLQueryEscaper returns the escaped value of the textual representation of
   994  // its arguments in a form suitable for embedding in a URL query.
   995  func URLQueryEscaper(args ...any) string {
   996  	return template.URLQueryEscaper(args...)
   997  }
   998  

View as plain text