mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-25 06:35:21 +00:00
style: fix remaining golangci-lint findings
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package pipaudit
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCopyInto(t *testing.T) {
|
||||
s := &Scanner{}
|
||||
src := filepath.Join(t.TempDir(), "pkg.whl")
|
||||
require.NoError(t, os.WriteFile(src, []byte("wheel"), 0600))
|
||||
|
||||
t.Run("copies inside dir", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
require.NoError(t, s.copyInto(src, dir, "pkg.whl"))
|
||||
got, err := os.ReadFile(filepath.Join(dir, "pkg.whl"))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "wheel", string(got))
|
||||
})
|
||||
|
||||
t.Run("refuses name escaping dir", func(t *testing.T) {
|
||||
parent := t.TempDir()
|
||||
dir := filepath.Join(parent, "sub")
|
||||
require.NoError(t, os.Mkdir(dir, 0750))
|
||||
require.Error(t, s.copyInto(src, dir, "../escape.whl"))
|
||||
require.NoFileExists(t, filepath.Join(parent, "escape.whl"))
|
||||
})
|
||||
|
||||
t.Run("missing source", func(t *testing.T) {
|
||||
require.Error(t, s.copyInto(filepath.Join(t.TempDir(), "none"), t.TempDir(), "x"))
|
||||
})
|
||||
}
|
||||
@@ -73,7 +73,7 @@ func (s *Scanner) Scan(ctx context.Context, registry, packageName, version strin
|
||||
|
||||
// Copy the wheel/tar.gz file to temp directory
|
||||
tmpFile := filepath.Join(tmpDir, filepath.Base(filePath))
|
||||
if err := s.copyFile(filePath, tmpFile); err != nil {
|
||||
if err := s.copyInto(filePath, tmpDir, filepath.Base(tmpFile)); err != nil {
|
||||
return nil, fmt.Errorf("failed to copy file: %w", err)
|
||||
}
|
||||
|
||||
@@ -125,13 +125,18 @@ func (s *Scanner) Health(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// copyFile copies a file from src to dst
|
||||
func (s *Scanner) copyFile(src, dst string) error {
|
||||
// copyInto copies src to name inside dir; name cannot escape dir.
|
||||
func (s *Scanner) copyInto(src, dir, name string) error {
|
||||
input, err := os.ReadFile(src) // #nosec G304 -- Source path is from scanner, controlled
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(dst, input, 0600)
|
||||
root, err := os.OpenRoot(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = root.Close() }()
|
||||
return root.WriteFile(name, input, 0600)
|
||||
}
|
||||
|
||||
// buildAuditCmd constructs the right pip-audit command for the input artifact.
|
||||
|
||||
Reference in New Issue
Block a user