diff --git a/django/README.md b/django/README.md index ce7799d..71ba37d 100644 --- a/django/README.md +++ b/django/README.md @@ -59,9 +59,9 @@ func main() { // Create a new engine engine := django.New("./views", ".django") - // Or from an embedded system - // See github.com/gofiber/embed for examples - // engine := html.NewFileSystem(http.Dir("./views", ".django")) + // Or from an embedded system + // See github.com/gofiber/embed for examples + // engine := html.NewFileSystem(http.Dir("./views", ".django")) // Pass the engine to the Views app := fiber.New(fiber.Config{ @@ -194,4 +194,44 @@ If you need to access a value in the template that doesn't adhere to the key nam c.Render("index", fiber.Map{ "Fiber": "Hello, World!\n\nGreetings from Fiber Team", "MyKey": c.Locals("my-key"), -}) \ No newline at end of file +}) + +### AutoEscape is enabled by default + +When you create a new instance of the `Engine`, the auto-escape is **enabled by default**. This setting automatically escapes output, providing a critical security measure against Cross-Site Scripting (XSS) attacks. + +### Disabling Auto-Escape + +Auto-escaping can be disabled if necessary, using the `SetAutoEscape` method: + +```go +engine := django.New("./views", ".django") +engine.SetAutoEscape(false) +``` + +### Setting AutoEscape using Django built-in template tags + +- Explicitly turning off autoescaping for a section: +```django + {% autoescape off %} + {{ "" }} + {% endautoescape %} +``` + +- Turning autoescaping back on for a section: +```django + {% autoescape on %} + {{ "" }} + {% endautoescape %} +``` +- It can also be done on a per variable basis using the *safe* built-in: +```django +

{{ someSafeVar | safe }}

+{{ "", + }, "layouts/main") + require.NoError(t, err) + + expect := `Main

Header

<script>alert('XSS')</script>

Footer

` + result := trim(buf.String()) + require.Equal(t, expect, result) +} + +func Test_XSS_WithAutoEscapeDisabled(t *testing.T) { + engine := New("./views", ".django") + engine.SetAutoEscape(false) + require.NoError(t, engine.Load()) + + var buf bytes.Buffer + err := engine.Render(&buf, "index", map[string]interface{}{ + "Title": "", + }, "layouts/main") + require.NoError(t, err) + + expect := `Main

Header

Footer

` + result := trim(buf.String()) + require.Equal(t, expect, result) +} + func Benchmark_Django(b *testing.B) { expectSimple := `

Hello, World!

` expectExtended := `Main

Header

Hello, Admin!

Footer

`