build-web-application-with-golang icon indicating copy to clipboard operation
build-web-application-with-golang copied to clipboard

中文版 4.3 预防跨站脚本

Open unix2dos opened this issue 7 years ago • 2 comments

原文如下: Go的html/template包默认帮你过滤了html标签,但是有时候你只想要输出这个看起来正常的信息,该怎么处理?请使用text/template。请看下面的例子:

import "text/template"
...
t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
err = t.ExecuteTemplate(out, "T", "<script>alert('you have been pwned')</script>")

输出 Hello, !


我的实验结果是

Hello, &lt;script&gt;alert(&#39;you have been pwned&#39;)&lt;/script&gt;!

t.ExecuteTemplate(out, "T", template.HTML("")) 才能输出 Hello, <script>alert('you have been pwned')</script>!

unix2dos avatar May 10 '17 06:05 unix2dos

你用的是text/template吗?

astaxie avatar May 10 '17 16:05 astaxie

@unix2dos template.HTML 是 html/template 才有的方法,我觉得你应该是用的html/template而不是text/template

abocd avatar May 27 '17 03:05 abocd