改行を含むテキスト ファイルを読み込んで、html/templates
.
\n
読み込まれた文字列にwithを代入すると、改行が発生する代わりに、<br>
テンプレートによって html にエスケープされ、ブラウザーに表示されます。<br>
text/templates
(XSS 保護がない)に切り替えることなく、この動作を変更するにはどうすればよいですか?
最初にテキストに対して template.HTMLEscape() を実行してサニタイズし、信頼できる置換に \n を実行してから
、それを事前にエスケープされた信頼できるテンプレート データとして使用できるようです。
更新:Kockaの例を拡張すると、これは私が念頭に置いていたことです:
package main
import (
"html/template"
"os"
"strings"
)
const page = `<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>{{.}}</p>
</body>
</html>`
const text = `first line
<script>dangerous</script>
last line`
func main() {
t := template.Must(template.New("page").Parse(page))
safe := template.HTMLEscapeString(text)
safe = strings.Replace(safe, "\n", "<br>", -1)
t.Execute(os.Stdout, template.HTML(safe)) // template.HTML encapsulates a known safe HTML document fragment.
}
http://play.golang.org/p/JiH0uD5Zh2
出力は
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>first line<br><script>dangerous</script><br>last line</p>
</body>
</html>
そして、ブラウザでレンダリングされるテキストは
first line
<script>dangerous</script>
last line
\n
どこに置き換えているのかわかりませんが、それが進行中の場合は、エスケープされないように<br>
文字列をキャストできます。template.HTML
参照: http://golang.org/pkg/html/template/#HTML
テンプレートにある場合は、利用可能なパイプラインがあるはずです。{{. | html}}
あなたはこのようにそれを行うことができます:
package main
import (
"html/template"
"os"
)
const page = `<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>{{.}}</p>
</body>
</html>`
func main() {
t := template.Must(template.New("page").Parse(page))
t.Execute(os.Stdout, template.HTML("<br>"))
}
テンプレート全体を安全でないテンプレートとして渡す必要はありません (これは悪い習慣です)。
テンプレートにマップを渡す必要があり、使用したい要素のみを明示的に「安全でない」ものにします。
package main
import "bytes"
import "fmt"
import "html/template"
import "strings"
var input = `
{{ define "LAYOUT" }}
<html>
<body>
{{ template "CONTENT" . }}
</body>
</html>
{{ end }}
{{ define "CONTENT" }}
Unsafe content: {{ .Unsafe }}
Newlines converted to <br/> follow:
{{ .Normal }}
{{ end }}
{{ template "LAYOUT" . }}
`
var other = `
Hello
World
Again
`
var other2 = `
<script>alert("Owned!");</script>
`
func main() {
var t, err = template.New("sample").Parse(input)
if err != nil {
panic(err)
}
var fixed = strings.Replace(other, "\n", "\n<br/>", -1)
var model = map[string]interface{}{
"Normal": template.HTML(fixed),
"Unsafe": other2,
}
var out bytes.Buffer
t.Execute(&out, model) # <--- !! Notice the model is NOT an HTML type.
var raw = out.String()
fmt.Printf("%s", raw)
}
収量:
Unsafe content: <script>alert("Owned!");</script> Newlines converted to <br/> follow: <br/> Hello <br/> World <br/> Again <br/> </body> </html>