A handler redirects to a location taken from the request without a check

tx0/go:open-redirectrecommendedwarningsecurity, cwe-601

What it finds

http.Redirect(w, r, r.FormValue("next"), http.StatusFound): a target taken from the request lets anyone build a link to this site that sends its visitors on to a site of their choosing, which makes phishing pages look trustworthy. Targets are checked to be local paths, or chosen from a fixed list.

The target of http.Redirect must start with a value the client chooses freely: a query or form value, a header, the path value or the referrer of an *http.Request parameter, directly, at the start of a concatenation, or through a local variable declared with such a value that the function does not check in a condition, as described for the request values of the pack. A target that starts with a constant, such as "/search?" + r.URL.RawQuery, stays on the site and is left alone.

From the rule's source, open_redirect.rs.

Example from the rule's tests
package p import (	"net/http"	"strings") func login(w http.ResponseWriter, r *http.Request) {	next := r.URL.Query().Get("next")	if next == "" {		next = "/"	}	http.Redirect(w, r, next, http.StatusFound)tx0 reports: the redirect goes to a location taken from the request} func back(w http.ResponseWriter, r *http.Request) {	to := r.FormValue("to")	if !strings.HasPrefix(to, "/") {		to = "/"	}	http.Redirect(w, r, to, http.StatusFound)	http.Redirect(w, r, "/home", http.StatusFound)}
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:open-redirect" = "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:open-redirect] reason="..."

tx0 explain tx0/go:open-redirect shows the same in the terminal. Found a false positive? Report it.