Files
pmg/sandbox/executor/apply.go
T

86 lines
2.6 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.
//
2026-01-08 16:17:59 +05:30
// This is a security sensitive operation. If sandbox is enabled via. config but not available on the platform,
// it will return an error to avoid running the command without sandbox protection.
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 {
2026-01-08 16:17:59 +05:30
return sandbox.NewExecutionResult(), nil
2026-01-08 13:40:22 +05:30
}
2026-01-08 16:22:31 +05:30
registry, err := sandbox.NewProfileRegistry()
if err != nil {
return nil, fmt.Errorf("failed to create profile registry: %w", err)
}
2026-01-08 14:20:47 +05:30
var policy *sandbox.SandboxPolicy
if cfg.SandboxProfileOverride != "" {
log.Debugf("Using sandbox profile override: %s", cfg.SandboxProfileOverride)
2026-01-08 16:17:59 +05:30
2026-01-08 14:20:47 +05:30
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 {
2026-01-08 16:17:59 +05:30
log.Debugf("Looking up sandbox policy for %s", pmName)
2026-01-08 14:20:47 +05:30
policyRef, exists := cfg.Config.Sandbox.Policies[pmName]
if !exists {
return nil, fmt.Errorf("no sandbox policy configured for %s", pmName)
}
// The policy is explicitly disabled for this package manager, so we skip sandbox
if !policyRef.Enabled {
log.Warnf("sandbox policy %s is explicitly disabled for %s, skipping sandbox", policyRef.Profile, pmName)
return sandbox.NewExecutionResult(), nil
2026-01-08 14:20:47 +05:30
}
2026-01-08 16:17:59 +05:30
log.Debugf("Loading sandbox policy %s", policyRef.Profile)
2026-01-08 14:20:47 +05:30
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 16:17:59 +05:30
log.Debugf("Loaded sandbox policy %s", policy.Name)
2026-01-08 13:40:22 +05:30
if !policy.AppliesToPackageManager(pmName) {
2026-01-08 16:17:59 +05:30
return nil, fmt.Errorf("sandbox policy %s does not apply to %s", policy.Name, pmName)
2026-01-08 13:40:22 +05:30
}
sb, err := platform.NewSandbox()
2026-01-08 13:40:22 +05:30
if err != nil {
2026-01-08 16:17:59 +05:30
return nil, fmt.Errorf("sandbox not available on this platform: %v", err)
2026-01-08 13:40:22 +05:30
}
if !sb.IsAvailable() {
2026-01-08 16:17:59 +05:30
return nil, fmt.Errorf("sandbox %s not available, running without sandbox", sb.Name())
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 16:17:59 +05:30
return result, nil
2026-01-08 13:40:22 +05:30
}