Mitigation summary
The Go template engine inhtml/template does automatic and contextual autoescaping, which mitigates many common XSS mistakes. Some aspects of the engine are confusingly named; therefore, proper use of the library should be enforced using code scanners. You may also consider using a stricter alternative, such as safehtml.
Check your project using Semgrep
1. Server code: Unescaped content
1.A. Using the text/template package
text/template does not perform any HTML escaping.
Example:
References
Mitigation
Bantext/template. Alternatively, use html/template, or a stricter alternative such as safehtml.
Semgrep rule
go.lang.security.audit.xss.import-text-template.import-text-template
1.B. Escaped types: template.HTML
template.HTML is a special type which instructs the template engine not to escape the content.
Example:
References
Mitigation
Bantemplate.HTML. Alternatively, if necessary, review each case carefully and exempt with # nosemgrep.
Semgrep rule
go.lang.security.audit.xss.template-html-does-not-escape.unsafe-template-type
1.C. Escaped types: template.HTMLAttr
template.HTMLAttr is a special type which instructs the template engine not to escape the content.
Example:
References
Mitigation
Bantemplate.HTMLAttr. Alternatively, prefer template.HTML, only if necessary.
Semgrep rule
go.template.security.insecure-types.go-insecure-templates
1.D. Escaped types: template.CSS
template.CSS is a special type which instructs the template engine not to escape the content in CSS contexts.
Example:
References
Mitigation
Bantemplate.CSS. Alternatively, if necessary, review each case carefully and exempt with # nosemgrep.
Semgrep rule
go.template.security.insecure-types.go-insecure-templates
1.E. Escaped types: template.JS
Thetemplate.JS is a special type which instructs the template engine not to escape the content in JavaScript contexts, such as between script tags.
Example:
References
Mitigation
Bantemplate.JS. Alternatively, place JavaScript code in files separate from HTML and serve them using the src attribute.
Semgrep rule
go.template.security.insecure-types.go-insecure-templates
1.F. Escaped types: template.JSStr
Thetemplate.JSStr is a special type which instructs the template engine not to escape the content when in JavaScript contexts and in a string.
Example:
References
Mitigation
Bantemplate.JSStr. Alternatively, place JavaScript code in files separate from HTML and serve them using the src attribute.
Semgrep rule
go.template.security.insecure-types.go-insecure-templates
1.G. Escaped types: template.Srcset
template.Srcset is a special type which instructs the template engine not to escape the content.
Example:
References
Mitigation
Bantemplate.Srcset. Alternatively, prefer template.HTML, only if necessary.
Semgrep rule
go.template.security.insecure-types.go-insecure-templates
1.H. Escaped types: template.URL
Thetemplate.URL is a special type which instructs the template engine not to escape the content.
Example:
References
Mitigation
Bantemplate.URL. Alternatively, if necessary, review each case carefully and exempt with # nosem.
Semgrep rule
go.template.security.insecure-types.go-insecure-templates
2. Server code: Bypassing the template engine
2.A. Writing directly to the response object: fmt.Fprintf()
Writing directly to the response object bypasses the template engine which means content will not be autoescaped. This could introduce a XSS vulnerability. Example:References
Mitigation
Ban usingfmt.Printf with the HTTP response writer. Alternatively, use html/template to render data to users.
Semgrep rule
go.lang.security.audit.xss.no-fprintf-to-responsewriter.no-fprintf-to-responsewriter
2.C. Writing directly to the response object: io.WriteString()
Writing directly to the response object bypasses the template engine which means content will not be autoescaped. This could introduce a XSS vulnerability.
Example:
References
Mitigation
Ban usingio.WriteString with the HTTP response writer. Alternatively, use html/template to render data to users.
Semgrep rule
go.lang.security.audit.xss.no-io-writestring-to-responsewriter.no-io-writestring-to-responsewriter
2.C. Writing directly to the response object: w.Write() method
Writing directly to the response object bypasses the template engine which means content will not be autoescaped. This could introduce a XSS vulnerability. Example:References
Mitigation
Ban using theWrite method of the HTTP response writer. Alternatively, use html/template to render data to users.
Semgrep rule
go.lang.security.audit.xss.no-direct-write-to-responsewriter.no-direct-write-to-responsewriter