doc.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 (html/template) implements data-driven templates for
  6. generating HTML output safe against code injection. It provides the
  7. same interface as package text/template and should be used instead of
  8. text/template whenever the output is HTML.
  9. The documentation here focuses on the security features of the package.
  10. For information about how to program the templates themselves, see the
  11. documentation for text/template.
  12. Introduction
  13. This package wraps package text/template so you can share its template API
  14. to parse and execute HTML templates safely.
  15. tmpl, err := template.New("name").Parse(...)
  16. // Error checking elided
  17. err = tmpl.Execute(out, data)
  18. If successful, tmpl will now be injection-safe. Otherwise, err is an error
  19. defined in the docs for ErrorCode.
  20. HTML templates treat data values as plain text which should be encoded so they
  21. can be safely embedded in an HTML document. The escaping is contextual, so
  22. actions can appear within JavaScript, CSS, and URI contexts.
  23. The security model used by this package assumes that template authors are
  24. trusted, while Execute's data parameter is not. More details are
  25. provided below.
  26. Example
  27. import "text/template"
  28. ...
  29. t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
  30. err = t.ExecuteTemplate(out, "T", "<script>alert('you have been pwned')</script>")
  31. produces
  32. Hello, <script>alert('you have been pwned')</script>!
  33. but the contextual autoescaping in html/template
  34. import "html/template"
  35. ...
  36. t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
  37. err = t.ExecuteTemplate(out, "T", "<script>alert('you have been pwned')</script>")
  38. produces safe, escaped HTML output
  39. Hello, &lt;script&gt;alert(&#39;you have been pwned&#39;)&lt;/script&gt;!
  40. Contexts
  41. This package understands HTML, CSS, JavaScript, and URIs. It adds sanitizing
  42. functions to each simple action pipeline, so given the excerpt
  43. <a href="/search?q={{.}}">{{.}}</a>
  44. At parse time each {{.}} is overwritten to add escaping functions as necessary.
  45. In this case it becomes
  46. <a href="/search?q={{. | urlescaper | attrescaper}}">{{. | htmlescaper}}</a>
  47. where urlescaper, attrescaper, and htmlescaper are aliases for internal escaping
  48. functions.
  49. Errors
  50. See the documentation of ErrorCode for details.
  51. A fuller picture
  52. The rest of this package comment may be skipped on first reading; it includes
  53. details necessary to understand escaping contexts and error messages. Most users
  54. will not need to understand these details.
  55. Contexts
  56. Assuming {{.}} is `O'Reilly: How are <i>you</i>?`, the table below shows
  57. how {{.}} appears when used in the context to the left.
  58. Context {{.}} After
  59. {{.}} O'Reilly: How are &lt;i&gt;you&lt;/i&gt;?
  60. <a title='{{.}}'> O&#39;Reilly: How are you?
  61. <a href="/{{.}}"> O&#39;Reilly: How are %3ci%3eyou%3c/i%3e?
  62. <a href="?q={{.}}"> O&#39;Reilly%3a%20How%20are%3ci%3e...%3f
  63. <a onx='f("{{.}}")'> O\x27Reilly: How are \x3ci\x3eyou...?
  64. <a onx='f({{.}})'> "O\x27Reilly: How are \x3ci\x3eyou...?"
  65. <a onx='pattern = /{{.}}/;'> O\x27Reilly: How are \x3ci\x3eyou...\x3f
  66. If used in an unsafe context, then the value might be filtered out:
  67. Context {{.}} After
  68. <a href="{{.}}"> #ZgotmplZ
  69. since "O'Reilly:" is not an allowed protocol like "http:".
  70. If {{.}} is the innocuous word, `left`, then it can appear more widely,
  71. Context {{.}} After
  72. {{.}} left
  73. <a title='{{.}}'> left
  74. <a href='{{.}}'> left
  75. <a href='/{{.}}'> left
  76. <a href='?dir={{.}}'> left
  77. <a style="border-{{.}}: 4px"> left
  78. <a style="align: {{.}}"> left
  79. <a style="background: '{{.}}'> left
  80. <a style="background: url('{{.}}')> left
  81. <style>p.{{.}} {color:red}</style> left
  82. Non-string values can be used in JavaScript contexts.
  83. If {{.}} is
  84. struct{A,B string}{ "foo", "bar" }
  85. in the escaped template
  86. <script>var pair = {{.}};</script>
  87. then the template output is
  88. <script>var pair = {"A": "foo", "B": "bar"};</script>
  89. See package json to understand how non-string content is marshaled for
  90. embedding in JavaScript contexts.
  91. Typed Strings
  92. By default, this package assumes that all pipelines produce a plain text string.
  93. It adds escaping pipeline stages necessary to correctly and safely embed that
  94. plain text string in the appropriate context.
  95. When a data value is not plain text, you can make sure it is not over-escaped
  96. by marking it with its type.
  97. Types HTML, JS, URL, and others from content.go can carry safe content that is
  98. exempted from escaping.
  99. The template
  100. Hello, {{.}}!
  101. can be invoked with
  102. tmpl.Execute(out, template.HTML(`<b>World</b>`))
  103. to produce
  104. Hello, <b>World</b>!
  105. instead of the
  106. Hello, &lt;b&gt;World&lt;b&gt;!
  107. that would have been produced if {{.}} was a regular string.
  108. Security Model
  109. https://rawgit.com/mikesamuel/sanitized-jquery-templates/trunk/safetemplate.html#problem_definition defines "safe" as used by this package.
  110. This package assumes that template authors are trusted, that Execute's data
  111. parameter is not, and seeks to preserve the properties below in the face
  112. of untrusted data:
  113. Structure Preservation Property:
  114. "... when a template author writes an HTML tag in a safe templating language,
  115. the browser will interpret the corresponding portion of the output as a tag
  116. regardless of the values of untrusted data, and similarly for other structures
  117. such as attribute boundaries and JS and CSS string boundaries."
  118. Code Effect Property:
  119. "... only code specified by the template author should run as a result of
  120. injecting the template output into a page and all code specified by the
  121. template author should run as a result of the same."
  122. Least Surprise Property:
  123. "A developer (or code reviewer) familiar with HTML, CSS, and JavaScript, who
  124. knows that contextual autoescaping happens should be able to look at a {{.}}
  125. and correctly infer what sanitization happens."
  126. */
  127. package template