2026-01-08 00:15:02 +05:30
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// IsTrustedPackage checks if a package version is trusted based on global configuration.
|
2026-07-22 15:19:19 +05:30
|
|
|
// This is the primary API that should be used by the proxy flow.
|
2026-01-08 00:15:02 +05:30
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-21 18:22:15 +05:30
|
|
|
// 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, "")
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-15 19:44:55 +05:30
|
|
|
// CooldownSkipInfo describes how a package is exempted from the dependency
|
2026-06-21 18:22:15 +05:30
|
|
|
// cooldown window by the dependency_cooldown.skip list. It is independent of
|
|
|
|
|
// trusted_packages, which is honored separately as a global waiver.
|
2026-06-15 19:44:55 +05:30
|
|
|
type CooldownSkipInfo struct {
|
2026-06-21 18:22:15 +05:30
|
|
|
// SkipAll is true when a version-less entry matches: every version of the
|
|
|
|
|
// package is exempt from the cooldown window.
|
2026-06-15 19:44:55 +05:30
|
|
|
SkipAll bool
|
|
|
|
|
|
2026-06-21 18:22:15 +05:30
|
|
|
// Versions holds the specific versions exempted by version-pinned entries.
|
|
|
|
|
// Only meaningful when SkipAll is false; nil when there are none.
|
2026-06-15 19:44:55 +05:30
|
|
|
Versions map[string]bool
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-21 18:22:15 +05:30
|
|
|
// ExemptsVersion reports whether the given version is exempt from cooldown.
|
2026-06-15 19:44:55 +05:30
|
|
|
func (s CooldownSkipInfo) ExemptsVersion(version string) bool {
|
|
|
|
|
return s.SkipAll || s.Versions[version]
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-21 18:22:15 +05:30
|
|
|
// 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.
|
2026-06-15 19:44:55 +05:30
|
|
|
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
|
2026-06-21 18:22:15 +05:30
|
|
|
info.Versions = nil
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if info.SkipAll {
|
2026-06-15 19:44:55 +05:30
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if info.Versions == nil {
|
|
|
|
|
info.Versions = make(map[string]bool)
|
|
|
|
|
}
|
|
|
|
|
info.Versions[v.version] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return info
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-09 17:49:00 +05:30
|
|
|
// PreprocessPackageRefs pre-parses all PURL strings in the trusted and
|
|
|
|
|
// cooldown skip package lists. Exported for use in cross-package tests that
|
|
|
|
|
// install synthetic configs without going through Load.
|
|
|
|
|
func PreprocessPackageRefs(cfg *Config) error {
|
|
|
|
|
return preprocessPackageRefs(cfg)
|
2026-06-21 18:22:15 +05:30
|
|
|
}
|
|
|
|
|
|
2026-07-09 17:49:00 +05:30
|
|
|
// preprocessPackageRefs parses the PURL of each list entry in place, populating
|
|
|
|
|
// the pre-parsed purlRef. This is called once during config load to avoid
|
|
|
|
|
// repeated parsing at match time. Invalid PURLs are logged but not fatal.
|
|
|
|
|
func preprocessPackageRefs(cfg *Config) error {
|
|
|
|
|
for i := range cfg.TrustedPackages {
|
|
|
|
|
cfg.TrustedPackages[i].parseFrom(cfg.TrustedPackages[i].Purl)
|
2026-01-08 00:15:02 +05:30
|
|
|
}
|
2026-07-09 17:49:00 +05:30
|
|
|
for i := range cfg.DependencyCooldown.Skip {
|
|
|
|
|
cfg.DependencyCooldown.Skip[i].parseFrom(cfg.DependencyCooldown.Skip[i].Purl)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2026-01-08 00:15:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
|
for _, v := range trustedPackages {
|
2026-07-09 17:49:00 +05:30
|
|
|
if v.matches(pkgVersion) {
|
|
|
|
|
return true
|
2026-01-08 00:15:02 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|