fix debugging with seatbelt, including in strict profile (#300)

This commit is contained in:
Olcan 2025-05-09 08:44:40 -07:00 committed by GitHub
parent baa26e9e2e
commit b35a3856a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 42 additions and 21 deletions

View File

@ -76,6 +76,9 @@
;; allow outbound network connections ;; allow outbound network connections
(allow network-outbound) (allow network-outbound)
;; allow inbound network connections to debugging port
(allow network-inbound (local ip (string-append "*:" "9229")))
;; allow communication with sysmond for process listing (e.g. for pgrep) ;; allow communication with sysmond for process listing (e.g. for pgrep)
(allow mach-lookup (global-name "com.apple.sysmond")) (allow mach-lookup (global-name "com.apple.sysmond"))

View File

@ -46,6 +46,7 @@ export function sandbox_command(sandbox?: string | boolean): string {
} }
} else { } else {
// if we are on macOS (Darwin) and sandbox-exec is available, use that for minimal sandboxing // if we are on macOS (Darwin) and sandbox-exec is available, use that for minimal sandboxing
// unless SEATBELT_PROFILE is set to 'none', which we allow as an escape hatch
if ( if (
os.platform() === 'darwin' && os.platform() === 'darwin' &&
execSync('command -v sandbox-exec || true').toString().trim() && execSync('command -v sandbox-exec || true').toString().trim() &&
@ -145,8 +146,18 @@ function entrypoint(workdir: string): string[] {
export async function start_sandbox(sandbox: string) { export async function start_sandbox(sandbox: string) {
if (sandbox === 'sandbox-exec') { if (sandbox === 'sandbox-exec') {
// disallow BUILD_SANDBOX
if (process.env.BUILD_SANDBOX) {
console.error('ERROR: cannot BUILD_SANDBOX when using MacOC Seatbelt');
process.exit(1);
}
const profile = (process.env.SEATBELT_PROFILE ??= 'minimal'); const profile = (process.env.SEATBELT_PROFILE ??= 'minimal');
console.log(`using macos seatbelt (profile: ${profile}) ...`); console.log(`using macos seatbelt (profile: ${profile}) ...`);
// if DEBUG is set, convert to --inspect-brk in NODE_OPTIONS
if (process.env.DEBUG) {
process.env.NODE_OPTIONS ??= '';
process.env.NODE_OPTIONS += ` --inspect-brk`;
}
const args = [ const args = [
'-D', '-D',
`TARGET_DIR=${fs.realpathSync(process.cwd())}`, `TARGET_DIR=${fs.realpathSync(process.cwd())}`,
@ -158,8 +169,11 @@ export async function start_sandbox(sandbox: string) {
new URL(`sandbox-macos-${profile}.sb`, import.meta.url).pathname, new URL(`sandbox-macos-${profile}.sb`, import.meta.url).pathname,
'bash', 'bash',
'-c', '-c',
`SANDBOX=sandbox-exec NODE_OPTIONS="${process.env.NODE_OPTIONS}" ` + [
process.argv.map((arg) => quote([arg])).join(' '), `SANDBOX=sandbox-exec`,
`NODE_OPTIONS="${process.env.NODE_OPTIONS}"`,
...process.argv.map((arg) => quote([arg])),
].join(' '),
]; ];
spawnSync(sandbox, args, { stdio: 'inherit' }); spawnSync(sandbox, args, { stdio: 'inherit' });
return; return;
@ -268,6 +282,7 @@ export async function start_sandbox(sandbox: string) {
// expose env-specified ports on the sandbox // expose env-specified ports on the sandbox
ports().forEach((p) => args.push('--publish', `${p}:${p}`)); ports().forEach((p) => args.push('--publish', `${p}:${p}`));
// if DEBUG is set, expose debugging port
if (process.env.DEBUG) { if (process.env.DEBUG) {
const debugPort = process.env.DEBUG_PORT || '9229'; const debugPort = process.env.DEBUG_PORT || '9229';
args.push(`--publish`, `${debugPort}:${debugPort}`); args.push(`--publish`, `${debugPort}:${debugPort}`);

View File

@ -54,35 +54,37 @@ if [ -z "${GEMINI_CODE_SANDBOX:-}" ]; then
done done
fi fi
# if GEMINI_CODE_SANDBOX is still not set, then exit immediately w/ code 1
if [ -z "${GEMINI_CODE_SANDBOX:-}" ]; then exit 1; fi
# lowercase GEMINI_CODE_SANDBOX # lowercase GEMINI_CODE_SANDBOX
GEMINI_CODE_SANDBOX=$(echo "${GEMINI_CODE_SANDBOX:-}" | tr '[:upper:]' '[:lower:]') GEMINI_CODE_SANDBOX=$(echo "${GEMINI_CODE_SANDBOX:-}" | tr '[:upper:]' '[:lower:]')
# if GEMINI_CODE_SANDBOX is set to 0 or false, then exit immediately w/ code 1 # if GEMINI_CODE_SANDBOX is set to 1|true, then try to use docker or podman
if [[ "${GEMINI_CODE_SANDBOX:-}" =~ ^(0|false)$ ]]; then # if non-empty and not 0|false, treat as custom command and check that it exists
exit 1 # if empty or 0|false, then fail silently (after checking for possible fallbacks)
fi command=""
# if GEMINI_CODE_SANDBOX is set to 1 or true, then try to use docker or podman
if [[ "${GEMINI_CODE_SANDBOX:-}" =~ ^(1|true)$ ]]; then if [[ "${GEMINI_CODE_SANDBOX:-}" =~ ^(1|true)$ ]]; then
if command -v docker &>/dev/null; then if command -v docker &>/dev/null; then
if [ "$QUIET" = false ]; then echo "docker"; fi command="docker"
exit 0
elif command -v podman &>/dev/null; then elif command -v podman &>/dev/null; then
if [ "$QUIET" = false ]; then echo "podman"; fi command="podman"
exit 0
else else
echo "ERROR: install docker or podman or specify command in GEMINI_CODE_SANDBOX" >&2 echo "ERROR: install docker or podman or specify command in GEMINI_CODE_SANDBOX" >&2
exit 1 exit 1
fi fi
elif [ -n "${GEMINI_CODE_SANDBOX:-}" ] && [[ ! "${GEMINI_CODE_SANDBOX:-}" =~ ^(0|false)$ ]]; then
if ! command -v "$GEMINI_CODE_SANDBOX" &>/dev/null; then
echo "ERROR: missing sandbox command '$GEMINI_CODE_SANDBOX' (from GEMINI_CODE_SANDBOX)" >&2
exit 1
fi
command="$GEMINI_CODE_SANDBOX"
else
# if we are on macOS and sandbox-exec is available, use that for minimal sandboxing
# unless SEATBELT_PROFILE is set to 'none', which we allow as an escape hatch
if [ "$(uname)" = "Darwin" ] && command -v sandbox-exec &>/dev/null && [ "${SEATBELT_PROFILE:-}" != "none" ]; then
command="sandbox-exec"
else # GEMINI_CODE_SANDBOX is empty or 0|false, so we fail w/o error msg
exit 1
fi
fi fi
if ! command -v "$GEMINI_CODE_SANDBOX" &>/dev/null; then if [ "$QUIET" = false ]; then echo "$command"; fi
echo "ERROR: missing sandbox command '$GEMINI_CODE_SANDBOX' (from GEMINI_CODE_SANDBOX)" >&2
exit 1
fi
if [ "$QUIET" = false ]; then echo "$GEMINI_CODE_SANDBOX"; fi
exit 0 exit 0

View File

@ -20,6 +20,7 @@ node ./scripts/check-build-status.js
# if debugging is enabled and sandboxing is disabled, use --inspect-brk flag # if debugging is enabled and sandboxing is disabled, use --inspect-brk flag
# note with sandboxing this flag is passed to the binary inside the sandbox # note with sandboxing this flag is passed to the binary inside the sandbox
# inside sandbox SANDBOX should be set and sandbox_command.sh should fail
node_args=() node_args=()
if [ -n "${DEBUG:-}" ] && ! scripts/sandbox_command.sh -q; then if [ -n "${DEBUG:-}" ] && ! scripts/sandbox_command.sh -q; then
if [ -n "${SANDBOX:-}" ]; then if [ -n "${SANDBOX:-}" ]; then