mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
fix(cache): scan npm and PyPI packages named like Go metadata
IsMetadataEntry treated any name ending in .mod or .info as metadata. An npm or PyPI package with such a name skipped the vulnerability scan and was never blocked. The suffix rule now applies to the go registry only.
This commit is contained in:
Vendored
+3
-5
@@ -8,7 +8,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -325,7 +324,7 @@ func (m *Manager) getOrFetch(ctx context.Context, registry, name, version string
|
||||
// entries (index pages, lists, .mod/.info) are not scannable
|
||||
// packages, so skip them to avoid a bogus "clean" verdict and
|
||||
// the extra database round-trip.
|
||||
isMetadataEntry := metadata.IsMetadataEntry(name, version)
|
||||
isMetadataEntry := metadata.IsMetadataEntry(registry, name, version)
|
||||
if m.scanner != nil && !isMetadataEntry {
|
||||
blocked, reason, vulnErr := m.scanner.CheckVulnerabilities(ctx, registry, name, version)
|
||||
if vulnErr != nil {
|
||||
@@ -393,8 +392,7 @@ func (m *Manager) getOrFetch(ctx context.Context, registry, name, version string
|
||||
|
||||
// Skip security scan wait for metadata entries (index pages, lists, etc.)
|
||||
// Also skip Go module metadata files (.mod, .info)
|
||||
isMetadataEntry := version == "list" || version == "page" || version == "latest" || version == "metadata" ||
|
||||
strings.HasSuffix(name, ".mod") || strings.HasSuffix(name, ".info")
|
||||
isMetadataEntry := metadata.IsMetadataEntry(registry, name, version)
|
||||
|
||||
// Wait briefly for initial scan to complete if scanner is enabled
|
||||
// This prevents serving vulnerable packages on first request.
|
||||
@@ -581,7 +579,7 @@ func (m *Manager) store(ctx context.Context, registry, name, version, releaseVer
|
||||
|
||||
// Create metadata entry
|
||||
now := m.now()
|
||||
isMetadataEntry := metadata.IsMetadataEntry(name, version)
|
||||
isMetadataEntry := metadata.IsMetadataEntry(registry, name, version)
|
||||
|
||||
// Artifacts are immutable, so only idle eviction removes them; metadata
|
||||
// entries must refresh and keep a freshness expiry.
|
||||
|
||||
Vendored
+1
-1
@@ -197,7 +197,7 @@ func (m *memMetadata) ClearArtifactExpiry(context.Context) (int64, error) {
|
||||
defer m.mu.Unlock()
|
||||
var n int64
|
||||
for _, p := range m.pkgs {
|
||||
if p.ExpiresAt != nil && !metadata.IsMetadataEntry(p.Name, p.Version) {
|
||||
if p.ExpiresAt != nil && !metadata.IsMetadataEntry(p.Registry, p.Name, p.Version) {
|
||||
p.ExpiresAt = nil
|
||||
n++
|
||||
}
|
||||
|
||||
Vendored
+70
@@ -0,0 +1,70 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// policyBlocker blocks every package it is asked about.
|
||||
type policyBlocker struct{ checks atomic.Int32 }
|
||||
|
||||
func (s *policyBlocker) ScanPackage(context.Context, string, string, string, string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *policyBlocker) CheckVulnerabilities(context.Context, string, string, string) (bool, string, error) {
|
||||
s.checks.Add(1)
|
||||
return true, "blocked by policy", nil
|
||||
}
|
||||
|
||||
// Only Go's .mod and .info files are exempt from scanning; a package of any
|
||||
// other registry whose name ends in .mod or .info is a real package.
|
||||
func TestCacheHitScansPackagesNamedLikeGoMetadata(t *testing.T) {
|
||||
tests := []struct {
|
||||
registry, name, version string
|
||||
wantBlocked bool
|
||||
}{
|
||||
{"npm", "left.info", "1.0.0", true},
|
||||
{"npm", "left.mod", "1.0.0", true},
|
||||
{"pypi", "foo.info", "foo.info-1.0.tar.gz", true},
|
||||
{"go", "github.com/a/b/@v/v1.0.0.zip", "v1.0.0", true},
|
||||
{"go", "github.com/a/b/@v/v1.0.0.mod", "v1.0.0", false},
|
||||
{"go", "github.com/a/b/@v/v1.0.0.info", "v1.0.0", false},
|
||||
{"npm", "left-pad", "metadata", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.registry+"/"+tt.name, func(t *testing.T) {
|
||||
clock := newFakeClock()
|
||||
store := newMemStorage()
|
||||
meta := newMemMetadata(clock)
|
||||
key := tt.registry + "/" + tt.name + "/" + tt.version
|
||||
require.NoError(t, store.Put(context.Background(), key, strings.NewReader("data"), nil))
|
||||
require.NoError(t, meta.SavePackage(context.Background(), &metadata.Package{
|
||||
Registry: tt.registry, Name: tt.name, Version: tt.version, StorageKey: key, Size: 4, LastAccessed: clock.Now(),
|
||||
}))
|
||||
|
||||
scanner := &policyBlocker{}
|
||||
mgr, err := New(store, meta, scanner, nil, Config{Now: clock.Now, CleanupInterval: 24 * time.Hour})
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = mgr.Close() })
|
||||
|
||||
entry, err := mgr.Get(context.Background(), tt.registry, tt.name, tt.version, nil)
|
||||
if tt.wantBlocked {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
_, _ = io.Copy(io.Discard, entry.Data)
|
||||
_ = entry.Data.Close()
|
||||
require.Zero(t, scanner.checks.Load(), "metadata entries must not be scanned")
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user