{"version":3,"file":"escape-DoNZLix8.mjs","names":[],"sources":["../src/batteries/sandbox/escape.ts"],"sourcesContent":["import { E_INVALID_SANDBOX_CONFIG } from './exceptions'\n\n/** Shells for which SRT's POSIX quote() and the `-c` invocation contract agree. */\nexport const POSIX_SHELLS = ['sh', 'bash', 'dash', 'zsh', 'ksh'] as const\n\n/** Binaries for which passing `--` terminates option parsing. */\n// Verified against the installed GNU/BSD implementations. In particular, ls is\n// intentionally absent: it has no portable `--` terminator, and find is not\n// treated as an argv-parser boundary by this table.\nexport const DOUBLE_DASH_BINARIES = ['cp', 'rg'] as const\n\n/**\n * Flags emitted by the sandbox's own ripgrep adapter.\n *\n * @remarks\n * EVERY flag the adapter actually emits must appear here, or the adapter rejects its own argv and the\n * capability fails for every invocation. `--json` is `searchContent`'s output format and is exactly such\n * a flag; omitting it made content search unusable while name search (which does not use it) worked.\n * The argv TERMINATOR `--` is deliberately absent: it is not a flag, and it is filtered out before\n * validation rather than admitted here, so this list stays a list of flags.\n */\nexport const ALLOWED_RIPGREP_FLAGS = [\n  '--files',\n  '--ignore-case',\n  '--fixed-strings',\n  '--hidden',\n  '--follow',\n  '--no-ignore',\n  '--glob',\n  '--iglob',\n  '--max-depth',\n  '--json',\n] as const\n\nconst allowedShellText = POSIX_SHELLS.join(', ')\n\n/** Validate the shell selected by the Node SRT adapter. */\nexport const validateBinShell = (value = '/bin/bash'): string => {\n  if (!value.startsWith('/')) {\n    throw new E_INVALID_SANDBOX_CONFIG([\n      `binShell ${JSON.stringify(value)} must be an absolute path; allowed shell basenames: ${allowedShellText}`,\n    ])\n  }\n  const basename = value.slice(value.lastIndexOf('/') + 1)\n  if (!(POSIX_SHELLS as readonly string[]).includes(basename)) {\n    throw new E_INVALID_SANDBOX_CONFIG([\n      `binShell ${JSON.stringify(value)} is not allowed; allowed shell basenames: ${allowedShellText}`,\n    ])\n  }\n  return value\n}\n\n/** Alias used by the SRT adapter at construction time. */\nexport const assertPosixBinShell = validateBinShell\n\n/** Validate a configurable binary path without checking whether the binary exists. */\nexport const validateAbsoluteBinaryPath = (\n  value: string | undefined,\n  optionName: string\n): string | undefined => {\n  if (value === undefined) return undefined\n  if (!value.startsWith('/')) {\n    throw new E_INVALID_SANDBOX_CONFIG([\n      `${optionName} ${JSON.stringify(value)} must be an absolute path`,\n    ])\n  }\n  if (value.includes('\\0')) {\n    throw new E_INVALID_SANDBOX_CONFIG([`${optionName} ${JSON.stringify(value)} contains NUL`])\n  }\n  return value\n}\n\n/** Reject an argv value which would otherwise be interpreted as an option. */\nexport const assertArgvValue = (value: string): string => {\n  if (value.startsWith('-')) {\n    throw new E_INVALID_SANDBOX_CONFIG([\n      `argv value ${JSON.stringify(value)} must not begin with '-'`,\n    ])\n  }\n  if (value.includes('\\0')) {\n    throw new E_INVALID_SANDBOX_CONFIG([`argv value ${JSON.stringify(value)} contains NUL`])\n  }\n  return value\n}\n\n/** Whether a binary in the table can receive the argv terminator. */\nexport const binaryHonorsDoubleDash = (binary: string): boolean => {\n  const basename = binary.slice(binary.lastIndexOf('/') + 1)\n  return (DOUBLE_DASH_BINARIES as readonly string[]).includes(basename)\n}\n\n/** Validate a flag emitted by a trusted adapter, not a model-supplied value. */\nexport const assertAllowedRipgrepFlag = (flag: string): string => {\n  const name = flag.includes('=') ? flag.slice(0, flag.indexOf('=')) : flag\n  if (!(ALLOWED_RIPGREP_FLAGS as readonly string[]).includes(name)) {\n    throw new E_INVALID_SANDBOX_CONFIG([\n      `unsupported rg flag ${JSON.stringify(flag)}; allowed flags: ${ALLOWED_RIPGREP_FLAGS.join(', ')}`,\n    ])\n  }\n  return flag\n}\n\n/**\n * Quote argv using SRT's maintained POSIX implementation.\n *\n * The import is deliberately lazy: SRT is an optional peer and is ESM-only.\n */\nexport const quoteShellArgs = async (args: readonly string[]): Promise<string> => {\n  const nulIndex = args.findIndex((arg) => arg.includes('\\0'))\n  if (nulIndex >= 0) {\n    throw new E_INVALID_SANDBOX_CONFIG([\n      `argv value at index ${nulIndex} contains NUL (reason: nul)`,\n    ])\n  }\n  const moduleName = '@anthropic-ai/sandbox-runtime/dist/utils/shell-quote.js'\n  const module = (await import(moduleName)) as { quote: (items: string[]) => string }\n  return module.quote([...args])\n}\n\n/** Quote one command string as one shell argument. */\nexport const quoteShellValue = async (value: string): Promise<string> => quoteShellArgs([value])\n\n/** Compatibility alias for consumers that describe this operation as escaping. */\nexport const escapeShellArgs = quoteShellArgs\n/** Compatibility alias for consumers that describe this operation as escaping. */\nexport const escapeShellValue = quoteShellValue\n"],"mappings":";;;AAGA,IAAa,eAAe;CAAC;CAAM;CAAQ;CAAQ;CAAO;AAAK;;AAM/D,IAAa,uBAAuB,CAAC,MAAM,IAAI;;;;;;;;;;;AAY/C,IAAa,wBAAwB;CACnC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,IAAM,mBAAmB,aAAa,KAAK,IAAI;;AAG/C,IAAa,oBAAoB,QAAQ,gBAAwB;CAC/D,IAAI,CAAC,MAAM,WAAW,GAAG,GACvB,MAAM,IAAI,yBAAyB,CACjC,YAAY,KAAK,UAAU,KAAK,EAAE,sDAAsD,kBAC1F,CAAC;CAEH,MAAM,WAAW,MAAM,MAAM,MAAM,YAAY,GAAG,IAAI,CAAC;CACvD,IAAI,CAAE,aAAmC,SAAS,QAAQ,GACxD,MAAM,IAAI,yBAAyB,CACjC,YAAY,KAAK,UAAU,KAAK,EAAE,4CAA4C,kBAChF,CAAC;CAEH,OAAO;AACT;;AAGA,IAAa,sBAAsB;;AAGnC,IAAa,8BACX,OACA,eACuB;CACvB,IAAI,UAAU,KAAA,GAAW,OAAO,KAAA;CAChC,IAAI,CAAC,MAAM,WAAW,GAAG,GACvB,MAAM,IAAI,yBAAyB,CACjC,GAAG,WAAW,GAAG,KAAK,UAAU,KAAK,EAAE,0BACzC,CAAC;CAEH,IAAI,MAAM,SAAS,IAAI,GACrB,MAAM,IAAI,yBAAyB,CAAC,GAAG,WAAW,GAAG,KAAK,UAAU,KAAK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