| 1 | package sandbox |
| 2 | |
| 3 | // Prepared is the unified launch plan for a sandboxed (or unconfined) command |
| 4 | // that may hold a session-private temporary directory lease. Callers must |
| 5 | // Release the lease after the process exits — including when Start fails. |
| 6 | type Prepared struct { |
| 7 | // Argv is the final process argv (possibly wrapped by bwrap/sandbox-exec). |
| 8 | Argv []string |
| 9 | // Wrapped reports whether an OS sandbox wrapper was applied. |
| 10 | Wrapped bool |
| 11 | // SessionTemp is the absolute host path of the private temporary directory |
| 12 | // (empty when the launch has no session temp). |
| 13 | SessionTemp string |
| 14 | // EnvOverrides are KEY=value pairs to merge into the child environment |
| 15 | // (TMPDIR/TMP/TEMP). Nil when no session temp is bound. |
| 16 | EnvOverrides []string |
| 17 | // LinuxSandboxed is true when SessionTemp is mapped at virtual /tmp under |
| 18 | // Linux bubblewrap; env overrides then point at /tmp. |
| 19 | LinuxSandboxed bool |
| 20 | } |
| 21 | |
| 22 | // PrepareShell builds argv for a shell command string. When sessionTemp is |
| 23 | // non-empty it is attached to a copy of spec so the platform wrapper can bind |
| 24 | // or allow that directory. |
| 25 | func PrepareShell(spec Spec, sh Shell, command, sessionTemp string) Prepared { |
| 26 | spec = withSessionTemp(spec, sessionTemp) |
| 27 | argv, wrapped := Command(spec, sh, command) |
| 28 | linuxSB := wrapped && sessionTemp != "" && isLinux() |
| 29 | return Prepared{ |
| 30 | Argv: argv, |
| 31 | Wrapped: wrapped, |
| 32 | SessionTemp: sessionTemp, |
| 33 | EnvOverrides: SessionTempEnv(sessionTemp, linuxSB), |
| 34 | LinuxSandboxed: linuxSB, |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // PrepareArgs builds argv for a raw argument vector (e.g. ripgrep). |
| 39 | func PrepareArgs(spec Spec, args []string, sessionTemp string) Prepared { |
| 40 | spec = withSessionTemp(spec, sessionTemp) |
| 41 | argv, wrapped := CommandArgs(spec, args) |
| 42 | linuxSB := wrapped && sessionTemp != "" && isLinux() |
| 43 | return Prepared{ |
| 44 | Argv: argv, |
| 45 | Wrapped: wrapped, |
| 46 | SessionTemp: sessionTemp, |
| 47 | EnvOverrides: SessionTempEnv(sessionTemp, linuxSB), |
| 48 | LinuxSandboxed: linuxSB, |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func withSessionTemp(spec Spec, sessionTemp string) Spec { |
| 53 | if sessionTemp == "" { |
| 54 | return spec |
| 55 | } |
| 56 | spec.SessionTemp = sessionTemp |
| 57 | return spec |
| 58 | } |
| 59 |