Files
pmg/config/sandbox_policy_for_test.go
648adcbda4 feat: add uvx (uv tool run) package executor (#357)
* feat(uvx): add uvx (uv tool run) package executor

Adds support for `uvx`, implemented as a PyPI Executor alongside pipx.
uvx is an alias for `uv tool run`: it installs a tool into an ephemeral
environment and runs it, so it has no install/list subcommand and the
first positional argument (or --from) is the package to audit.

Parsing highlights:
- --from overrides the positional command as the package to audit
- --with packages are audited as additional environment dependencies
- name@version shorthand (ruff@0.3.0, ruff@latest) is normalized
- flag parsing stops at the tool name so the tool's own flags are not
  misread as uvx options; uvx's value/boolean flags are registered so
  none greedily consume the package positional
- VCS/URL/local-path specs are skipped for registry auditing

Wires up command registration, analytics, shell alias/shim, cloud audit
mapping, a dedicated `uvx` sandbox profile (UV_*/PIP_* env, uv cache and
tool dirs), config policy, docs, unit tests and an E2E workflow step.

Closes #326

https://claude.ai/code/session_011hyLxq7oWJX5Dp4tCEfG19

* chore(uvx): align docs and base profile with uvx support

Incorporates the low-risk, non-parser improvements from the community
PR #345 (author non-responsive) into our implementation:

- list uvx (and the previously-missing pipx) as PyPI managers in the
  pypi-restrictive base profile package_managers and its README, so the
  base profile applies directly when selected via --sandbox-profile
- document uvx in docs/github-action.md and docs/proxy-mode.md
- add version / IsExplicitVersion assertions to the uvx parser tests

Our pflag-based parser is kept as-is: unlike #345 it audits --with
packages and handles all uvx short flags (e.g. -w), both of which the
community PR misses.

* fix(uvx): skip interpreter requests; use require in tests

Addresses review feedback on PR #357:

- uvx interpreter requests (`uvx python`, `uvx python@3.12`, `uvx pypy`,
  ...) launch an isolated interpreter rather than installing a PyPI tool.
  Treating the positional as a package made the guard flow resolve/analyze
  pkg:pypi/python (and python==3.12), which could wrongly block or fail a
  valid invocation. Skip these for the positional; --with packages on the
  same command are still audited.
- Use require.NoError / require.Len for fatal assertions in the uvx tests,
  matching the repo's testing convention, so a failure stops the subtest
  before a nil dereference instead of panicking.

* docs(uvx): document fail-open and --with-requirements trade-offs

Record the two deliberate parsing decisions raised in review as in-code
trade-off comments (no behavior change):

- unknown flags are tolerated (fail open), consistent with the other
  executors; the residual gap only affects non-proxy guard mode since the
  default proxy flow intercepts every registry download.
- --with-requirements / --with-editable values are consumed but not
  expanded into audit targets; expanding them needs manifest-extractor and
  guard changes, tracked as follow-up. Proxy mode still covers them.

* docs(uvx): drop --with-requirements limitation note

Per maintainer review: guard mode is being deprecated and auditing the
contents of an existing requirements file is a scanner's responsibility,
not PMG's. Remove the "known limitation / follow-up" note; the flags stay
registered only so their values are not mistaken for the tool positional.

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-07-02 18:49:31 +05:30

142 lines
4.2 KiB
Go

package config
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSandboxConfigPolicyFor(t *testing.T) {
tests := []struct {
name string
policies map[string]SandboxPolicyRef
policyTemplates map[string]SandboxPolicyTemplate
pmName string
wantProfile string
wantExists bool
}{
{
name: "legacy default re-mapped for npm",
policies: map[string]SandboxPolicyRef{"npm": {Enabled: true, Profile: "npm-restrictive"}},
pmName: "npm",
wantProfile: "npm",
wantExists: true,
},
{
name: "legacy default re-mapped for yarn",
policies: map[string]SandboxPolicyRef{"yarn": {Enabled: true, Profile: "npm-restrictive"}},
pmName: "yarn",
wantProfile: "yarn",
wantExists: true,
},
{
name: "legacy default re-mapped for bun",
policies: map[string]SandboxPolicyRef{"bun": {Enabled: true, Profile: "npm-restrictive"}},
pmName: "bun",
wantProfile: "bun",
wantExists: true,
},
{
name: "custom profile kept verbatim",
policies: map[string]SandboxPolicyRef{"npm": {Enabled: true, Profile: "my-corp-npm"}},
pmName: "npm",
wantProfile: "my-corp-npm",
wantExists: true,
},
{
name: "legacy pnpm-restrictive re-mapped for pnpm",
policies: map[string]SandboxPolicyRef{"pnpm": {Enabled: true, Profile: "pnpm-restrictive"}},
pmName: "pnpm",
wantProfile: "pnpm",
wantExists: true,
},
{
name: "legacy profile for unrelated package manager kept verbatim",
policies: map[string]SandboxPolicyRef{"pip": {Enabled: true, Profile: "npm-restrictive"}},
pmName: "pip",
wantProfile: "npm-restrictive",
wantExists: true,
},
{
name: "legacy pypi-restrictive re-mapped for pip",
policies: map[string]SandboxPolicyRef{"pip": {Enabled: true, Profile: "pypi-restrictive"}},
pmName: "pip",
wantProfile: "pip",
wantExists: true,
},
{
name: "legacy pypi-restrictive re-mapped for pip3",
policies: map[string]SandboxPolicyRef{"pip3": {Enabled: true, Profile: "pypi-restrictive"}},
pmName: "pip3",
wantProfile: "pip",
wantExists: true,
},
{
name: "legacy pypi-restrictive re-mapped for pipx",
policies: map[string]SandboxPolicyRef{"pipx": {Enabled: true, Profile: "pypi-restrictive"}},
pmName: "pipx",
wantProfile: "pipx",
wantExists: true,
},
{
name: "legacy pypi-restrictive re-mapped for poetry",
policies: map[string]SandboxPolicyRef{"poetry": {Enabled: true, Profile: "pypi-restrictive"}},
pmName: "poetry",
wantProfile: "poetry",
wantExists: true,
},
{
name: "legacy pypi-restrictive re-mapped for uv",
policies: map[string]SandboxPolicyRef{"uv": {Enabled: true, Profile: "pypi-restrictive"}},
pmName: "uv",
wantProfile: "uv",
wantExists: true,
},
{
name: "legacy pypi-restrictive re-mapped for uvx",
policies: map[string]SandboxPolicyRef{"uvx": {Enabled: true, Profile: "pypi-restrictive"}},
pmName: "uvx",
wantProfile: "uvx",
wantExists: true,
},
{
name: "template override disables re-mapping",
policies: map[string]SandboxPolicyRef{"npm": {Enabled: true, Profile: "npm-restrictive"}},
policyTemplates: map[string]SandboxPolicyTemplate{
"npm-restrictive": {Path: "./custom-npm.yml"},
},
pmName: "npm",
wantProfile: "npm-restrictive",
wantExists: true,
},
{
name: "missing package manager",
policies: map[string]SandboxPolicyRef{},
pmName: "npm",
wantExists: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := SandboxConfig{Policies: tt.policies, PolicyTemplates: tt.policyTemplates}
ref, exists := cfg.PolicyFor(tt.pmName)
assert.Equal(t, tt.wantExists, exists)
if tt.wantExists {
assert.Equal(t, tt.wantProfile, ref.Profile)
}
})
}
}
func TestSandboxConfigPolicyForDoesNotMutateConfig(t *testing.T) {
cfg := SandboxConfig{
Policies: map[string]SandboxPolicyRef{"npm": {Enabled: true, Profile: "npm-restrictive"}},
}
_, _ = cfg.PolicyFor("npm")
assert.Equal(t, "npm-restrictive", cfg.Policies["npm"].Profile)
}