Cross-site scripting (XSS)

Cross-site scripting (XSS)

Description

Rendering user input in HTML without appropriate escaping may let attackers inject <script> tags or event handlers such as onclick. If a browser interprets the input as active page content, the attacker's JavaScript can execute in the victim's browser, enabling session abuse, phishing or keylogging.

Potential impact

  • Attackers may steal script-readable cookies or tokens, or make requests with the user's permissions.
  • Malicious JavaScript can alter the DOM, redirect the browser or record keystrokes.
  • Forged login or payment forms may trick users into disclosing sensitive data.
  • Malicious popups and redirects can damage trust and cause financial or legal consequences.

Remediation

Use a trusted html/template for HTML responses. Pass user input as ordinary strings so the template can escape each value for its HTML, URL, or JavaScript context. http.ResponseWriter.Write() sends the bytes you supply, including any HTML assembled from input.

Validate URL and name formats separately. Keep input out of the template source and avoid converting it to template.HTML, template.URL, or template.JS, which mark the value as trusted.

Examples

If links must point to specific external domains, check the domain separately. html/template handles the URL’s placement in HTML.

Before

go
package main

import (
    "fmt"
    "net/http"
)

// Before: write user input directly through ResponseWriter
func errorPage(w http.ResponseWriter, r *http.Request) {
    // A query parameter controlled by the user
    url := r.URL.Query().Get("url")

    // HTML containing user input
    const template = `
    <html>
    <body>
      <h1>error; page not found. <a href="%s">go back</a></h1>
    </body>
    </html>`

    // Format the string, then write it directly
    // The url may contain "javascript:alert(1)" or "\" onclick=\"alert(1)"
    w.WriteHeader(http.StatusBadRequest)
    w.Write([]byte(fmt.Sprintf(template, url)))
}

func main() {
    http.HandleFunc("/error", errorPage)
    http.ListenAndServe(":8080", nil)
}

After

go
package main

import (
    "html/template"
    "log"
    "net/http"
)

// After: automatic escaping with html/template

// The template receives data at {{.URL}},
// and html/template applies context-aware escaping
var errorTmpl = template.Must(template.New("error").Parse(`
<html>
<body>
  <h1>error; page not found. <a href="{{.URL}}">go back</a></h1>
</body>
</html>`))

func errorPageSafe(w http.ResponseWriter, r *http.Request) {
    url := r.URL.Query().Get("url")

    // Use a default when the URL is empty
    if url == "" {
        url = "/"
    }

    w.WriteHeader(http.StatusBadRequest)

    // Pass the data structure to the template
    data := struct {
        URL string
    }{URL: url}

    if err := errorTmpl.Execute(w, data); err != nil {
        log.Println("template execute error:", err)
    }
}

func main() {
    http.HandleFunc("/error", errorPageSafe)
    http.ListenAndServe(":8080", nil)
}

The template receives the URL as an ordinary string in href. It restricts unsafe schemes and escapes the value so it stays inside the attribute.

References