A value taken from an HTTP request is written into an HTML response without escaping

tx0/go:request-value-in-htmlrecommendedwarningsecurity/xssdepth fastsecurity, cwe-79

What it finds

fmt.Fprintf(w, "<p>Hello %s</p>", r.FormValue("name")): a value from the request is written into an HTML response without escaping, so markup or script in it runs in the page of whoever follows a crafted link. html/template escapes values as it inserts them; html.EscapeString does for a single value.

Reported is a request value, as described for the request values of the pack, that is written to a parameter declared as http.ResponseWriter by fmt.Fprintf with a constant format that contains <, or by fmt.Fprint, fmt.Fprintln, io.WriteString or the Write method of the writer with a constant part that contains <; and a request value given as the HTML of c.HTML or c.HTMLBlob of echo or c.Data of gin with an HTML content type. Functions that set a Content-Type header to a constant without html are left alone.

From the rule's source, request_value_in_html.rs.

Example from the rule's tests
package p import (	"fmt"	"html"	"net/http" 	"github.com/labstack/echo/v4") func hello(w http.ResponseWriter, r *http.Request) {	name := r.FormValue("name")	fmt.Fprintf(w, "<p>Hello %s</p>", name)tx0 reports: a value from the request is written into the page without escaping	fmt.Fprintf(w, "<p>Hello %s</p>", html.EscapeString(name))	w.Write([]byte("<b>" + r.URL.Query().Get("q") + "</b>"))tx0 reports: a value from the request is written into the page without escaping} func plain(w http.ResponseWriter, r *http.Request) {	w.Header().Set("Content-Type", "text/plain")	fmt.Fprintf(w, "<%s>", r.FormValue("tag"))} func page(c echo.Context) error {	return c.HTML(http.StatusOK, "<h1>"+c.QueryParam("title")+"</h1>")tx0 reports: a value from the request is written into the page without escaping}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:request-value-in-html" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:request-value-in-html] reason="..."

tx0 explain tx0/go:request-value-in-html shows the same in the terminal. Found a false positive? Report it.