Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

G204: False positive when variable is a value from a hard-coded locally-scoped map #1199

Open
mholt opened this issue Aug 29, 2024 · 0 comments

Comments

@mholt
Copy link

mholt commented Aug 29, 2024

Summary

The linter reports a variable executing a shell command when the variable is hard-coded and not changed.

Steps to reproduce the behavior

// first element of each slice is the hard-coded command
osCommand := map[string][]string{
	"darwin":  {"open"},
	"freebsd": {"xdg-open"},
	"linux":   {"xdg-open"},
	"netbsd":  {"xdg-open"},
	"openbsd": {"xdg-open"},
	"windows": {"cmd", "/c", "start"},
}

// (probably irrelevant detail, but keeping it here just in case)
if runtime.GOOS == "windows" {
	// escape characters not allowed by cmd
	url = strings.ReplaceAll(url, "&", `^&`)
}

// read from the map -- no mutations
all := osCommand[runtime.GOOS]

// extract the command from the args -- again, no mutations
exe := all[0]
args := all[1:]

// false positive here
cmd := exec.Command(exe, append(args, url)...)

// it also fires if it's just exec.Command(exe)

gosec version

v2.20

Go version (output of 'go version')

go version go1.22.6 linux/amd64

Operating system / Environment

See above

Expected behavior

The command does not rely on external input in any way, so there should be no lint warning.

Actual behavior

G204 fires.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants