|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/BackendStack21/odek/internal/config" |
| 15 | + "golang.org/x/term" |
| 16 | +) |
| 17 | + |
| 18 | +// projectSandboxApprovalsFile is the persistent store for user-approved |
| 19 | +// project-level sandbox configurations. It lives under ~/.odek and is created |
| 20 | +// 0600. |
| 21 | +const projectSandboxApprovalsFile = "project_sandbox_approvals.json" |
| 22 | + |
| 23 | +// approveProjectSandbox requires explicit operator approval before any |
| 24 | +// project-level ./odek.json sandbox knobs are applied. This closes the C-1 |
| 25 | +// vector where a malicious repo exfiltrates host secrets via ${VAR} |
| 26 | +// interpolation in sandbox_env, pulls an attacker-controlled image, or widens |
| 27 | +// the container's network access. |
| 28 | +// |
| 29 | +// Approval can be granted in three ways: |
| 30 | +// 1. Set ODEK_APPROVE_PROJECT_SANDBOX=1 (useful for CI/non-interactive use). |
| 31 | +// 2. Answer the interactive prompt when running on a TTY. |
| 32 | +// 3. A prior approval for the same project/sandbox fingerprint is persisted |
| 33 | +// in ~/.odek/project_sandbox_approvals.json. |
| 34 | +// |
| 35 | +// If approval is required and cannot be obtained, approveProjectSandbox |
| 36 | +// returns an error and the command should abort before creating the sandbox. |
| 37 | +func approveProjectSandbox(resolved config.ResolvedConfig, stdin io.Reader, stdout io.Writer) error { |
| 38 | + isTTY := stdin == os.Stdin && term.IsTerminal(int(os.Stdin.Fd())) |
| 39 | + return approveProjectSandboxWithTTY(resolved, stdin, stdout, isTTY) |
| 40 | +} |
| 41 | + |
| 42 | +// approveProjectSandboxWithTTY is the testable core of approveProjectSandbox. |
| 43 | +func approveProjectSandboxWithTTY(resolved config.ResolvedConfig, stdin io.Reader, stdout io.Writer, tty bool) error { |
| 44 | + o := resolved.ProjectSandboxOverride |
| 45 | + if !o.HasEnv && !o.HasImage && !o.HasNetwork && !o.HasVolumes { |
| 46 | + return nil |
| 47 | + } |
| 48 | + |
| 49 | + if os.Getenv("ODEK_APPROVE_PROJECT_SANDBOX") == "1" { |
| 50 | + return nil |
| 51 | + } |
| 52 | + |
| 53 | + projectDir, err := os.Getwd() |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("project sandbox approval: get working directory: %w", err) |
| 56 | + } |
| 57 | + projectDir, err = filepath.Abs(projectDir) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("project sandbox approval: abs working directory: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + approved, err := loadProjectSandboxApprovals() |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("project sandbox approval: load approvals: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + key := projectSandboxApprovalKey(projectDir, o) |
| 68 | + if approved[key] { |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + if !tty { |
| 73 | + return fmt.Errorf( |
| 74 | + "project-level sandbox config in %s requires explicit approval\n"+ |
| 75 | + "set ODEK_APPROVE_PROJECT_SANDBOX=1 to approve, or run interactively", |
| 76 | + config.ProjectConfigPath(), |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + reader := bufio.NewReader(stdin) |
| 81 | + |
| 82 | + fmt.Fprintln(stdout) |
| 83 | + fmt.Fprintf(stdout, "WARNING: project config (%s) requests sandbox overrides:\n", config.ProjectConfigPath()) |
| 84 | + if o.HasImage { |
| 85 | + fmt.Fprintf(stdout, " image: %s\n", o.Image) |
| 86 | + } |
| 87 | + if o.HasNetwork { |
| 88 | + fmt.Fprintf(stdout, " network: %s\n", o.Network) |
| 89 | + } |
| 90 | + if o.HasEnv { |
| 91 | + fmt.Fprintf(stdout, " env: %s\n", strings.Join(o.EnvKeys, ", ")) |
| 92 | + if o.EnvHasInterpolation { |
| 93 | + fmt.Fprintln(stdout, " ⚠️ sandbox_env values contain ${...} interpolation against host environment variables") |
| 94 | + } |
| 95 | + } |
| 96 | + if o.HasVolumes { |
| 97 | + fmt.Fprintf(stdout, " volumes: %s\n", strings.Join(o.Volumes, ", ")) |
| 98 | + } |
| 99 | + fmt.Fprintln(stdout) |
| 100 | + fmt.Fprintln(stdout, "Allowing this means code in the sandbox can read workspace files and,") |
| 101 | + fmt.Fprintln(stdout, "depending on network mode, contact external hosts.") |
| 102 | + fmt.Fprintln(stdout) |
| 103 | + fmt.Fprint(stdout, "Approve? [y = once / t = trust this project / N] ") |
| 104 | + |
| 105 | + line, err := reader.ReadString('\n') |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("project sandbox approval: read prompt: %w", err) |
| 108 | + } |
| 109 | + line = strings.ToLower(strings.TrimSpace(line)) |
| 110 | + |
| 111 | + switch line { |
| 112 | + case "y", "yes": |
| 113 | + return nil |
| 114 | + case "t", "trust": |
| 115 | + approved[key] = true |
| 116 | + if err := saveProjectSandboxApprovals(approved); err != nil { |
| 117 | + return fmt.Errorf("project sandbox approval: save approvals: %w", err) |
| 118 | + } |
| 119 | + return nil |
| 120 | + default: |
| 121 | + return fmt.Errorf("project sandbox config was not approved") |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// projectSandboxApprovalKey returns a stable key for the persisted approval |
| 126 | +// store. A change to the project directory, image, network, env keys, or |
| 127 | +// volumes invalidates the prior approval. |
| 128 | +func projectSandboxApprovalKey(projectDir string, o config.ProjectSandboxOverride) string { |
| 129 | + h := sha256.New() |
| 130 | + fmt.Fprintf(h, "%s\x00%s\x00%s", projectDir, o.Image, o.Network) |
| 131 | + for _, k := range o.EnvKeys { |
| 132 | + fmt.Fprintf(h, "\x00env:%s", k) |
| 133 | + } |
| 134 | + for _, v := range o.Volumes { |
| 135 | + fmt.Fprintf(h, "\x00vol:%s", v) |
| 136 | + } |
| 137 | + return hex.EncodeToString(h.Sum(nil)) |
| 138 | +} |
| 139 | + |
| 140 | +// loadProjectSandboxApprovals reads the persisted approval map. A missing file |
| 141 | +// is treated as an empty approval set. |
| 142 | +func loadProjectSandboxApprovals() (map[string]bool, error) { |
| 143 | + path := filepath.Join(expandHome("~/.odek"), projectSandboxApprovalsFile) |
| 144 | + data, err := os.ReadFile(path) |
| 145 | + if err != nil { |
| 146 | + if os.IsNotExist(err) { |
| 147 | + return make(map[string]bool), nil |
| 148 | + } |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + |
| 152 | + var approvals map[string]bool |
| 153 | + if err := json.Unmarshal(data, &approvals); err != nil { |
| 154 | + return nil, fmt.Errorf("parse %s: %w", path, err) |
| 155 | + } |
| 156 | + if approvals == nil { |
| 157 | + approvals = make(map[string]bool) |
| 158 | + } |
| 159 | + return approvals, nil |
| 160 | +} |
| 161 | + |
| 162 | +// saveProjectSandboxApprovals writes the approval map to disk with 0600 |
| 163 | +// permissions. |
| 164 | +func saveProjectSandboxApprovals(approvals map[string]bool) error { |
| 165 | + dir := expandHome("~/.odek") |
| 166 | + if err := os.MkdirAll(dir, 0700); err != nil { |
| 167 | + return err |
| 168 | + } |
| 169 | + path := filepath.Join(dir, projectSandboxApprovalsFile) |
| 170 | + data, err := json.MarshalIndent(approvals, "", " ") |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + return os.WriteFile(path, data, 0600) |
| 175 | +} |
0 commit comments