Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Make go env vars static #456

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions internal/goenv/goenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"strings"
)

func Read(varNames []string) (map[string]string, error) {
out, err := exec.Command("go", append([]string{"env"}, varNames...)...).CombinedOutput()
func Read() (map[string]string, error) {
// pass in a fixed set of var names to avoid needing to unescape output
// pass in literals here instead of a variable list to avoid security linter warnings about command injection
out, err := exec.Command("go", "env", "GOROOT", "GOPATH", "GOARCH", "GOOS", "CGO_ENABLED").CombinedOutput()
if err != nil {
return nil, err
}
return parseGoEnv(varNames, out)
return parseGoEnv([]string{"GOROOT", "GOPATH", "GOARCH", "GOOS", "CGO_ENABLED"}, out)
}

func parseGoEnv(varNames []string, data []byte) (map[string]string, error) {
Expand Down
2 changes: 1 addition & 1 deletion ruleguard/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func inferBuildContext() *build.Context {
// Inherit most fields from the build.Default.
ctx := build.Default

env, err := goenv.Read([]string{"GOROOT", "GOPATH", "GOARCH", "GOOS", "CGO_ENABLED"})
env, err := goenv.Read()
if err != nil {
return &ctx
}
Expand Down
Loading