Cross-site scripting (XSS)

Cross-site scripting (XSS)

Description

Go's text/template does not automatically escape HTML. Rendering user input or external data such as titles, comments or profiles directly into an HTML document can therefore execute embedded scripts such as <script>...</script> in another user's browser. Attackers may use stored data or request parameters for session abuse, page manipulation or phishing.

Potential impact

  • Attackers may steal script-readable cookies or tokens, or send requests with the user's permissions.
  • JavaScript may record keystrokes, call APIs or access browser data.
  • Modified pages may show fake login or payment forms to collect sensitive information.
  • DOM changes can falsify displayed data or trigger additional requests.

Remediation

Use html/template for HTML and text/template for plain text. Keep the template source fixed and trusted. Pass external values as ordinary strings so html/template can escape them for their output context. Converting input to template.HTML or template.JS bypasses that handling.

Validate the expected input format and apply a Content Security Policy (CSP) to limit script execution. Keep context-aware output escaping in place.

Examples

Before

go
package main

import (
    "html"
    "log"
    "net/http"
    "text/template" // No automatic HTML escaping
)

// Simple post data structure
type Post struct {
    Title   string
    Content string // User-supplied content
}

// text/template does not escape HTML
var tmpl = template.Must(template.New("post").Parse(`
<!DOCTYPE html>
<html>
  <head><title>{{.Title}}</title></head>
  <body>
    <h1>{{.Title}}</h1>
    <div>
      내용: {{.Content}}
    </div>
  </body>
</html>
`))

func handler(w http.ResponseWriter, r *http.Request) {
    // Example: receive a title and content from query parameters
    title := r.URL.Query().Get("title")
    content := r.URL.Query().Get("content")

    p := Post{Title: title, Content: content}

    if err := tmpl.Execute(w, p); err != nil {
        log.Println(err)
        http.Error(w, html.EscapeString("template error"), http.StatusInternalServerError)
        return
    }
}

func main() {
    http.HandleFunc("/post", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

After

go
package main

import (
    "html"
    "html/template" // Automatic context-aware HTML escaping
    "log"
    "net/http"
)

// Simple post data structure
type Post struct {
    Title   string
    Content string // User-supplied content
}

// html/template applies automatic escaping
var tmpl = template.Must(template.New("post").Parse(`
<!DOCTYPE html>
<html>
  <head><title>{{.Title}}</title></head>
  <body>
    <h1>{{.Title}}</h1>
    <div>
      내용: {{.Content}}
    </div>
  </body>
</html>
`))

func handler(w http.ResponseWriter, r *http.Request) {
    title := r.URL.Query().Get("title")
    content := r.URL.Query().Get("content")

    p := Post{Title: title, Content: content}

    if err := tmpl.Execute(w, p); err != nil {
        log.Println(err)
        http.Error(w, html.EscapeString("template error"), http.StatusInternalServerError)
        return
    }
}

func main() {
    http.HandleFunc("/post", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

{{.Content}} appears in a text node. With html/template, markup supplied in Content is displayed as text.

References