| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- #!/usr/bin/env node
- import { quote } from "./utils/shell-quote.js";
- import { Command } from "commander";
- import { SandboxManager } from "./index.js";
- import type { SandboxRuntimeConfig } from "./sandbox/sandbox-config.js";
- import { spawn } from "child_process";
- import { logForDebugging } from "./utils/debug.js";
- import { loadConfig, loadConfigFromString } from "./utils/config-loader.js";
- import * as readline from "readline";
- import * as fs from "fs";
- import * as path from "path";
- import * as os from "os";
- /**
- * Get default config path
- */
- function getDefaultConfigPath(): string {
- return path.join(os.homedir(), ".srt-settings.json");
- }
- /**
- * Create a minimal default config if no config file exists
- */
- function getDefaultConfig(): SandboxRuntimeConfig {
- return {
- network: {
- allowedDomains: [],
- deniedDomains: [],
- },
- filesystem: {
- denyRead: [],
- allowRead: [],
- allowWrite: [],
- denyWrite: [],
- },
- };
- }
- async function main(): Promise<void> {
- const program = new Command();
- program
- .name("srt")
- .description(
- "Run commands in a sandbox with network and filesystem restrictions",
- )
- .version(process.env.npm_package_version || "1.0.0");
- // ── Windows install/uninstall ─────────────────────────────────
- // Self-elevating one-shot install (one UAC prompt). Also
- // available programmatically as installWindowsSandbox().
- program
- .command("windows-install")
- .description(
- "Windows: provision the `srt-sandbox` user account + install WFP " +
- "filters (one UAC prompt). No logout needed.",
- )
- .option("--sublayer-guid <guid>", "WFP sublayer GUID")
- .option(
- "--proxy-port-range <lo-hi>",
- "loopback PERMIT port range (e.g. 60080-60089)",
- )
- .option(
- "--sandbox-user <name>",
- "name for the sandbox user account (default: srt-sandbox)",
- )
- .option("--force", "replace an existing install with different config")
- .action(async (o: Record<string, string | boolean | undefined>) => {
- const { installWindowsSandbox, resolveSrtWin, VENDORED_SRT_WIN_EXE } =
- await import("./sandbox/windows-sandbox-utils.js");
- const range =
- typeof o.proxyPortRange === "string"
- ? (o.proxyPortRange.split("-").map(Number) as [number, number])
- : undefined;
- try {
- const r = installWindowsSandbox({
- sublayerGuid: o.sublayerGuid as string | undefined,
- proxyPortRange: range,
- sandboxUser: o.sandboxUser as string | undefined,
- force: Boolean(o.force),
- // Our own CLI opts into the packaged exe explicitly —
- // there is no ambient vendor fallback.
- srtWin: resolveSrtWin({ path: VENDORED_SRT_WIN_EXE }),
- });
- if (r.cancelled) {
- console.error(
- "Install cancelled at the UAC prompt. Nothing changed.",
- );
- process.exit(2);
- }
- console.log(
- `Installed.\n` +
- ` sandbox user: ${r.user.provisioned ? "provisioned" : "MISSING"}` +
- (r.user.sid ? ` (${r.user.sid})` : "") +
- `\n` +
- ` WFP: ${r.wfp.state}, ${r.wfp.filters} filters` +
- (r.wfp.portRange
- ? `, port range ${r.wfp.portRange[0]}-${r.wfp.portRange[1]}`
- : "") +
- `\n\n` +
- `No logout needed — the WFP filter keys on the dedicated ` +
- `\`srt-sandbox\` user's SID, so your network is unaffected.`,
- );
- } catch (e) {
- console.error(`Error: ${(e as Error).message}`);
- process.exit(1);
- }
- });
- program
- .command("windows-uninstall")
- .description(
- "Windows: remove WFP filters + the `srt-sandbox` account (one UAC prompt).",
- )
- .option("--sublayer-guid <guid>", "WFP sublayer GUID")
- .action(async (o: Record<string, string | undefined>) => {
- const { uninstallWindowsSandbox, resolveSrtWin, VENDORED_SRT_WIN_EXE } =
- await import("./sandbox/windows-sandbox-utils.js");
- try {
- const r = uninstallWindowsSandbox({
- sublayerGuid: o.sublayerGuid,
- srtWin: resolveSrtWin({ path: VENDORED_SRT_WIN_EXE }),
- });
- if (r.cancelled) {
- console.error("Uninstall cancelled at the UAC prompt.");
- process.exit(2);
- }
- console.log("WFP filters and `srt-sandbox` account removed.");
- } catch (e) {
- console.error(`Error: ${(e as Error).message}`);
- process.exit(1);
- }
- });
- // Default command - run command in sandbox
- program
- .argument("[command...]", "command to run in the sandbox")
- .option("-d, --debug", "enable debug logging")
- .option(
- "-s, --settings <path>",
- "path to config file (default: ~/.srt-settings.json)",
- )
- .option(
- "-c <command>",
- "run command string directly (like sh -c), no escaping applied",
- )
- .option(
- "--control-fd <fd>",
- "read config updates from file descriptor (JSON lines protocol)",
- parseInt,
- )
- .allowUnknownOption()
- .action(
- async (
- commandArgs: string[],
- options: {
- debug?: boolean;
- settings?: string;
- c?: string;
- controlFd?: number;
- },
- ) => {
- try {
- // Enable debug logging if requested. logForDebugging() reads
- // SRT_DEBUG (not DEBUG, to avoid clashing with the npm `debug`
- // package and other tools) — keep this in sync with utils/debug.ts.
- if (options.debug) {
- process.env.SRT_DEBUG = "true";
- }
- // Load config from file
- const configPath = options.settings || getDefaultConfigPath();
- let runtimeConfig = loadConfig(configPath);
- if (!runtimeConfig) {
- // An explicitly requested settings file must load successfully —
- // silently falling back to the default config would run the
- // command without the restrictions the caller asked for.
- if (options.settings) {
- console.error(
- `Error: Could not load settings from ${configPath} (missing, unreadable, or invalid). ` +
- "Refusing to run with the default config.",
- );
- process.exit(1);
- }
- logForDebugging(
- `No config found at ${configPath}, using default config`,
- );
- runtimeConfig = getDefaultConfig();
- }
- // Windows: srtWin.path is required (no ambient vendor
- // fallback). When the user's config doesn't set it, this
- // CLI opts into the packaged exe explicitly — that's our
- // code making the choice, not a library default.
- if (
- process.platform === "win32" &&
- runtimeConfig.windows?.srtWin?.path === undefined
- ) {
- const { VENDORED_SRT_WIN_EXE } = await import(
- "./sandbox/windows-sandbox-utils.js"
- );
- runtimeConfig = {
- ...runtimeConfig,
- windows: {
- ...runtimeConfig.windows,
- srtWin: { path: VENDORED_SRT_WIN_EXE },
- },
- };
- }
- // Initialize sandbox with config
- logForDebugging("Initializing sandbox...");
- await SandboxManager.initialize(runtimeConfig);
- // Set up control fd for dynamic config updates if specified
- let controlReader: readline.Interface | null = null;
- if (options.controlFd !== undefined) {
- try {
- const controlStream = fs.createReadStream("", {
- fd: options.controlFd,
- });
- controlReader = readline.createInterface({
- input: controlStream,
- crlfDelay: Infinity,
- });
- controlReader.on("line", (line) => {
- const newConfig = loadConfigFromString(line);
- if (newConfig) {
- logForDebugging(
- `Config updated from control fd: ${JSON.stringify(newConfig)}`,
- );
- SandboxManager.updateConfig(newConfig);
- } else if (line.trim()) {
- // Only log non-empty lines that failed to parse
- logForDebugging(
- `Invalid config on control fd (ignored): ${line}`,
- );
- }
- });
- controlReader.on("error", (err) => {
- logForDebugging(`Control fd error: ${err.message}`);
- });
- logForDebugging(
- `Listening for config updates on fd ${options.controlFd}`,
- );
- } catch (err) {
- logForDebugging(
- `Failed to open control fd ${options.controlFd}: ${err instanceof Error ? err.message : String(err)}`,
- );
- }
- }
- // Cleanup control reader on exit
- process.on("exit", () => {
- controlReader?.close();
- });
- // Determine command string based on mode
- let command: string;
- if (options.c) {
- // -c mode: use command string directly, no escaping
- command = options.c;
- logForDebugging(`Command string mode (-c): ${command}`);
- } else if (commandArgs.length > 0) {
- // Default mode: argv-style invocation. The result is later
- // executed via `bash -c <command>`, so each arg must be
- // shell-quoted to survive that re-parse — a plain join(' ')
- // splits arguments containing whitespace (#157).
- command = quote(commandArgs);
- logForDebugging(`Original command: ${command}`);
- } else {
- console.error(
- "Error: No command specified. Use -c <command> or provide command arguments.",
- );
- process.exit(1);
- }
- logForDebugging(
- JSON.stringify(
- SandboxManager.getNetworkRestrictionConfig(),
- null,
- 2,
- ),
- );
- // Wrap the command with sandbox restrictions. On Windows
- // the wrapper returns an argv array that MUST be spawned
- // with {shell:false} — that's the boundary keeping the
- // command bytes off the host shell. On other platforms
- // we keep the existing shell-string path.
- let child;
- if (process.platform === "win32") {
- // env carries the proxy vars the sandboxed child must inherit.
- const { argv, env } =
- await SandboxManager.wrapWithSandboxArgv(command);
- child = spawn(argv[0], argv.slice(1), {
- shell: false,
- stdio: "inherit",
- env,
- });
- } else {
- const sandboxedCommand =
- await SandboxManager.wrapWithSandbox(command);
- child = spawn(sandboxedCommand, {
- shell: true,
- stdio: "inherit",
- });
- }
- // Handle process exit
- child.on("exit", (code, signal) => {
- // Clean up bwrap mount point artifacts before exiting.
- // On Linux, bwrap creates empty files on the host when protecting
- // non-existent deny paths. This removes them.
- SandboxManager.cleanupAfterCommand();
- if (signal) {
- if (signal === "SIGINT" || signal === "SIGTERM") {
- process.exit(0);
- } else {
- console.error(`Process killed by signal: ${signal}`);
- process.exit(1);
- }
- }
- process.exit(code ?? 0);
- });
- child.on("error", (error) => {
- console.error(`Failed to execute command: ${error.message}`);
- process.exit(1);
- });
- // Handle cleanup on interrupt
- process.on("SIGINT", () => {
- child.kill("SIGINT");
- });
- process.on("SIGTERM", () => {
- child.kill("SIGTERM");
- });
- } catch (error) {
- console.error(
- `Error: ${error instanceof Error ? error.message : String(error)}`,
- );
- process.exit(1);
- }
- },
- );
- program.parse();
- }
- main().catch((error) => {
- console.error("Fatal error:", error);
- process.exit(1);
- });
|