Files
pmg/packagemanager/npm.go
T
365deb1897 feat: Add proxy_install_only config to restrict proxy to download commands (#222)
* 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>
2026-04-17 01:13:30 +05:30

269 lines
7.6 KiB
Go

package packagemanager
import (
"fmt"
"io"
"slices"
"strings"
packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1"
"github.com/spf13/pflag"
)
type NpmPackageManagerConfig struct {
InstallCommands []string
NonDownloadCommands []string
CommandName string
}
func DefaultNpmPackageManagerConfig() NpmPackageManagerConfig {
return NpmPackageManagerConfig{
InstallCommands: []string{"install", "i", "add"},
// Commands that are known to never download packages from a registry.
// Anything not in this list (including unknown future commands) runs with the proxy.
//
// Script runners: "run", "start", "stop", "restart", "test"/"t" are all shorthand for
// "npm run <script>". They spin up local processes (dev servers, test runners) that make
// their own HTTP calls — setting proxy env vars breaks them without providing any security
// benefit since they don't contact the package registry themselves.
//
// "exec" is intentionally excluded — it downloads and runs a package (npx equivalent).
NonDownloadCommands: []string{
// Script runners — may start servers or long-running processes
"run", "start", "stop", "restart", "test", "t",
// Removal — uninstalls local packages, no registry download
"uninstall", "remove", "rm", "r", "un", "unlink",
// Local operations — no registry contact
"rebuild", "prune", "link", "cache", "pack",
// Inspection / read-only registry queries
"ls", "list", "outdated", "view", "info", "show", "search",
"config", "ping", "whoami", "version", "help",
},
CommandName: "npm",
}
}
func DefaultPnpmPackageManagerConfig() NpmPackageManagerConfig {
return NpmPackageManagerConfig{
InstallCommands: []string{"install", "i", "add"},
NonDownloadCommands: []string{
"run", "start", "stop", "restart", "test",
"remove", "rm", "uninstall", "un",
"prune", "link", "unlink",
"ls", "list", "outdated", "info", "view", "config", "why",
},
CommandName: "pnpm",
}
}
func DefaultBunPackageManagerConfig() NpmPackageManagerConfig {
return NpmPackageManagerConfig{
InstallCommands: []string{"install", "i", "add"},
NonDownloadCommands: []string{
// Script runners and local operations
"run", "test", "build",
// Removal
"remove", "rm",
},
CommandName: "bun",
}
}
func DefaultYarnPackageManagerConfig() NpmPackageManagerConfig {
return NpmPackageManagerConfig{
InstallCommands: []string{"install", "add", ""},
NonDownloadCommands: []string{
"run", "start", "stop", "restart", "test",
"remove", "unlink",
"ls", "list", "outdated", "info", "config", "why",
},
CommandName: "yarn",
}
}
type npmPackageManager struct {
Config NpmPackageManagerConfig
}
func NewNpmPackageManager(config NpmPackageManagerConfig) (*npmPackageManager, error) {
return &npmPackageManager{
Config: config,
}, nil
}
var _ PackageManager = &npmPackageManager{}
func (npm *npmPackageManager) Name() string {
return npm.Config.CommandName
}
func (npm *npmPackageManager) Ecosystem() packagev1.Ecosystem {
return packagev1.Ecosystem_ECOSYSTEM_NPM
}
func (npm *npmPackageManager) ParseCommand(args []string) (*ParsedCommand, error) {
if len(args) > 0 && args[0] == npm.Config.CommandName {
args = args[1:]
}
command := Command{Exe: npm.Config.CommandName, Args: args}
// Since manifest-based installs like 'npm i' are now valid commands
if len(args) < 1 {
if npm.Config.CommandName == "yarn" {
return &ParsedCommand{
Command: command,
InstallTargets: []*PackageInstallTarget{},
IsManifestInstall: true,
ManifestFiles: []string{},
}, nil
}
return &ParsedCommand{
Command: command,
}, nil
}
// Find the install command position
var installCmdIndex = -1
for idx, arg := range args {
if slices.Contains(npm.Config.InstallCommands, arg) {
installCmdIndex = idx
break
}
}
if installCmdIndex == -1 {
return &ParsedCommand{Command: command, IsKnownNonDownloadCommand: isFirstNonFlagArgInList(args, npm.Config.NonDownloadCommands)}, nil
}
// Extract arguments after the install command
installArgs := args[installCmdIndex+1:]
// Extract packages from args
var packages []string
var isManifestInstall bool
var devPackages []string
flagSet := pflag.NewFlagSet(npm.Config.CommandName, pflag.ContinueOnError)
flagSet.SetOutput(io.Discard)
flagSet.ParseErrorsAllowlist.UnknownFlags = true
switch npm.Config.CommandName {
case "npm", "pnpm":
flagSet.StringArrayVarP(&devPackages, "save-dev", "D", nil, "Install dev packages")
case "bun":
flagSet.StringArrayVarP(&devPackages, "dev", "d", nil, "Install dev packages")
case "yarn":
flagSet.StringArrayVarP(&devPackages, "dev", "D", nil, "Install dev packages")
}
// Known only to prevent UnknownFlags mode from swallowing the next package arg.
flagSet.BoolP("global", "g", false, "Install packages globally")
err := flagSet.Parse(installArgs)
if err != nil {
return &ParsedCommand{Command: command}, nil
}
packages = flagSet.Args()
// If install command was found but no explicit packages,
// this is a manifest-based installation
if installCmdIndex != -1 && len(packages) == 0 {
isManifestInstall = true
}
// Yarn-specific validation: yarn install does not accept package names
if npm.Config.CommandName == "yarn" && args[installCmdIndex] == "install" && len(packages) > 0 {
return &ParsedCommand{
Command: command,
}, nil
}
// No packages found and not a manifest install
if len(packages) == 0 && !isManifestInstall {
return &ParsedCommand{
Command: command,
}, nil
}
packages = append(packages, devPackages...)
// Process all package arguments
var installTargets []*PackageInstallTarget
for _, pkg := range packages {
packageName, version, err := npmParsePackageInfo(pkg)
if err != nil {
return nil, ErrFailedToParsePackage.Wrap(err)
}
// Clean version if specified
if version != "" {
version = npmCleanVersion(version)
}
installTargets = append(installTargets, &PackageInstallTarget{
PackageVersion: &packagev1.PackageVersion{
Package: &packagev1.Package{
Ecosystem: packagev1.Ecosystem_ECOSYSTEM_NPM,
Name: packageName,
},
Version: version,
},
})
}
return &ParsedCommand{
Command: command,
InstallTargets: installTargets,
IsManifestInstall: isManifestInstall,
ManifestFiles: []string{},
}, nil
}
func npmParsePackageInfo(input string) (packageName, version string, err error) {
if input == "" {
return "", "", fmt.Errorf("package info cannot be empty")
}
input = strings.TrimSpace(input)
if strings.HasPrefix(input, "@") {
// Scoped package (e.g. @types/node or @types/node@1.0.0)
lastAtIndex := strings.LastIndex(input, "@")
if lastAtIndex > 0 {
packageName = strings.TrimSpace(input[:lastAtIndex])
version = strings.TrimSpace(input[lastAtIndex+1:])
return packageName, version, nil
}
// If no version specifier, return the whole input as package name
return strings.TrimSpace(input), "", nil
}
// Normal package (e.g. lodash or lodash@4.17.21)
parts := strings.Split(input, "@")
if len(parts) == 2 {
packageName = strings.TrimSpace(parts[0])
version = strings.TrimSpace(parts[1])
return packageName, version, nil
}
if len(parts) == 1 {
packageName = strings.TrimSpace(parts[0])
return packageName, "", nil
}
return "", "", fmt.Errorf("invalid format: expected 'package' OR 'package@version', got '%s'", input)
}
func npmCleanVersion(version string) string {
version = strings.TrimPrefix(version, "^")
version = strings.TrimPrefix(version, "~")
if version == "*" || version == "" {
return "latest"
}
return version
}