Repository navigation
Description
Description of the false positive
go/unvalidated-url-redirection reports a redirect whose target has gone through a sanitizer function, when that function returns the cleaned value rather than a boolean.
The sanitizer keeps only a site-relative path:
- the value must start with a single
/(//hostand/\hostare rejected) - it must contain no control characters
- after
url.Parse, it must have no scheme, opaque part, host or userinfo
Any other input is replaced by the constant "/". There is no path from user input to http.Redirect that can produce an off-site URL, but the query still reports one.
From the query library (OpenUrlRedirectCustomizations.qll and barrierguardutil/RedirectCheckBarrierGuard.qll), a barrier is recognised only as one of:
- a boolean guard call whose name matches
(?i)(is_?)?(local_?url|valid_?redir(ect)?)(ur[li])?, used as a branch condition - a regexp match check
- a comparison against a constant or
Hostname()
A function that returns either its validated argument or a safe constant isn't modelled, so taint flows straight through it. This is a common Go idiom for redirect targets (safeRedirect(s) string, sanitizeNext(s) string), so the false positive doesn't depend on what the function checks.
Code samples or links to source code
Flagged (CodeQL 2.27.4, default code scanning setup):
package main
import (
"net/http"
"net/url"
"unicode"
)
// safeReturnTo returns raw when it is a path on this site, and "/" otherwise.
func safeReturnTo(raw string) string {
if raw == "" || len(raw) > 2048 || raw[0] != '/' {
return "/"
}
if len(raw) > 1 && (raw[1] == '/' || raw[1] == '\\') {
return "/"
}
for _, r := range raw {
if unicode.IsControl(r) {
return "/"
}
}
u, err := url.Parse(raw)
if err != nil || u.Scheme != "" || u.Opaque != "" || u.Host != "" || u.User != nil {
return "/"
}
return raw
}
func handler(w http.ResponseWriter, r *http.Request) {
next := safeReturnTo(r.URL.Query().Get("return_to"))
http.Redirect(w, r, next, http.StatusFound) // reported: go/unvalidated-url-redirection
}
func main() {
http.HandleFunc("/login/done", handler)
_ = http.ListenAndServe("127.0.0.1:8080", nil)
}Not flagged. The checks are identical; only the shape changes to a boolean guard with a recognised name:
func isLocalURL(raw string) bool {
if raw == "" || len(raw) > 2048 || raw[0] != '/' {
return false
}
if len(raw) > 1 && (raw[1] == '/' || raw[1] == '\\') {
return false
}
for _, r := range raw {
if unicode.IsControl(r) {
return false
}
}
u, err := url.Parse(raw)
return err == nil && u.Scheme == "" && u.Opaque == "" && u.Host == "" && u.User == nil
}
func safeReturnTo(raw string) string {
if isLocalURL(raw) {
return raw
}
return "/"
}With the second version the alerts close on re-scan, which confirms that only the shape matters.
Suggestion
Any of these would remove this class of false positive:
- Treat a function as a sanitizer when every return path returns either a constant or its parameter under a recognised guard. Equivalently, propagate the guard through a function whose result is its argument only on the guarded branch.
- Document a way to mark a user-defined function as a
url-redirectionbarrier in Go, asbarrierNodemodels allow for library functions. Teams could then annotate their own sanitizer without renaming or restructuring it. - At least mention, in the query help, that a value-returning sanitizer isn't recognised and that the guard must be a boolean function with a name such as
isLocalUrl.
Environment
- CodeQL 2.27.4, GitHub code scanning default setup, language
go - Go 1.26,
net/http
URL to the alert on GitHub code scanning (optional)
PRIVATE REPO
Activity