Skip to content

Commit 271492b

Browse files
authored
fix: G704 false positive on const URL (#1551)
1 parent 1341aea commit 271492b

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

taint/taint.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,12 @@ func (a *Analyzer) isTainted(v ssa.Value, fn *ssa.Function, visited map[ssa.Valu
521521
}
522522
visited[v] = true
523523

524+
// Constants are compile-time literals and can never carry attacker-controlled
525+
// data. Short-circuit immediately — no taint possible.
526+
if _, ok := v.(*ssa.Const); ok {
527+
return false
528+
}
529+
524530
// Trace back through SSA instructions
525531
switch val := v.(type) {
526532
case *ssa.Parameter:

testutils/g704_samples.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,40 @@ func GetPublicIP() (string, error) {
6565
return "", nil
6666
}
6767
`}, 0, gosec.NewConfig()},
68+
// Constant URL string must NOT trigger G704.
69+
{[]string{`
70+
package main
71+
72+
import (
73+
"context"
74+
"net/http"
75+
)
76+
77+
const url = "https://go.dev/"
78+
79+
func main() {
80+
ctx := context.Background()
81+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
82+
if err != nil {
83+
panic(err)
84+
}
85+
_, err = new(http.Client).Do(req)
86+
if err != nil {
87+
panic(err)
88+
}
89+
}
90+
`}, 0, gosec.NewConfig()},
91+
// Sanity check: variable URL from request still fires.
92+
{[]string{`
93+
package main
94+
95+
import (
96+
"net/http"
97+
)
98+
99+
func handler(r *http.Request) {
100+
target := r.URL.Query().Get("url")
101+
http.Get(target) //nolint:errcheck
102+
}
103+
`}, 1, gosec.NewConfig()},
68104
}

0 commit comments

Comments
 (0)