mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
* feat: Add proxy_install_only config to restrict proxy to download commands Introduces proxy_install_only (default: false) which, when enabled, skips the proxy for package manager commands that do not download packages (e.g. npm ls, pip list), avoiding unnecessary MITM overhead. - Add ProxyInstallOnly to Config and config template - Add IsKnownDownloadCommand / MayDownloadPackages to ParsedCommand - Add DownloadCommands to npm and pypi PM configs covering update, ci, audit, dlx, exec, x, download, run and equivalents per PM - Extract shared runner.Execute used by both proxy flow and guard - Proxy flow short-circuits to runner.Execute for non-download commands when proxy_install_only=true * refactor: Inject CommandExecutor into guard to fix dependency direction guard depended on internal/runner, which inverted the intended layer hierarchy. Now guard defines a CommandExecutor function type and accepts it as a constructor argument. internal/flows (the composition root) creates the executor closure wrapping runner.Execute and injects it, keeping guard free of internal/ dependencies. * refactor: Invert proxy_install_only logic to use known non-download commands Replace the DownloadCommands allowlist (opt-in, fail-open) with a NonDownloadCommands denylist (opt-out, fail-safe). The proxy now runs for all commands except those explicitly known to not download packages. Unknown or future package manager subcommands default to running with the proxy. Includes script runners (run, start, test, stop, restart) that can spin up local servers — setting proxy env vars on these breaks them without providing any security benefit. Also covers removal commands and local operations that never contact the registry. * fix: Support PMG_* env vars regardless of config file state AutomaticEnv only resolves env vars for keys Viper already knows about via AllKeys(). When a key is absent from the config file (commented out, new key added after last setup, or no config file at all), Viper had no knowledge of it and silently skipped the env var. Fix by registering all Config struct fields as Viper defaults via reflection (using mapstructure tags) before reading the config file. This ensures PMG_* env vars work in all cases. Precedence: cobra flags > env vars > config file > defaults. SetDefault is used (not Set) so env vars and config file can still override the Go defaults freely. Tests added covering all precedence levels including the key-absent- from-config-file case that was the original bug report. * fix: Only check first non-flag arg against NonDownloadCommands Scanning all args caused false proxy bypasses when package names or script arguments matched a NonDownloadCommands entry. For example: - npm exec test → "test" matched, proxy incorrectly skipped - npm update config → "config" matched, proxy skipped - npm publish --tag version → "version" matched, proxy skipped Fix by checking only the first non-flag argument (the subcommand). If it is not in NonDownloadCommands we break immediately, so trailing args never influence the classification. Applied to all four parsers: npm, pip/pip3, uv, and poetry. Regression tests added for the false positive cases. * refactor: Replace reflection-based Viper defaults with embedded template Load the embedded config template as the Viper base so all keys are registered upfront, enabling PMG_* env vars to work regardless of whether a key exists in the user's config file. * fix: Restore trusted_packages template entry and revert DefaultConfig change * docs: Document environment variable overrides for config keys * update npm test cmd * refactor: extract shared non-download command detection helper Replaces duplicated first-non-flag-arg detection loops in npm.go and pypi.go (pip + poetry parsers) with a shared isFirstNonFlagArgInList helper in packagemanager.go. https://claude.ai/code/session_01AHaKF3vc2Haj9tK3jgUBAs --------- Co-authored-by: Claude <noreply@anthropic.com>
113 lines
3.9 KiB
Go
113 lines
3.9 KiB
Go
package packagemanager
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"strings"
|
|
|
|
packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1"
|
|
)
|
|
|
|
type Command struct {
|
|
Exe string
|
|
Args []string
|
|
}
|
|
|
|
type PackageInstallTarget struct {
|
|
PackageVersion *packagev1.PackageVersion
|
|
|
|
// Extras specifies additional features to be installed with a Python package
|
|
// Example: "django[mysql,redis]" has Extras as ["mysql", "redis"]
|
|
// Currently only specific to Python packages
|
|
Extras []string
|
|
}
|
|
|
|
func (pit *PackageInstallTarget) HasVersion() bool {
|
|
return pit.PackageVersion != nil && pit.PackageVersion.GetVersion() != ""
|
|
}
|
|
|
|
type ParsedCommand struct {
|
|
// Original command
|
|
Command Command
|
|
|
|
// Parsed install target if this is an install command
|
|
InstallTargets []*PackageInstallTarget
|
|
|
|
// IsManifestInstall indicates if this is a manifest-based installation
|
|
// (e.g., npm install, pip install -r requirements.txt)
|
|
IsManifestInstall bool
|
|
|
|
// ManifestFiles contains the list of manifest files to install from
|
|
// (e.g., ["requirements.txt"] for pip install -r requirements.txt)
|
|
ManifestFiles []string
|
|
|
|
// IsKnownNonDownloadCommand is true for commands that are known to not download packages
|
|
// (e.g., npm ls, pip list, yarn why). Used by the proxy to decide whether to skip
|
|
// interception when proxy_install_only is enabled. Unknown commands default to false so
|
|
// the proxy runs — fail safe when a new subcommand is added to a package manager.
|
|
IsKnownNonDownloadCommand bool
|
|
}
|
|
|
|
// IsInstallationCommand returns true if command installs packages (explicit targets or from manifest).
|
|
// This is used by guard mode where we need to know which packages are being installed.
|
|
func (pc *ParsedCommand) IsInstallationCommand() bool {
|
|
return pc.HasInstallTarget() || pc.HasManifestInstall()
|
|
}
|
|
|
|
// MayDownloadPackages returns true if the command may download packages from a registry.
|
|
// Returns false only for commands explicitly known to be non-download (e.g., npm ls, pip list).
|
|
// Unknown commands return true by default — fail safe when new package manager subcommands appear.
|
|
func (pc *ParsedCommand) MayDownloadPackages() bool {
|
|
return !pc.IsKnownNonDownloadCommand
|
|
}
|
|
|
|
func (pc *ParsedCommand) HasInstallTarget() bool {
|
|
return len(pc.InstallTargets) > 0
|
|
}
|
|
|
|
func (pc *ParsedCommand) HasManifestInstall() bool {
|
|
return pc.IsManifestInstall
|
|
}
|
|
|
|
func (pc *ParsedCommand) ShouldExtractFromManifest() bool {
|
|
return pc.IsManifestInstall && !pc.HasInstallTarget()
|
|
}
|
|
|
|
// isFirstNonFlagArgInList checks if the first non-flag argument in args is in nonDownloadCmds.
|
|
// Only the first non-flag arg (the subcommand) is checked to avoid false positives when package
|
|
// names or script arguments happen to match a known non-download command.
|
|
func isFirstNonFlagArgInList(args []string, nonDownloadCmds []string) bool {
|
|
for _, arg := range args {
|
|
if strings.HasPrefix(arg, "-") {
|
|
continue
|
|
}
|
|
return slices.Contains(nonDownloadCmds, arg)
|
|
}
|
|
return false
|
|
}
|
|
|
|
// PackageManager is the contract for implementing a package manager
|
|
type PackageManager interface {
|
|
// Name of the package manager implementation
|
|
Name() string
|
|
|
|
// ParseCommand parses the command and returns a parsed command
|
|
// specific to the package manager implementation
|
|
ParseCommand(args []string) (*ParsedCommand, error)
|
|
|
|
// Ecosystem of the package manager
|
|
Ecosystem() packagev1.Ecosystem
|
|
}
|
|
|
|
// PackageResolver is the contract for resolving package info
|
|
type PackageResolver interface {
|
|
// ResolveLatestVersion resolves the latest version for a given package
|
|
ResolveLatestVersion(context.Context, *packagev1.Package) (*packagev1.PackageVersion, error)
|
|
|
|
// ResolveDependencies resolves the dependencies for a given package version
|
|
// It returns a flattened list of all the dependencies based on implementation
|
|
// specific config. The version resolution is based on minimum version selection
|
|
// for a given version range.
|
|
ResolveDependencies(context.Context, *packagev1.PackageVersion) ([]*packagev1.PackageVersion, error)
|
|
}
|