Files
pmg/sandbox/executor/apply.go
T

90 lines
3.1 KiB
Go
Raw Normal View History

package executor
2026-01-08 13:40:22 +05:30
import (
"context"
"fmt"
"os/exec"
"github.com/safedep/dry/log"
"github.com/safedep/pmg/config"
"github.com/safedep/pmg/sandbox"
"github.com/safedep/pmg/sandbox/platform"
2026-01-08 13:40:22 +05:30
)
// ApplySandbox applies sandbox isolation to the command if sandbox mode is enabled.
// This is a helper function used by both guard and proxy flows to avoid code duplication.
//
// Parameters:
// - ctx: Context for the sandbox execution
// - cmd: The exec.Cmd to be sandboxed (will be modified in place)
2026-01-08 13:42:05 +05:30
// - pmName: Package manager name (e.g., "npm", "pip") used to determine the sandbox policy to apply
2026-01-08 13:40:22 +05:30
// - mode: Optional mode description for logging (e.g., "proxy mode", empty for default)
//
// Returns:
// - ExecutionResult: Contains execution state. Callers must check result.ShouldRun() before calling cmd.Run().
// - error: Non-nil if sandbox setup fails
//
// If sandbox is not enabled/available, returns a result indicating the caller should run the command.
2026-01-08 13:40:22 +05:30
// Gracefully degrades with warnings if sandbox is unavailable on the platform.
2026-01-08 14:20:47 +05:30
func ApplySandbox(ctx context.Context, cmd *exec.Cmd, pmName string) (*sandbox.ExecutionResult, error) {
2026-01-08 13:40:22 +05:30
cfg := config.Get()
if !cfg.Config.Sandbox.Enabled {
return sandbox.NewExecutionResult(false), nil
2026-01-08 13:40:22 +05:30
}
registry := sandbox.NewProfileRegistry()
2026-01-08 14:20:47 +05:30
var policy *sandbox.SandboxPolicy
var err error
// Check for runtime profile override first (--sandbox-profile flag)
if cfg.SandboxProfileOverride != "" {
log.Debugf("Using sandbox profile override: %s", cfg.SandboxProfileOverride)
policy, err = registry.GetProfile(cfg.SandboxProfileOverride)
if err != nil {
return nil, fmt.Errorf("failed to load override sandbox policy %s: %w", cfg.SandboxProfileOverride, err)
}
} else {
// Use configured per-package-manager policy
policyRef, exists := cfg.Config.Sandbox.Policies[pmName]
if !exists || !policyRef.Enabled {
log.Debugf("No sandbox policy enabled for %s", pmName)
return sandbox.NewExecutionResult(false), nil
}
policy, err = registry.GetProfile(policyRef.Profile)
if err != nil {
return nil, fmt.Errorf("failed to load sandbox policy %s: %w", policyRef.Profile, err)
}
2026-01-08 13:40:22 +05:30
}
2026-01-08 14:20:47 +05:30
// Validate that the loaded policy applies to this package manager
2026-01-08 13:40:22 +05:30
if !policy.AppliesToPackageManager(pmName) {
log.Warnf("Sandbox policy %s does not apply to %s", policy.Name, pmName)
return sandbox.NewExecutionResult(false), nil
2026-01-08 13:40:22 +05:30
}
// Create platform-specific sandbox
sb, err := platform.NewSandbox()
2026-01-08 13:40:22 +05:30
if err != nil {
log.Warnf("Sandbox not available on this platform: %v", err)
log.Warnf("Continuing without sandbox protection")
return sandbox.NewExecutionResult(false), nil
2026-01-08 13:40:22 +05:30
}
if !sb.IsAvailable() {
log.Warnf("Sandbox %s not available, running without sandbox", sb.Name())
return sandbox.NewExecutionResult(false), nil
2026-01-08 13:40:22 +05:30
}
2026-01-08 14:20:47 +05:30
log.Debugf("Running %s in %s sandbox with policy %s", pmName, sb.Name(), policy.Name)
2026-01-08 13:50:21 +05:30
result, err := sb.Execute(ctx, cmd, policy)
if err != nil {
return nil, fmt.Errorf("failed to setup sandbox: %w", err)
2026-01-08 13:40:22 +05:30
}
2026-01-08 14:30:33 +05:30
// Return result with sandbox reference so caller can defer result.Close()
return sandbox.NewExecutionResultWithSandbox(result.WasExecuted(), sb), nil
2026-01-08 13:40:22 +05:30
}