mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-10-18 01:17:46 +00:00
fix: remediate severity-ordered review findings across cache, scanners, and proxies
- H2: gormstore soft-delete + storage_key UNIQUE collision; re-save after eviction succeeds
- H1: cache getOrFetch cache-hit branch no longer runs CheckVulnerabilities on metadata entries
- M2: cache store() streams upstream body to temp file + SHA256 instead of double-buffering
- H3/L3: FetchResult/FetchFunc plumbing (+RequiresAuth/AuthProvider); proxy closures adapted;
goproxy strips git+ prefix from UpstreamURL
- H4: scanner normalizes bypass package names; CheckVulnerabilities builds normalized packageKey
- M4: requireAdmin routes through extractAPIKey -> ValidateAPIKey -> admin role check
- M5: network client keeps only final RecordFailure (one count per attempt cycle)
- M3: normalizeOriginHost + buildCheckOrigin rewrite; origin_test.go added
- M1: app lifecycle cancel func; Run -> Shutdown on listen error (sole cache owner); JoinHostPort
- M6: VulnerabilityThresholds defaults all -1 so BlockOnSeverity governs
- L4: metadata.NormalizeSeverity at 4 severity-count sites (MEDIUM->MODERATE consistency)
- L1: handleDeleteBypass uses structured *errors.Error + ErrCodeNotFound
- L2: pypi extractVersionFromFilename (wheel/2nd segment, non-wheel/last digit segment);
cacheVersion uses full unique basename; regression tests added
- L5: document per-instance ValidationCache key scoping; rename packageURL param to resource
All: go build ./... && go vet ./... && go test ./... pass; golangci-lint clean.
This commit is contained in:
@@ -265,8 +265,22 @@ func (s *GORMStoreV2) SavePackage(ctx context.Context, pkg *metadata.Package) er
|
||||
return errors.Wrap(result.Error, errors.ErrCodeStorageFailure, "failed to update package")
|
||||
}
|
||||
|
||||
// If no rows were updated, create new record
|
||||
// If no rows were updated, create new record.
|
||||
// RowsAffected == 0 here means no LIVE row matches the key, because
|
||||
// GORM's default scope appends `deleted_at IS NULL` to the UPDATE.
|
||||
// The only possible match is a soft-deleted row (e.g. after TTL/evict
|
||||
// called DeletePackage). That retained row still holds the same
|
||||
// `storage_key`, which has a UNIQUE index, so a plain Create would
|
||||
// collide (UNIQUE constraint failed: packages.storage_key) and the
|
||||
// re-fetch would permanently 502. Hard-delete any such stale row
|
||||
// before creating.
|
||||
if result.RowsAffected == 0 {
|
||||
if err := tx.Unscoped().
|
||||
Where("registry_id = ? AND name = ? AND version = ?", registryID, pkg.Name, pkg.Version).
|
||||
Delete(&PackageModel{}).Error; err != nil {
|
||||
return errors.Wrap(err, errors.ErrCodeStorageFailure, "failed to purge stale soft-deleted package before create")
|
||||
}
|
||||
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return errors.Wrap(err, errors.ErrCodeStorageFailure, "failed to create package")
|
||||
}
|
||||
|
||||
@@ -209,6 +209,57 @@ func (s *GORMStoreV2TestSuite) Test_V2_DeletePackage_Success() {
|
||||
s.Equal(int64(1), count) // Still in DB, just soft deleted
|
||||
}
|
||||
|
||||
// Test_V2_SavePackage_AfterSoftDelete tests that saving the same
|
||||
// registry/name/version key after a soft delete (e.g. TTL expiry or
|
||||
// cache eviction) succeeds instead of colliding on the retained
|
||||
// storage_key UNIQUE index. Regression for the permanent 502 on
|
||||
// re-fetch of a once-evicted package.
|
||||
func (s *GORMStoreV2TestSuite) Test_V2_SavePackage_AfterSoftDelete() {
|
||||
pkg := &metadata.Package{
|
||||
Registry: "npm",
|
||||
Name: "re-add-test",
|
||||
Version: "1.0.0",
|
||||
StorageKey: "npm/re-add-test/1.0.0.tgz",
|
||||
Size: 1000,
|
||||
CachedAt: time.Now(),
|
||||
LastAccessed: time.Now(),
|
||||
}
|
||||
err := s.store.SavePackage(s.ctx, pkg)
|
||||
s.NoError(err)
|
||||
|
||||
// Soft-delete (simulates TTL expiry / eviction)
|
||||
err = s.store.DeletePackage(s.ctx, "npm", "re-add-test", "1.0.0")
|
||||
s.NoError(err)
|
||||
|
||||
// Re-save the same key (simulates re-fetch after cache miss).
|
||||
// Before the fix this failed with:
|
||||
// UNIQUE constraint failed: packages.storage_key
|
||||
pkg2 := &metadata.Package{
|
||||
Registry: "npm",
|
||||
Name: "re-add-test",
|
||||
Version: "1.0.0",
|
||||
StorageKey: "npm/re-add-test/1.0.0.tgz",
|
||||
Size: 2000,
|
||||
CachedAt: time.Now(),
|
||||
LastAccessed: time.Now(),
|
||||
}
|
||||
err = s.store.SavePackage(s.ctx, pkg2)
|
||||
s.NoError(err)
|
||||
|
||||
// The re-saved package must be retrievable
|
||||
retrieved, err := s.store.GetPackage(s.ctx, "npm", "re-add-test", "1.0.0")
|
||||
s.NoError(err)
|
||||
s.NotNil(retrieved)
|
||||
s.Equal(int64(2000), retrieved.Size)
|
||||
|
||||
// No duplicate rows retained after the hard purge
|
||||
var count int64
|
||||
s.store.db.Unscoped().Model(&PackageModel{}).
|
||||
Where("name = ?", "re-add-test").
|
||||
Count(&count)
|
||||
s.Equal(int64(1), count)
|
||||
}
|
||||
|
||||
// Test_V2_ListPackages_All tests listing all packages
|
||||
func (s *GORMStoreV2TestSuite) Test_V2_ListPackages_All() {
|
||||
// Create multiple packages
|
||||
|
||||
Reference in New Issue
Block a user