Sandbox fails for unsupported cmds (#129)

* introduce enforce_always sandbox config & hooks for flows

* fix sandbox failing for unsupported commands

* add hooks for pypi package managers

* add tests for sandbox hook

* introduce enforce_always flag for ease use & CI

* make comments descriptive

* remove hooks & update config to add API to configure sandbox

* add comments

* rm unused function
This commit is contained in:
Sahil Bansal
2026-01-19 22:24:59 +05:30
committed by GitHub
parent cc2dd993ed
commit 736c63a7b6
7 changed files with 44 additions and 11 deletions
+2
View File
@@ -23,6 +23,8 @@ func ApplyCobraFlags(cmd *cobra.Command) {
globalConfig.Config.ExperimentalProxyMode, "Use experimental proxy-based interception (EXPERIMENTAL)")
cmd.PersistentFlags().BoolVar(&globalConfig.Config.Sandbox.Enabled, "sandbox",
globalConfig.Config.Sandbox.Enabled, "Enable sandbox mode to isolate package manager processes (EXPERIMENTAL)")
cmd.PersistentFlags().BoolVar(&globalConfig.Config.Sandbox.EnforceAlways, "sandbox-enforce",
globalConfig.Config.Sandbox.EnforceAlways, "Apply sandbox to all commands, not just install commands (requires --sandbox)")
cmd.PersistentFlags().StringVar(&globalConfig.SandboxProfileOverride, "sandbox-profile",
globalConfig.SandboxProfileOverride, "Override sandbox policy profile (built-in name or path to custom YAML)")
}
+15 -1
View File
@@ -70,6 +70,11 @@ type SandboxConfig struct {
// Enabled enables sandbox mode (opt-in by default for backward compatibility).
Enabled bool `mapstructure:"enabled"`
// EnforceAlways controls scope of sandbox enforcement:
// - When true: sandbox applies to all package manager commands
// - When false: sandbox only applies to install commands, others run unrestricted (default)
EnforceAlways bool `mapstructure:"enforce_always"`
// Policies maps package manager names to their sandbox policy references.
// Key is package manager name (e.g., "npm", "pip"), value is policy reference.
Policies map[string]SandboxPolicyRef `mapstructure:"policies"`
@@ -168,7 +173,8 @@ func DefaultConfig() RuntimeConfig {
ExperimentalProxyMode: false,
TrustedPackages: []TrustedPackage{},
Sandbox: SandboxConfig{
Enabled: false,
Enabled: false,
EnforceAlways: false,
},
},
DryRun: false,
@@ -284,6 +290,14 @@ func Get() *RuntimeConfig {
return globalConfig
}
func ConfigureSandbox(isInstallationCommand bool) {
if globalConfig.Config.Sandbox.Enabled {
// Apply sandbox to all commands if EnforceAlways=true, otherwise only to
// installation commands else disable the sandbox
globalConfig.Config.Sandbox.Enabled = globalConfig.Config.Sandbox.EnforceAlways || isInstallationCommand
}
}
// WriteTemplateConfig writes the template configuration file to disk if it doesn't already exist.
func WriteTemplateConfig() error {
configDir, err := configDir()
+7 -2
View File
@@ -69,6 +69,12 @@ sandbox:
# Enable sandbox mode (opt-in, default: false for backward compatibility)
enabled: false
# Controls scope of sandbox enforcement:
# - true: sandbox applies to all package manager commands
# - false (default): sandbox only applies to install commands, others run unrestricted
# Requires 'enabled: true' and per-PM policies to be active. May break workflows expecting unrestricted commands.
enforce_always: false
# Policy templates define policy profiles by name and path.
# They can be used to override a built-in profile or create a custom profile.
# Note: Custom profiles loaded via policy_templates can inherit from built-in
@@ -87,7 +93,7 @@ sandbox:
# npm ecosystem. npm-restrictive is a built-in profile.
npm:
enabled: true
profile: npm-restrictive # Built-in profile, template name, or path to custom YAML
profile: npm-restrictive # Built-in profile, template name, or path to custom YAML
pnpm:
enabled: true
@@ -125,4 +131,3 @@ sandbox:
uv:
enabled: true
profile: pypi-restrictive