( sandbox?: boolean | string, )
| 29 | } |
| 30 | |
| 31 | function getSandboxCommand( |
| 32 | sandbox?: boolean | string, |
| 33 | ): SandboxConfig['command'] | '' { |
| 34 | // If the SANDBOX env var is set, we're already inside the sandbox. |
| 35 | if (process.env['SANDBOX']) { |
| 36 | return ''; |
| 37 | } |
| 38 | |
| 39 | // note environment variable takes precedence over argument (from command line or settings) |
| 40 | const environmentConfiguredSandbox = |
| 41 | process.env['ANUS_SANDBOX']?.toLowerCase().trim() ?? ''; |
| 42 | sandbox = |
| 43 | environmentConfiguredSandbox?.length > 0 |
| 44 | ? environmentConfiguredSandbox |
| 45 | : sandbox; |
| 46 | if (sandbox === '1' || sandbox === 'true') sandbox = true; |
| 47 | else if (sandbox === '0' || sandbox === 'false' || !sandbox) sandbox = false; |
| 48 | |
| 49 | if (sandbox === false) { |
| 50 | return ''; |
| 51 | } |
| 52 | |
| 53 | if (typeof sandbox === 'string' && sandbox) { |
| 54 | if (!isSandboxCommand(sandbox)) { |
| 55 | console.error( |
| 56 | `ERROR: invalid sandbox command '${sandbox}'. Must be one of ${VALID_SANDBOX_COMMANDS.join( |
| 57 | ', ', |
| 58 | )}`, |
| 59 | ); |
| 60 | process.exit(1); |
| 61 | } |
| 62 | // confirm that specified command exists |
| 63 | if (commandExists.sync(sandbox)) { |
| 64 | return sandbox; |
| 65 | } |
| 66 | console.error( |
| 67 | `ERROR: missing sandbox command '${sandbox}' (from ANUS_SANDBOX)`, |
| 68 | ); |
| 69 | process.exit(1); |
| 70 | } |
| 71 | |
| 72 | // look for seatbelt, docker, or podman, in that order |
| 73 | // for container-based sandboxing, require sandbox to be enabled explicitly |
| 74 | if (os.platform() === 'darwin' && commandExists.sync('sandbox-exec')) { |
| 75 | return 'sandbox-exec'; |
| 76 | } else if (commandExists.sync('docker') && sandbox === true) { |
| 77 | return 'docker'; |
| 78 | } else if (commandExists.sync('podman') && sandbox === true) { |
| 79 | return 'podman'; |
| 80 | } |
| 81 | |
| 82 | // throw an error if user requested sandbox but no command was found |
| 83 | if (sandbox === true) { |
| 84 | console.error( |
| 85 | 'ERROR: ANUS_SANDBOX is true but failed to determine command for sandbox; ' + |
| 86 | 'install docker or podman or specify command in ANUS_SANDBOX', |
| 87 | ); |
| 88 | process.exit(1); |
no test coverage detected