Files
loki/desktop/scripts/afterPack.cjs
T
Mao NakamotoandClaude Opus 4.7 60b828ac98 fix(desktop): v0.4.3 — afterPack wraps Electron binary with --no-sandbox stub
v0.4.2 set linux.executableArgs: ["--no-sandbox"] but verification on a
fresh AppImage download showed it STILL crashed at FATAL setuid_sandbox_host.
Inspected the generated AppRun script and confirmed the flag was missing:
electron-builder only injects executableArgs into the .desktop file's
Exec= line, not the AppRun. So OS-menu launches via .desktop worked but
direct execution of the AppImage (double-click, ./binary from a shell,
electron-updater swap) did not.

Fix: a custom afterPack hook (desktop/scripts/afterPack.cjs) wraps the
real Electron binary in a small shell stub that bakes --no-sandbox into
argv before exec'ing the real binary. The AppRun unknowingly invokes the
wrapper, which in turn invokes the real binary with the flag. This is
the pattern Slack, Discord, VSCode, Cursor, and Obsidian all use.

Layout in appOutDir after afterPack:
  fleet-runner      ← bash wrapper (new, 0755)
  fleet-runner-bin  ← real Electron binary (renamed from fleet-runner)

Idempotent: skips if -bin already exists, so multi-arch builds don't
double-wrap. Scoped to electronPlatformName === 'linux'.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-03 18:23:07 +02:00

77 lines
3.5 KiB
JavaScript

// electron-builder afterPack hook — wraps the Electron binary in a small
// shell script that always passes --no-sandbox to the real binary.
//
// Why this exists: Linux AppImages mount a squashfs volume that strips the
// SUID bit from chrome-sandbox. Chromium aborts at FATAL before any of our
// own code runs:
//
// FATAL:setuid_sandbox_host.cc(163)] The SUID sandbox helper binary was
// found, but is not configured correctly. ... You need to make sure that
// ... chrome-sandbox is owned by root and has mode 4755.
//
// electron-builder's `linux.executableArgs` only injects --no-sandbox into
// the generated .desktop file's Exec= line, NOT into the AppRun script,
// so direct launches of the AppImage binary (double-click in a file
// manager, `./Fleet-Runner-*.AppImage` from a shell) still crash.
//
// app.commandLine.appendSwitch('no-sandbox') in main.ts runs too late —
// chrome-sandbox aborts before Electron's main process loads.
//
// This hook adds the only working layer: wrap the real binary in a tiny
// shell stub that bakes --no-sandbox into argv before exec'ing the real
// thing. Pattern used by Slack, Discord, VSCode, Cursor, Obsidian.
//
// Scoped to Linux because mac .dmg and Windows .exe don't have the SUID
// wrinkle (.deb installs handle it via dpkg postinst chmod 4755).
const fs = require('fs');
const path = require('path');
/** @param {import('electron-builder').AfterPackContext} context */
exports.default = async function afterPack(context) {
if (context.electronPlatformName !== 'linux') return;
// electron-builder copies the Electron binary into appOutDir at the
// executable name (defaults to product name lowercased and dasherized).
// For "Fleet Runner" → "fleet-runner".
const execName = context.packager.executableName;
const appOutDir = context.appOutDir;
const realBinaryPath = path.join(appOutDir, execName);
const wrappedBinaryPath = path.join(appOutDir, `${execName}-bin`);
if (!fs.existsSync(realBinaryPath)) {
console.warn(`[afterPack] expected Electron binary at ${realBinaryPath} but it's missing — skipping no-sandbox wrap`);
return;
}
// Avoid double-wrap if afterPack runs twice (multi-arch build, retries).
if (fs.existsSync(wrappedBinaryPath)) {
console.log(`[afterPack] ${execName} appears already wrapped (${execName}-bin exists) — skipping`);
return;
}
// Move the real Electron binary aside.
fs.renameSync(realBinaryPath, wrappedBinaryPath);
// Write a wrapper shell script under the original name. The AppRun
// script generated by app-builder will exec this wrapper, which adds
// --no-sandbox to argv before exec'ing the real binary.
//
// Notes:
// - `exec` so the wrapper process is replaced (no extra layer).
// - `"$@"` preserves any args the user passed.
// - `--no-sandbox` is appended BEFORE user args so it can't be undone.
// - The HERE_DIR trick handles both AppImage-mounted paths and direct
// execution from a deb install (no relative-path fragility).
const wrapper = `#!/bin/bash
# Auto-generated by desktop/scripts/afterPack.cjs.
# Wraps the real Electron binary with --no-sandbox so the AppImage launches
# on Linux without the SUID-sandbox helper FATAL. See afterPack.cjs for
# the why.
HERE_DIR="$(dirname "$(readlink -f "$0")")"
exec "$HERE_DIR/${execName}-bin" --no-sandbox "$@"
`;
fs.writeFileSync(realBinaryPath, wrapper, { mode: 0o755 });
console.log(`[afterPack] wrapped ${execName} with --no-sandbox stub (real binary moved to ${execName}-bin)`);
};