Skip to main content
Version: 2.8.x(Latest)

By default, the template engine does not use HTML encoding for all variable outputs, which means that if not handled properly by developers, there might be XSS vulnerabilities.

No worries, the GoFrame framework has taken this into full consideration and provides developers with flexible configuration parameters to control whether to encode HTML content of variable outputs by default. This feature can be enabled/disabled via the AutoEncode configuration item or the SetAutoEncode method.

tip

It is important to note that this feature does not affect the built-in function of include templates.

Usage example:

  1. Configuration file
[viewer]
delimiters = ["${", "}"]
autoencode = true
  1. Sample code
package main

import (
"context"
"fmt"
"github.com/gogf/gf/v2/frame/g"
)

func main() {
result, _ := g.View().ParseContent(context.TODO(), "Name: ${.name}", g.Map{
"name": "<script>alert('john');</script>",
})
fmt.Println(result)
}
  1. Execution output
Name: &lt;script&gt;alert(&#39;john&#39;);&lt;/script&gt;