opencode-fs-plugin 3.4.0 → 3.6.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/README.md +268 -132
- package/SPEC.md +171 -56
- package/package.json +2 -2
- package/skills/filesystem/SKILL.md +172 -67
- package/src/core/format.ts +3 -9
- package/src/core/fsutil.ts +84 -23
- package/src/core/jobs.ts +210 -34
- package/src/core/permissions.ts +32 -14
- package/src/core/relay-control.ts +343 -0
- package/src/core/relay-protocol.ts +274 -0
- package/src/core/safe-output.ts +290 -0
- package/src/core/sandbox.ts +43 -0
- package/src/core/types.ts +0 -5
- package/src/index.ts +78 -72
- package/src/tools/ar.ts +62 -29
- package/src/tools/cat.ts +31 -46
- package/src/tools/chmod.ts +30 -19
- package/src/tools/chown.ts +24 -16
- package/src/tools/command.ts +536 -0
- package/src/tools/cp.ts +84 -20
- package/src/tools/cpio.ts +204 -65
- package/src/tools/{http_request.ts → curl.ts} +127 -10
- package/src/tools/dd.ts +15 -9
- package/src/tools/diff.ts +46 -15
- package/src/tools/grep.ts +5 -2
- package/src/tools/gzip.ts +13 -15
- package/src/tools/mktemp.ts +41 -26
- package/src/tools/mv.ts +69 -28
- package/src/tools/pipe.ts +24 -16
- package/src/tools/split.ts +26 -12
- package/src/tools/tar.ts +132 -57
- package/src/tools/tee.ts +7 -4
- package/src/tools/touch.ts +12 -4
- package/src/tools/truncate.ts +17 -7
- package/src/tools/zip.ts +119 -54
- package/src/core/spill.ts +0 -145
- package/src/tools/exec.ts +0 -483
|
@@ -65,12 +65,83 @@ interface Checksum {
|
|
|
65
65
|
digest: string;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
/** Headers that are safe to resend to a different origin on a redirect. */
|
|
69
|
+
const SAFE_CROSS_ORIGIN_HEADERS: ReadonlySet<string> = new Set([
|
|
70
|
+
"accept",
|
|
71
|
+
"accept-language",
|
|
72
|
+
"cache-control",
|
|
73
|
+
"pragma",
|
|
74
|
+
"range",
|
|
75
|
+
"user-agent",
|
|
76
|
+
]);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A permission assertion failed. It is wrapped so the outer fetch/stream error
|
|
80
|
+
* mapper can rethrow the original denial instead of turning it into an
|
|
81
|
+
* ordinary `io` error result.
|
|
82
|
+
*/
|
|
83
|
+
class PermissionDenied extends Error {
|
|
84
|
+
constructor(readonly original: unknown) {
|
|
85
|
+
super(errorMessage(original));
|
|
86
|
+
this.name = "PermissionDenied";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Decide whether a redirect hop may run, and with which headers. Same-origin
|
|
92
|
+
* hops keep the caller's request unchanged. A cross-origin hop is fail-closed:
|
|
93
|
+
* only a bodyless GET/HEAD with caller headers from the safe set is allowed.
|
|
94
|
+
* Credentials, custom headers, unsafe methods and request bodies are refused
|
|
95
|
+
* instead of being stripped or forwarded. An HTTPS to HTTP downgrade is always
|
|
96
|
+
* refused. The input object is never mutated.
|
|
97
|
+
*/
|
|
98
|
+
export function headersForRedirect(
|
|
99
|
+
headers: Record<string, string>,
|
|
100
|
+
from: URL,
|
|
101
|
+
to: URL,
|
|
102
|
+
method: string,
|
|
103
|
+
hasBody: boolean,
|
|
104
|
+
): Record<string, string> {
|
|
105
|
+
if (from.protocol === "https:" && to.protocol === "http:") {
|
|
106
|
+
throw new FsError(`Refusing an HTTPS to HTTP redirect to "${to}".`, "unsupported");
|
|
107
|
+
}
|
|
108
|
+
if (from.origin === to.origin) return headers;
|
|
109
|
+
if (to.username.length > 0 || to.password.length > 0) {
|
|
110
|
+
throw new FsError(
|
|
111
|
+
`Refusing a cross-origin redirect to a URL with embedded credentials: "${to.origin}".`,
|
|
112
|
+
"unsupported",
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (hasBody) {
|
|
116
|
+
throw new FsError(
|
|
117
|
+
`Refusing a cross-origin redirect that would resend a request body to "${to.origin}".`,
|
|
118
|
+
"unsupported",
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
122
|
+
throw new FsError(
|
|
123
|
+
`Refusing a cross-origin redirect for a ${method} request to "${to.origin}".`,
|
|
124
|
+
"unsupported",
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
const unsafe = Object.keys(headers).filter(
|
|
128
|
+
(name) => !SAFE_CROSS_ORIGIN_HEADERS.has(name.toLowerCase()),
|
|
129
|
+
);
|
|
130
|
+
if (unsafe.length > 0) {
|
|
131
|
+
throw new FsError(
|
|
132
|
+
`Refusing a cross-origin redirect with caller headers: ${unsafe.join(", ")}.`,
|
|
133
|
+
"unsupported",
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
return headers;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export const curlTool: ToolSpec<Input> = {
|
|
140
|
+
name: "curl",
|
|
70
141
|
description:
|
|
71
142
|
"Make an HTTP(S) request and either return the body or stream it to a file (`output`). " +
|
|
72
|
-
"Supports methods, headers, a text or JSON
|
|
73
|
-
"`checksum` verification. Usable as a `pipe` source.",
|
|
143
|
+
"Uses structured options, not curl CLI flags. Supports methods, headers, a text or JSON " +
|
|
144
|
+
"body, redirects, timeouts, `maxBytes`, and `checksum` verification. Usable as a `pipe` source.",
|
|
74
145
|
schema: Schema,
|
|
75
146
|
pipeable: "source",
|
|
76
147
|
mutates: true,
|
|
@@ -102,6 +173,9 @@ export const httpRequestTool: ToolSpec<Input> = {
|
|
|
102
173
|
const forwardAbort = () => controller.abort();
|
|
103
174
|
ctx.signal.addEventListener("abort", forwardAbort, { once: true });
|
|
104
175
|
const mapError = (error: unknown): FsError => {
|
|
176
|
+
// A permission denial is not a transport failure: preserve the host's
|
|
177
|
+
// error so plugin execute propagates it instead of rendering `io`.
|
|
178
|
+
if (error instanceof PermissionDenied) throw error.original;
|
|
105
179
|
if (error instanceof FsError) return error;
|
|
106
180
|
if (controller.signal.aborted) {
|
|
107
181
|
return new FsError(
|
|
@@ -146,7 +220,11 @@ export const httpRequestTool: ToolSpec<Input> = {
|
|
|
146
220
|
|
|
147
221
|
if (input.output !== undefined) {
|
|
148
222
|
const target = await ctx.sandbox.resolveForCreate(input.output);
|
|
149
|
-
|
|
223
|
+
try {
|
|
224
|
+
await ctx.permission.assert([{ target, boundary: path.dirname(target.full) }]);
|
|
225
|
+
} catch (error) {
|
|
226
|
+
throw new PermissionDenied(error);
|
|
227
|
+
}
|
|
150
228
|
const downloaded = await download(target.full, response, input.maxBytes, checksum, mapError);
|
|
151
229
|
return textResult(
|
|
152
230
|
`Downloaded ${downloaded.bytes} bytes to ${input.output}\n`,
|
|
@@ -198,12 +276,17 @@ async function request(
|
|
|
198
276
|
let current = url;
|
|
199
277
|
let method = options.method;
|
|
200
278
|
let body = options.body;
|
|
279
|
+
let headers = options.headers;
|
|
201
280
|
|
|
202
281
|
for (let redirects = 0; ; redirects++) {
|
|
203
|
-
|
|
282
|
+
try {
|
|
283
|
+
await gate.assertTrusted([current.toString()]);
|
|
284
|
+
} catch (error) {
|
|
285
|
+
throw new PermissionDenied(error);
|
|
286
|
+
}
|
|
204
287
|
const response = await fetch(current, {
|
|
205
288
|
method,
|
|
206
|
-
headers
|
|
289
|
+
headers,
|
|
207
290
|
redirect: "manual",
|
|
208
291
|
signal: options.signal,
|
|
209
292
|
...(body === undefined ? {} : { body }),
|
|
@@ -230,11 +313,21 @@ async function request(
|
|
|
230
313
|
} catch (error) {
|
|
231
314
|
throw options.mapError(error);
|
|
232
315
|
}
|
|
233
|
-
|
|
234
|
-
|
|
316
|
+
// A 303 always turns into a bodyless GET before the redirect policy is
|
|
317
|
+
// evaluated, so a safe resulting GET may cross origins; 301/302 keep their
|
|
318
|
+
// existing POST rewrite after the policy, and 307/308 keep method/body.
|
|
319
|
+
if (response.status === 303) {
|
|
235
320
|
method = "GET";
|
|
236
321
|
body = undefined;
|
|
237
322
|
}
|
|
323
|
+
headers = headersForRedirect(headers, current, next, method, body !== undefined);
|
|
324
|
+
current = next;
|
|
325
|
+
if (response.status === 301 || response.status === 302) {
|
|
326
|
+
if (method === "POST") {
|
|
327
|
+
method = "GET";
|
|
328
|
+
body = undefined;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
238
331
|
}
|
|
239
332
|
}
|
|
240
333
|
|
|
@@ -291,7 +384,7 @@ async function download(
|
|
|
291
384
|
throw new FsError(`Response exceeded maxBytes (${maxBytes}).`, "invalid");
|
|
292
385
|
}
|
|
293
386
|
hashers.update(chunk);
|
|
294
|
-
await handle
|
|
387
|
+
await writeChunkFully(handle, chunk);
|
|
295
388
|
}
|
|
296
389
|
await handle.close();
|
|
297
390
|
handle = undefined;
|
|
@@ -308,6 +401,30 @@ async function download(
|
|
|
308
401
|
}
|
|
309
402
|
}
|
|
310
403
|
|
|
404
|
+
interface WriteHandle {
|
|
405
|
+
write(
|
|
406
|
+
buffer: Buffer,
|
|
407
|
+
offset: number,
|
|
408
|
+
length: number,
|
|
409
|
+
): Promise<{ bytesWritten: number }>;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Write every byte of a chunk. `FileHandle.write` may write less than the whole
|
|
414
|
+
* buffer in one call; keep going until the chunk is complete and fail on a
|
|
415
|
+
* zero-progress write rather than silently truncating the download.
|
|
416
|
+
*/
|
|
417
|
+
export async function writeChunkFully(handle: WriteHandle, chunk: Buffer): Promise<void> {
|
|
418
|
+
let offset = 0;
|
|
419
|
+
while (offset < chunk.length) {
|
|
420
|
+
const { bytesWritten } = await handle.write(chunk, offset, chunk.length - offset);
|
|
421
|
+
if (bytesWritten <= 0) {
|
|
422
|
+
throw new FsError("Download write made no progress.", "io");
|
|
423
|
+
}
|
|
424
|
+
offset += bytesWritten;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
311
428
|
function makeHashers(checksum: Checksum | undefined): {
|
|
312
429
|
update: (chunk: Buffer) => void;
|
|
313
430
|
digests: () => { sha256: string; checksum: string | undefined };
|
package/src/tools/dd.ts
CHANGED
|
@@ -6,6 +6,13 @@
|
|
|
6
6
|
import * as fs from "node:fs/promises";
|
|
7
7
|
import { Type, type Static } from "@sinclair/typebox";
|
|
8
8
|
import { FsError, fromErrno } from "../core/errors.js";
|
|
9
|
+
import {
|
|
10
|
+
openOutput,
|
|
11
|
+
outputBoundary,
|
|
12
|
+
resolveOutputTarget,
|
|
13
|
+
throwIfAborted,
|
|
14
|
+
writeFully,
|
|
15
|
+
} from "../core/safe-output.js";
|
|
9
16
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
10
17
|
|
|
11
18
|
const Schema = Type.Object(
|
|
@@ -35,13 +42,13 @@ export const ddTool: ToolSpec<Input> = {
|
|
|
35
42
|
let inputPath: string | undefined;
|
|
36
43
|
if (input.input !== undefined) {
|
|
37
44
|
const target = await ctx.sandbox.resolveFollow(input.input);
|
|
38
|
-
await ctx.permission.assert([{ target, boundary: target.full }]);
|
|
45
|
+
await ctx.permission.assert([{ target, boundary: outputBoundary(target.full) }]);
|
|
39
46
|
inputPath = target.full;
|
|
40
47
|
}
|
|
41
48
|
let outputPath: string | undefined;
|
|
42
49
|
if (input.output !== undefined) {
|
|
43
|
-
const target = await ctx.sandbox
|
|
44
|
-
await ctx.permission.assert([{ target, boundary: target.full }]);
|
|
50
|
+
const target = await resolveOutputTarget(ctx.sandbox, input.output);
|
|
51
|
+
await ctx.permission.assert([{ target, boundary: outputBoundary(target.full) }]);
|
|
45
52
|
outputPath = target.full;
|
|
46
53
|
}
|
|
47
54
|
const source = inputPath === undefined ? io.stdin : fileChunks(inputPath, block);
|
|
@@ -54,6 +61,7 @@ export const ddTool: ToolSpec<Input> = {
|
|
|
54
61
|
let skipped = 0;
|
|
55
62
|
let copied = 0;
|
|
56
63
|
for await (const chunk of source) {
|
|
64
|
+
throwIfAborted(ctx.signal);
|
|
57
65
|
let data = chunk;
|
|
58
66
|
if (skipped < skipBytes) {
|
|
59
67
|
const drop = Math.min(skipBytes - skipped, data.length);
|
|
@@ -70,13 +78,11 @@ export const ddTool: ToolSpec<Input> = {
|
|
|
70
78
|
const data = Buffer.concat(chunks);
|
|
71
79
|
|
|
72
80
|
if (outputPath !== undefined) {
|
|
73
|
-
|
|
81
|
+
throwIfAborted(ctx.signal);
|
|
82
|
+
let handle: Awaited<ReturnType<typeof openOutput>> | undefined;
|
|
74
83
|
try {
|
|
75
|
-
handle = await
|
|
76
|
-
if (data.length > 0) await handle
|
|
77
|
-
} catch (error) {
|
|
78
|
-
/* v8 ignore next -- write errors are environment-dependent */
|
|
79
|
-
throw fromErrno(error);
|
|
84
|
+
handle = await openOutput(outputPath);
|
|
85
|
+
if (data.length > 0) await writeFully(handle, data, seekBytes, ctx.signal);
|
|
80
86
|
} finally {
|
|
81
87
|
await handle?.close();
|
|
82
88
|
}
|
package/src/tools/diff.ts
CHANGED
|
@@ -9,7 +9,8 @@ import { createTwoFilesPatch } from "diff";
|
|
|
9
9
|
import { Type, type Static } from "@sinclair/typebox";
|
|
10
10
|
import { fromErrno } from "../core/errors.js";
|
|
11
11
|
import { mapLimit } from "../core/parallel.js";
|
|
12
|
-
import {
|
|
12
|
+
import { outputBoundary, throwIfAborted } from "../core/safe-output.js";
|
|
13
|
+
import { textResult, type RunContext, type ToolSpec } from "../core/types.js";
|
|
13
14
|
|
|
14
15
|
const Schema = Type.Object(
|
|
15
16
|
{
|
|
@@ -36,14 +37,20 @@ export const diffTool: ToolSpec<Input> = {
|
|
|
36
37
|
pipeable: false,
|
|
37
38
|
mutates: false,
|
|
38
39
|
async run(ctx, input) {
|
|
39
|
-
const left = await ctx.sandbox.
|
|
40
|
-
const right = await ctx.sandbox.
|
|
41
|
-
await ctx.permission.assert([
|
|
42
|
-
{ target: left, boundary: left.full },
|
|
43
|
-
{ target: right, boundary: right.full },
|
|
44
|
-
]);
|
|
40
|
+
const left = await ctx.sandbox.resolveFollow(input.left);
|
|
41
|
+
const right = await ctx.sandbox.resolveFollow(input.right);
|
|
45
42
|
const leftStats = await ctx.sandbox.lstat(left.full, input.left);
|
|
46
43
|
const rightStats = await ctx.sandbox.lstat(right.full, input.right);
|
|
44
|
+
await ctx.permission.assert([
|
|
45
|
+
{
|
|
46
|
+
target: left,
|
|
47
|
+
boundary: ctx.sandbox.boundaryFor(left.full, leftStats.isDirectory() ? "directory" : "file"),
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
target: right,
|
|
51
|
+
boundary: ctx.sandbox.boundaryFor(right.full, rightStats.isDirectory() ? "directory" : "file"),
|
|
52
|
+
},
|
|
53
|
+
]);
|
|
47
54
|
if (leftStats.isDirectory() || rightStats.isDirectory()) {
|
|
48
55
|
if (leftStats.isDirectory() !== rightStats.isDirectory()) {
|
|
49
56
|
return textResult(`File ${input.left} is a directory while file ${input.right} is not\n`, {}, 1);
|
|
@@ -51,7 +58,7 @@ export const diffTool: ToolSpec<Input> = {
|
|
|
51
58
|
if (input.recursive !== true) {
|
|
52
59
|
return textResult(`Common subdirectories: ${input.left} and ${input.right}\n`, {}, 1);
|
|
53
60
|
}
|
|
54
|
-
const lines = await diffDirs(left.full, right.full, input, ctx
|
|
61
|
+
const lines = await diffDirs(left.full, right.full, input, ctx);
|
|
55
62
|
return textResult(lines.join(""), { differences: lines.length > 0 }, lines.length > 0 ? 1 : 0);
|
|
56
63
|
}
|
|
57
64
|
|
|
@@ -96,14 +103,17 @@ async function diffDirs(
|
|
|
96
103
|
left: string,
|
|
97
104
|
right: string,
|
|
98
105
|
input: Input,
|
|
99
|
-
|
|
106
|
+
ctx: RunContext,
|
|
100
107
|
): Promise<string[]> {
|
|
101
108
|
const items: DiffItem[] = [];
|
|
102
|
-
await planDir(left, right, input, items);
|
|
109
|
+
await planDir(left, right, input, items, ctx.signal);
|
|
103
110
|
|
|
104
111
|
const files = items.filter((item): item is Extract<DiffItem, { kind: "file" }> => item.kind === "file");
|
|
105
|
-
const patches = await mapLimit(files, Math.max(1, concurrency), async (item) => {
|
|
106
|
-
const [leftData, rightData] = await Promise.all([
|
|
112
|
+
const patches = await mapLimit(files, Math.max(1, ctx.concurrency), async (item) => {
|
|
113
|
+
const [leftData, rightData] = await Promise.all([
|
|
114
|
+
readDiffFile(ctx, item.left),
|
|
115
|
+
readDiffFile(ctx, item.right),
|
|
116
|
+
]);
|
|
107
117
|
/* v8 ignore next */
|
|
108
118
|
if (leftData.equals(rightData)) return undefined;
|
|
109
119
|
return createTwoFilesPatch(
|
|
@@ -115,7 +125,7 @@ async function diffDirs(
|
|
|
115
125
|
"",
|
|
116
126
|
{ context: input.unified ?? 3 },
|
|
117
127
|
);
|
|
118
|
-
});
|
|
128
|
+
}, ctx.signal);
|
|
119
129
|
|
|
120
130
|
const output: string[] = [];
|
|
121
131
|
let index = 0;
|
|
@@ -130,15 +140,24 @@ async function diffDirs(
|
|
|
130
140
|
return output;
|
|
131
141
|
}
|
|
132
142
|
|
|
133
|
-
async function planDir(
|
|
143
|
+
async function planDir(
|
|
144
|
+
left: string,
|
|
145
|
+
right: string,
|
|
146
|
+
input: Input,
|
|
147
|
+
items: DiffItem[],
|
|
148
|
+
signal: AbortSignal,
|
|
149
|
+
): Promise<void> {
|
|
134
150
|
const names = new Set<string>();
|
|
135
151
|
for (const dir of [left, right]) {
|
|
152
|
+
throwIfAborted(signal);
|
|
136
153
|
for (const name of await fs.readdir(dir)) {
|
|
154
|
+
throwIfAborted(signal);
|
|
137
155
|
if (input.exclude?.some((pattern) => name.includes(pattern.replaceAll("*", ""))) === true) continue;
|
|
138
156
|
names.add(name);
|
|
139
157
|
}
|
|
140
158
|
}
|
|
141
159
|
for (const name of [...names].sort()) {
|
|
160
|
+
throwIfAborted(signal);
|
|
142
161
|
const leftPath = path.join(left, name);
|
|
143
162
|
const rightPath = path.join(right, name);
|
|
144
163
|
const leftExists = await exists(leftPath);
|
|
@@ -151,7 +170,7 @@ async function planDir(left: string, right: string, input: Input, items: DiffIte
|
|
|
151
170
|
const leftStats = await fs.lstat(leftPath);
|
|
152
171
|
const rightStats = await fs.lstat(rightPath);
|
|
153
172
|
if (leftStats.isDirectory() && rightStats.isDirectory()) {
|
|
154
|
-
await planDir(leftPath, rightPath, input, items);
|
|
173
|
+
await planDir(leftPath, rightPath, input, items, signal);
|
|
155
174
|
/* v8 ignore next 4 */
|
|
156
175
|
} else if (leftStats.isDirectory() !== rightStats.isDirectory()) {
|
|
157
176
|
/* v8 ignore next */
|
|
@@ -162,6 +181,18 @@ async function planDir(left: string, right: string, input: Input, items: DiffIte
|
|
|
162
181
|
}
|
|
163
182
|
}
|
|
164
183
|
|
|
184
|
+
/**
|
|
185
|
+
* Recursive diff reads regular children directly. A symlink child is resolved
|
|
186
|
+
* and gated first, so an external link cannot read without permission.
|
|
187
|
+
*/
|
|
188
|
+
async function readDiffFile(ctx: RunContext, file: string): Promise<Buffer> {
|
|
189
|
+
const stats = await fs.lstat(file);
|
|
190
|
+
if (!stats.isSymbolicLink()) return fs.readFile(file);
|
|
191
|
+
const target = await ctx.sandbox.resolveFollow(file);
|
|
192
|
+
await ctx.permission.assert([{ target, boundary: outputBoundary(target.full) }]);
|
|
193
|
+
return fs.readFile(target.full);
|
|
194
|
+
}
|
|
195
|
+
|
|
165
196
|
async function exists(target: string): Promise<boolean> {
|
|
166
197
|
try {
|
|
167
198
|
await fs.lstat(target);
|
package/src/tools/grep.ts
CHANGED
|
@@ -74,15 +74,18 @@ export const grepTool: ToolSpec<Input> = {
|
|
|
74
74
|
for (const requested of paths) {
|
|
75
75
|
let target;
|
|
76
76
|
try {
|
|
77
|
-
target = await ctx.sandbox.
|
|
77
|
+
target = await ctx.sandbox.resolveFollow(requested);
|
|
78
78
|
} catch (error) {
|
|
79
79
|
/* v8 ignore next 3 */
|
|
80
80
|
if (input.noMessages === true) continue;
|
|
81
81
|
/* v8 ignore next */
|
|
82
82
|
throw error;
|
|
83
83
|
}
|
|
84
|
-
await ctx.permission.assert([{ target, boundary: target.full }]);
|
|
85
84
|
const stats = await ctx.sandbox.lstat(target.full, requested);
|
|
85
|
+
await ctx.permission.assert([{
|
|
86
|
+
target,
|
|
87
|
+
boundary: ctx.sandbox.boundaryFor(target.full, stats.isDirectory() ? "directory" : "file"),
|
|
88
|
+
}]);
|
|
86
89
|
if (stats.isDirectory()) {
|
|
87
90
|
if (input.recursive !== true) {
|
|
88
91
|
throw new FsError(`"${requested}" is a directory; recursive is required.`, "not-file");
|
package/src/tools/gzip.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { promisify } from "node:util";
|
|
|
9
9
|
import { Type, type Static } from "@sinclair/typebox";
|
|
10
10
|
import { FsError, fromErrno } from "../core/errors.js";
|
|
11
11
|
import { mapLimit } from "../core/parallel.js";
|
|
12
|
+
import { outputBoundary, resolveOutputTarget, writeOutput } from "../core/safe-output.js";
|
|
12
13
|
import { collect } from "../core/stream.js";
|
|
13
14
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
14
15
|
|
|
@@ -57,14 +58,16 @@ export const gzipTool: ToolSpec<GzipInput> = {
|
|
|
57
58
|
const targets = [];
|
|
58
59
|
for (const requested of input.paths) {
|
|
59
60
|
const source = await ctx.sandbox.resolveFollow(requested);
|
|
60
|
-
const destination =
|
|
61
|
+
const destination = input.stdout === true
|
|
62
|
+
? source
|
|
63
|
+
: await resolveOutputTarget(ctx.sandbox, `${requested}.gz`);
|
|
61
64
|
targets.push({ requested, source, destination });
|
|
62
65
|
}
|
|
63
66
|
await ctx.permission.assert(
|
|
64
67
|
targets.flatMap(({ source, destination }) => [
|
|
65
|
-
{ target: source, boundary: source.full },
|
|
68
|
+
{ target: source, boundary: outputBoundary(source.full) },
|
|
66
69
|
/* v8 ignore next */
|
|
67
|
-
...(input.stdout === true ? [] : [{ target: destination, boundary: destination.full }]),
|
|
70
|
+
...(input.stdout === true ? [] : [{ target: destination, boundary: outputBoundary(destination.full) }]),
|
|
68
71
|
]),
|
|
69
72
|
);
|
|
70
73
|
const results = await mapLimit(targets, ctx.concurrency, async ({ source, destination }) => {
|
|
@@ -77,10 +80,7 @@ export const gzipTool: ToolSpec<GzipInput> = {
|
|
|
77
80
|
if (input.force !== true && (await exists(destination.full))) {
|
|
78
81
|
throw new FsError(`Output exists: "${destination.full}"`, "exists");
|
|
79
82
|
}
|
|
80
|
-
|
|
81
|
-
await fs.writeFile(destination.full, compressed).catch((error) => {
|
|
82
|
-
throw fromErrno(error);
|
|
83
|
-
});
|
|
83
|
+
await writeOutput(destination.full, compressed, false, 0o666, ctx.signal);
|
|
84
84
|
/* v8 ignore next */
|
|
85
85
|
if (input.keep !== true) await fs.unlink(source.full).catch(() => undefined);
|
|
86
86
|
return Buffer.alloc(0);
|
|
@@ -112,15 +112,16 @@ export const gunzipTool: ToolSpec<GunzipInput> = {
|
|
|
112
112
|
if (!requested.endsWith(suffix)) throw new FsError(`"${requested}" does not end with ${suffix}.`, "invalid");
|
|
113
113
|
const source = await ctx.sandbox.resolveFollow(requested);
|
|
114
114
|
const output = requested.slice(0, -suffix.length);
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
const destination = input.stdout === true
|
|
116
|
+
? source
|
|
117
|
+
: await resolveOutputTarget(ctx.sandbox, output);
|
|
117
118
|
targets.push({ requested, source, destination });
|
|
118
119
|
}
|
|
119
120
|
await ctx.permission.assert(
|
|
120
121
|
targets.flatMap(({ source, destination }) => [
|
|
121
|
-
{ target: source, boundary: source.full },
|
|
122
|
+
{ target: source, boundary: outputBoundary(source.full) },
|
|
122
123
|
/* v8 ignore next */
|
|
123
|
-
...(input.stdout === true ? [] : [{ target: destination, boundary: destination.full }]),
|
|
124
|
+
...(input.stdout === true ? [] : [{ target: destination, boundary: outputBoundary(destination.full) }]),
|
|
124
125
|
]),
|
|
125
126
|
);
|
|
126
127
|
const results = await mapLimit(targets, ctx.concurrency, async ({ source, destination }) => {
|
|
@@ -138,10 +139,7 @@ export const gunzipTool: ToolSpec<GunzipInput> = {
|
|
|
138
139
|
if (input.force !== true && (await exists(destination.full))) {
|
|
139
140
|
throw new FsError(`Output exists: "${destination.full}"`, "exists");
|
|
140
141
|
}
|
|
141
|
-
|
|
142
|
-
await fs.writeFile(destination.full, decompressed).catch((error) => {
|
|
143
|
-
throw fromErrno(error);
|
|
144
|
-
});
|
|
142
|
+
await writeOutput(destination.full, decompressed, false, 0o666, ctx.signal);
|
|
145
143
|
/* v8 ignore next */
|
|
146
144
|
if (input.keep !== true) await fs.unlink(source.full).catch(() => undefined);
|
|
147
145
|
return Buffer.alloc(0);
|
package/src/tools/mktemp.ts
CHANGED
|
@@ -7,7 +7,7 @@ import * as fs from "node:fs/promises";
|
|
|
7
7
|
import * as path from "node:path";
|
|
8
8
|
import { randomBytes } from "node:crypto";
|
|
9
9
|
import { Type, type Static } from "@sinclair/typebox";
|
|
10
|
-
import { FsError, fromErrno } from "../core/errors.js";
|
|
10
|
+
import { FsError, errnoCode, fromErrno } from "../core/errors.js";
|
|
11
11
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
12
12
|
|
|
13
13
|
const Schema = Type.Object(
|
|
@@ -31,25 +31,27 @@ export const mktempTool: ToolSpec<Input> = {
|
|
|
31
31
|
pipeable: false,
|
|
32
32
|
mutates: true,
|
|
33
33
|
async run(ctx, input) {
|
|
34
|
-
/* v8 ignore next 2 */
|
|
35
|
-
const dir = path.resolve(input.tmpdir ?? ctx.tmpdir);
|
|
36
|
-
/* v8 ignore next */
|
|
37
34
|
const template = input.template ?? "tmp.XXXXXXXXXX";
|
|
38
35
|
const tail = /X+$/.exec(template)?.[0] ?? "";
|
|
39
36
|
if (tail.length < 3) throw new FsError("template must end with at least three X characters.", "invalid");
|
|
40
|
-
|
|
37
|
+
assertComponent(template, "template");
|
|
38
|
+
if (input.suffix !== undefined) assertComponent(input.suffix, "suffix");
|
|
41
39
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
40
|
+
const userTarget = input.tmpdir === undefined
|
|
41
|
+
? undefined
|
|
42
|
+
: await ctx.sandbox.resolveForWrite(input.tmpdir);
|
|
43
|
+
const dir = userTarget?.full ?? path.resolve(ctx.tmpdir);
|
|
44
|
+
if (userTarget === undefined) {
|
|
45
|
+
await ctx.permission.assertTrusted([path.join(dir, "*")]);
|
|
46
|
+
} else {
|
|
47
|
+
const relative = ctx.sandbox.resource(dir);
|
|
48
|
+
await ctx.permission.assert([
|
|
49
|
+
{
|
|
50
|
+
target: { ...userTarget, full: path.join(dir, "*") },
|
|
51
|
+
boundary: dir,
|
|
52
|
+
resource: relative === "." ? "./*" : `${relative}/*`,
|
|
53
|
+
},
|
|
54
|
+
]);
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
try {
|
|
@@ -58,26 +60,39 @@ export const mktempTool: ToolSpec<Input> = {
|
|
|
58
60
|
/* v8 ignore next */
|
|
59
61
|
throw fromErrno(error);
|
|
60
62
|
}
|
|
63
|
+
|
|
64
|
+
const prefix = template.slice(0, -tail.length);
|
|
61
65
|
for (let attempt = 0; attempt < 20; attempt++) {
|
|
62
|
-
const candidate = path.join(dir,
|
|
66
|
+
const candidate = path.join(dir, `${prefix}${randomToken(tail.length)}${input.suffix ?? ""}`);
|
|
63
67
|
try {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
68
|
+
if (input.directory === true) {
|
|
69
|
+
await fs.mkdir(candidate, { mode: 0o700 });
|
|
70
|
+
} else {
|
|
71
|
+
const handle = await fs.open(candidate, "wx", 0o600);
|
|
72
|
+
await handle.close();
|
|
73
|
+
}
|
|
74
|
+
return textResult(`${candidate}\n`, { path: candidate, directory: input.directory === true });
|
|
69
75
|
} catch (error) {
|
|
70
|
-
|
|
71
|
-
if ((error as NodeJS.ErrnoException)?.code === "EEXIST") continue;
|
|
72
|
-
/* v8 ignore next */
|
|
76
|
+
if (errnoCode(error) === "EEXIST") continue;
|
|
73
77
|
throw fromErrno(error);
|
|
74
78
|
}
|
|
75
79
|
}
|
|
76
|
-
/* v8 ignore next */
|
|
77
80
|
throw new FsError("could not create a unique temporary path.", "exists");
|
|
78
81
|
},
|
|
79
82
|
};
|
|
80
83
|
|
|
84
|
+
function assertComponent(value: string, label: string): void {
|
|
85
|
+
if (
|
|
86
|
+
value === "." ||
|
|
87
|
+
value === ".." ||
|
|
88
|
+
value.includes("\0") ||
|
|
89
|
+
value.includes("/") ||
|
|
90
|
+
value.includes("\\")
|
|
91
|
+
) {
|
|
92
|
+
throw new FsError(`${label} must be a single path component.`, "invalid");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
81
96
|
function randomToken(length: number): string {
|
|
82
97
|
return randomBytes(Math.ceil(length / 2)).toString("hex").slice(0, length);
|
|
83
98
|
}
|