fix: G704 false positive on const URL (#1551)

This commit is contained in:
Ravi Sastry Kadali
2026-02-27 11:51:08 +01:00
committed by GitHub
parent 1341aeadb4
commit 271492bcd9
2 changed files with 42 additions and 0 deletions
+6
View File
@@ -521,6 +521,12 @@ func (a *Analyzer) isTainted(v ssa.Value, fn *ssa.Function, visited map[ssa.Valu
}
visited[v] = true
// Constants are compile-time literals and can never carry attacker-controlled
// data. Short-circuit immediately — no taint possible.
if _, ok := v.(*ssa.Const); ok {
return false
}
// Trace back through SSA instructions
switch val := v.(type) {
case *ssa.Parameter:
+36
View File
@@ -65,4 +65,40 @@ func GetPublicIP() (string, error) {
return "", nil
}
`}, 0, gosec.NewConfig()},
// Constant URL string must NOT trigger G704.
{[]string{`
package main
import (
"context"
"net/http"
)
const url = "https://go.dev/"
func main() {
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
panic(err)
}
_, err = new(http.Client).Do(req)
if err != nil {
panic(err)
}
}
`}, 0, gosec.NewConfig()},
// Sanity check: variable URL from request still fires.
{[]string{`
package main
import (
"net/http"
)
func handler(r *http.Request) {
target := r.URL.Query().Get("url")
http.Get(target) //nolint:errcheck
}
`}, 1, gosec.NewConfig()},
}