mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
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:
@@ -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
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user