Files
pmg/sandbox/preset_registry_test.go
ee684a29a9 feat(sandbox): presets — additive workload allowance bundles (#387)
* feat(sandbox): introduce presets - additive workload allowance bundles

Presets are named, additive-only bundles of sandbox allowances for a
specific workload (git hooks tooling, Astro/Vite/Next.js dev servers).
They solve the per-workload tuning friction from #384 without weakening
the default posture: no built-in profile references a preset, presets
cannot carry deny rules or profile booleans (strict YAML decoding), and
mandatory denies still win everywhere except the existing exact-match
suppression.

- Preset schema with metadata (author, labels) and schema_version gating
- Registry over ordered sources (embedded builtin, user dir); builtin
  wins name collisions; source abstraction is the extension point for a
  future hosted registry and SafeDep cloud sync
- Official presets: git, astro, vite, nextjs (with threat notes)
- Overlay/runtime integration: pmg sandbox allow preset=<name> and
  --sandbox-allow preset=<name>, stored by reference, resolved at apply
  time, missing presets warn (fail closed) instead of aborting
- Profile integration: presets: [...] list resolved after inherits
- CLI: pmg sandbox preset list (metadata filters, --json), show (prints
  YAML with threat notes), lint
- Docs: user guide (docs/sandbox-presets.md) and design spec

Closes #384

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PETCfE4crLcmodosz12qRn

* fix(sandbox): address review findings on presets

- Presets never modify deny lists: a profile authored deny now survives a
  preset allowing the same path (deny-beats-allow keeps it enforced).
  Regression test added.
- Profile inspection commands (show, diff, lint) construct the profile
  registry with the user-aware preset registry so they agree with runtime
  resolution of custom profiles referencing user presets.
- Handle stderr write error when warning about unresolvable presets.
- Compute preset show underline from the uncolored header.
- Use path.Join for embed.FS reads (slash-separated on all platforms).
- Clarify in docs that lint-staged/astro are examples of preset workloads.
- Drop the design spec from the PR per review.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PETCfE4crLcmodosz12qRn

* fix(sandbox): harden preset precedence against authored denies

Addresses external security review findings on the preset mechanism:

- Bubblewrap: a mandatory write-denied path listed in allow_read lost its
  protection when a later writable parent bind covered it (bwrap last
  mount wins) - exactly the git preset shape (allow_read .git/config +
  allow_write .git/**). The mandatory deny now re-binds the path
  read-only after all writable mounts instead of being skipped.
  Regression test asserts mount ordering. Landlock and Seatbelt were
  unaffected (tests added for the same policy shape on Landlock).
- Environment: ScrubEnv is allow-wins, so a preset environment allowance
  could override a profile-authored deny. Preset env allowances
  overlapping an authored deny pattern are now dropped at application
  time (conservative bidirectional glob overlap, fail closed). Surviving
  entries still opt out of built-in credential scrubbing as intended.
- Network: removed allow_outbound from the preset schema. Both platform
  translators are all-or-nothing for outbound (one allow rule means
  blanket network access), so a preset outbound entry would silently
  change network posture far beyond what its YAML conveys. Strict
  decoding rejects the key.
- Added a dual-path expansion equivalence test (profile presets: field
  vs overlay/--sandbox-allow) and documented the precedence guarantees
  in docs/sandbox-presets.md.

Explicit --sandbox-allow and pmg sandbox allow overrides keep their
existing semantics.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PETCfE4crLcmodosz12qRn

* docs(sandbox): document env and preset allowances in allow command and overlay docs

pmg sandbox allow help, the --sandbox-allow flag usage, and the project
overlay docs enumerated only read/write/exec/net types. Add env and
preset to all of them, with an overlay example for persisting an env
allowance and a note on why env entries are not auto-promoted by --last.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PETCfE4crLcmodosz12qRn

* chore(sandbox): trim preset code comments to corner cases and minimal godocs

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PETCfE4crLcmodosz12qRn

* fix(sandbox): exact glob intersection for preset env deny overlap

The bidirectional literal-text heuristic missed overlapping globs with
different literal structure: preset allow AWS_*_KEY and authored deny
AWS_SECRET_* both match AWS_SECRET_ACCESS_KEY but neither pattern
matches the other's text, so the allowance merged and allow-wins
scrubbing exposed the variable. EnvPatternsOverlap now computes exact
intersection non-emptiness for the name glob dialect (case-insensitive,
'*' any sequence, '?' single char) via memoized DP.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PETCfE4crLcmodosz12qRn

* fix(sandbox): preset env allowances are exact names, not globs

Glob-vs-glob intersection is a losing game: every dialect extension
(character classes today) silently reopens the deny-bypass hole.
Restricting preset environment allowances to literal variable names
makes the authored-deny precedence check exact by construction: each
deny pattern is evaluated against the concrete name with the same
matcher ScrubEnv uses at runtime, so the decision cannot diverge from
enforcement regardless of deny dialect. Removes the glob intersection
machinery. Profile and --sandbox-allow env globs are unaffected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PETCfE4crLcmodosz12qRn

* fix(sandbox): reject mandatory-deny targets in preset paths

Preset validation relied on IsSensitiveProjectTarget, which covers fewer
files than util.DANGEROUS_FILES. A preset naming .git-credentials,
.pgpass, .docker/config.json or .config/gh exactly would exact-match
suppress the mandatory deny; .git/config in allow_write would suppress
the write protection. Preset paths are now checked against
DANGEROUS_FILES (single source of truth), .git/hooks is rejected in any
direction, and .git/config is rejected for write/exec while read stays
allowed for git repo discovery. Docs state the two deliberate opt-outs
precisely.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PETCfE4crLcmodosz12qRn

* feat(sandbox): preset init and edit commands for community authoring

pmg sandbox preset init scaffolds a valid user preset (metadata flags,
threat-note template, starter rule) and refuses built-in names since
builtins win resolution. pmg sandbox preset edit opens the file via the
shared editor package and validates the result, warning when a user
preset is shadowed by a built-in. Docs lead with the scaffolded flow and
spell out builtin-vs-community provenance in preset list.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PETCfE4crLcmodosz12qRn

* refactor(sandbox): move mandatory-target matching into util

Preset path validation re-encoded knowledge util already owns: the
dangerous-files comparison and hardcoded .git/config and .git/hooks
strings. util now exports GitConfigPath, GitHooksPath (also used by
GetMandatoryDenyPatterns), PathCoveredBy and DangerousFileMatch, and
preset validation consumes them so the mandatory deny policy has a
single definition.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PETCfE4crLcmodosz12qRn

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-07-21 15:14:07 +05:30

207 lines
5.6 KiB
Go

package sandbox
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func writePresetFile(t *testing.T, dir, fileName, content string) string {
t.Helper()
path := filepath.Join(dir, fileName)
require.NoError(t, os.WriteFile(path, []byte(content), 0o600))
return path
}
func TestPresetRegistryBuiltins(t *testing.T) {
registry, err := NewPresetRegistry()
require.NoError(t, err)
t.Run("official presets load and validate", func(t *testing.T) {
infos, err := registry.List()
require.NoError(t, err)
names := make([]string, 0, len(infos))
for _, info := range infos {
names = append(names, info.Preset.Name)
assert.Equal(t, PresetSourceBuiltin, info.Source)
assert.NotEmpty(t, info.Raw, "raw YAML preserved for show")
assert.NoError(t, info.Preset.Validate())
}
assert.Contains(t, names, "git")
assert.Contains(t, names, "astro")
assert.Contains(t, names, "vite")
assert.Contains(t, names, "nextjs")
})
t.Run("get by name", func(t *testing.T) {
info, err := registry.Get("git")
require.NoError(t, err)
assert.Equal(t, "git", info.Preset.Name)
assert.Contains(t, info.Preset.Filesystem.AllowRead, "${CWD}/.git/config")
})
t.Run("unknown preset is ErrPresetNotFound", func(t *testing.T) {
_, err := registry.Get("does-not-exist")
require.ErrorIs(t, err, ErrPresetNotFound)
})
}
func TestPresetRegistryUserDir(t *testing.T) {
dir := t.TempDir()
writePresetFile(t, dir, "myapp.yml", `
kind: preset
name: myapp
description: Custom app preset
metadata:
author: Community
labels: [myapp]
filesystem:
allow_write:
- ${CWD}/.myapp/**
`)
writePresetFile(t, dir, "git.yml", `
kind: preset
name: git
description: Attempted builtin override
filesystem:
allow_write:
- ${CWD}/anything/**
`)
writePresetFile(t, dir, "broken.yml", `
kind: preset
name: broken
filesystem:
deny_read: ["${CWD}/x"]
`)
registry, err := NewPresetRegistry(WithUserPresetDir(dir))
require.NoError(t, err)
t.Run("user preset resolves", func(t *testing.T) {
info, err := registry.Get("myapp")
require.NoError(t, err)
assert.Equal(t, PresetSourceUser, info.Source)
assert.Equal(t, filepath.Join(dir, "myapp.yml"), info.Path)
})
t.Run("builtin wins name collisions", func(t *testing.T) {
info, err := registry.Get("git")
require.NoError(t, err)
assert.Equal(t, PresetSourceBuiltin, info.Source)
assert.Contains(t, info.Preset.Filesystem.AllowRead, "${CWD}/.git/config")
})
t.Run("list marks shadowed user presets and skips invalid files", func(t *testing.T) {
infos, err := registry.List()
require.NoError(t, err)
var shadowedGit, sawBroken bool
for _, info := range infos {
if info.Preset.Name == "git" && info.Source == PresetSourceUser {
shadowedGit = info.Shadowed
}
if info.Preset.Name == "broken" {
sawBroken = true
}
}
assert.True(t, shadowedGit)
assert.False(t, sawBroken, "invalid preset files are skipped")
})
t.Run("missing user dir is a clean no-op", func(t *testing.T) {
registry, err := NewPresetRegistry(WithUserPresetDir(filepath.Join(dir, "missing")))
require.NoError(t, err)
_, err = registry.Get("git")
assert.NoError(t, err)
})
}
func TestProfilePresetsExpansion(t *testing.T) {
t.Run("custom profile with presets gets allowances", func(t *testing.T) {
dir := t.TempDir()
path := writePresetFile(t, dir, "pnpm-custom.yml", `
name: pnpm-custom
description: Custom pnpm profile with presets
inherits: pnpm
package_managers: [pnpm]
presets: [git, astro]
`)
registry, err := NewProfileRegistry()
require.NoError(t, err)
policy, err := registry.LoadCustomProfile(path)
require.NoError(t, err)
assert.Contains(t, policy.Filesystem.AllowRead, "${CWD}/.git/config")
assert.Contains(t, policy.Filesystem.AllowWrite, "${CWD}/.git/**")
assert.Contains(t, policy.Filesystem.AllowWrite, "${CWD}/.astro/**")
assert.Contains(t, policy.Network.AllowBind, "localhost:4321")
assert.Equal(t, []string{"git", "astro"}, policy.Presets, "names kept for provenance")
// Inherited base profile rules are still present
assert.Contains(t, policy.Filesystem.AllowWrite, "${CWD}/node_modules/**")
})
t.Run("unknown preset in profile is a hard error", func(t *testing.T) {
dir := t.TempDir()
path := writePresetFile(t, dir, "bad.yml", `
name: bad
package_managers: [pnpm]
presets: [does-not-exist]
filesystem:
allow_read: ["${CWD}/**"]
`)
registry, err := NewProfileRegistry()
require.NoError(t, err)
_, err = registry.LoadCustomProfile(path)
require.Error(t, err)
assert.ErrorIs(t, err, ErrPresetNotFound)
})
t.Run("profile authored deny survives a preset allowing the same path", func(t *testing.T) {
dir := t.TempDir()
path := writePresetFile(t, dir, "deny-wins.yml", `
name: deny-wins
package_managers: [pnpm]
presets: [git]
filesystem:
deny_read: ["${CWD}/.git/config"]
`)
registry, err := NewProfileRegistry()
require.NoError(t, err)
policy, err := registry.LoadCustomProfile(path)
require.NoError(t, err)
assert.Contains(t, policy.Filesystem.AllowRead, "${CWD}/.git/config")
assert.Contains(t, policy.Filesystem.DenyRead, "${CWD}/.git/config",
"presets are additive-only, they never remove authored deny rules")
})
t.Run("profile with only presets passes resolved validation", func(t *testing.T) {
dir := t.TempDir()
path := writePresetFile(t, dir, "presets-only.yml", `
name: presets-only
package_managers: [pnpm]
presets: [git]
`)
registry, err := NewProfileRegistry()
require.NoError(t, err)
policy, err := registry.LoadCustomProfile(path)
require.NoError(t, err)
assert.Contains(t, policy.Filesystem.AllowWrite, "${CWD}/.git/**")
})
}