mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
* feat(cooldown): respect trusted_packages in dependency cooldown Trusted packages are now treated as a superset waiver that bypasses every PMG control (malware analysis, cooldown, and any future controls). A globally trusted package is automatically exempt from the cooldown window and no longer needs a duplicate entry in dependency_cooldown.skip. The skip list remains the narrower, cooldown-only waiver for packages that must bypass the cooldown wait but still be malware-scanned. * refactor(cooldown): tag skip reason and audit-log skipped packages Address review feedback on #342: - Restore cooldownSkip to a pure single-list function (SRP); the merge into trusted_packages now happens in a separate mergeCooldownSkip step, driven by the exported CooldownSkip wrapper. - Extend CooldownSkipInfo with a CooldownSkipReason (TrustedPackage / CooldownSkipList) on both SkipAll and per-version entries, so callers can tell apart the broad waiver from the cooldown-only one. When both lists match the same package, trusted_packages wins. - Add audit.LogCooldownSkipped and emit it from the npm and PyPI interceptors on the SkipAll path, alongside the existing info log, carrying the source list as the reason. * refactor(cooldown): inline list merge, audit per-version exemptions Address further review feedback: - Drop the separate mergeCooldownSkip helper; cooldownSkip now writes into a shared *CooldownSkipInfo and is called twice from CooldownSkip (cooldown skip list first, trusted_packages on top so trusted entries override the reason on overlap). - Audit log every exemption, not just SkipAll: a new auditCooldownSkip helper in proxy/interceptors/cooldown.go emits one event per match (package-wide or per-version), each tagged with its source list. LogCooldownSkipped gains a version argument for the per-version case. - Cover the trusted_packages reason path in TestCooldownSkip. * fix(cooldown): avoid double-auditing trusted package exemptions auditCooldownSkip now only emits EventTypeCooldownSkipped for entries that came from dependency_cooldown.skip. Trusted-package exemptions already get an EventTypeInstallTrustedAllowed event at tarball-download time (proxy/interceptors/base_registry.go), so emitting a cooldown event for them too would double-count the same waiver. * emit trusted and cooldown skip events to cloud * fix tests * refactor(cooldown): return value from collectCooldownSkip, short-circuit on trusted SkipAll Address PR review feedback: - Rename cooldownSkip to collectCooldownSkip and return CooldownSkipInfo instead of mutating an input pointer. - Add mergeCooldownSkip to combine per-list results with trusted_packages taking precedence on overlap. - CooldownSkip now consults trusted_packages first and returns immediately on a package-wide trusted exemption (DC skip list cannot add anything). - Extend tests to cover disjoint pinned entries across both lists and the case where DC version-less subsumes a trusted pinned entry. * fix(audit): address cooldown review feedback * fix(cooldown): audit cooldown skips at download time with concrete version Backend rejects PackageVersion messages without a version, and audit logs should reflect the runtime fact (a specific version was skipped) rather than the config rule. Move the audit emission from metadata-request handling to download-request handling, where the concrete version is known, and require version in LogCooldownSkipped. * chore(audit): drop dead scope assignment in LogCooldownSkipped * refactor(cooldown): move skip-list logic into cooldown handlers Registry interceptors no longer compute CooldownSkip or branch on SkipAll; they just call HandleMetadataRequest. The npm and pypi cooldown handlers own the skip lookup, the package-wide exemption short-circuit, and (for pypi) the canonical-name denormalization. Also align LogCooldownSkipped with other LogXxx signatures by taking *packagev1.PackageVersion. * fix: Simplify audit logging for dependency cooldown skip * refactor: Simplify cooldown handling and maintain separation of concepts for trusted and DC skip packages * fix: Code review fixes * fix: Emit cooldown skipped audit event ONLY when an in-window version is skipped --------- Co-authored-by: Abhisek Datta <abhisek.datta@gmail.com>
161 lines
5.2 KiB
Go
161 lines
5.2 KiB
Go
package config
|
|
|
|
import (
|
|
packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1"
|
|
"github.com/safedep/dry/api/pb"
|
|
"github.com/safedep/dry/log"
|
|
)
|
|
|
|
// IsTrustedPackage checks if a package version is trusted based on global configuration.
|
|
// This is the primary API that should be used by guard and proxy flows.
|
|
// It returns true if the package is in the trusted packages list, false otherwise.
|
|
func IsTrustedPackage(pkgVersion *packagev1.PackageVersion) bool {
|
|
return isTrustedPackageVersion(Get().Config.TrustedPackages, pkgVersion)
|
|
}
|
|
|
|
// IsTrustedPackageRef reports whether a specific package version is trusted.
|
|
func IsTrustedPackageRef(ecosystem packagev1.Ecosystem, name, version string) bool {
|
|
return isTrustedPackageVersion(Get().Config.TrustedPackages, &packagev1.PackageVersion{
|
|
Package: &packagev1.Package{Ecosystem: ecosystem, Name: name},
|
|
Version: version,
|
|
})
|
|
}
|
|
|
|
// IsTrustedPackageAllVersions reports whether every version of a package is
|
|
// trusted. It checks with an empty version, which matches only a version-less
|
|
// trusted entry — never a version-pinned one.
|
|
func IsTrustedPackageAllVersions(ecosystem packagev1.Ecosystem, name string) bool {
|
|
return IsTrustedPackageRef(ecosystem, name, "")
|
|
}
|
|
|
|
// CooldownSkipInfo describes how a package is exempted from the dependency
|
|
// cooldown window by the dependency_cooldown.skip list. It is independent of
|
|
// trusted_packages, which is honored separately as a global waiver.
|
|
type CooldownSkipInfo struct {
|
|
// SkipAll is true when a version-less entry matches: every version of the
|
|
// package is exempt from the cooldown window.
|
|
SkipAll bool
|
|
|
|
// Versions holds the specific versions exempted by version-pinned entries.
|
|
// Only meaningful when SkipAll is false; nil when there are none.
|
|
Versions map[string]bool
|
|
}
|
|
|
|
// ExemptsVersion reports whether the given version is exempt from cooldown.
|
|
func (s CooldownSkipInfo) ExemptsVersion(version string) bool {
|
|
return s.SkipAll || s.Versions[version]
|
|
}
|
|
|
|
// CooldownSkip returns how a package is exempted from the dependency cooldown
|
|
// window via dependency_cooldown.skip. A version-less entry exempts every
|
|
// version; a version-pinned entry exempts only that version. The skip list
|
|
// waives ONLY the cooldown wait — exempt packages are still malware-analyzed.
|
|
func CooldownSkip(ecosystem packagev1.Ecosystem, name string) CooldownSkipInfo {
|
|
return cooldownSkip(Get().Config.DependencyCooldown.Skip, ecosystem, name)
|
|
}
|
|
|
|
func cooldownSkip(skip []TrustedPackage, ecosystem packagev1.Ecosystem, name string) CooldownSkipInfo {
|
|
info := CooldownSkipInfo{}
|
|
if name == "" {
|
|
return info
|
|
}
|
|
|
|
for _, v := range skip {
|
|
if !v.parsed || v.ecosystem != ecosystem || v.name != name {
|
|
continue
|
|
}
|
|
|
|
if v.version == "" {
|
|
info.SkipAll = true
|
|
info.Versions = nil
|
|
continue
|
|
}
|
|
|
|
if info.SkipAll {
|
|
continue
|
|
}
|
|
|
|
if info.Versions == nil {
|
|
info.Versions = make(map[string]bool)
|
|
}
|
|
info.Versions[v.version] = true
|
|
}
|
|
|
|
return info
|
|
}
|
|
|
|
// PreprocessTrustedPackages pre-parses all PURL strings in the trusted package
|
|
// lists. Exported for use in cross-package tests that install synthetic configs
|
|
// without going through Load.
|
|
func PreprocessTrustedPackages(cfg *Config) error {
|
|
return preprocessTrustedPackages(cfg)
|
|
}
|
|
|
|
// preprocessTrustedPackages pre-parses all PURL strings in the trusted package
|
|
// lists (both the top-level guardrail list and the cooldown-exemption list).
|
|
// This is called once during config load to avoid repeated parsing during
|
|
// trusted package checks. Invalid PURLs are logged but not fatal.
|
|
func preprocessTrustedPackages(cfg *Config) error {
|
|
preprocessTrustedPackageList(cfg.TrustedPackages)
|
|
preprocessTrustedPackageList(cfg.DependencyCooldown.Skip)
|
|
return nil
|
|
}
|
|
|
|
// preprocessTrustedPackageList parses the PURL of each entry in place, populating
|
|
// the pre-parsed ecosystem/name/version fields. Entries with an invalid PURL are
|
|
// marked unparsed (and skipped at match time) rather than failing the load.
|
|
func preprocessTrustedPackageList(packages []TrustedPackage) {
|
|
for i := range packages {
|
|
tp := &packages[i]
|
|
|
|
parsedPurl, err := pb.NewPurlPackageVersion(tp.Purl)
|
|
if err != nil {
|
|
log.Warnf("Failed to parse trusted package PURL: %s: %v", tp.Purl, err)
|
|
tp.parsed = false
|
|
continue
|
|
}
|
|
|
|
tp.parsed = true
|
|
tp.ecosystem = parsedPurl.Ecosystem()
|
|
tp.name = parsedPurl.Name()
|
|
tp.version = parsedPurl.Version()
|
|
}
|
|
}
|
|
|
|
// isTrustedPackageVersion checks if a package version is in the trusted packages list.
|
|
//
|
|
// It matches based on ecosystem, package name, and optionally version.
|
|
// If the trusted package PURL doesn't specify a version, all versions of that package are trusted.
|
|
// Returns false if pkgVersion is nil or if trustedPackages is empty.
|
|
func isTrustedPackageVersion(trustedPackages []TrustedPackage, pkgVersion *packagev1.PackageVersion) bool {
|
|
if pkgVersion == nil {
|
|
return false
|
|
}
|
|
|
|
if len(trustedPackages) == 0 {
|
|
return false
|
|
}
|
|
|
|
for _, v := range trustedPackages {
|
|
if !v.parsed {
|
|
continue
|
|
}
|
|
|
|
if v.ecosystem != pkgVersion.GetPackage().GetEcosystem() {
|
|
continue
|
|
}
|
|
|
|
if v.name != pkgVersion.GetPackage().GetName() {
|
|
continue
|
|
}
|
|
|
|
if v.version != "" && v.version != pkgVersion.GetVersion() {
|
|
continue
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|