Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/container-runtime.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { resolveDockerRuntime, runtimeNeedsStaticDns, runtimeUsesComposeAgent, runtimeUsesIptables, isGvisorRuntime } from './container-runtime';
import { sanitizeEnvForSbx } from './sbx-manager';
import { testHelpers } from './sbx-manager';

const { sanitizeEnvForSbx } = testHelpers;

describe('container-runtime', () => {
describe('resolveDockerRuntime', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/sbx-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {
execInSandbox,
isSbxAvailable,
removeSandbox,
restoreHomeCredentials,
sanitizeEnvForSbx,
withLocalBinOnPath,
SBX_DEFAULT_NAME,
testHelpers,
} from './sbx-manager';
import * as fs from 'fs';
import { mockExecaFn } from './test-helpers/mock-execa.test-utils';
import { logger } from './logger';

const { restoreHomeCredentials, sanitizeEnvForSbx, withLocalBinOnPath } = testHelpers;

// eslint-disable-next-line @typescript-eslint/no-require-imports
jest.mock('execa', () => require('./test-helpers/mock-execa.test-utils').execaMockFactory());
// eslint-disable-next-line @typescript-eslint/no-require-imports
Expand Down
54 changes: 29 additions & 25 deletions src/sbx-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,12 @@ const SECRET_ENV_PATTERNS = [
/** Default sandbox name (single-sandbox-per-run model). */
export const SBX_DEFAULT_NAME = `${SBX_NAME_PREFIX}-${process.pid}`;

export interface SbxConfig {
/** Sandbox name (defaults to `awf-agent-<pid>`). */
name?: string;
/** Workspace directory to mount into the sandbox. */
workspaceDir: string;
/** Squid proxy IP for DOCKER_SANDBOXES_PROXY. */
squidIp: string;
/** Squid proxy port (default 3128). */
squidPort?: number;
/** Additional workspace mounts (read-only paths). */
extraMounts?: string[];
}

export interface SbxExecOptions {
timeoutMinutes?: number;
workDir?: string;
environment?: Record<string, string>;
tty?: boolean;
}

/**
* Strips secret-bearing env vars from process.env so they never reach
* the sbx CLI or the sandbox interior. Returns a shallow copy with
* only non-secret entries plus any explicit overrides.
*/
export function sanitizeEnvForSbx(
function sanitizeEnvForSbx(
overrides: Record<string, string> = {},
): Record<string, string | undefined> {
const clean: Record<string, string | undefined> = {};
Expand Down Expand Up @@ -157,7 +137,7 @@ function scrubHomeCredentials(homePath: string): void {
* MUST run only after the sandbox is removed, because the home dirs are live
* mounts — restoring while the VM is running would re-expose the secrets.
*/
export function restoreHomeCredentials(): void {
function restoreHomeCredentials(): void {
for (const { original, backup } of scrubbedCredentials) {
try {
if (fs.existsSync(backup)) {
Expand Down Expand Up @@ -186,7 +166,18 @@ export function restoreHomeCredentials(): void {
* Creates a Docker sbx sandbox with workspace mounts.
* Sets `DOCKER_SANDBOXES_PROXY` to chain all egress through AWF's Squid.
*/
export async function createSandbox(config: SbxConfig): Promise<string> {
export async function createSandbox(config: {
/** Sandbox name (defaults to `awf-agent-<pid>`). */
name?: string;
/** Workspace directory to mount into the sandbox. */
workspaceDir: string;
/** Squid proxy IP for DOCKER_SANDBOXES_PROXY. */
squidIp: string;
/** Squid proxy port (default 3128). */
squidPort?: number;
/** Additional workspace mounts (read-only paths). */
extraMounts?: string[];
}): Promise<string> {
const name = config.name || SBX_DEFAULT_NAME;
const squidPort = config.squidPort || 3128;
const proxyUrl = `http://${config.squidIp}:${squidPort}`;
Expand Down Expand Up @@ -356,18 +347,31 @@ export async function createSandbox(config: SbxConfig): Promise<string> {
* resolvable by name. `$HOME` resolves to the injected HOME (getRealUserHome),
* which matches the wholesale-mounted home tool dirs.
*/
export function withLocalBinOnPath(command: string): string {
function withLocalBinOnPath(command: string): string {
return `export PATH="$HOME/.local/bin\${PATH:+:$PATH}"; ${command}`;
}

/** @internal Exposed for unit tests only. */
// ts-prune-ignore-next
export const testHelpers = {
sanitizeEnvForSbx,
restoreHomeCredentials,
withLocalBinOnPath,
};

/**
* Executes a command inside the sandbox, streaming stdout/stderr.
* Returns the exit code of the command.
*/
export async function execInSandbox(
name: string,
command: string,
options?: SbxExecOptions,
options?: {
timeoutMinutes?: number;
workDir?: string;
environment?: Record<string, string>;
tty?: boolean;
},
): Promise<{ exitCode: number }> {
logger.info(`Executing in sandbox "${name}": ${command}`);

Expand Down
Loading