@foldrun/core 23.4.0 → 24.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/blob-types.d.ts +6 -2
- package/dist/src/blob-types.js +17 -5
- package/dist/src/capture.d.ts +7 -0
- package/dist/src/capture.js +10 -1
- package/dist/src/data-import.d.ts +2 -1
- package/dist/src/data-import.js +7 -4
- package/dist/src/run-container.js +18 -3
- package/dist/src/run-output-refusal.js +2 -0
- package/dist/src/run-outputs.js +50 -29
- package/dist/src/runner.js +26 -3
- package/dist/src/safe-open.d.ts +65 -0
- package/dist/src/safe-open.js +160 -0
- package/dist/src/store.d.ts +1 -1
- package/dist/src/store.js +25 -3
- package/dist/src/workspace-revisions.js +29 -10
- package/package.json +1 -1
- package/src/blob-types.ts +15 -4
- package/src/capture.ts +11 -1
- package/src/data-import.ts +7 -4
- package/src/run-container.ts +16 -3
- package/src/run-output-refusal.ts +2 -0
- package/src/run-outputs.ts +50 -29
- package/src/runner.ts +27 -4
- package/src/safe-open.ts +164 -0
- package/src/store.ts +22 -4
- package/src/workspace-revisions.ts +27 -10
package/dist/src/blob-types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type PlainOpts } from "./safe-open.ts";
|
|
1
2
|
export declare class BlobNotFound extends Error {
|
|
2
3
|
readonly key: string;
|
|
3
4
|
constructor(key: string);
|
|
@@ -136,8 +137,11 @@ export declare function assertKey(key: string): string;
|
|
|
136
137
|
export declare function bufferSource(buf: Uint8Array): BlobSource;
|
|
137
138
|
/** A file on disk. The size is taken now; if the file is shorter (or longer)
|
|
138
139
|
* when it is read, the read throws BlobSourceError rather than send a body
|
|
139
|
-
* that no longer matches what was announced.
|
|
140
|
-
|
|
140
|
+
* that no longer matches what was announced. A link is never followed, and a
|
|
141
|
+
* different file (or a FIFO) swapped in since the size was taken is refused:
|
|
142
|
+
* each read opens the path without following links and must find the same
|
|
143
|
+
* inode (safe-open.ts, SYMRACE). */
|
|
144
|
+
export declare function fileSource(filePath: string, expect?: PlainOpts): BlobSource;
|
|
141
145
|
/** sha256 (and md5, which is what a single-request ETag is) of a range. */
|
|
142
146
|
export declare function hashRange(source: BlobSource, start: number, end: number, signal?: AbortSignal): Promise<{
|
|
143
147
|
sha256: string;
|
package/dist/src/blob-types.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// Split out of blob-store.ts so the s3 driver (s3-blob-store.ts) and the fs
|
|
3
3
|
// driver can both use the errors and the source type without importing each
|
|
4
4
|
// other.
|
|
5
|
-
import fs from "node:fs";
|
|
6
5
|
import crypto from "node:crypto";
|
|
6
|
+
import { plainStream, statPlain, UnsafeFileError } from "./safe-open.js";
|
|
7
7
|
export class BlobNotFound extends Error {
|
|
8
8
|
key;
|
|
9
9
|
constructor(key) {
|
|
@@ -83,16 +83,28 @@ export function bufferSource(buf) {
|
|
|
83
83
|
}
|
|
84
84
|
/** A file on disk. The size is taken now; if the file is shorter (or longer)
|
|
85
85
|
* when it is read, the read throws BlobSourceError rather than send a body
|
|
86
|
-
* that no longer matches what was announced.
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
* that no longer matches what was announced. A link is never followed, and a
|
|
87
|
+
* different file (or a FIFO) swapped in since the size was taken is refused:
|
|
88
|
+
* each read opens the path without following links and must find the same
|
|
89
|
+
* inode (safe-open.ts, SYMRACE). */
|
|
90
|
+
export function fileSource(filePath, expect = {}) {
|
|
91
|
+
const seen = statPlain(filePath, expect);
|
|
92
|
+
const size = seen.size;
|
|
89
93
|
return {
|
|
90
94
|
size,
|
|
91
95
|
async *read(start, end, signal) {
|
|
92
96
|
const hi = Math.min(end, size);
|
|
93
97
|
if (hi <= start)
|
|
94
98
|
return;
|
|
95
|
-
|
|
99
|
+
let stream;
|
|
100
|
+
try {
|
|
101
|
+
stream = plainStream(filePath, { start, end: hi - 1, highWaterMark: 1 << 20, signal, dev: seen.dev, ino: seen.ino, root: expect.root });
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
if (err instanceof UnsafeFileError)
|
|
105
|
+
throw new BlobSourceError(`${filePath} changed while it was being uploaded: ${err.message}`);
|
|
106
|
+
throw err;
|
|
107
|
+
}
|
|
96
108
|
let got = 0;
|
|
97
109
|
for await (const chunk of stream) {
|
|
98
110
|
got += chunk.length;
|
package/dist/src/capture.d.ts
CHANGED
|
@@ -30,6 +30,13 @@ export declare function encodedForms(v: string): string[];
|
|
|
30
30
|
* masked as that value, plain or in any of the encoded forms in the list.
|
|
31
31
|
*/
|
|
32
32
|
export declare function scrubWith(redactions: [string, string][], text: string): string;
|
|
33
|
+
/** What no vault value covers: a credential-shaped string (a GitHub, Anthropic
|
|
34
|
+
* or AWS key, a JWT, `token=…`, a bearer) and a private key's whole block.
|
|
35
|
+
* Applied after the step's own value masks, to every piece of stored text that
|
|
36
|
+
* came out of a sandbox or a model — a tool's output, a prompt, stderr — so a
|
|
37
|
+
* key a tool read off disk is not kept just because it was never a secret
|
|
38
|
+
* of ours. Ordinary text (ids, URLs, paths, hashes) is left as it was. */
|
|
39
|
+
export declare function maskPatterns(text: string): string;
|
|
33
40
|
/** Masked, then cut to the cap. */
|
|
34
41
|
export declare function maskCall(text: string, redactSecrets: (text: string) => string): {
|
|
35
42
|
text: string;
|
package/dist/src/capture.js
CHANGED
|
@@ -182,9 +182,18 @@ export function scrubWith(redactions, text) {
|
|
|
182
182
|
// Unterminated too: a key cut short, or printed without its END line, is
|
|
183
183
|
// masked to the end of the text.
|
|
184
184
|
const PRIVATE_KEY = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY-----|$(?![\s\S]))/g;
|
|
185
|
+
/** What no vault value covers: a credential-shaped string (a GitHub, Anthropic
|
|
186
|
+
* or AWS key, a JWT, `token=…`, a bearer) and a private key's whole block.
|
|
187
|
+
* Applied after the step's own value masks, to every piece of stored text that
|
|
188
|
+
* came out of a sandbox or a model — a tool's output, a prompt, stderr — so a
|
|
189
|
+
* key a tool read off disk is not kept just because it was never a secret
|
|
190
|
+
* of ours. Ordinary text (ids, URLs, paths, hashes) is left as it was. */
|
|
191
|
+
export function maskPatterns(text) {
|
|
192
|
+
return redactText(text).replace(PRIVATE_KEY, "[redacted:private key]");
|
|
193
|
+
}
|
|
185
194
|
/** Masked, then cut to the cap. */
|
|
186
195
|
export function maskCall(text, redactSecrets) {
|
|
187
|
-
const masked =
|
|
196
|
+
const masked = maskPatterns(redactSecrets(text));
|
|
188
197
|
return masked.length > CAPTURE_CAP ? { text: masked.slice(0, CAPTURE_CAP), cut: true } : { text: masked, cut: false };
|
|
189
198
|
}
|
|
190
199
|
export function recordCallIo(workspaceRoot, runId, callId, tool, io,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Deps } from "./publish.ts";
|
|
2
|
+
import { type PlainOpts } from "./safe-open.ts";
|
|
2
3
|
export declare const IMPORT_BY = "import:data-folder";
|
|
3
4
|
/** Files at or under this are read whole for the publish; a larger one is
|
|
4
5
|
* streamed from disk. */
|
|
@@ -60,7 +61,7 @@ export interface ScanOptions {
|
|
|
60
61
|
tenants?: string[];
|
|
61
62
|
workspaces?: string[];
|
|
62
63
|
}
|
|
63
|
-
export declare function hashFile(abs: string): Promise<string>;
|
|
64
|
+
export declare function hashFile(abs: string, expect?: PlainOpts): Promise<string>;
|
|
64
65
|
/**
|
|
65
66
|
* Two names that differ only by case cannot share a manifest. The first in
|
|
66
67
|
* byte order keeps its name; each later one is kept as a conflict copy beside
|
package/dist/src/data-import.js
CHANGED
|
@@ -53,6 +53,7 @@ import { pipeline } from "node:stream/promises";
|
|
|
53
53
|
import { AUTHORED_DIRS, MAX_MANIFEST_FILES, MODE_EXEC, MODE_FILE, assertManifestPath, blobObjectKey, conflictPath, diffManifests, emptyManifest, sha256Hex, sideOf, } from "./manifest.js";
|
|
54
54
|
import { headsOf, manifestsAt, publish, requestDigest } from "./publish.js";
|
|
55
55
|
import { BlobNotFound, BlobSourceError, downloadToFile, fileSource } from "./blob-store.js";
|
|
56
|
+
import { plainStream, readPlain } from "./safe-open.js";
|
|
56
57
|
import { isPlatformPath } from "./store.js";
|
|
57
58
|
// store.ts keeps this pattern private; a name that fails it is not an account or workspace.
|
|
58
59
|
const SAFE_NAME = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
@@ -63,9 +64,11 @@ export const LARGE_FILE_BYTES = 8 * 1024 * 1024;
|
|
|
63
64
|
/** How long the importer's own pin on a streamed blob lives (a publish's is
|
|
64
65
|
* shorter); released as soon as the publish is done. */
|
|
65
66
|
const LARGE_PIN_TTL_MS = 30 * 60_000;
|
|
66
|
-
export async function hashFile(abs) {
|
|
67
|
+
export async function hashFile(abs, expect = {}) {
|
|
67
68
|
const h = crypto.createHash("sha256");
|
|
68
|
-
|
|
69
|
+
// Never through a link, never waiting on a pipe, and (with `st`) the file the
|
|
70
|
+
// scan listed: the scan's lstat said "regular file" earlier (safe-open.ts).
|
|
71
|
+
await pipeline(plainStream(abs, expect), h);
|
|
69
72
|
return h.digest("hex");
|
|
70
73
|
}
|
|
71
74
|
/** Count what is under a folder without following links or reading content. */
|
|
@@ -253,7 +256,7 @@ async function scanWorkspace(tenant, workspace, dir) {
|
|
|
253
256
|
ws.conflicts.push({ path: from, keptAs: rel });
|
|
254
257
|
let sha;
|
|
255
258
|
try {
|
|
256
|
-
sha = await hashFile(f.abs);
|
|
259
|
+
sha = await hashFile(f.abs, { dev: f.st.dev, ino: f.st.ino, root: dir });
|
|
257
260
|
}
|
|
258
261
|
catch (err) {
|
|
259
262
|
ws.unmapped.push({ path: f.rel, reason: `unreadable (${err.code ?? "error"})`, files: 1, bytes: f.st.size });
|
|
@@ -432,7 +435,7 @@ export async function importWorkspace(scan, opts) {
|
|
|
432
435
|
const f = bySha.get(sha);
|
|
433
436
|
if (!f)
|
|
434
437
|
throw new Error(`no source file for ${sha.slice(0, 12)}…`);
|
|
435
|
-
const b =
|
|
438
|
+
const b = readPlain(f.abs);
|
|
436
439
|
if (sha256Hex(b) !== sha)
|
|
437
440
|
throw new Error(`${f.path} changed while it was being imported`);
|
|
438
441
|
return b;
|
|
@@ -39,6 +39,7 @@ import { putStepState, getStepState, dropStepState } from "./step-state.js";
|
|
|
39
39
|
import { stageRunInputs } from "./storage.js";
|
|
40
40
|
import { INPUTS_DIR, stagedLine } from "./run-inputs.js";
|
|
41
41
|
import { textBesideInputs } from "./input-text.js";
|
|
42
|
+
import { readPlain, readPlainText, UnsafeFileError } from "./safe-open.js";
|
|
42
43
|
/** The start-up phases and what each marks — startup.ts, where every
|
|
43
44
|
* surface that shows them reads them from (FOL-74). */
|
|
44
45
|
export { STARTUP_PHASES };
|
|
@@ -358,8 +359,8 @@ export function applyContainerChanges(hostWs, containerWs, baseline, note, diver
|
|
|
358
359
|
// the same rule as every file below.
|
|
359
360
|
const dir = path.join(containerWs, ".foldrun");
|
|
360
361
|
const file = path.join(dir, "copied-back.json");
|
|
361
|
-
if (fs.lstatSync(dir).isDirectory()
|
|
362
|
-
const m = JSON.parse(
|
|
362
|
+
if (fs.lstatSync(dir).isDirectory()) {
|
|
363
|
+
const m = JSON.parse(readPlainText(file, { root: containerWs, maxBytes: 16 * 1024 * 1024 }));
|
|
363
364
|
if (m && typeof m === "object" && !Array.isArray(m)) {
|
|
364
365
|
for (const [rel, h] of Object.entries(m)) {
|
|
365
366
|
// Every version the host handed back for the path counts: two jobs
|
|
@@ -404,7 +405,21 @@ export function applyContainerChanges(hostWs, containerWs, baseline, note, diver
|
|
|
404
405
|
continue;
|
|
405
406
|
if (!allowedBack(rel))
|
|
406
407
|
continue;
|
|
407
|
-
|
|
408
|
+
// Read from a descriptor opened without following a link, inside the
|
|
409
|
+
// sandbox's folder: the walk's readdir said "regular file" a moment ago,
|
|
410
|
+
// and the step may have swapped it for a link to the host's
|
|
411
|
+
// /proc/self/environ, or a FIFO that would block this read forever
|
|
412
|
+
// (SYMRACE). Such a path is skipped and said, like any other refusal.
|
|
413
|
+
let next;
|
|
414
|
+
try {
|
|
415
|
+
next = readPlain(abs, { root: containerWs });
|
|
416
|
+
}
|
|
417
|
+
catch (err) {
|
|
418
|
+
if (!(err instanceof UnsafeFileError))
|
|
419
|
+
throw err;
|
|
420
|
+
note?.(`${posix} was not copied back: ${err.message}`);
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
408
423
|
// What the step was handed: its bytes (a staging dir) or its hash.
|
|
409
424
|
// null when the file did not exist then; undefined when no baseline.
|
|
410
425
|
let was;
|
|
@@ -10,6 +10,8 @@ export function refusalText(r) {
|
|
|
10
10
|
return `${r.agent}/${r.path} (${mib(r.bytes)}) was not kept: this run's outputs may total at most ${mib(r.limit ?? 0)}`;
|
|
11
11
|
case "count":
|
|
12
12
|
return `${r.agent}/${r.path} was not kept: a run keeps at most ${r.limit ?? 50_000} files per agent`;
|
|
13
|
+
case "unsafe":
|
|
14
|
+
return `${r.agent}/${r.path} was not kept: it was replaced by a link or a pipe while the run ended, so it was not read`;
|
|
13
15
|
default:
|
|
14
16
|
return `${r.agent}/${r.path} was not kept: its name cannot be stored (control characters, over 400 bytes, or differs from another file only by case)`;
|
|
15
17
|
}
|
package/dist/src/run-outputs.js
CHANGED
|
@@ -43,6 +43,7 @@ import crypto from "node:crypto";
|
|
|
43
43
|
import { dataRoot } from "./paths.js";
|
|
44
44
|
import { processState } from "./process-state.js";
|
|
45
45
|
import { BlobNotFound, fileSource, hashRange } from "./blob-store.js";
|
|
46
|
+
import { plainStream, readPlain, statPlain, UnsafeFileError } from "./safe-open.js";
|
|
46
47
|
import { PUBLISH_PIN_TTL_MS, noPins } from "./blob-pins.js";
|
|
47
48
|
import { MAX_MANIFEST_FILES, MODE_EXEC, MODE_FILE, assertManifest, assertManifestPath, blobObjectKey, canonicalManifest, manifestId, manifestObjectKey, sha256Hex, } from "./manifest.js";
|
|
48
49
|
import { BlobMissing, loadManifest } from "./publish.js";
|
|
@@ -176,6 +177,11 @@ export const HARD_OUTPUT_FILE_BYTES = 32 * 1024 * 1024 * 1024;
|
|
|
176
177
|
const MAX_REFUSALS_ON_RECORD = 100;
|
|
177
178
|
function walk(root) {
|
|
178
179
|
const out = [];
|
|
180
|
+
// The folder itself a link (an agent folder pointed elsewhere): its
|
|
181
|
+
// contents are not this run's outputs, and the per-file root check would
|
|
182
|
+
// take the link's target for the root. Nothing is kept from it.
|
|
183
|
+
if (fs.lstatSync(root).isSymbolicLink())
|
|
184
|
+
return out;
|
|
179
185
|
const rec = (dir, rel) => {
|
|
180
186
|
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
181
187
|
const abs = path.join(dir, e.name);
|
|
@@ -184,7 +190,7 @@ function walk(root) {
|
|
|
184
190
|
rec(abs, r);
|
|
185
191
|
else if (e.isFile()) {
|
|
186
192
|
const st = fs.lstatSync(abs);
|
|
187
|
-
out.push({ rel: r, abs, size: st.size, mode: st.mode & 0o111 ? MODE_EXEC : MODE_FILE });
|
|
193
|
+
out.push({ rel: r, abs, size: st.size, mode: st.mode & 0o111 ? MODE_EXEC : MODE_FILE, dev: st.dev, ino: st.ino });
|
|
188
194
|
}
|
|
189
195
|
// symlinks, sockets and the like are not outputs (the volume copy skipped them too)
|
|
190
196
|
}
|
|
@@ -264,34 +270,47 @@ export async function publishRunOutputs(b, tenant, workspace, runId, agent, dir)
|
|
|
264
270
|
const c = kept[i];
|
|
265
271
|
let sha;
|
|
266
272
|
let size;
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
+
// The walk looked with lstat; the read opens the path without following
|
|
274
|
+
// a link and must find the inode the walk saw (a file the agent swapped
|
|
275
|
+
// for a link to a secret, or a FIFO, since is refused, not read). The
|
|
276
|
+
// outputs folder is the root: a folder swapped for a link out is too.
|
|
277
|
+
const want = { dev: c.dev, ino: c.ino, root: dir };
|
|
278
|
+
try {
|
|
279
|
+
if (c.size <= SMALL_OUTPUT_FILE_BYTES) {
|
|
280
|
+
// A file that grew past twice the small size since the walk is not read whole.
|
|
281
|
+
const body = readPlain(c.abs, { ...want, maxBytes: 2 * SMALL_OUTPUT_FILE_BYTES });
|
|
282
|
+
// Grew since it was measured (an agent still writing): same rule, same cap.
|
|
283
|
+
if (body.length > fileCap) {
|
|
284
|
+
refused.push({ agent, path: c.rel, bytes: body.length, reason: "file-cap", limit: fileCap });
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
sha = sha256Hex(body);
|
|
288
|
+
size = body.length;
|
|
289
|
+
// Pin each blob before its put; a repeat extends the lease.
|
|
290
|
+
await pins.pinBlobs(tenant, [sha], publishHolder, { ttlMs: PUBLISH_PIN_TTL_MS });
|
|
291
|
+
await b.blobs.putIfAbsent(blobObjectKey(tenant, sha), body);
|
|
273
292
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
293
|
+
else {
|
|
294
|
+
const source = fileSource(c.abs, want);
|
|
295
|
+
if (source.size > fileCap) {
|
|
296
|
+
refused.push({ agent, path: c.rel, bytes: source.size, reason: "file-cap", limit: fileCap });
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
// A file that changes under the read throws BlobSourceError (and the
|
|
300
|
+
// store checks the hash it is given): the publish fails, never stores
|
|
301
|
+
// bytes under a name they do not have.
|
|
302
|
+
sha = (await hashRange(source, 0, source.size)).sha256;
|
|
303
|
+
size = source.size;
|
|
304
|
+
await pins.pinBlobs(tenant, [sha], publishHolder, { ttlMs: PUBLISH_PIN_TTL_MS });
|
|
305
|
+
await b.blobs.putSourceIfAbsent(blobObjectKey(tenant, sha), source, { sha256: sha });
|
|
285
306
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
await b.blobs.putSourceIfAbsent(blobObjectKey(tenant, sha), source, { sha256: sha });
|
|
307
|
+
files[c.rel] = { sha, size, mode: c.mode };
|
|
308
|
+
}
|
|
309
|
+
catch (err) {
|
|
310
|
+
if (!(err instanceof UnsafeFileError))
|
|
311
|
+
throw err;
|
|
312
|
+
refused.push({ agent, path: c.rel, bytes: c.size, reason: "unsafe" });
|
|
293
313
|
}
|
|
294
|
-
files[c.rel] = { sha, size, mode: c.mode };
|
|
295
314
|
}
|
|
296
315
|
};
|
|
297
316
|
await Promise.all(Array.from({ length: Math.min(UPLOAD_CONCURRENCY, kept.length) }, lane));
|
|
@@ -485,11 +504,13 @@ export async function openRunOutput(tenant, workspace, runId, agent, rel) {
|
|
|
485
504
|
const file = runOutputPathOnVolume(tenant, workspace, runId, agent, rel);
|
|
486
505
|
if (!file)
|
|
487
506
|
return null;
|
|
488
|
-
|
|
507
|
+
// Opened without following a link: the path was checked by
|
|
508
|
+
// runOutputPathOnVolume, and a swap since (link, FIFO) is refused (SYMRACE).
|
|
509
|
+
const seen = statPlain(file);
|
|
489
510
|
return {
|
|
490
|
-
size,
|
|
511
|
+
size: seen.size,
|
|
491
512
|
// Node's own bridge keeps the file stream paused until the reader pulls.
|
|
492
|
-
read: (range) => Readable.toWeb(
|
|
513
|
+
read: (range) => Readable.toWeb(plainStream(file, { dev: seen.dev, ino: seen.ino, ...(range ? { start: range.start, end: range.end } : {}) })),
|
|
493
514
|
};
|
|
494
515
|
}
|
|
495
516
|
/** The whole file as bytes (a small one: a test, a preview). */
|
package/dist/src/runner.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { opsLog, opsError } from "./ops-log.js";
|
|
6
6
|
import { globMatch } from "./bounded-match.js";
|
|
7
7
|
import { latestRevisionId } from "./history.js";
|
|
8
|
-
import { recordCallIo, redactionsFor, scrubWith } from "./capture.js";
|
|
8
|
+
import { maskPatterns, recordCallIo, redactionsFor, scrubWith } from "./capture.js";
|
|
9
9
|
import { callSink, flushCalls } from "./captured-calls.js";
|
|
10
10
|
import { AUTH_REFUSAL, platformCause } from "./issues.js";
|
|
11
11
|
import { hookNames } from "./hooks.js";
|
|
@@ -2100,6 +2100,27 @@ conversation) {
|
|
|
2100
2100
|
let canActOutward = false;
|
|
2101
2101
|
const eventsAtStart = step.events.length;
|
|
2102
2102
|
const redact = (text) => scrubWith(redactions, text);
|
|
2103
|
+
// An event line, a question, a message: text that came out of a sandbox or a
|
|
2104
|
+
// model, read by viewers, A2A callers, notifications and exports. The values
|
|
2105
|
+
// above, then what no vault value covers (a key a tool printed, a private
|
|
2106
|
+
// key block) — the mask a captured call already gets. One place: the retry
|
|
2107
|
+
// row's error, the run's failure reason and a notification's summary are all
|
|
2108
|
+
// read back off these events.
|
|
2109
|
+
const mask = (text) => maskPatterns(redact(text));
|
|
2110
|
+
const maskOperator = (ev) => {
|
|
2111
|
+
switch (ev.kind) {
|
|
2112
|
+
case "asked":
|
|
2113
|
+
return { ...ev, question: mask(ev.question), ...(ev.options ? { options: ev.options.map(mask) } : {}) };
|
|
2114
|
+
case "message":
|
|
2115
|
+
case "peer":
|
|
2116
|
+
case "told":
|
|
2117
|
+
return { ...ev, text: mask(ev.text) };
|
|
2118
|
+
case "answered":
|
|
2119
|
+
return { ...ev, answer: mask(ev.answer) };
|
|
2120
|
+
default:
|
|
2121
|
+
return ev;
|
|
2122
|
+
}
|
|
2123
|
+
};
|
|
2103
2124
|
// The reply, the conclusion and an `output: json` value land on the same
|
|
2104
2125
|
// record the events do, and a model that quotes a script's stdout puts
|
|
2105
2126
|
// the credential in its answer rather than in a tool event. Same scrub,
|
|
@@ -2135,7 +2156,9 @@ conversation) {
|
|
|
2135
2156
|
let captureFull = false;
|
|
2136
2157
|
const callsRoot = path.resolve(agentDir, "..", "..");
|
|
2137
2158
|
const sink = runId ? callSink(step, { tenant, workspace: path.basename(callsRoot), runId, workspaceRoot: callsRoot }) : undefined;
|
|
2138
|
-
const push = (type, text,
|
|
2159
|
+
const push = (type, text, given) => {
|
|
2160
|
+
// A question or a message a model wrote is masked before it is stored or sent on.
|
|
2161
|
+
const extra = given?.operator ? { ...given, operator: maskOperator(given.operator) } : given;
|
|
2139
2162
|
const t = new Date().toISOString();
|
|
2140
2163
|
// The model has begun its first response: held only in memory, for
|
|
2141
2164
|
// the fan-out gate (warmGate). Not a line in the trace.
|
|
@@ -2176,7 +2199,7 @@ conversation) {
|
|
|
2176
2199
|
rest = { ...others, captured: true };
|
|
2177
2200
|
}
|
|
2178
2201
|
}
|
|
2179
|
-
step.events.push({ t, type, text:
|
|
2202
|
+
step.events.push({ t, type, text: mask(text), ...rest });
|
|
2180
2203
|
// A person in the loop: the question, its answer, a message delivered —
|
|
2181
2204
|
// onto the step, so the run page, the API and history show them.
|
|
2182
2205
|
if (extra?.operator) {
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a file a sandbox, an agent or a customer's upload could have
|
|
3
|
+
* touched — without following a link it swapped in after we looked.
|
|
4
|
+
*
|
|
5
|
+
* The walkers (run outputs, copy-back, folder scan, import) list a tree with
|
|
6
|
+
* lstat, which is honest at that moment. The read comes later, by path, and a
|
|
7
|
+
* path is looked up again: a regular file replaced by a symlink to /proc/self/environ
|
|
8
|
+
* or another tenant's file in between was followed and its bytes stored or
|
|
9
|
+
* served; one replaced by a FIFO blocked the whole worker in a synchronous
|
|
10
|
+
* read. So the read opens with O_NOFOLLOW (a link at the last step is ELOOP)
|
|
11
|
+
* and O_NONBLOCK (a FIFO opens at once instead of waiting for a writer), asks
|
|
12
|
+
* the OPEN file what it is (fstat: a regular file, within the size cap, the
|
|
13
|
+
* same device and inode the walk saw, when it knows them) and reads from that
|
|
14
|
+
* descriptor, so what was checked is what is read.
|
|
15
|
+
*
|
|
16
|
+
* `root`: a directory ON THE WAY can also be swapped for a link (the open only
|
|
17
|
+
* refuses a link at the last step). With a root given, after the open the
|
|
18
|
+
* file's real path must be inside the real root and be the very inode that was
|
|
19
|
+
* opened. That narrows the window to an attacker who flips a directory link
|
|
20
|
+
* back and forth across two syscalls; it does not close it (Node has no
|
|
21
|
+
* openat2 / RESOLVE_NO_SYMLINKS). Callers that need no link anywhere on the way
|
|
22
|
+
* must keep the directory off the sandbox's reach, as the live archive does.
|
|
23
|
+
*
|
|
24
|
+
* Windows has no O_NOFOLLOW: there the check stays what it was (lstat, then
|
|
25
|
+
* open, then fstat) — no weaker than before, with the same small window.
|
|
26
|
+
*/
|
|
27
|
+
import fs from "node:fs";
|
|
28
|
+
export declare class UnsafeFileError extends Error {
|
|
29
|
+
readonly code = "EUNSAFEFILE";
|
|
30
|
+
constructor(file: string, why: string);
|
|
31
|
+
}
|
|
32
|
+
export interface PlainOpts {
|
|
33
|
+
/** Refuse a file bigger than this (checked on the open file, not on a stat before). */
|
|
34
|
+
maxBytes?: number;
|
|
35
|
+
/** The inode the walk saw: a different one was swapped in since. */
|
|
36
|
+
dev?: number | bigint;
|
|
37
|
+
ino?: number | bigint;
|
|
38
|
+
/** A real (symlink-free) directory the file must really be inside. */
|
|
39
|
+
root?: string;
|
|
40
|
+
}
|
|
41
|
+
/** Open a regular file for reading without following a link at its last step
|
|
42
|
+
* or waiting on a FIFO. The caller closes `fd`. Throws UnsafeFileError. */
|
|
43
|
+
export declare function openPlain(file: string, opts?: PlainOpts): {
|
|
44
|
+
fd: number;
|
|
45
|
+
st: fs.Stats;
|
|
46
|
+
};
|
|
47
|
+
/** The whole file as bytes, from the checked descriptor. */
|
|
48
|
+
export declare function readPlain(file: string, opts?: PlainOpts): Buffer;
|
|
49
|
+
export declare function readPlainText(file: string, opts?: PlainOpts): string;
|
|
50
|
+
/** What the file is right now (size, device, inode), without following a link. */
|
|
51
|
+
export declare function statPlain(file: string, opts?: PlainOpts): fs.Stats;
|
|
52
|
+
/** A read stream over the checked descriptor (a byte range is inclusive, as
|
|
53
|
+
* createReadStream's). The descriptor is closed with the stream. */
|
|
54
|
+
export declare function plainStream(file: string, opts?: PlainOpts & {
|
|
55
|
+
start?: number;
|
|
56
|
+
end?: number;
|
|
57
|
+
highWaterMark?: number;
|
|
58
|
+
signal?: AbortSignal;
|
|
59
|
+
}): fs.ReadStream;
|
|
60
|
+
/** The whole file as bytes, read off the event loop from the checked descriptor. */
|
|
61
|
+
export declare function readPlainAsync(file: string, opts?: PlainOpts): Promise<Buffer>;
|
|
62
|
+
/** copyFileSync, but from a checked descriptor: a link or a pipe swapped in
|
|
63
|
+
* since the caller's lstat is refused, not followed or waited on. The copy
|
|
64
|
+
* keeps the file's permission bits and is streamed (1 MiB at a time). */
|
|
65
|
+
export declare function copyPlain(from: string, to: string, opts?: PlainOpts): void;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a file a sandbox, an agent or a customer's upload could have
|
|
3
|
+
* touched — without following a link it swapped in after we looked.
|
|
4
|
+
*
|
|
5
|
+
* The walkers (run outputs, copy-back, folder scan, import) list a tree with
|
|
6
|
+
* lstat, which is honest at that moment. The read comes later, by path, and a
|
|
7
|
+
* path is looked up again: a regular file replaced by a symlink to /proc/self/environ
|
|
8
|
+
* or another tenant's file in between was followed and its bytes stored or
|
|
9
|
+
* served; one replaced by a FIFO blocked the whole worker in a synchronous
|
|
10
|
+
* read. So the read opens with O_NOFOLLOW (a link at the last step is ELOOP)
|
|
11
|
+
* and O_NONBLOCK (a FIFO opens at once instead of waiting for a writer), asks
|
|
12
|
+
* the OPEN file what it is (fstat: a regular file, within the size cap, the
|
|
13
|
+
* same device and inode the walk saw, when it knows them) and reads from that
|
|
14
|
+
* descriptor, so what was checked is what is read.
|
|
15
|
+
*
|
|
16
|
+
* `root`: a directory ON THE WAY can also be swapped for a link (the open only
|
|
17
|
+
* refuses a link at the last step). With a root given, after the open the
|
|
18
|
+
* file's real path must be inside the real root and be the very inode that was
|
|
19
|
+
* opened. That narrows the window to an attacker who flips a directory link
|
|
20
|
+
* back and forth across two syscalls; it does not close it (Node has no
|
|
21
|
+
* openat2 / RESOLVE_NO_SYMLINKS). Callers that need no link anywhere on the way
|
|
22
|
+
* must keep the directory off the sandbox's reach, as the live archive does.
|
|
23
|
+
*
|
|
24
|
+
* Windows has no O_NOFOLLOW: there the check stays what it was (lstat, then
|
|
25
|
+
* open, then fstat) — no weaker than before, with the same small window.
|
|
26
|
+
*/
|
|
27
|
+
import fs from "node:fs";
|
|
28
|
+
import path from "node:path";
|
|
29
|
+
const C = fs.constants;
|
|
30
|
+
// Missing on Windows: 0 there.
|
|
31
|
+
const NOFOLLOW = C.O_NOFOLLOW ?? 0;
|
|
32
|
+
const NONBLOCK = C.O_NONBLOCK ?? 0;
|
|
33
|
+
const NOCTTY = C.O_NOCTTY ?? 0;
|
|
34
|
+
export class UnsafeFileError extends Error {
|
|
35
|
+
code = "EUNSAFEFILE";
|
|
36
|
+
constructor(file, why) {
|
|
37
|
+
super(`${path.basename(file)}: ${why}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Open a regular file for reading without following a link at its last step
|
|
41
|
+
* or waiting on a FIFO. The caller closes `fd`. Throws UnsafeFileError. */
|
|
42
|
+
export function openPlain(file, opts = {}) {
|
|
43
|
+
if (!NOFOLLOW) {
|
|
44
|
+
// No O_NOFOLLOW here: look first, as every caller did before.
|
|
45
|
+
if (fs.lstatSync(file).isSymbolicLink())
|
|
46
|
+
throw new UnsafeFileError(file, "a symbolic link (refused, never followed)");
|
|
47
|
+
}
|
|
48
|
+
let fd;
|
|
49
|
+
try {
|
|
50
|
+
fd = fs.openSync(file, C.O_RDONLY | NOFOLLOW | NONBLOCK | NOCTTY);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
const code = err.code;
|
|
54
|
+
if (code === "ELOOP")
|
|
55
|
+
throw new UnsafeFileError(file, "a symbolic link (refused, never followed)");
|
|
56
|
+
if (code === "ENXIO")
|
|
57
|
+
throw new UnsafeFileError(file, "not a regular file");
|
|
58
|
+
throw err;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const st = fs.fstatSync(fd);
|
|
62
|
+
if (!st.isFile())
|
|
63
|
+
throw new UnsafeFileError(file, "not a regular file");
|
|
64
|
+
if (opts.ino !== undefined && (st.ino !== opts.ino || (opts.dev !== undefined && st.dev !== opts.dev))) {
|
|
65
|
+
throw new UnsafeFileError(file, "changed while it was being read (another file took its place)");
|
|
66
|
+
}
|
|
67
|
+
if (opts.maxBytes !== undefined && st.size > opts.maxBytes)
|
|
68
|
+
throw new UnsafeFileError(file, `larger than ${opts.maxBytes} bytes`);
|
|
69
|
+
if (opts.root !== undefined) {
|
|
70
|
+
const real = fs.realpathSync(file);
|
|
71
|
+
const root = fs.realpathSync(opts.root);
|
|
72
|
+
if (!(real === root || real.startsWith(root + path.sep)))
|
|
73
|
+
throw new UnsafeFileError(file, "leads out of its folder through a symbolic link");
|
|
74
|
+
const there = fs.lstatSync(real);
|
|
75
|
+
if (there.ino !== st.ino || there.dev !== st.dev)
|
|
76
|
+
throw new UnsafeFileError(file, "changed while it was being read (another file took its place)");
|
|
77
|
+
}
|
|
78
|
+
return { fd, st };
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
fs.closeSync(fd);
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** The whole file as bytes, from the checked descriptor. */
|
|
86
|
+
export function readPlain(file, opts = {}) {
|
|
87
|
+
const { fd, st } = openPlain(file, opts);
|
|
88
|
+
try {
|
|
89
|
+
// Never trust the size past the cap: a file growing under the read stops there.
|
|
90
|
+
if (opts.maxBytes === undefined)
|
|
91
|
+
return fs.readFileSync(fd);
|
|
92
|
+
const buf = Buffer.alloc(Math.min(st.size, opts.maxBytes) + 1);
|
|
93
|
+
let n = 0;
|
|
94
|
+
for (;;) {
|
|
95
|
+
const got = fs.readSync(fd, buf, n, buf.length - n, null);
|
|
96
|
+
if (!got)
|
|
97
|
+
break;
|
|
98
|
+
n += got;
|
|
99
|
+
if (n >= buf.length)
|
|
100
|
+
throw new UnsafeFileError(file, `larger than ${opts.maxBytes} bytes`);
|
|
101
|
+
}
|
|
102
|
+
return buf.subarray(0, n);
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
fs.closeSync(fd);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export function readPlainText(file, opts = {}) {
|
|
109
|
+
return readPlain(file, opts).toString("utf8");
|
|
110
|
+
}
|
|
111
|
+
/** What the file is right now (size, device, inode), without following a link. */
|
|
112
|
+
export function statPlain(file, opts = {}) {
|
|
113
|
+
const { fd, st } = openPlain(file, opts);
|
|
114
|
+
fs.closeSync(fd);
|
|
115
|
+
return st;
|
|
116
|
+
}
|
|
117
|
+
/** A read stream over the checked descriptor (a byte range is inclusive, as
|
|
118
|
+
* createReadStream's). The descriptor is closed with the stream. */
|
|
119
|
+
export function plainStream(file, opts = {}) {
|
|
120
|
+
const { fd } = openPlain(file, opts);
|
|
121
|
+
// O_NONBLOCK was for the open; a regular file reads the same either way.
|
|
122
|
+
return fs.createReadStream(file, { fd, start: opts.start, end: opts.end, highWaterMark: opts.highWaterMark, signal: opts.signal, autoClose: true });
|
|
123
|
+
}
|
|
124
|
+
/** The whole file as bytes, read off the event loop from the checked descriptor. */
|
|
125
|
+
export async function readPlainAsync(file, opts = {}) {
|
|
126
|
+
const { fd } = openPlain(file, opts);
|
|
127
|
+
try {
|
|
128
|
+
return await new Promise((resolve, reject) => fs.readFile(fd, (err, data) => (err ? reject(err) : resolve(data))));
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
fs.closeSync(fd);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** copyFileSync, but from a checked descriptor: a link or a pipe swapped in
|
|
135
|
+
* since the caller's lstat is refused, not followed or waited on. The copy
|
|
136
|
+
* keeps the file's permission bits and is streamed (1 MiB at a time). */
|
|
137
|
+
export function copyPlain(from, to, opts = {}) {
|
|
138
|
+
const { fd, st } = openPlain(from, opts);
|
|
139
|
+
try {
|
|
140
|
+
const out = fs.openSync(to, C.O_WRONLY | C.O_CREAT | C.O_TRUNC, st.mode & 0o777);
|
|
141
|
+
try {
|
|
142
|
+
const buf = Buffer.allocUnsafe(1 << 20);
|
|
143
|
+
for (;;) {
|
|
144
|
+
const n = fs.readSync(fd, buf, 0, buf.length, null);
|
|
145
|
+
if (!n)
|
|
146
|
+
break;
|
|
147
|
+
let at = 0;
|
|
148
|
+
while (at < n)
|
|
149
|
+
at += fs.writeSync(out, buf, at, n - at);
|
|
150
|
+
}
|
|
151
|
+
fs.fchmodSync(out, st.mode & 0o777);
|
|
152
|
+
}
|
|
153
|
+
finally {
|
|
154
|
+
fs.closeSync(out);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
fs.closeSync(fd);
|
|
159
|
+
}
|
|
160
|
+
}
|
package/dist/src/store.d.ts
CHANGED
|
@@ -1545,7 +1545,7 @@ export interface RefusedOutput {
|
|
|
1545
1545
|
/** Relative to that agent's outputs/. */
|
|
1546
1546
|
path: string;
|
|
1547
1547
|
bytes: number;
|
|
1548
|
-
reason: "file-cap" | "run-cap" | "name" | "count";
|
|
1548
|
+
reason: "file-cap" | "run-cap" | "name" | "count" | "unsafe";
|
|
1549
1549
|
/** The cap in bytes that was exceeded (the count for "count"). */
|
|
1550
1550
|
limit?: number;
|
|
1551
1551
|
}
|