mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
ConfigureSandbox was only triggered by IsInstallationCommand(), missing update commands (npm update, pnpm update, etc.) that pull new versions and run postinstall scripts. Use MayDownloadPackages() as the sandbox signal so all package-downloading commands are sandboxed. Co-authored-by: Abhisek Datta <abhisek.datta@gmail.com>
This commit is contained in:
co-authored by
Abhisek Datta
parent
1a983c1dc1
commit
1d9045d770
+3
-3
@@ -379,11 +379,11 @@ func Get() *RuntimeConfig {
|
|||||||
return globalConfig
|
return globalConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfigureSandbox(isInstallationCommand bool) {
|
func ConfigureSandbox(mayDownloadPackages bool) {
|
||||||
if globalConfig.Config.Sandbox.Enabled {
|
if globalConfig.Config.Sandbox.Enabled {
|
||||||
// Apply sandbox to all commands if EnforceAlways=true, otherwise only to
|
// Apply sandbox to all commands if EnforceAlways=true, otherwise only to
|
||||||
// installation commands else disable the sandbox
|
// commands that may download packages (install, update, etc.)
|
||||||
globalConfig.Config.Sandbox.Enabled = globalConfig.Config.Sandbox.EnforceAlways || isInstallationCommand
|
globalConfig.Config.Sandbox.Enabled = globalConfig.Config.Sandbox.EnforceAlways || mayDownloadPackages
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -221,6 +221,64 @@ func TestConfigPrecedence(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigureSandbox(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
sandboxEnabled bool
|
||||||
|
enforceAlways bool
|
||||||
|
mayDownloadPackages bool
|
||||||
|
expectedSandboxState bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "sandbox disabled stays disabled regardless of command",
|
||||||
|
sandboxEnabled: false,
|
||||||
|
mayDownloadPackages: true,
|
||||||
|
expectedSandboxState: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "sandbox enabled with download command stays enabled",
|
||||||
|
sandboxEnabled: true,
|
||||||
|
mayDownloadPackages: true,
|
||||||
|
expectedSandboxState: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "sandbox enabled with non-download command gets disabled",
|
||||||
|
sandboxEnabled: true,
|
||||||
|
mayDownloadPackages: false,
|
||||||
|
expectedSandboxState: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "enforce_always keeps sandbox enabled for non-download command",
|
||||||
|
sandboxEnabled: true,
|
||||||
|
enforceAlways: true,
|
||||||
|
mayDownloadPackages: false,
|
||||||
|
expectedSandboxState: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "enforce_always with download command stays enabled",
|
||||||
|
sandboxEnabled: true,
|
||||||
|
enforceAlways: true,
|
||||||
|
mayDownloadPackages: true,
|
||||||
|
expectedSandboxState: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Setenv("PMG_CONFIG_DIR", "/tmp/pmg-test/random-does-not-exist")
|
||||||
|
initConfig()
|
||||||
|
|
||||||
|
cfg := Get()
|
||||||
|
cfg.Config.Sandbox.Enabled = tc.sandboxEnabled
|
||||||
|
cfg.Config.Sandbox.EnforceAlways = tc.enforceAlways
|
||||||
|
|
||||||
|
ConfigureSandbox(tc.mayDownloadPackages)
|
||||||
|
|
||||||
|
assert.Equal(t, tc.expectedSandboxState, cfg.Config.Sandbox.Enabled)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteTemplateConfigMergesExistingConfig(t *testing.T) {
|
func TestWriteTemplateConfigMergesExistingConfig(t *testing.T) {
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
t.Setenv("PMG_CONFIG_DIR", tmpDir)
|
t.Setenv("PMG_CONFIG_DIR", tmpDir)
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func (f *commonFlow) Run(ctx context.Context, args []string, parsedCmd *packagem
|
|||||||
var analyzers []analyzer.PackageVersionAnalyzer
|
var analyzers []analyzer.PackageVersionAnalyzer
|
||||||
|
|
||||||
// Configure sandbox based on command type and enforcement policy
|
// Configure sandbox based on command type and enforcement policy
|
||||||
config.ConfigureSandbox(parsedCmd.IsInstallationCommand())
|
config.ConfigureSandbox(parsedCmd.IsInstallationCommand() || parsedCmd.MayDownloadPackages())
|
||||||
|
|
||||||
cfg := config.Get()
|
cfg := config.Get()
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ func (f *proxyFlow) Run(ctx context.Context, args []string, parsedCmd *packagema
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Configure sandbox based on command type and enforcement policy
|
// Configure sandbox based on command type and enforcement policy
|
||||||
config.ConfigureSandbox(parsedCmd.IsInstallationCommand())
|
config.ConfigureSandbox(parsedCmd.IsInstallationCommand() || parsedCmd.MayDownloadPackages())
|
||||||
|
|
||||||
cfg := config.Get()
|
cfg := config.Get()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user