|
| 1 | +package cooldown |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// Config holds cooldown settings for version filtering. |
| 11 | +// Cooldown hides package versions published too recently, giving the community |
| 12 | +// time to spot malicious releases before they're pulled into projects. |
| 13 | +type Config struct { |
| 14 | + // Default is the global default cooldown duration (e.g., "3d", "48h"). |
| 15 | + Default string `json:"default" yaml:"default"` |
| 16 | + |
| 17 | + // Ecosystems overrides the default for specific ecosystems. |
| 18 | + // Keys are ecosystem names (e.g., "npm", "pypi"). |
| 19 | + Ecosystems map[string]string `json:"ecosystems" yaml:"ecosystems"` |
| 20 | + |
| 21 | + // Packages overrides the cooldown for specific packages. |
| 22 | + // Keys are PURLs (e.g., "pkg:npm/lodash", "pkg:npm/@babel/core"). |
| 23 | + Packages map[string]string `json:"packages" yaml:"packages"` |
| 24 | + |
| 25 | + defaultDuration time.Duration |
| 26 | + ecosystemDurations map[string]time.Duration |
| 27 | + packageDurations map[string]time.Duration |
| 28 | + parsed bool |
| 29 | +} |
| 30 | + |
| 31 | +// parse resolves all string durations into time.Duration values. |
| 32 | +// Called lazily on first use. |
| 33 | +func (c *Config) parse() { |
| 34 | + if c.parsed { |
| 35 | + return |
| 36 | + } |
| 37 | + c.parsed = true |
| 38 | + |
| 39 | + c.defaultDuration, _ = ParseDuration(c.Default) |
| 40 | + |
| 41 | + c.ecosystemDurations = make(map[string]time.Duration, len(c.Ecosystems)) |
| 42 | + for k, v := range c.Ecosystems { |
| 43 | + d, _ := ParseDuration(v) |
| 44 | + c.ecosystemDurations[k] = d |
| 45 | + } |
| 46 | + |
| 47 | + c.packageDurations = make(map[string]time.Duration, len(c.Packages)) |
| 48 | + for k, v := range c.Packages { |
| 49 | + d, _ := ParseDuration(v) |
| 50 | + c.packageDurations[k] = d |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// For returns the effective cooldown duration for a given ecosystem and package PURL. |
| 55 | +// Resolution order: package override > ecosystem override > global default. |
| 56 | +func (c *Config) For(ecosystem, packagePURL string) time.Duration { |
| 57 | + c.parse() |
| 58 | + |
| 59 | + if d, ok := c.packageDurations[packagePURL]; ok { |
| 60 | + return d |
| 61 | + } |
| 62 | + if d, ok := c.ecosystemDurations[ecosystem]; ok { |
| 63 | + return d |
| 64 | + } |
| 65 | + return c.defaultDuration |
| 66 | +} |
| 67 | + |
| 68 | +// IsAllowed returns true if a version with the given publish time has passed |
| 69 | +// the cooldown period for this ecosystem/package. |
| 70 | +func (c *Config) IsAllowed(ecosystem, packagePURL string, publishedAt time.Time) bool { |
| 71 | + d := c.For(ecosystem, packagePURL) |
| 72 | + if d == 0 { |
| 73 | + return true |
| 74 | + } |
| 75 | + if publishedAt.IsZero() { |
| 76 | + return true |
| 77 | + } |
| 78 | + return time.Since(publishedAt) >= d |
| 79 | +} |
| 80 | + |
| 81 | +// Enabled returns true if any cooldown is configured. |
| 82 | +func (c *Config) Enabled() bool { |
| 83 | + c.parse() |
| 84 | + if c.defaultDuration > 0 { |
| 85 | + return true |
| 86 | + } |
| 87 | + for _, d := range c.ecosystemDurations { |
| 88 | + if d > 0 { |
| 89 | + return true |
| 90 | + } |
| 91 | + } |
| 92 | + for _, d := range c.packageDurations { |
| 93 | + if d > 0 { |
| 94 | + return true |
| 95 | + } |
| 96 | + } |
| 97 | + return false |
| 98 | +} |
| 99 | + |
| 100 | +// ParseDuration parses a duration string supporting days (e.g., "3d"), |
| 101 | +// in addition to Go's standard time.ParseDuration formats ("48h", "30m"). |
| 102 | +// "0" means disabled (returns 0). |
| 103 | +func ParseDuration(s string) (time.Duration, error) { |
| 104 | + s = strings.TrimSpace(s) |
| 105 | + if s == "" || s == "0" { |
| 106 | + return 0, nil |
| 107 | + } |
| 108 | + |
| 109 | + // Handle day suffix |
| 110 | + if numStr, ok := strings.CutSuffix(s, "d"); ok { |
| 111 | + days, err := strconv.ParseFloat(numStr, 64) |
| 112 | + if err != nil { |
| 113 | + return 0, fmt.Errorf("invalid duration %q: %w", s, err) |
| 114 | + } |
| 115 | + return time.Duration(days * float64(24*time.Hour)), nil |
| 116 | + } |
| 117 | + |
| 118 | + d, err := time.ParseDuration(s) |
| 119 | + if err != nil { |
| 120 | + return 0, fmt.Errorf("invalid duration %q: %w", s, err) |
| 121 | + } |
| 122 | + return d, nil |
| 123 | +} |
0 commit comments