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:
@@ -246,8 +246,8 @@ storage:
|
||||
endpoint: s3.amazonaws.com
|
||||
region: us-east-1
|
||||
bucket: my-cache
|
||||
access_key_id: AKIAIOSFODNN7EXAMPLE
|
||||
secret_access_key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
|
||||
access_key_id: test-access-key-id
|
||||
secret_access_key: test-secret-access-key
|
||||
metadata:
|
||||
backend: postgresql
|
||||
postgresql:
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -102,7 +102,7 @@ func (fs *FilesystemStorage) Put(ctx context.Context, key string, data io.Reader
|
||||
metrics.RecordStorageOperation("filesystem", "put", "error")
|
||||
return errors.Wrap(err, errors.ErrCodeStorageFailure, "failed to open storage root")
|
||||
}
|
||||
defer root.Close() // #nosec G307 -- Best-effort cleanup of opened storage root
|
||||
defer func() { _ = root.Close() }()
|
||||
|
||||
// Create directory
|
||||
if dir != "." {
|
||||
|
||||
@@ -140,7 +140,7 @@ func (s *Storage) Put(ctx context.Context, key string, data io.Reader, opts *sto
|
||||
s.logger.Warn().Err(err).Str("path", s.path).Msg("nfs: failed to open configured base; skipping fsync")
|
||||
return nil
|
||||
}
|
||||
defer root.Close() // #nosec G307 -- Best-effort cleanup of opened NFS root
|
||||
defer func() { _ = root.Close() }()
|
||||
f, err := root.OpenFile(relPath, os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
s.logger.Warn().Err(err).Str("key", key).Msg("nfs: post-put open failed; skipping fsync")
|
||||
|
||||
@@ -27,8 +27,8 @@ func (s *S3StorageTestSuite) TestNewS3Storage() {
|
||||
Region: "us-east-1",
|
||||
Bucket: "test-bucket",
|
||||
Prefix: "packages/",
|
||||
AccessKeyID: "AKIAIOSFODNN7EXAMPLE",
|
||||
SecretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
||||
AccessKeyID: "test-access-key-id",
|
||||
SecretAccessKey: "test-secret-access-key",
|
||||
MaxSizeBytes: 1024 * 1024,
|
||||
},
|
||||
expectError: false,
|
||||
|
||||
Reference in New Issue
Block a user