The text of a template is taken from an HTTP request

tx0/go:request-value-parsed-as-templaterecommendedwarningsecurity/injectiondepth fastsecurity, cwe-1336

What it finds

template.New("page").Parse(r.FormValue("layout")): the text of a template comes from the request, so the client writes the template actions itself. With text/template it can print any field or call any method of the data passed to Execute; with either package it can call the functions of the template's FuncMap. Templates come from the program; values from the request belong in the data.

Reported is the argument of a method Parse in a file that imports text/template or html/template, when it holds a request value as described for the request values of the pack. The receiver is a value, such as template.New("x") or a variable, not a package.

From the rule's source, request_value_parsed_as_template.rs.

Example from the rule's tests
package p import (	"net/http"	"text/template") func render(w http.ResponseWriter, r *http.Request) {	t, _ := template.New("page").Parse(r.FormValue("layout"))tx0 reports: the template is parsed from text taken from the request	t.Execute(w, nil)	u := template.Must(template.New("fixed").Parse("Hello {{.}}"))	u.Execute(w, r.FormValue("name"))}
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-parsed-as-template" = "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-parsed-as-template] reason="..."

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