Static analysis (SAST) check · Go

XSS from request input (Go)

A value read from the HTTP request is written into the response without escaping.

Why it matters

If the response is served as HTML, markup in the value runs as script in the user's browser.

How to fix it

Render the value through html/template, which escapes for the context it lands in, or escape it with html.EscapeString. Do not convert request data to template.HTML.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (Go)
package fixture

import (
	"fmt"
	"net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
	name := r.FormValue("name")
	fmt.Fprintf(w, "<h1>Hello %s</h1>", name)
}
Fixed: not reported (Go)
package fixture

import (
	"fmt"
	"html"
	"net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
	name := html.EscapeString(r.FormValue("name"))
	fmt.Fprintf(w, "<h1>Hello %s</h1>", name)
}

What TrustIvan reads

Runs on repository scans of Go source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.