Skip to content

Commit d520226

Browse files
committed
html/template: mention risks of the CSS, HTML, JS, etc. types
Fixes #15399 Change-Id: I5b9645cb9ddede6981ce0a005e0c6fdd8a751c6f Reviewed-on: https://go-review.googlesource.com/22824 Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Minux Ma <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 8f13080 commit d520226

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/html/template/content.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,28 @@ type (
1818
// 4. The CSS3 value production, such as `rgba(0, 0, 255, 127)`.
1919
// See http://www.w3.org/TR/css3-syntax/#parsing and
2020
// https://web.archive.org/web/20090211114933/http://w3.org/TR/css3-syntax#style
21+
//
22+
// Use of this type presents a security risk:
23+
// the encapsulated content should come from a trusted source,
24+
// as it will be included verbatim in the template output.
2125
CSS string
2226

2327
// HTML encapsulates a known safe HTML document fragment.
2428
// It should not be used for HTML from a third-party, or HTML with
2529
// unclosed tags or comments. The outputs of a sound HTML sanitizer
2630
// and a template escaped by this package are fine for use with HTML.
31+
//
32+
// Use of this type presents a security risk:
33+
// the encapsulated content should come from a trusted source,
34+
// as it will be included verbatim in the template output.
2735
HTML string
2836

2937
// HTMLAttr encapsulates an HTML attribute from a trusted source,
3038
// for example, ` dir="ltr"`.
39+
//
40+
// Use of this type presents a security risk:
41+
// the encapsulated content should come from a trusted source,
42+
// as it will be included verbatim in the template output.
3143
HTMLAttr string
3244

3345
// JS encapsulates a known safe EcmaScript5 Expression, for example,
@@ -37,6 +49,15 @@ type (
3749
// statement/expression ambiguity as when passing an expression like
3850
// "{ foo: bar() }\n['foo']()", which is both a valid Expression and a
3951
// valid Program with a very different meaning.
52+
//
53+
// Use of this type presents a security risk:
54+
// the encapsulated content should come from a trusted source,
55+
// as it will be included verbatim in the template output.
56+
//
57+
// Using JS to include valid but untrusted JSON is not safe.
58+
// A safe alternative is to parse the JSON with json.Unmarshal and then
59+
// pass the resultant object into the template, where it will be
60+
// converted to sanitized JSON when presented in a JavaScript context.
4061
JS string
4162

4263
// JSStr encapsulates a sequence of characters meant to be embedded
@@ -46,13 +67,21 @@ type (
4667
// | EscapeSequence
4768
// Note that LineContinuations are not allowed.
4869
// JSStr("foo\\nbar") is fine, but JSStr("foo\\\nbar") is not.
70+
//
71+
// Use of this type presents a security risk:
72+
// the encapsulated content should come from a trusted source,
73+
// as it will be included verbatim in the template output.
4974
JSStr string
5075

5176
// URL encapsulates a known safe URL or URL substring (see RFC 3986).
5277
// A URL like `javascript:checkThatFormNotEditedBeforeLeavingPage()`
5378
// from a trusted source should go in the page, but by default dynamic
5479
// `javascript:` URLs are filtered out since they are a frequently
5580
// exploited injection vector.
81+
//
82+
// Use of this type presents a security risk:
83+
// the encapsulated content should come from a trusted source,
84+
// as it will be included verbatim in the template output.
5685
URL string
5786
)
5887

0 commit comments

Comments
 (0)