mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 04:29:01 +01:00
## Thinking Path > - Paperclip preserves agent work across failed runs and server restarts. > - A remote workspace must have a durable save before its source can be released. > - Portable capture normally writes a Git mirror and a full archive inside the sandbox. > - A full sandbox can prevent capture before the provider's streaming download starts. > - Retrying that write cannot create the required free space. > - This change transfers a checked standalone source and seals its archive on the controller. > - The source remains retained unless the existing durable save completes. ## Linked Issues or Issue Description Related: #18644 reduced temporary capture space. #18172 added the provider's disk-free directory transfer. This repair connects portable capture to that transfer when the archive itself cannot be created. **What happened?** Workspace capture returns `remote_workspace_sandbox_disk_full` after provider work has finished. File recovery repeats the same sandbox write. **Expected behavior** Recover a stable standalone workspace without requiring another full archive on the source disk. Preserve staged changes, untracked work, confined links and the original source. **Steps to reproduce** 1. Create a standalone Git repository with a commit, staged changes and an untracked result. 2. Use the existing real-command transport fixture. Inject ENOSPC into the sandbox helper write or archive write. 3. Run portable capture. Before this change it rejects the helper failure. 4. With the change, restore the saved archive after removing the fixture source. Check the commit, staged content, untracked result and Git connectivity. ## What Changed - Attempt one controller-side capture after an exact sandbox disk-full failure. - Read a source inventory without installing a helper. Use the existing provider bulk transfer with confined links and runtime exclusions. - Compare the transferred inventory with source fingerprints before and after sealing. - Reject copied Git pointers, alternates and administrative symlinks. Parse config as data without includes; allow only portable keys before controller Git normalization. - Preserve socket exclusions. Retain the source and remove incomplete controller staging after failure. - Update execution semantics and focused regression coverage. No database migration or new execution authority. ## Verification - TDD red on base `2f35eb1080`: the new full-sandbox test failed with `remote_workspace_sandbox_disk_full` before production edits. - Green: 202 transport and archive tests passed before the additional Git configuration guards. At head `8541086cb5`, all 116 transport tests and the PostgreSQL durability acceptance pass. The final `e24e173ec9` exclusion guard passes those 116 cases plus its new Git-history regression across the full run and focused rerun. Environment: macOS arm64, Node 26.4.0; real Node/Git commands and filesystem transfer. - Review regressions were also red before their fixes: an internal Git config symlink bypassed the check, and `core.attributesFile` could select a controller path. Git administrative symlinks and nonportable config now fail before normalization. The focused safety selection passes 11 cases. - Cases cover helper and archive ENOSPC, commit/staged/untracked continuity after source removal, confined links, sockets, changed source, corrupt or extra copied files, unsafe Git metadata, retained diagnostic text and failed download. - Real PostgreSQL acceptance passes: normal workspace save recovers one sandbox ENOSPC, commits the encrypted snapshot, clears its writer, loses the original allocation, then independently reloads the saved result twice. The first acceptance attempt used an incorrect expected status (`available`); the documented state is `committed`. No production change was needed for that correction. - Review regressions for untracked Python/Node environments and excessive Git roots failed before their fixes. Dependency exclusions now match normal capture. Controller parsing starts only after whole-tree/repository budgets and layout checks; refspec/settings budgets and a shared two-minute verification/normalization deadline bound further work. The excessive-root test verifies zero Git subprocesses. - Real provider-boundary red/green: Daytona direct tar and capacity-fallback PTY transfers first dropped `._result.txt`. The new optional mapping contract preserves all user names and excludes only current transfer scratch. Kubernetes implements the same contract. Explicit caller exclusions and archive validation remain in force. - Standalone Linux qualification on Node 24.22.2/Debian bookworm: fresh shared/SDK build; Daytona and Kubernetes typecheck and build; Daytona file-sync/capacity/diagnostics 68/68 pass; Kubernetes file-sync 31/31 pass, including the actual `/proc` case. Qualification was refreshed again on the final `be6f7feacc` source tree: current shared/SDK rebuilt; all 815 staged source/config/manifest hashes match the delivered tree before and after testing. An initial container run as root bypassed a read-only file test; the same source passes as uid 1000. Installing zstd enabled eight initially skipped cases. - A further review regression proved `.git/pyvenv.cfg` could exclude Git history as a dependency. The inventory now handles `.git` before dependency filtering, matching normal capture. The full capture/restore test verifies the original commit and saved work. The first green attempt incorrectly compared command-result object identity; correcting the test to compare stdout passed with no production change. - Further red/green regressions cover read-only private-copy cleanup, a regular file replaced by a FIFO before open, case aliases of Git administrative metadata, and permission preservation under restrictive umask. Nonblocking no-follow file handles verify identity before and after reading. Cleanup changes only private controller directories. Provider extraction preserves ordinary permissions, refuses special mode bits before writing, and does not adopt source ownership, ACLs, attributes or flags. Final macOS Daytona tests pass 65/65; the final Linux qualification above was repeated on these exact provider sources. - Focused strict TypeScript compilation of the inventory and archive modules passes. - The first sandboxed archive run failed three Unix socket fixtures with EPERM. The permitted run outside that filesystem sandbox passed all tests. - Initial full local server typecheck could not resolve the unbuilt Runner package and produced cascading errors. Exact-head `e24e173ec9` CI typecheck, build, Runner and browser checks pass. That CI attempt failed two server shards: the unchanged retained-cleanup backlog test timed out at 15 seconds and passes locally unchanged; ten cache-reclaim fixture oracles failed because directory fallback consumed their sealed-archive counters/faults. Final commit `caed4e4f83` isolates the intended unavailable-directory boundary while preserving archive counts, recovery status, integrity and no-second-reclamation assertions. Those ten failures reproduced locally before the fixture edit. The full affected file passes 394/395; its sole other HTTP socket-hang-up case passes unchanged in an isolated rerun. This is combined coverage of all 395 cases, not a single complete green run. The final branch integrates master `53645a0104` at `98ea07225592930cbb0d18bcf250bc64f6bb13b4`, preserving upstream workspace phase timings. After integration: 117 transport cases, the PostgreSQL durability acceptance, the existing object-storage/timing regression, and 12 affected cache cases pass. The expanded recovery file now has 421 cases. Final-head `be6f7feacc45c1e32a26a2417e973f1e3752fd8d` CI passes all 55 applicable checks (three skipped, one neutral review), including the expanded recovery suite, typecheck, builds and Runner coverage. [CI result](https://github.com/paperclipai/paperclip/actions/runs/42251345554). - Provider exclusion regressions were red through four real Linux transfer paths: Daytona direct archive, direct safe-link repair, capacity PTY repair, and Kubernetes streaming. A root socket and nested regular file share a basename; literal glob-character files/subtrees also survive. Preserve mode now anchors tar exclusions as literal root-relative paths and escapes `find -path` pruning. Default callers keep glob semantics. Receipt-count regressions were also red before correcting preserved-output counting. All 31 Kubernetes and 68 Daytona tests, builds and typechecks pass on the final source with 815 matching inputs. - The final semantics documentation explicitly records special mode bits, the stricter config allowlist (including innocuous unlisted keys), and provider qualification limited to Daytona/Kubernetes; these address the optional current-head review observations without broadening controller Git authority. - No Daytona credential is available in this session. Provider transfer is a local filesystem boundary in these tests. Hosted task recovery and rollout remain unverified. ## Risks - Recovery reads the source repeatedly and transfers the standalone tree. It can take longer than normal capture. - Linked worktrees, initialized submodules, Git administrative symlinks, nonportable Git configuration, special mode bits and alternate object stores remain retained if the normal capture cannot finish. This fallback does not follow their pointers onto the controller. - A provider without usable directory export, a changed source, or insufficient controller space cannot produce a receipt. The original source stays retained. - The server, SDK and affected provider plugins must be built from this revision together. An older provider can fail inventory verification on names it omits; it cannot publish an incomplete save. No merge, publication or deployment is included. ## Model Used OpenAI GPT-6 family through Codex, with reasoning, code editing, shell tools and automated tests. The exact serving model ID and context window are not exposed in this session. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub references) - [x] My branch name describes the change and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
939 lines
43 KiB
TypeScript
939 lines
43 KiB
TypeScript
/**
|
||
* Native file-sync transport for the Kubernetes sandbox provider.
|
||
*
|
||
* Implements the opt-in `onEnvironmentSyncIn`/`onEnvironmentSyncOut` hooks over
|
||
* the pod exec data channel, so workspace/asset transfers stream through a
|
||
* SINGLE exec per operation instead of the default base64 chunk loop (which
|
||
* costs one exec per ~4 MB chunk — see `upload-interceptor.ts`).
|
||
*
|
||
* The transport is pure streaming: raw tar bytes move over the exec's stdin /
|
||
* stdout channels straight to and from a host file on disk, so neither the host
|
||
* nor the pod ever holds the whole payload in memory.
|
||
*
|
||
* - syncIn: build one host tarball of the operation's payload on disk, stream
|
||
* its raw bytes over the exec's stdin into `head -c <N> | tar -x`
|
||
* inside the pod (N is the exact archive size, known host-side, so
|
||
* the pod command self-terminates without depending on stdin EOF),
|
||
* then stage-then-`mv -f` each single file onto its target so an
|
||
* interrupted transfer never leaves a truncated file.
|
||
* - syncOut: run `tar -c … -f -` inside the pod over one exec, streaming the
|
||
* archive on stdout straight into a host file, then reassemble it.
|
||
*
|
||
* The exec primitive is injected as `PodStreamExec` so this module stays free of
|
||
* any `@kubernetes/client-node` coupling and is unit-testable against a
|
||
* shell-backed fake. `plugin.ts` binds it to `execInPodStreaming` for a resolved
|
||
* pod.
|
||
*
|
||
* Security model (carried from the seam's design review): every sandbox path is
|
||
* confined to the workspace remote dir both lexically on the host AND via an
|
||
* in-pod `realpath` check that pins the resolved directory inode through
|
||
* `/proc/self/fd` before any byte is written — closing the symlink-swap TOCTOU
|
||
* window. Secret files land at their requested `mode` with no world-readable
|
||
* window (the staging dir is created `0700` and each file is `chmod`ed before
|
||
* the rename). All interpolated paths are single-quoted. Sandbox-authored
|
||
* tarballs (outbound) are member-confined before host extraction.
|
||
*
|
||
* Memory + disk posture: because the archive streams to/from disk, a transfer of
|
||
* any size keeps host and pod memory flat — there is no whole-payload buffer and
|
||
* therefore no 100 MB in-memory cap (the old base64-buffered transport needed
|
||
* one). Inbound bytes are host-authored (the tar this module builds), so they
|
||
* need no untrusted-size bound. Outbound bytes are authored by the untrusted
|
||
* pod, which controls how much it emits, so the sink `Writable` is wrapped in a
|
||
* streamed-bytes guard that fails the transfer closed above `maxOutputBytes`
|
||
* before it can fill the host's disk; the pod's stderr is separately capped in
|
||
* `execInPodStreaming`.
|
||
*/
|
||
import path from "node:path";
|
||
import os from "node:os";
|
||
import { promises as fs, createReadStream, createWriteStream } from "node:fs";
|
||
import { Transform } from "node:stream";
|
||
import { finished } from "node:stream/promises";
|
||
import type { Readable, Writable } from "node:stream";
|
||
import { randomUUID } from "node:crypto";
|
||
import { execFile } from "node:child_process";
|
||
import { promisify } from "node:util";
|
||
import type {
|
||
PluginEnvironmentSyncResult,
|
||
PluginPostUploadCommand,
|
||
PluginSyncFileMapping,
|
||
PluginSyncOperation,
|
||
} from "@paperclipai/plugin-sdk";
|
||
|
||
const execFileAsync = promisify(execFile);
|
||
|
||
/**
|
||
* Injected streaming pod-exec primitive. `command` is always
|
||
* `["/bin/sh", "-c", <script>]`; `io.stdin` (when present) is streamed to the
|
||
* command's stdin over the exec data channel, and `io.stdout` (when present)
|
||
* receives the command's stdout. Returns the command's exit code plus its
|
||
* captured (bounded) stderr. `plugin.ts` binds this to
|
||
* `execInPodStreaming(kc, namespace, podName, "agent", …)`.
|
||
*/
|
||
export type PodStreamExec = (
|
||
command: string[],
|
||
io: {
|
||
stdin?: Readable;
|
||
stdout?: Writable;
|
||
timeoutMs?: number;
|
||
maxStderrBytes?: number;
|
||
},
|
||
) => Promise<{ exitCode: number; stderr: string }>;
|
||
|
||
// Reserved scratch-name stem for staged transfers and remote tarballs. The
|
||
// runtime's base64 fallback stages to `<path>.paperclip-upload`; the native
|
||
// transport reuses the same reserved prefix so a provider temp never collides
|
||
// with a real target or with the fallback's scratch name.
|
||
const SCRATCH_PREFIX = ".paperclip-upload";
|
||
|
||
// Fail-closed guard on the number of bytes an outbound (`syncOut`) transfer will
|
||
// stream to host disk. The archive is authored by the untrusted pod, which
|
||
// controls how many bytes it emits on stdout; streaming keeps memory flat but a
|
||
// hostile pod could still try to exhaust the worker's ephemeral disk. The
|
||
// transfer aborts the instant the pod exceeds this bound. It is deliberately
|
||
// generous (real workspace/asset syncs are far smaller) and overridable per
|
||
// call so operators can tune it to the worker's actual disk budget.
|
||
const MAX_SYNC_OUTPUT_BYTES = 8 * 1024 * 1024 * 1024;
|
||
|
||
// Bound on the pod's stderr for a sync exec. stderr carries only the in-pod
|
||
// script's fail-loud diagnostics, so a modest cap is ample; it exists solely so
|
||
// a pod that floods stderr cannot grow the host accumulator without limit.
|
||
const SYNC_STDERR_CAP_BYTES = 1024 * 1024;
|
||
|
||
function scratchName(suffix = ""): string {
|
||
return `${SCRATCH_PREFIX}-${randomUUID()}${suffix}`;
|
||
}
|
||
|
||
/**
|
||
* Single-quote a string for safe interpolation into a `sh -c` script (close,
|
||
* escape, reopen). Mirrors `pod-exec.ts`'s `shQuote`; duplicated here so this
|
||
* module carries no `@kubernetes/client-node` import and stays hermetically
|
||
* testable. Every path handed to an exec MUST pass through this.
|
||
*/
|
||
function shQuote(value: string): string {
|
||
return `'${value.replace(/'/g, "'\\''")}'`;
|
||
}
|
||
|
||
/**
|
||
* Host-side complete-mediation guard applied as defense-in-depth below the
|
||
* orchestrator's own confinement. Every sandbox-side path (the sync target for
|
||
* inbound, the sync source for outbound) MUST be an absolute path that
|
||
* canonicalizes lexically inside the workspace remote dir; absolute escapes and
|
||
* `..` traversal are rejected fail-closed before any bytes move. Sandbox paths
|
||
* are POSIX.
|
||
*/
|
||
export function assertConfinedSandboxPath(remoteDir: string, candidate: string, label: string): void {
|
||
const normalizedRoot = path.posix.normalize(remoteDir);
|
||
const normalized = path.posix.normalize(candidate);
|
||
if (
|
||
!path.posix.isAbsolute(normalized) ||
|
||
normalized === ".." ||
|
||
normalized.includes("/../") ||
|
||
normalized.endsWith("/..")
|
||
) {
|
||
throw new Error(`Kubernetes sync ${label} path is not a confined absolute path: ${candidate}`);
|
||
}
|
||
const prefix = normalizedRoot.endsWith("/") ? normalizedRoot : `${normalizedRoot}/`;
|
||
if (normalized !== normalizedRoot && !normalized.startsWith(prefix)) {
|
||
throw new Error(`Kubernetes sync ${label} path escapes the workspace remote dir: ${candidate}`);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* True when `relative` (a POSIX path) escapes its anchoring directory once
|
||
* normalized: an absolute path, `..`, or a `..`-leading traversal all break out.
|
||
*/
|
||
function posixPathEscapes(relative: string): boolean {
|
||
const normalized = path.posix.normalize(relative);
|
||
return normalized === ".." || normalized.startsWith("../") || path.posix.isAbsolute(normalized);
|
||
}
|
||
|
||
async function withHostTempDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-k8s-sync-"));
|
||
try {
|
||
return await fn(dir);
|
||
} finally {
|
||
await fs.rm(dir, { recursive: true, force: true }).catch(() => undefined);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* POSIX-sh preamble defining a `_pc_resolve` canonicalizer (prefer `realpath`,
|
||
* fall back to `readlink -f`; fail closed with exit 40 if neither exists so the
|
||
* host-side lexical check is never the only line of defense) and `_pc_root` =
|
||
* the resolved workspace remote dir. Shared by every in-pod symlink-escape guard.
|
||
*
|
||
* Also defines `_pc_confine_ancestor <path>`: it walks up to the DEEPEST already-
|
||
* existing ancestor of `<path>`, canonicalizes it, and confines it inside
|
||
* `_pc_root` (exit 42 on escape). `mkdir -p` follows an existing symlink ancestor,
|
||
* so creating a target directory before confining could materialize a dir OUTSIDE
|
||
* the root and only reject afterward — the escape has already mutated the host.
|
||
* Confining the closest existing prefix BEFORE `mkdir` closes that window: any
|
||
* component `mkdir -p` newly creates is a real directory, so only a pre-existing
|
||
* symlink ancestor can redirect the write, and that ancestor is exactly what this
|
||
* check canonicalizes and rejects.
|
||
*/
|
||
function canonicalizerPreamble(quotedRoot: string): string[] {
|
||
return [
|
||
'if command -v realpath >/dev/null 2>&1; then _pc_resolve() { realpath -- "$1"; };',
|
||
'elif command -v readlink >/dev/null 2>&1; then _pc_resolve() { readlink -f -- "$1"; };',
|
||
'else echo "no path canonicalizer available" >&2; exit 40; fi;',
|
||
`_pc_root=$(_pc_resolve ${quotedRoot}) || { echo "cannot resolve root" >&2; exit 41; };`,
|
||
'_pc_confine_ancestor() {',
|
||
// Confinement targets are always absolute (asserted host-side), so `dirname`
|
||
// needs no `--` end-of-options guard — and omitting it keeps the walk correct
|
||
// on minimal shells (e.g. BusyBox `dirname`, which does not parse `--`).
|
||
' _pc_a=$1;',
|
||
' while [ ! -e "$_pc_a" ] && [ ! -L "$_pc_a" ]; do',
|
||
' _pc_p=$(dirname "$_pc_a");',
|
||
' [ "$_pc_p" = "$_pc_a" ] && break;',
|
||
' _pc_a=$_pc_p;',
|
||
' done;',
|
||
' _pc_ar=$(_pc_resolve "$_pc_a") || return 42;',
|
||
' case "$_pc_ar/" in "$_pc_root"/*) return 0 ;; *) return 42 ;; esac;',
|
||
'};',
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Stream a host file's raw bytes into the pod command's stdin over one exec and
|
||
* assert it exited 0; otherwise throw with the captured stderr so a failed
|
||
* transfer surfaces fail-loud to the orchestrator (never a silent partial
|
||
* success). The pod command bounds its own read (`head -c <N>`), so stdin EOF is
|
||
* never load-bearing.
|
||
*/
|
||
async function streamFileToPodStdin(input: {
|
||
exec: PodStreamExec;
|
||
script: string;
|
||
filePath: string;
|
||
timeoutMs: number;
|
||
label: string;
|
||
}): Promise<void> {
|
||
const source = createReadStream(input.filePath);
|
||
try {
|
||
const result = await input.exec(["/bin/sh", "-c", input.script], {
|
||
stdin: source,
|
||
timeoutMs: input.timeoutMs,
|
||
maxStderrBytes: SYNC_STDERR_CAP_BYTES,
|
||
});
|
||
if (result.exitCode !== 0) {
|
||
const detail = (result.stderr || "").trim();
|
||
throw new Error(
|
||
`Kubernetes ${input.label} failed (exit ${result.exitCode})${detail ? `: ${detail}` : ""}`,
|
||
);
|
||
}
|
||
} finally {
|
||
source.destroy();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Run the pod command over one exec and stream its stdout straight into a host
|
||
* file, guarding the byte count against `maxOutputBytes` so an untrusted pod
|
||
* cannot fill the host disk. Resolves only after the file is fully flushed, so a
|
||
* caller that reads it back always sees the complete archive; throws fail-loud on
|
||
* a non-zero exit, a tripped disk guard, or a stream error, writing no file the
|
||
* caller then trusts.
|
||
*/
|
||
async function streamPodStdoutToFile(input: {
|
||
exec: PodStreamExec;
|
||
script: string;
|
||
filePath: string;
|
||
maxOutputBytes: number;
|
||
timeoutMs: number;
|
||
label: string;
|
||
}): Promise<void> {
|
||
const fileStream = createWriteStream(input.filePath);
|
||
let written = 0;
|
||
const guard = new Transform({
|
||
transform(chunk: Buffer, _enc, cb) {
|
||
written += chunk.length;
|
||
if (input.maxOutputBytes >= 0 && written > input.maxOutputBytes) {
|
||
cb(
|
||
new Error(
|
||
`Kubernetes ${input.label} payload exceeded the ${input.maxOutputBytes}-byte streamed-output disk guard; the sandbox emitted more bytes than allowed.`,
|
||
),
|
||
);
|
||
return;
|
||
}
|
||
cb(null, chunk);
|
||
},
|
||
});
|
||
guard.pipe(fileStream);
|
||
const flushed = finished(fileStream);
|
||
try {
|
||
const result = await input.exec(["/bin/sh", "-c", input.script], {
|
||
stdout: guard,
|
||
timeoutMs: input.timeoutMs,
|
||
maxStderrBytes: SYNC_STDERR_CAP_BYTES,
|
||
});
|
||
await flushed;
|
||
if (result.exitCode !== 0) {
|
||
const detail = (result.stderr || "").trim();
|
||
throw new Error(
|
||
`Kubernetes ${input.label} failed (exit ${result.exitCode})${detail ? `: ${detail}` : ""}`,
|
||
);
|
||
}
|
||
} catch (err) {
|
||
// Tear the sink down so a tripped guard / rejected exec never leaves the
|
||
// file stream open (and its `finished` promise dangling as unhandled).
|
||
guard.destroy();
|
||
fileStream.destroy();
|
||
await flushed.catch(() => undefined);
|
||
throw err;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Build a host-side tarball, mirroring the runtime's own `createTarballFromDirectory`:
|
||
* archive the named top-level entries (no "." self entry), suppress xattr
|
||
* sidecars, honor `exclude`, and reproduce the `followSymlinks` → `-h` mapping so
|
||
* the native path is observationally identical to the base64 fallback's tar. An
|
||
* empty entry set writes a valid empty tar (1024-byte zero EOF marker).
|
||
*/
|
||
async function createHostTarball(input: {
|
||
localDir: string;
|
||
archivePath: string;
|
||
exclude?: string[];
|
||
followSymlinks?: boolean;
|
||
entries?: string[];
|
||
}): Promise<void> {
|
||
const excludeArgs = ["._*", ...(input.exclude ?? [])].flatMap((entry) => ["--exclude", entry]);
|
||
const entries =
|
||
input.entries ??
|
||
(await fs.readdir(input.localDir)).sort((left, right) => left.localeCompare(right));
|
||
if (entries.length === 0) {
|
||
await fs.writeFile(input.archivePath, Buffer.alloc(1024));
|
||
return;
|
||
}
|
||
await execFileAsync(
|
||
"tar",
|
||
[
|
||
"-c",
|
||
"--no-xattrs",
|
||
...(input.followSymlinks ? ["-h"] : []),
|
||
"-f",
|
||
input.archivePath,
|
||
"-C",
|
||
input.localDir,
|
||
...excludeArgs,
|
||
"--",
|
||
...entries,
|
||
],
|
||
{ env: { ...process.env, COPYFILE_DISABLE: "1" }, maxBuffer: 64 * 1024 * 1024 },
|
||
);
|
||
}
|
||
|
||
/**
|
||
* Parse one `tar -tvf` verbose listing line into its leading type flag and the
|
||
* trailing name-and-link-target field. The listing dialect depends on which tar
|
||
* the host ships: GNU/busybox emit
|
||
* `<perms> <owner>/<group> <size> <date> <time> <rest>`, while bsdtar
|
||
* (libarchive — the system tar on macOS) emits the ls-style
|
||
* `<perms> <links> <user> <group> <size> <Mon> <day> <time|year> <rest>`.
|
||
* The second field disambiguates: GNU always slash-joins owner/group, bsdtar
|
||
* puts a pure-digit link count there, so no line satisfies both shapes — the
|
||
* slash requirement is load-bearing, since a bsdtar line with numeric uid/gid
|
||
* would otherwise match the GNU shape shifted, hiding traversal in `<rest>`.
|
||
* Entries whose size column is not a plain byte count (e.g. a device node's
|
||
* `major,minor`) match neither shape. Returns null when nothing matches so
|
||
* callers can fail closed.
|
||
*/
|
||
export function parseTarVerboseListingLine(line: string): { typeFlag: string; rest: string } | null {
|
||
const gnu = line.match(/^(\S+)\s+\S+\/\S+\s+\d+\s+\S+\s+\S+\s+(.*)$/);
|
||
if (gnu) return { typeFlag: gnu[1][0], rest: gnu[2] };
|
||
const bsd = line.match(/^(\S+)\s+\d+\s+\S+\s+\S+\s+\d+\s+\S+\s+\d{1,2}\s+(?:\d{4}|\d{1,2}:\d{2}(?::\d{2})?)\s+(.*)$/);
|
||
if (bsd) return { typeFlag: bsd[1][0], rest: bsd[2] };
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* Split a verbose-listing link field (`<name><delimiter><target>`) exactly
|
||
* once. The sandbox controls both halves, so a field with zero or multiple
|
||
* delimiter occurrences is unresolvable: a link name that itself contains the
|
||
* delimiter shifts the split point, and taking the first (or last) occurrence
|
||
* would let a crafted name or target hide an escaping link target from the
|
||
* confinement check. Returns null so callers fail closed.
|
||
*/
|
||
export function splitLinkEntryOnce(field: string, delimiter: string): { name: string; target: string } | null {
|
||
const first = field.indexOf(delimiter);
|
||
if (first === -1) return null;
|
||
if (field.indexOf(delimiter, first + delimiter.length) !== -1) return null;
|
||
return { name: field.slice(0, first), target: field.slice(first + delimiter.length) };
|
||
}
|
||
|
||
/**
|
||
* Reject a sandbox-authored tarball before extraction if any member would land
|
||
* outside the extraction dir. The archive is produced by the (untrusted) sandbox,
|
||
* so host-side `tar -xf` must never be handed an archive whose entries carry
|
||
* absolute paths or `../` traversal, nor a symlink/hardlink member whose target
|
||
* escapes the tree. Legitimate in-tree relative links are preserved. Parses the
|
||
* `-tvf` verbose listing; any unparseable line fails closed.
|
||
*/
|
||
async function assertTarballEntriesConfined(archivePath: string, rejectSpecialModes = false): Promise<void> {
|
||
// Validate the same raw `._*` members the preserving extractor will write.
|
||
const rawMacEntries = rejectSpecialModes && process.platform === "darwin"
|
||
&& (await execFileAsync("tar", ["--version"])).stdout.includes("bsdtar");
|
||
const { stdout } = await execFileAsync("tar", ["-tvf", archivePath, ...(rawMacEntries ? ["--options=!mac-ext"] : [])], {
|
||
env: { ...process.env, COPYFILE_DISABLE: "1" },
|
||
maxBuffer: 64 * 1024 * 1024,
|
||
});
|
||
const lines = stdout.split("\n").filter((line) => line.trim().length > 0);
|
||
for (const line of lines) {
|
||
const parsed = parseTarVerboseListingLine(line);
|
||
if (!parsed) {
|
||
throw new Error(`Kubernetes syncOut refusing tarball with an unparseable entry listing: ${line}`);
|
||
}
|
||
// Source preservation promises 0777 modes only. Refuse special execution
|
||
// bits before the host extractor can apply them, retaining every source file.
|
||
if (rejectSpecialModes && /[sStT]/.test(line.trimStart().slice(0, 10))) {
|
||
throw new Error("Kubernetes syncOut source preservation does not support special mode bits");
|
||
}
|
||
const typeFlag = parsed.typeFlag;
|
||
let name = parsed.rest;
|
||
let linkTarget: string | null = null;
|
||
if (typeFlag === "l") {
|
||
const split = splitLinkEntryOnce(name, " -> ");
|
||
if (!split) throw new Error(`Kubernetes syncOut refusing unparseable or ambiguous symlink entry: ${line}`);
|
||
name = split.name;
|
||
linkTarget = split.target;
|
||
} else if (typeFlag === "h") {
|
||
const split = splitLinkEntryOnce(name, " link to ");
|
||
if (!split) throw new Error(`Kubernetes syncOut refusing unparseable or ambiguous hardlink entry: ${line}`);
|
||
name = split.name;
|
||
linkTarget = split.target;
|
||
}
|
||
const cleanName = name.replace(/\/+$/, "");
|
||
if (cleanName.length > 0 && posixPathEscapes(cleanName)) {
|
||
throw new Error(`Kubernetes syncOut refusing tarball member that escapes the extraction dir: ${name}`);
|
||
}
|
||
if (linkTarget !== null) {
|
||
const resolved = path.posix.join(path.posix.dirname(cleanName), linkTarget);
|
||
if (path.posix.isAbsolute(linkTarget) || posixPathEscapes(resolved)) {
|
||
throw new Error(
|
||
`Kubernetes syncOut refusing tarball link whose target escapes the extraction dir: ${name} -> ${linkTarget}`,
|
||
);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
async function extractHostTarball(input: { archivePath: string; localDir: string; preserveSourceEntries?: boolean }): Promise<void> {
|
||
// The archive is sandbox-authored and untrusted: validate every member (and
|
||
// link target) is confined before letting host-side tar write a single byte.
|
||
await assertTarballEntriesConfined(input.archivePath, input.preserveSourceEntries);
|
||
await fs.mkdir(input.localDir, { recursive: true });
|
||
// macOS tar otherwise consumes source files named `._*` as AppleDouble
|
||
// metadata instead of writing their bytes. These are ordinary user entries.
|
||
// Preserve mode also ignores the host umask without adopting archive owners,
|
||
// ACLs, attributes or file flags from the sandbox.
|
||
const hostTarIsBsd = input.preserveSourceEntries
|
||
&& (await execFileAsync("tar", ["--version"])).stdout.includes("bsdtar");
|
||
await execFileAsync("tar", ["-xf", input.archivePath, "-C", input.localDir,
|
||
...(input.preserveSourceEntries ? ["-p", "--no-same-owner", "--no-acls", "--no-xattrs"] : []),
|
||
...(hostTarIsBsd ? ["--no-fflags"] : []),
|
||
...(hostTarIsBsd && process.platform === "darwin" ? ["--no-mac-metadata", "--options=!mac-ext"] : [])], {
|
||
env: { ...process.env, COPYFILE_DISABLE: "1" },
|
||
maxBuffer: 64 * 1024 * 1024,
|
||
});
|
||
}
|
||
|
||
async function countHostFiles(root: string, exclude?: string[]): Promise<number> {
|
||
const excludeSet = new Set(exclude ?? []);
|
||
let total = 0;
|
||
const walk = async (dir: string): Promise<void> => {
|
||
const entries = await fs.readdir(dir, { withFileTypes: true }).catch(() => []);
|
||
for (const entry of entries) {
|
||
if (excludeSet.has(entry.name)) continue;
|
||
const full = path.join(dir, entry.name);
|
||
if (entry.isDirectory()) {
|
||
await walk(full);
|
||
} else {
|
||
total += 1;
|
||
}
|
||
}
|
||
};
|
||
await walk(root).catch(() => undefined);
|
||
return total;
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Inbound (host → sandbox)
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* Transfer all `kind:"file"` mappings of an operation in ONE exec. The host tars
|
||
* the sources under stable index names on disk, streams the archive's raw bytes
|
||
* over the exec's stdin, and a single in-pod script reads exactly the archive's
|
||
* byte count (`head -c <N>`), extracts into a reserved `0700` staging dir,
|
||
* applies each requested mode BEFORE the rename (no widened window), then
|
||
* atomic-`mv -f`s each file onto its target through a `/proc/self/fd`-pinned
|
||
* parent directory so a symlink swap cannot redirect the write outside the root.
|
||
*/
|
||
async function syncInFileMappings(input: {
|
||
exec: PodStreamExec;
|
||
mappings: PluginSyncFileMapping[];
|
||
remoteDir: string;
|
||
timeoutMs: number;
|
||
}): Promise<{ filesTransferred: number; bytesTransferred: number }> {
|
||
const { exec, mappings, remoteDir, timeoutMs } = input;
|
||
if (mappings.length === 0) return { filesTransferred: 0, bytesTransferred: 0 };
|
||
|
||
for (const mapping of mappings) {
|
||
assertConfinedSandboxPath(remoteDir, mapping.targetPath, "target");
|
||
}
|
||
|
||
return withHostTempDir(async (tmp) => {
|
||
// Stage each source under a flat index name so one tar carries the whole
|
||
// operation; the index maps back to the mapping for mode + rename below.
|
||
const stage = path.join(tmp, "stage");
|
||
await fs.mkdir(stage);
|
||
let bytesTransferred = 0;
|
||
const indexNames: string[] = [];
|
||
for (let i = 0; i < mappings.length; i += 1) {
|
||
const name = String(i);
|
||
await fs.copyFile(mappings[i].sourcePath, path.join(stage, name));
|
||
bytesTransferred += (await fs.stat(mappings[i].sourcePath)).size;
|
||
indexNames.push(name);
|
||
}
|
||
|
||
const archivePath = path.join(tmp, "sync-in.tar");
|
||
await createHostTarball({ localDir: stage, archivePath, entries: indexNames });
|
||
// Exact archive size: the pod reads precisely this many bytes so its extract
|
||
// pipeline self-terminates without depending on stdin EOF delivery.
|
||
const archiveBytes = (await fs.stat(archivePath)).size;
|
||
|
||
// Reserved staging dir is a DIRECT child of the workspace root (created
|
||
// `0700`), so a secret is never world-readable and the closing `mv -f` is an
|
||
// atomic same-filesystem rename.
|
||
const remoteStage = path.posix.join(remoteDir, scratchName());
|
||
const parentDirs = [...new Set(mappings.map((m) => path.posix.dirname(m.targetPath)))];
|
||
|
||
const script: string[] = [...canonicalizerPreamble(shQuote(remoteDir))];
|
||
// Sweep the staging dir on ANY exit so a failed transfer leaves no scratch.
|
||
script.push(`trap 'rm -rf ${shQuote(remoteStage)}' EXIT;`);
|
||
script.push(`mkdir -p -m 700 ${shQuote(remoteStage)} || { echo "stage mkdir failed" >&2; exit 46; };`);
|
||
// Confine the deepest existing ancestor of each target parent BEFORE creating
|
||
// it: `mkdir -p` follows a sandbox-planted symlink ancestor and would
|
||
// otherwise materialize the directory OUTSIDE the root before any post-mkdir
|
||
// check could reject it (P0 escape). Confine-then-create closes that window.
|
||
for (const dir of parentDirs) {
|
||
script.push(
|
||
`_pc_confine_ancestor ${shQuote(dir)} || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`mkdir -p ${shQuote(dir)} || { echo "mkdir failed" >&2; exit 46; };`,
|
||
);
|
||
}
|
||
// Re-confine every (now-materialized) target parent dir through realpath before
|
||
// any bytes land: a sandbox-planted symlink parent is caught here too.
|
||
for (const dir of parentDirs) {
|
||
script.push(
|
||
`_pc_d=$(_pc_resolve ${shQuote(dir)}) || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`case "$_pc_d/" in "$_pc_root"/*) : ;; *) echo "ESCAPE" >&2; exit 42 ;; esac;`,
|
||
);
|
||
}
|
||
// Read exactly the archive's bytes off stdin and extract the whole payload
|
||
// into the staging dir in one pipeline.
|
||
script.push(
|
||
`head -c ${archiveBytes} | tar -xf - -C ${shQuote(remoteStage)} || { echo "extract failed" >&2; exit 43; };`,
|
||
);
|
||
// Promote each staged file onto its target: chmod the temp first (secret
|
||
// lands at its mode from the instant it exists at targetPath), then bind the
|
||
// parent-dir re-check and the rename into a `/proc/self/fd`-pinned open so an
|
||
// ancestor swap after the check cannot redirect the rename.
|
||
mappings.forEach((mapping, i) => {
|
||
const staged = path.posix.join(remoteStage, indexNames[i]);
|
||
const parentDir = path.posix.dirname(mapping.targetPath);
|
||
const base = path.posix.basename(mapping.targetPath);
|
||
if (typeof mapping.mode === "number") {
|
||
script.push(
|
||
`chmod ${(mapping.mode & 0o7777).toString(8).padStart(3, "0")} ${shQuote(staged)} || { echo "chmod failed" >&2; exit 48; };`,
|
||
);
|
||
}
|
||
script.push(
|
||
`_pc_tgt_dir=$(_pc_resolve ${shQuote(parentDir)}) || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`case "$_pc_tgt_dir/" in "$_pc_root"/*) : ;; *) echo "ESCAPE" >&2; exit 42 ;; esac;`,
|
||
`exec 8<"$_pc_tgt_dir" || { echo "open failed" >&2; exit 47; };`,
|
||
`_pc_fd_dir=$(_pc_resolve /proc/self/fd/8) || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`case "$_pc_fd_dir/" in "$_pc_root"/*) : ;; *) echo "ESCAPE" >&2; exit 42 ;; esac;`,
|
||
`mv -f ${shQuote(staged)} /proc/self/fd/8/${shQuote(base)} || { echo "rename failed" >&2; exit 44; };`,
|
||
`exec 8>&-;`,
|
||
);
|
||
});
|
||
|
||
await streamFileToPodStdin({
|
||
exec,
|
||
script: script.join("\n"),
|
||
filePath: archivePath,
|
||
timeoutMs,
|
||
label: "syncIn file transfer",
|
||
});
|
||
return { filesTransferred: mappings.length, bytesTransferred };
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Transfer one `kind:"directory"` mapping in ONE exec: the host tars the source
|
||
* on disk (reproducing `followSymlinks` → `-h` and `exclude`), streams its raw
|
||
* bytes over stdin, and the pod reads exactly that byte count and extracts into
|
||
* the target through a `/proc/self/fd`-pinned directory inode. Directory
|
||
* extraction is destroy-into (non-atomic), matching the base64 fallback's tar;
|
||
* only single files are atomic.
|
||
*/
|
||
async function syncInDirectoryMapping(input: {
|
||
exec: PodStreamExec;
|
||
mapping: PluginSyncFileMapping;
|
||
remoteDir: string;
|
||
timeoutMs: number;
|
||
}): Promise<{ filesTransferred: number; bytesTransferred: number }> {
|
||
const { exec, mapping, remoteDir, timeoutMs } = input;
|
||
assertConfinedSandboxPath(remoteDir, mapping.targetPath, "target");
|
||
return withHostTempDir(async (tmp) => {
|
||
const archivePath = path.join(tmp, "sync-in.tar");
|
||
await createHostTarball({
|
||
localDir: mapping.sourcePath,
|
||
archivePath,
|
||
exclude: mapping.exclude,
|
||
followSymlinks: mapping.followSymlinks,
|
||
});
|
||
const archiveBytes = (await fs.stat(archivePath)).size;
|
||
|
||
const target = mapping.targetPath;
|
||
const script = [
|
||
...canonicalizerPreamble(shQuote(remoteDir)),
|
||
// Confine the deepest existing ancestor before `mkdir -p` runs: it follows a
|
||
// sandbox-planted symlink ancestor and would otherwise create the target dir
|
||
// OUTSIDE the root before the post-mkdir realpath check below could reject it.
|
||
`_pc_confine_ancestor ${shQuote(target)} || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`mkdir -p ${shQuote(target)} || { echo "mkdir failed" >&2; exit 46; };`,
|
||
// Open-then-verify the extraction dir: `open()` walks every ancestor, so a
|
||
// post-resolve ancestor swap is caught by re-canonicalizing the pinned fd
|
||
// before extracting; `tar -C /proc/self/fd/9` then chdir's through the
|
||
// pinned inode, immune to a later path swap.
|
||
`_pc_real=$(_pc_resolve ${shQuote(target)}) || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`case "$_pc_real/" in "$_pc_root"/*) : ;; *) echo "ESCAPE" >&2; exit 42 ;; esac;`,
|
||
`exec 9<"$_pc_real" || { echo "open failed" >&2; exit 46; };`,
|
||
`_pc_fd_real=$(_pc_resolve /proc/self/fd/9) || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`case "$_pc_fd_real/" in "$_pc_root"/*) : ;; *) echo "ESCAPE" >&2; exit 42 ;; esac;`,
|
||
`head -c ${archiveBytes} | tar -xf - -C /proc/self/fd/9 || { echo "extract failed" >&2; exit 43; };`,
|
||
`exec 9>&-;`,
|
||
].join("\n");
|
||
|
||
await streamFileToPodStdin({
|
||
exec,
|
||
script,
|
||
filePath: archivePath,
|
||
timeoutMs,
|
||
label: "syncIn directory transfer",
|
||
});
|
||
const filesTransferred = await countHostFiles(mapping.sourcePath, mapping.exclude);
|
||
return { filesTransferred, bytesTransferred: archiveBytes };
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Execute an operation's ordered `postUploadCommands` in the pod AFTER its files
|
||
* have landed (Phase 3 / Security Conditions C1–C4 + C7). Kubernetes is a native
|
||
* provider, and once the inbound-staging router stops diverting command-bearing
|
||
* (custom-provision) assets away from the native seam, a K8s deployment receives
|
||
* these operations — so it EXECUTES them symmetrically with Daytona rather than
|
||
* silently dropping them (C7 fail-open).
|
||
*
|
||
* Commands run in array order, fail-fast: the first non-zero exit or timeout throws
|
||
* and stops the rest (C4). Each `command` is executed VERBATIM: it rides as a
|
||
* positional argument (`$1`) to a fixed wrapper, run by `sh -c "$1"`, and is never
|
||
* string-concatenated into the wrapper — so the provider adds no shell fragment of
|
||
* its own (C1/C3), exactly as the generic fallback runs `sh -c <command>`. Before
|
||
* the command runs, the wrapper re-confines the command `cwd` under the workspace
|
||
* remote dir with the same realpath + `/proc/self/fd`-pinned open used for file
|
||
* placement (C2): a `..`, absolute-escape, or symlink-escape `cwd` is rejected
|
||
* fail-closed (exit 42) before the command runs, and the pin makes the confinement
|
||
* race-free against a post-resolve ancestor swap. `cwd` is validated lexically on
|
||
* the host first; when absent it defaults to the remote dir — never a process
|
||
* default cwd.
|
||
*
|
||
* Shared by the file- and directory-mapping paths: it runs once per operation,
|
||
* after every mapping of that operation has been placed.
|
||
*/
|
||
async function runPostUploadCommands(input: {
|
||
exec: PodStreamExec;
|
||
commands: PluginPostUploadCommand[];
|
||
remoteDir: string;
|
||
timeoutMs: number;
|
||
}): Promise<void> {
|
||
const { exec, commands, remoteDir, timeoutMs } = input;
|
||
for (const command of commands) {
|
||
const cwd = command.cwd ?? remoteDir;
|
||
// C2 lexical guard on the host before the pod ever sees the cwd.
|
||
assertConfinedSandboxPath(remoteDir, cwd, "post-upload command cwd");
|
||
// The wrapper confines `cwd` ($1) through a realpath + /proc/self/fd pin, cd's
|
||
// into the pinned inode, then execs the VERBATIM command ($2). Both `cwd` and
|
||
// the command ride as positional parameters — the wrapper interpolates NEITHER
|
||
// into its own script text, so the command runs byte-for-byte as authored.
|
||
const wrapper = [
|
||
...canonicalizerPreamble(shQuote(remoteDir)),
|
||
`_pc_real=$(_pc_resolve "$1") || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`case "$_pc_real/" in "$_pc_root"/*) : ;; *) echo "ESCAPE" >&2; exit 42 ;; esac;`,
|
||
`exec 9<"$_pc_real" || { echo "open failed" >&2; exit 46; };`,
|
||
`_pc_fd_real=$(_pc_resolve /proc/self/fd/9) || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`case "$_pc_fd_real/" in "$_pc_root"/*) : ;; *) echo "ESCAPE" >&2; exit 42 ;; esac;`,
|
||
`cd /proc/self/fd/9 || { echo "cd failed" >&2; exit 46; };`,
|
||
`exec /bin/sh -c "$2" pc-post-upload;`,
|
||
].join("\n");
|
||
const commandTimeoutMs = command.timeoutMs ?? timeoutMs;
|
||
// Positional args: $0=pc-post-upload, $1=cwd, $2=the verbatim command string.
|
||
const result = await exec(["/bin/sh", "-c", wrapper, "pc-post-upload", cwd, command.command], {
|
||
timeoutMs: commandTimeoutMs,
|
||
maxStderrBytes: SYNC_STDERR_CAP_BYTES,
|
||
});
|
||
if (result.exitCode !== 0) {
|
||
const detail = (result.stderr || "").trim();
|
||
throw new Error(
|
||
`Kubernetes post-upload command failed (exit ${result.exitCode})${detail ? `: ${detail}` : ""}`,
|
||
);
|
||
}
|
||
}
|
||
}
|
||
|
||
export async function performSyncIn(input: {
|
||
exec: PodStreamExec;
|
||
operations: PluginSyncOperation[];
|
||
remoteDir: string;
|
||
timeoutMs: number;
|
||
}): Promise<PluginEnvironmentSyncResult> {
|
||
const operations: PluginEnvironmentSyncResult["operations"] = [];
|
||
for (const operation of input.operations) {
|
||
let filesTransferred = 0;
|
||
let bytesTransferred = 0;
|
||
|
||
const fileMappings = operation.files.filter((mapping) => mapping.kind === "file");
|
||
const directoryMappings = operation.files.filter((mapping) => mapping.kind === "directory");
|
||
|
||
const fileResult = await syncInFileMappings({
|
||
exec: input.exec,
|
||
mappings: fileMappings,
|
||
remoteDir: input.remoteDir,
|
||
timeoutMs: input.timeoutMs,
|
||
});
|
||
filesTransferred += fileResult.filesTransferred;
|
||
bytesTransferred += fileResult.bytesTransferred;
|
||
|
||
for (const mapping of directoryMappings) {
|
||
const dirResult = await syncInDirectoryMapping({
|
||
exec: input.exec,
|
||
mapping,
|
||
remoteDir: input.remoteDir,
|
||
timeoutMs: input.timeoutMs,
|
||
});
|
||
filesTransferred += dirResult.filesTransferred;
|
||
bytesTransferred += dirResult.bytesTransferred;
|
||
}
|
||
|
||
// Run the operation's ordered post-upload commands AFTER every file/directory
|
||
// mapping of this operation has landed (Phase 3 / C1–C4 + C7). Absent/empty →
|
||
// no extra exec, byte-identical to a pre-contract operation.
|
||
await runPostUploadCommands({
|
||
exec: input.exec,
|
||
commands: operation.postUploadCommands ?? [],
|
||
remoteDir: input.remoteDir,
|
||
timeoutMs: input.timeoutMs,
|
||
});
|
||
|
||
operations.push({ operationId: operation.operationId, filesTransferred, bytesTransferred });
|
||
}
|
||
return { operations };
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Outbound (sandbox → host)
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* Stream all `kind:"file"` mappings of an operation back in ONE exec. The in-pod
|
||
* script validates + snapshots each source (confinement realpath check, then a
|
||
* non-symlink regular-file re-check immediately before `cp` closes the
|
||
* validation→copy window), tars the immutable snapshots under index names, and
|
||
* streams the archive on stdout straight into a host file. The host decodes,
|
||
* extracts to a temp, and atomic-renames each file onto its host target
|
||
* (chmod-before-rename for secret modes) so an interrupted reassembly never
|
||
* truncates a target.
|
||
*/
|
||
async function syncOutFileMappings(input: {
|
||
exec: PodStreamExec;
|
||
mappings: PluginSyncFileMapping[];
|
||
remoteDir: string;
|
||
timeoutMs: number;
|
||
maxOutputBytes: number;
|
||
}): Promise<{ filesTransferred: number; bytesTransferred: number }> {
|
||
const { exec, mappings, remoteDir, timeoutMs, maxOutputBytes } = input;
|
||
if (mappings.length === 0) return { filesTransferred: 0, bytesTransferred: 0 };
|
||
|
||
for (const mapping of mappings) {
|
||
assertConfinedSandboxPath(remoteDir, mapping.sourcePath, "source");
|
||
}
|
||
|
||
return withHostTempDir(async (tmp) => {
|
||
const remoteStage = path.posix.join(remoteDir, scratchName());
|
||
const indexNames = mappings.map((_, i) => String(i));
|
||
|
||
const script: string[] = [...canonicalizerPreamble(shQuote(remoteDir))];
|
||
script.push(`trap 'rm -rf ${shQuote(remoteStage)}' EXIT;`);
|
||
script.push(`mkdir -p -m 700 ${shQuote(remoteStage)} || { echo "stage mkdir failed" >&2; exit 46; };`);
|
||
mappings.forEach((mapping, i) => {
|
||
const snapshot = path.posix.join(remoteStage, indexNames[i]);
|
||
script.push(
|
||
`_pc_real=$(_pc_resolve ${shQuote(mapping.sourcePath)}) || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`case "$_pc_real/" in "$_pc_root"/*) : ;; *) echo "ESCAPE" >&2; exit 42 ;; esac;`,
|
||
// Close the validation→copy TOCTOU by pinning the file to an FD and
|
||
// snapshotting THROUGH it, never re-opening by name: `open()` captures the
|
||
// inode, so a source replacement (symlink swap) after this point cannot
|
||
// redirect the copy. Re-resolving the pinned fd re-confines the TRUE inode
|
||
// we opened — if the sandbox swapped `$_pc_real` for a symlink between the
|
||
// resolve above and this open, the fd now points at that target and the
|
||
// re-confinement (or the regular-file check) rejects it before any byte is
|
||
// copied. `cp` then reads the fd's inode, immune to any further swap.
|
||
`exec 7<"$_pc_real" || { echo "REPLACED" >&2; exit 45; };`,
|
||
`_pc_fd_real=$(_pc_resolve /proc/self/fd/7) || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`case "$_pc_fd_real/" in "$_pc_root"/*) : ;; *) echo "ESCAPE" >&2; exit 42 ;; esac;`,
|
||
`[ -f /proc/self/fd/7 ] || { echo "NOTREG" >&2; exit 45; };`,
|
||
`cp -- /proc/self/fd/7 ${shQuote(snapshot)} || { echo "snapshot copy failed" >&2; exit 43; };`,
|
||
`exec 7>&-;`,
|
||
);
|
||
});
|
||
// Tar the immutable snapshots straight to stdout (streamed to a host file).
|
||
script.push(
|
||
`tar -c --no-xattrs -f - -C ${shQuote(remoteStage)} -- ${indexNames
|
||
.map(shQuote)
|
||
.join(" ")} || { echo "tar failed" >&2; exit 43; };`,
|
||
);
|
||
|
||
const localTar = path.join(tmp, "sync-out.tar");
|
||
await streamPodStdoutToFile({
|
||
exec,
|
||
script: script.join("\n"),
|
||
filePath: localTar,
|
||
maxOutputBytes,
|
||
timeoutMs,
|
||
label: "syncOut file transfer",
|
||
});
|
||
|
||
// Reassemble on the host: member-confine the sandbox-authored archive,
|
||
// extract to a temp, then atomic-rename each index file onto its host target.
|
||
const extractDir = path.join(tmp, "extract");
|
||
await extractHostTarball({ archivePath: localTar, localDir: extractDir });
|
||
|
||
let bytesTransferred = 0;
|
||
for (let i = 0; i < mappings.length; i += 1) {
|
||
const mapping = mappings[i];
|
||
const staged = path.join(extractDir, indexNames[i]);
|
||
await fs.mkdir(path.dirname(mapping.targetPath), { recursive: true });
|
||
if (typeof mapping.mode === "number") {
|
||
await fs.chmod(staged, mapping.mode);
|
||
}
|
||
bytesTransferred += (await fs.stat(staged)).size;
|
||
await fs.rename(staged, mapping.targetPath);
|
||
}
|
||
return { filesTransferred: mappings.length, bytesTransferred };
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Stream one `kind:"directory"` mapping back in ONE exec: the in-pod script
|
||
* confines the source (realpath through `/proc/self/fd`), tars it (reproducing
|
||
* `followSymlinks` → `-h` and `exclude`, naming top-level entries so no "." self
|
||
* entry is embedded) straight to stdout, and the host streams that into a file,
|
||
* member-confines the sandbox-authored tar, and extracts into the target dir.
|
||
*/
|
||
async function syncOutDirectoryMapping(input: {
|
||
exec: PodStreamExec;
|
||
mapping: PluginSyncFileMapping;
|
||
remoteDir: string;
|
||
timeoutMs: number;
|
||
maxOutputBytes: number;
|
||
}): Promise<{ filesTransferred: number; bytesTransferred: number }> {
|
||
const { exec, mapping, remoteDir, timeoutMs, maxOutputBytes } = input;
|
||
assertConfinedSandboxPath(remoteDir, mapping.sourcePath, "source");
|
||
return withHostTempDir(async (tmp) => {
|
||
// Preservation exclusions name literal source-relative paths. GNU tar's
|
||
// default basename/glob matching could silently omit unrelated user files.
|
||
const excludes = [...(mapping.preserveSourceEntries ? [] : ["._*"]), ...(mapping.exclude ?? [])];
|
||
const excludeFlags = (mapping.preserveSourceEntries ? "--anchored --no-wildcards " : "") +
|
||
(mapping.preserveSourceEntries
|
||
? [...new Set(excludes.map(entry => entry.replace(/^(?:\.\/)+/, "").replace(/\/+$/, "")))].flatMap(entry => [entry, `./${entry}`])
|
||
: excludes).map(entry => `--exclude ${shQuote(entry)}`).join(" ");
|
||
const script = [
|
||
...canonicalizerPreamble(shQuote(remoteDir)),
|
||
// Confine the source dir through an open-then-verify pinned fd before taring.
|
||
`_pc_real=$(_pc_resolve ${shQuote(mapping.sourcePath)}) || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`case "$_pc_real/" in "$_pc_root"/*) : ;; *) echo "ESCAPE" >&2; exit 42 ;; esac;`,
|
||
`exec 9<"$_pc_real" || { echo "open failed" >&2; exit 46; };`,
|
||
`_pc_fd_real=$(_pc_resolve /proc/self/fd/9) || { echo "ESCAPE" >&2; exit 42; };`,
|
||
`case "$_pc_fd_real/" in "$_pc_root"/*) : ;; *) echo "ESCAPE" >&2; exit 42 ;; esac;`,
|
||
`cd /proc/self/fd/9 || { echo "cd failed" >&2; exit 46; };`,
|
||
// Collect top-level entries (incl. dotfiles) without a "." self-entry, then
|
||
// tar with the `followSymlinks` → `-h` mapping, streaming to stdout.
|
||
"set -- *",
|
||
'if [ "$#" -eq 1 ] && [ "$1" = "*" ] && [ ! -e "$1" ] && [ ! -L "$1" ]; then set --; fi',
|
||
'for entry in .[!.]* ..?*; do [ -e "$entry" ] || [ -L "$entry" ] || continue; set -- "$@" "$entry"; done',
|
||
`if [ "$#" -eq 0 ]; then dd if=/dev/zero bs=1024 count=1 2>/dev/null; ` +
|
||
`else tar -c --no-xattrs ${mapping.followSymlinks ? "-h " : ""}${excludeFlags} -f - -- "$@" || { echo "tar failed" >&2; exit 43; }; fi`,
|
||
`exec 9>&-;`,
|
||
].join("\n");
|
||
|
||
const localTar = path.join(tmp, "sync-out.tar");
|
||
await streamPodStdoutToFile({
|
||
exec,
|
||
script,
|
||
filePath: localTar,
|
||
maxOutputBytes,
|
||
timeoutMs,
|
||
label: "syncOut directory transfer",
|
||
});
|
||
const bytesTransferred = (await fs.stat(localTar)).size;
|
||
await extractHostTarball({ archivePath: localTar, localDir: mapping.targetPath, preserveSourceEntries: mapping.preserveSourceEntries });
|
||
const filesTransferred = await countHostFiles(mapping.targetPath, mapping.preserveSourceEntries ? undefined : mapping.exclude);
|
||
return { filesTransferred, bytesTransferred };
|
||
});
|
||
}
|
||
|
||
export async function performSyncOut(input: {
|
||
exec: PodStreamExec;
|
||
operations: PluginSyncOperation[];
|
||
remoteDir: string;
|
||
timeoutMs: number;
|
||
maxOutputBytes?: number;
|
||
}): Promise<PluginEnvironmentSyncResult> {
|
||
const maxOutputBytes = input.maxOutputBytes ?? MAX_SYNC_OUTPUT_BYTES;
|
||
const operations: PluginEnvironmentSyncResult["operations"] = [];
|
||
for (const operation of input.operations) {
|
||
let filesTransferred = 0;
|
||
let bytesTransferred = 0;
|
||
|
||
const fileMappings = operation.files.filter((mapping) => mapping.kind === "file");
|
||
const directoryMappings = operation.files.filter((mapping) => mapping.kind === "directory");
|
||
|
||
const fileResult = await syncOutFileMappings({
|
||
exec: input.exec,
|
||
mappings: fileMappings,
|
||
remoteDir: input.remoteDir,
|
||
timeoutMs: input.timeoutMs,
|
||
maxOutputBytes,
|
||
});
|
||
filesTransferred += fileResult.filesTransferred;
|
||
bytesTransferred += fileResult.bytesTransferred;
|
||
|
||
for (const mapping of directoryMappings) {
|
||
const dirResult = await syncOutDirectoryMapping({
|
||
exec: input.exec,
|
||
mapping,
|
||
remoteDir: input.remoteDir,
|
||
timeoutMs: input.timeoutMs,
|
||
maxOutputBytes,
|
||
});
|
||
filesTransferred += dirResult.filesTransferred;
|
||
bytesTransferred += dirResult.bytesTransferred;
|
||
}
|
||
|
||
operations.push({ operationId: operation.operationId, filesTransferred, bytesTransferred });
|
||
}
|
||
return { operations };
|
||
}
|