Skip to content

Commit

Permalink
chore: address additional gosec warnings (#1605)
Browse files Browse the repository at this point in the history
  • Loading branch information
bassosimone authored May 14, 2024
1 parent 584dac8 commit 8a8e1c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/experiment/ndt7/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func newDialManager(ndt7URL string, logger model.Logger, userAgent string) dialM
}
}

func (mgr dialManager) dialWithTestName(ctx context.Context, testName string) (*websocket.Conn, error) {
func (mgr dialManager) dialWithTestName(ctx context.Context, _ string) (*websocket.Conn, error) {
netx := &netxlite.Netx{}
reso := netx.NewStdlibResolver(mgr.logger)
dlr := netx.NewDialerWithResolver(mgr.logger, reso)
Expand Down
36 changes: 19 additions & 17 deletions pkg/gobash/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Run(version string) {

func runGo(root string) {
gobin := filepath.Join(root, "bin", "go"+exe())
cmd := exec.Command(gobin, os.Args[1:]...)
cmd := exec.Command(gobin, os.Args[1:]...) // #nosec G204 - this is working as intended
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down Expand Up @@ -105,11 +105,11 @@ func install(targetDir, version string) error {
return nil
}

if err := os.MkdirAll(targetDir, 0755); err != nil {
if err := os.MkdirAll(targetDir, 0700); err != nil {
return err
}
goURL := versionArchiveURL(version)
res, err := http.Head(goURL)
res, err := http.Head(goURL) // #nosec G107 -- this is working as intended
if err != nil {
return err
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func install(targetDir, version string) error {
if err := unpackArchive(targetDir, archiveFile); err != nil {
return fmt.Errorf("extracting archive %v: %v", archiveFile, err)
}
if err := ioutil.WriteFile(filepath.Join(targetDir, unpackedOkay), nil, 0644); err != nil {
if err := ioutil.WriteFile(filepath.Join(targetDir, unpackedOkay), nil, 0600); err != nil {
return err
}
log.Printf("Success. You may now run '%v'", version)
Expand All @@ -170,7 +170,7 @@ func unpackArchive(targetDir, archiveFile string) error {

// unpackTarGz is the tar.gz implementation of unpackArchive.
func unpackTarGz(targetDir, archiveFile string) error {
r, err := os.Open(archiveFile)
r, err := os.Open(archiveFile) // #nosec G304 - this is working as intended
if err != nil {
return err
}
Expand Down Expand Up @@ -205,12 +205,13 @@ func unpackTarGz(targetDir, archiveFile string) error {
// write will fail with the same error.
dir := filepath.Dir(abs)
if !madeDir[dir] {
if err := os.MkdirAll(filepath.Dir(abs), 0755); err != nil {
if err := os.MkdirAll(filepath.Dir(abs), 0700); err != nil {
return err
}
madeDir[dir] = true
}
wf, err := os.OpenFile(abs, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode.Perm())
wf, err := os.OpenFile( // #nosec G304 - this is working as intended
abs, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode.Perm())
if err != nil {
return err
}
Expand All @@ -235,7 +236,7 @@ func unpackTarGz(targetDir, archiveFile string) error {
}
}
case mode.IsDir():
if err := os.MkdirAll(abs, 0755); err != nil {
if err := os.MkdirAll(abs, 0700); err != nil {
return err
}
madeDir[abs] = true
Expand Down Expand Up @@ -271,7 +272,7 @@ func unpackZip(targetDir, archiveFile string) error {

outpath := filepath.Join(targetDir, name)
if f.FileInfo().IsDir() {
if err := os.MkdirAll(outpath, 0755); err != nil {
if err := os.MkdirAll(outpath, 0700); err != nil {
return err
}
continue
Expand All @@ -286,14 +287,15 @@ func unpackZip(targetDir, archiveFile string) error {
if err := os.MkdirAll(filepath.Dir(outpath), 0755); err != nil {
return err
}
out, err := os.OpenFile(outpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
out, err := os.OpenFile( // #nosec G304 - this is working as intended
outpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
_, err = io.Copy(out, rc)
rc.Close()
_ = rc.Close()
if err != nil {
out.Close()
_ = out.Close()
return err
}
if err := out.Close(); err != nil {
Expand All @@ -306,7 +308,7 @@ func unpackZip(targetDir, archiveFile string) error {
// verifySHA256 reports whether the named file has contents with
// SHA-256 of the given wantHex value.
func verifySHA256(file, wantHex string) error {
f, err := os.Open(file)
f, err := os.Open(file) // #nosec G304 - this is working as intended
if err != nil {
return err
}
Expand All @@ -323,7 +325,7 @@ func verifySHA256(file, wantHex string) error {

// slurpURLToString downloads the given URL and returns it as a string.
func slurpURLToString(url_ string) (string, error) {
res, err := http.Get(url_)
res, err := http.Get(url_) // #nosec G107 -- this is working as intended
if err != nil {
return "", err
}
Expand All @@ -340,14 +342,14 @@ func slurpURLToString(url_ string) (string, error) {

// copyFromURL downloads srcURL to dstFile.
func copyFromURL(dstFile, srcURL string) (err error) {
f, err := os.Create(dstFile)
f, err := os.Create(dstFile) // #nosec G304 - this is working as intended
if err != nil {
return err
}
defer func() {
if err != nil {
f.Close()
os.Remove(dstFile)
_ = f.Close()
_ = os.Remove(dstFile)
}
}()
c := &http.Client{
Expand Down

0 comments on commit 8a8e1c4

Please sign in to comment.