opencode-fs-plugin 3.5.0 → 3.6.1
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 +118 -28
- package/SPEC.md +119 -20
- package/package.json +1 -1
- package/skills/filesystem/SKILL.md +92 -21
- package/src/core/fsutil.ts +84 -23
- package/src/core/jobs.ts +210 -34
- package/src/core/relay-protocol.ts +45 -0
- package/src/core/safe-output.ts +290 -0
- package/src/core/sandbox.ts +43 -0
- package/src/index.ts +23 -6
- package/src/tools/ar.ts +62 -29
- package/src/tools/chmod.ts +30 -19
- package/src/tools/chown.ts +24 -16
- package/src/tools/command.ts +8 -5
- package/src/tools/cp.ts +84 -20
- package/src/tools/cpio.ts +204 -65
- package/src/tools/curl.ts +123 -6
- 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 +17 -9
- 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/tools/mv.ts
CHANGED
|
@@ -8,7 +8,8 @@ import * as path from "node:path";
|
|
|
8
8
|
import { Type, type Static } from "@sinclair/typebox";
|
|
9
9
|
import { sourcesFrom } from "../core/aliases.js";
|
|
10
10
|
import { FsError, fromErrno, isMissing } from "../core/errors.js";
|
|
11
|
-
import { copyTree, ensureParent } from "../core/fsutil.js";
|
|
11
|
+
import { copyTree, ensureParent, isSubpath, num } from "../core/fsutil.js";
|
|
12
|
+
import { createLimiter } from "../core/parallel.js";
|
|
12
13
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
13
14
|
|
|
14
15
|
const Schema = Type.Object(
|
|
@@ -42,7 +43,18 @@ export const mvTool: ToolSpec<Input> = {
|
|
|
42
43
|
const target = await ctx.sandbox.resolveExisting(requested);
|
|
43
44
|
ctx.sandbox.assertSafeToDelete(target.full);
|
|
44
45
|
const stats = await ctx.sandbox.lstat(target.full, requested);
|
|
45
|
-
|
|
46
|
+
let referent: { dev: number; ino: number } | undefined;
|
|
47
|
+
if (stats.isSymbolicLink()) {
|
|
48
|
+
try {
|
|
49
|
+
const followed = await ctx.sandbox.resolveFollow(requested);
|
|
50
|
+
const referentStats = await ctx.sandbox.stat(followed.full, requested);
|
|
51
|
+
referent = { dev: num(referentStats.dev), ino: num(referentStats.ino) };
|
|
52
|
+
} catch (error) {
|
|
53
|
+
/* v8 ignore next */
|
|
54
|
+
if (!(error instanceof FsError) || error.code !== "not-found") throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
sources.push({ requested, target, stats, referent });
|
|
46
58
|
}
|
|
47
59
|
|
|
48
60
|
const destTarget = await ctx.sandbox.resolveForCreate(input.destination);
|
|
@@ -69,6 +81,21 @@ export const mvTool: ToolSpec<Input> = {
|
|
|
69
81
|
if (job.target.full === job.destination) {
|
|
70
82
|
throw new FsError(`"${job.requested}" and destination are the same file.`, "invalid");
|
|
71
83
|
}
|
|
84
|
+
if (job.stats.isDirectory() && isSubpath(job.target.full, job.destination)) {
|
|
85
|
+
throw new FsError(`Cannot move a directory into itself: "${job.requested}".`, "invalid");
|
|
86
|
+
}
|
|
87
|
+
const destinationStats = await lstatOptional(job.destination);
|
|
88
|
+
if (
|
|
89
|
+
job.referent !== undefined &&
|
|
90
|
+
destinationStats !== undefined &&
|
|
91
|
+
job.referent.dev === num(destinationStats.dev) &&
|
|
92
|
+
job.referent.ino === num(destinationStats.ino)
|
|
93
|
+
) {
|
|
94
|
+
throw new FsError(
|
|
95
|
+
`"${job.requested}" refers to the destination file itself.`,
|
|
96
|
+
"invalid",
|
|
97
|
+
);
|
|
98
|
+
}
|
|
72
99
|
}
|
|
73
100
|
|
|
74
101
|
const entries = jobs.map((job) => ({
|
|
@@ -81,6 +108,7 @@ export const mvTool: ToolSpec<Input> = {
|
|
|
81
108
|
}
|
|
82
109
|
await ctx.permission.assert(entries);
|
|
83
110
|
|
|
111
|
+
const limiter = createLimiter(ctx.concurrency);
|
|
84
112
|
const moved: string[] = [];
|
|
85
113
|
for (const job of jobs) {
|
|
86
114
|
if (input.noClobber === true && (await exists(job.destination))) continue;
|
|
@@ -93,28 +121,39 @@ export const mvTool: ToolSpec<Input> = {
|
|
|
93
121
|
try {
|
|
94
122
|
await fs.rename(job.target.full, job.destination);
|
|
95
123
|
} catch (error) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
await fs.rm(job.destination, { recursive: true, force: true });
|
|
101
|
-
await fs.rename(job.target.full, job.destination);
|
|
102
|
-
} else {
|
|
103
|
-
/* v8 ignore next */
|
|
104
|
-
throw fromErrno(error);
|
|
105
|
-
}
|
|
106
|
-
} else {
|
|
107
|
-
await copyTree(job.target.full, job.destination, {
|
|
108
|
-
recursive: true,
|
|
109
|
-
dereference: false,
|
|
110
|
-
preserve: true,
|
|
111
|
-
noClobber: input.noClobber === true,
|
|
112
|
-
update: input.update === true,
|
|
113
|
-
signal: ctx.signal,
|
|
114
|
-
concurrency: ctx.concurrency,
|
|
115
|
-
});
|
|
116
|
-
await fs.rm(job.target.full, { recursive: true, force: true });
|
|
124
|
+
if ((error as NodeJS.ErrnoException)?.code !== "EXDEV") throw fromErrno(error);
|
|
125
|
+
const destinationStats = await lstatOptional(job.destination);
|
|
126
|
+
if (destinationStats?.isSymbolicLink() === true) {
|
|
127
|
+
throw new FsError(`Refusing to move across devices onto a symbolic link: "${job.destination}".`, "invalid");
|
|
117
128
|
}
|
|
129
|
+
const skipped = { count: 0 };
|
|
130
|
+
const copied = await copyTree(job.target.full, job.destination, {
|
|
131
|
+
recursive: true,
|
|
132
|
+
dereference: false,
|
|
133
|
+
preserve: true,
|
|
134
|
+
noClobber: input.noClobber === true,
|
|
135
|
+
update: input.update === true,
|
|
136
|
+
signal: ctx.signal,
|
|
137
|
+
concurrency: ctx.concurrency,
|
|
138
|
+
limiter,
|
|
139
|
+
onSkip: () => {
|
|
140
|
+
skipped.count++;
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
if (copied === 0) {
|
|
144
|
+
throw new FsError(
|
|
145
|
+
`Cross-device move did not transfer "${job.requested}"; source preserved.`,
|
|
146
|
+
"cross-device",
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
if (skipped.count > 0) {
|
|
150
|
+
throw new FsError(
|
|
151
|
+
`Cross-device move left a partial destination at "${job.destination}"; ` +
|
|
152
|
+
`source "${job.requested}" preserved.`,
|
|
153
|
+
"cross-device",
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
await fs.rm(job.target.full, { recursive: true, force: true });
|
|
118
157
|
}
|
|
119
158
|
moved.push(job.requested);
|
|
120
159
|
}
|
|
@@ -127,13 +166,15 @@ export const mvTool: ToolSpec<Input> = {
|
|
|
127
166
|
};
|
|
128
167
|
|
|
129
168
|
async function exists(target: string): Promise<boolean> {
|
|
169
|
+
return (await lstatOptional(target)) !== undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function lstatOptional(target: string): Promise<Awaited<ReturnType<typeof fs.lstat>> | undefined> {
|
|
130
173
|
try {
|
|
131
|
-
await fs.lstat(target);
|
|
132
|
-
return true;
|
|
174
|
+
return await fs.lstat(target);
|
|
133
175
|
} catch (error) {
|
|
134
176
|
/* v8 ignore next */
|
|
135
|
-
if (isMissing(error))
|
|
136
|
-
|
|
137
|
-
throw fromErrno(error, "access");
|
|
177
|
+
if (!isMissing(error)) throw fromErrno(error, "access");
|
|
178
|
+
return undefined;
|
|
138
179
|
}
|
|
139
180
|
}
|
package/src/tools/pipe.ts
CHANGED
|
@@ -5,8 +5,13 @@
|
|
|
5
5
|
|
|
6
6
|
import { Type, type Static } from "@sinclair/typebox";
|
|
7
7
|
import { FsError } from "../core/errors.js";
|
|
8
|
+
import {
|
|
9
|
+
outputBoundary,
|
|
10
|
+
resolveOutputTarget,
|
|
11
|
+
throwIfAborted,
|
|
12
|
+
writeOutput,
|
|
13
|
+
} from "../core/safe-output.js";
|
|
8
14
|
import { parseSchema } from "../core/schema.js";
|
|
9
|
-
import { writeAll } from "../core/stream.js";
|
|
10
15
|
import { textResult, type ToolSpec, type ToolResult } from "../core/types.js";
|
|
11
16
|
import { base64Tool } from "./base64.js";
|
|
12
17
|
import { catTool } from "./cat.js";
|
|
@@ -81,6 +86,7 @@ export const pipeTool: ToolSpec<Input> = {
|
|
|
81
86
|
const stages: Array<{ tool: string; bytesIn: number; bytesOut: number; exitCode: number }> = [];
|
|
82
87
|
|
|
83
88
|
for (let index = 0; index < input.stages.length; index++) {
|
|
89
|
+
throwIfAborted(ctx.signal);
|
|
84
90
|
const stage = input.stages[index] as Static<typeof Stage>;
|
|
85
91
|
const tool = REGISTRY.get(stage.tool);
|
|
86
92
|
if (tool === undefined) {
|
|
@@ -97,27 +103,29 @@ export const pipeTool: ToolSpec<Input> = {
|
|
|
97
103
|
parsed,
|
|
98
104
|
{ ...(data === undefined ? {} : { stdin: fromBuffer(data) }), piped: true },
|
|
99
105
|
) as ToolResult;
|
|
106
|
+
const stdout = Buffer.isBuffer(result.stdout) ? result.stdout : Buffer.alloc(0);
|
|
100
107
|
stages.push({
|
|
101
108
|
tool: stage.tool,
|
|
102
109
|
/* v8 ignore next */
|
|
103
110
|
bytesIn: data?.length ?? 0,
|
|
104
|
-
bytesOut:
|
|
111
|
+
bytesOut: stdout.length,
|
|
105
112
|
/* v8 ignore next */
|
|
106
113
|
exitCode: result.exitCode ?? 0,
|
|
107
114
|
});
|
|
108
|
-
data =
|
|
115
|
+
data = stdout;
|
|
109
116
|
}
|
|
110
117
|
|
|
118
|
+
// A stage always yields a Buffer; non-Buffer output was normalized to an
|
|
119
|
+
// empty Buffer in the stage loop above.
|
|
120
|
+
const stdout = data as Buffer;
|
|
111
121
|
if (input.output !== undefined) {
|
|
112
|
-
const target = await ctx.sandbox
|
|
122
|
+
const target = await resolveOutputTarget(ctx.sandbox, input.output);
|
|
113
123
|
const gate = ctx.permissionFor("fs_tee");
|
|
114
|
-
await gate.assert([{ target, boundary: target.full }]);
|
|
115
|
-
|
|
116
|
-
await writeAll(target.full, data ?? Buffer.alloc(0), input.append === true);
|
|
124
|
+
await gate.assert([{ target, boundary: outputBoundary(target.full) }]);
|
|
125
|
+
await writeOutput(target.full, stdout, input.append === true, 0o666, ctx.signal);
|
|
117
126
|
}
|
|
118
127
|
|
|
119
|
-
|
|
120
|
-
return textResult(data?.toString("utf8") ?? "", { stages, output: input.output ?? null });
|
|
128
|
+
return textResult(stdout.toString("utf8"), { stages, output: input.output ?? null });
|
|
121
129
|
},
|
|
122
130
|
};
|
|
123
131
|
|
package/src/tools/split.ts
CHANGED
|
@@ -3,10 +3,14 @@
|
|
|
3
3
|
* license-wizard managed-header v1 EPL-2.0 short 73897e75828f
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import * as fs from "node:fs/promises";
|
|
7
6
|
import { Type, type Static } from "@sinclair/typebox";
|
|
8
|
-
import { FsError
|
|
7
|
+
import { FsError } from "../core/errors.js";
|
|
9
8
|
import { mapLimit } from "../core/parallel.js";
|
|
9
|
+
import {
|
|
10
|
+
outputBoundary,
|
|
11
|
+
resolveOutputTarget,
|
|
12
|
+
writeOutput,
|
|
13
|
+
} from "../core/safe-output.js";
|
|
10
14
|
import { collect, inputStream, lines } from "../core/stream.js";
|
|
11
15
|
import type { ResolvedTarget } from "../core/sandbox.js";
|
|
12
16
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
@@ -43,7 +47,7 @@ export const splitTool: ToolSpec<Input> = {
|
|
|
43
47
|
let inputPath: string | undefined;
|
|
44
48
|
if (input.input !== undefined) {
|
|
45
49
|
const target = await ctx.sandbox.resolveFollow(input.input);
|
|
46
|
-
await ctx.permission.assert([{ target, boundary: target.full }]);
|
|
50
|
+
await ctx.permission.assert([{ target, boundary: outputBoundary(target.full) }]);
|
|
47
51
|
inputPath = target.full;
|
|
48
52
|
}
|
|
49
53
|
const data = await collect(inputStream(inputPath === undefined ? [] : [inputPath], io.stdin));
|
|
@@ -51,20 +55,30 @@ export const splitTool: ToolSpec<Input> = {
|
|
|
51
55
|
? byteChunks(data, input.bytes as number)
|
|
52
56
|
: await lineChunks(data, input.lines);
|
|
53
57
|
|
|
58
|
+
const capacity = (input.numeric === true ? 10 : 26) ** suffixLength;
|
|
59
|
+
if (chunks.length > capacity) {
|
|
60
|
+
throw new FsError(
|
|
61
|
+
`Suffix length ${suffixLength} cannot name ${chunks.length} chunks.`,
|
|
62
|
+
"invalid",
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
54
66
|
const names = chunks.map((_chunk, index) => `${prefix}${suffix(index, suffixLength, input.numeric === true)}${additional}`);
|
|
55
67
|
const targets: ResolvedTarget[] = [];
|
|
56
68
|
for (const name of names) {
|
|
57
|
-
|
|
58
|
-
targets.push(target);
|
|
69
|
+
targets.push(await resolveOutputTarget(ctx.sandbox, name));
|
|
59
70
|
}
|
|
60
|
-
await ctx.permission.assert(
|
|
71
|
+
await ctx.permission.assert(
|
|
72
|
+
targets.map((target) => ({ target, boundary: outputBoundary(target.full) })),
|
|
73
|
+
);
|
|
61
74
|
await mapLimit(chunks, ctx.concurrency, async (chunk, index) => {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
75
|
+
await writeOutput(
|
|
76
|
+
(targets[index] as { full: string }).full,
|
|
77
|
+
chunk,
|
|
78
|
+
false,
|
|
79
|
+
0o666,
|
|
80
|
+
ctx.signal,
|
|
81
|
+
);
|
|
68
82
|
}, ctx.signal);
|
|
69
83
|
return textResult("", { files: names, chunks: chunks.length });
|
|
70
84
|
},
|
package/src/tools/tar.ts
CHANGED
|
@@ -11,6 +11,24 @@ import * as tar from "tar-stream";
|
|
|
11
11
|
import picomatch from "picomatch";
|
|
12
12
|
import { Type, type Static } from "@sinclair/typebox";
|
|
13
13
|
import { FsError, fromErrno } from "../core/errors.js";
|
|
14
|
+
import { walkAll } from "../core/fsutil.js";
|
|
15
|
+
import { mapLimit } from "../core/parallel.js";
|
|
16
|
+
import {
|
|
17
|
+
archiveSourceName,
|
|
18
|
+
assertArchivePlan,
|
|
19
|
+
assertArchiveSymlinkPlan,
|
|
20
|
+
assertSafeLinkTarget,
|
|
21
|
+
chmodDirectoryNoFollow,
|
|
22
|
+
finalDirectoryPlans,
|
|
23
|
+
outputBoundary,
|
|
24
|
+
resolveArchiveTarget,
|
|
25
|
+
resolveOutputTarget,
|
|
26
|
+
safeDirectoryMode,
|
|
27
|
+
safeMode,
|
|
28
|
+
throwIfAborted,
|
|
29
|
+
writeOutput,
|
|
30
|
+
} from "../core/safe-output.js";
|
|
31
|
+
import type { ResolvedTarget } from "../core/sandbox.js";
|
|
14
32
|
import { collect } from "../core/stream.js";
|
|
15
33
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
16
34
|
|
|
@@ -47,10 +65,15 @@ export const tarTool: ToolSpec<Input> = {
|
|
|
47
65
|
pipeable: "sink",
|
|
48
66
|
mutates: true,
|
|
49
67
|
async run(ctx, input, io) {
|
|
50
|
-
const archive =
|
|
68
|
+
const archive = input.mode === "create"
|
|
69
|
+
? await resolveOutputTarget(ctx.sandbox, input.archive)
|
|
70
|
+
: await ctx.sandbox.resolveFollow(input.archive);
|
|
51
71
|
const useGzip = input.gzip ?? /\.(?:tgz|tar\.gz)$/i.test(input.archive);
|
|
52
72
|
const excludes = input.exclude === undefined ? undefined : picomatch(input.exclude, { dot: true, basename: true });
|
|
53
73
|
const names: string[] = [];
|
|
74
|
+
if (input.mode !== "create") {
|
|
75
|
+
await ctx.permission.assert([{ target: archive, boundary: outputBoundary(archive.full) }]);
|
|
76
|
+
}
|
|
54
77
|
|
|
55
78
|
if (input.mode === "create") {
|
|
56
79
|
if (input.paths === undefined || input.paths.length === 0) {
|
|
@@ -62,7 +85,7 @@ export const tarTool: ToolSpec<Input> = {
|
|
|
62
85
|
: await collect(io.stdin);
|
|
63
86
|
const name = safeMember(input.member ?? "stdin", 0);
|
|
64
87
|
if (name === undefined) throw new FsError("member must not be empty.", "invalid");
|
|
65
|
-
await ctx.permission.assert([{ target: archive, boundary: archive.full }]);
|
|
88
|
+
await ctx.permission.assert([{ target: archive, boundary: outputBoundary(archive.full) }]);
|
|
66
89
|
const pack = tar.pack();
|
|
67
90
|
const header: tar.Header = {
|
|
68
91
|
name: name.split(path.sep).join("/"),
|
|
@@ -82,34 +105,47 @@ export const tarTool: ToolSpec<Input> = {
|
|
|
82
105
|
pack.finalize();
|
|
83
106
|
let buffer = await collect(pack as AsyncIterable<Buffer>);
|
|
84
107
|
if (useGzip) buffer = await gzipAsync(buffer);
|
|
85
|
-
|
|
86
|
-
await fs.writeFile(archive.full, buffer).catch((error) => {
|
|
87
|
-
throw fromErrno(error);
|
|
88
|
-
});
|
|
108
|
+
await writeOutput(archive.full, buffer, false, 0o666, ctx.signal);
|
|
89
109
|
return textResult(input.verbose === true ? `${header.name}\n` : "", {
|
|
90
110
|
archive: input.archive,
|
|
91
111
|
members: [header.name],
|
|
92
112
|
});
|
|
93
113
|
}
|
|
114
|
+
const createBase = input.directory === undefined
|
|
115
|
+
? ctx.sandbox.root
|
|
116
|
+
: (await ctx.sandbox.resolveFollow(input.directory)).full;
|
|
94
117
|
const members: Array<{ name: string; full: string; stats: Awaited<ReturnType<typeof fs.lstat>> }> = [];
|
|
95
118
|
for (const requested of input.paths) {
|
|
96
|
-
|
|
119
|
+
throwIfAborted(ctx.signal);
|
|
120
|
+
const source = path.isAbsolute(requested) ? requested : path.join(createBase, requested);
|
|
121
|
+
const target = await ctx.sandbox.resolveExisting(source, { allowRoot: true });
|
|
97
122
|
const stats = await ctx.sandbox.lstat(target.full, requested);
|
|
98
|
-
const
|
|
99
|
-
members.push({ name
|
|
123
|
+
const name = archiveSourceName(requested, target.full, createBase, ctx.sandbox.root);
|
|
124
|
+
members.push({ name, full: target.full, stats });
|
|
100
125
|
if (stats.isDirectory()) {
|
|
101
|
-
await collectMembers(
|
|
126
|
+
await collectMembers(
|
|
127
|
+
target.full,
|
|
128
|
+
name,
|
|
129
|
+
members,
|
|
130
|
+
excludes,
|
|
131
|
+
ctx.concurrency,
|
|
132
|
+
ctx.signal,
|
|
133
|
+
);
|
|
102
134
|
}
|
|
103
135
|
}
|
|
104
|
-
const entries = await
|
|
105
|
-
members
|
|
136
|
+
const entries = await mapLimit(
|
|
137
|
+
members,
|
|
138
|
+
ctx.concurrency,
|
|
139
|
+
async (member) => ({
|
|
106
140
|
target: await ctx.sandbox.resolveExisting(member.full, { allowRoot: true }),
|
|
107
141
|
boundary: member.stats.isDirectory() ? member.full : path.dirname(member.full),
|
|
108
|
-
})
|
|
142
|
+
}),
|
|
143
|
+
ctx.signal,
|
|
109
144
|
);
|
|
110
|
-
await ctx.permission.assert([...entries, { target: archive, boundary: archive.full }]);
|
|
145
|
+
await ctx.permission.assert([...entries, { target: archive, boundary: outputBoundary(archive.full) }]);
|
|
111
146
|
const pack = tar.pack();
|
|
112
147
|
for (const member of members) {
|
|
148
|
+
throwIfAborted(ctx.signal);
|
|
113
149
|
const header: tar.Header = {
|
|
114
150
|
name: member.name.split(path.sep).join("/"),
|
|
115
151
|
size: member.stats.isFile() ? Number(member.stats.size) : 0,
|
|
@@ -134,10 +170,7 @@ export const tarTool: ToolSpec<Input> = {
|
|
|
134
170
|
pack.finalize();
|
|
135
171
|
let buffer = await collect(pack as AsyncIterable<Buffer>);
|
|
136
172
|
if (useGzip) buffer = await gzipAsync(buffer);
|
|
137
|
-
|
|
138
|
-
await fs.writeFile(archive.full, buffer).catch((error) => {
|
|
139
|
-
throw fromErrno(error);
|
|
140
|
-
});
|
|
173
|
+
await writeOutput(archive.full, buffer, false, 0o666, ctx.signal);
|
|
141
174
|
return textResult(input.verbose === true ? names.map((name) => `${name}\n`).join("") : "", {
|
|
142
175
|
archive: input.archive,
|
|
143
176
|
members: names,
|
|
@@ -156,6 +189,7 @@ export const tarTool: ToolSpec<Input> = {
|
|
|
156
189
|
const extract = tar.extract();
|
|
157
190
|
extract.end(buffer);
|
|
158
191
|
for await (const entry of extract) {
|
|
192
|
+
throwIfAborted(ctx.signal);
|
|
159
193
|
names.push(entry.header.name);
|
|
160
194
|
await collect(entry as AsyncIterable<Buffer>);
|
|
161
195
|
}
|
|
@@ -163,40 +197,71 @@ export const tarTool: ToolSpec<Input> = {
|
|
|
163
197
|
}
|
|
164
198
|
|
|
165
199
|
const destTarget = input.directory === undefined
|
|
166
|
-
? await ctx.sandbox
|
|
167
|
-
: await ctx.sandbox
|
|
200
|
+
? await resolveOutputTarget(ctx.sandbox, ".")
|
|
201
|
+
: await resolveOutputTarget(ctx.sandbox, input.directory);
|
|
168
202
|
const destRoot = destTarget.full;
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
203
|
+
const planned: Array<{
|
|
204
|
+
header: tar.Header;
|
|
205
|
+
data: Buffer;
|
|
206
|
+
name: string;
|
|
207
|
+
kind: "file" | "directory" | "symlink";
|
|
208
|
+
target: ResolvedTarget;
|
|
209
|
+
}> = [];
|
|
173
210
|
const extract = tar.extract();
|
|
174
211
|
extract.end(buffer);
|
|
175
212
|
for await (const entry of extract) {
|
|
213
|
+
throwIfAborted(ctx.signal);
|
|
176
214
|
const name = safeMember(entry.header.name, input.stripComponents ?? 0);
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
215
|
+
const data = await collect(entry as AsyncIterable<Buffer>);
|
|
216
|
+
if (name === undefined) continue;
|
|
217
|
+
const kind = entry.header.type === "directory"
|
|
218
|
+
? "directory"
|
|
219
|
+
: entry.header.type === "symlink"
|
|
220
|
+
? "symlink"
|
|
221
|
+
: "file";
|
|
222
|
+
if (kind === "symlink") assertSafeLinkTarget(entry.header.linkname);
|
|
223
|
+
const target = await resolveArchiveTarget(
|
|
224
|
+
ctx.sandbox,
|
|
225
|
+
destRoot,
|
|
226
|
+
path.join(destRoot, name),
|
|
227
|
+
kind,
|
|
228
|
+
);
|
|
229
|
+
planned.push({ header: entry.header, data, name, kind, target });
|
|
230
|
+
}
|
|
231
|
+
throwIfAborted(ctx.signal);
|
|
232
|
+
assertArchiveSymlinkPlan(planned);
|
|
233
|
+
await assertArchivePlan(ctx.permission, planned, { target: destTarget, boundary: destRoot });
|
|
234
|
+
await ctx.sandbox.lstat(destRoot).catch(async () => {
|
|
235
|
+
await fs.mkdir(destRoot, { recursive: true });
|
|
236
|
+
});
|
|
237
|
+
for (const item of planned) {
|
|
238
|
+
throwIfAborted(ctx.signal);
|
|
239
|
+
if (item.kind === "directory") {
|
|
240
|
+
await fs.mkdir(item.target.full, { recursive: true });
|
|
241
|
+
} else if (item.kind === "symlink") {
|
|
242
|
+
await fs.mkdir(path.dirname(item.target.full), { recursive: true });
|
|
243
|
+
await fs.symlink(item.header.linkname, item.target.full);
|
|
194
244
|
} else {
|
|
195
|
-
await fs.mkdir(path.dirname(
|
|
196
|
-
const
|
|
197
|
-
await
|
|
245
|
+
await fs.mkdir(path.dirname(item.target.full), { recursive: true });
|
|
246
|
+
const mode = safeMode(Number(item.header.mode));
|
|
247
|
+
await writeOutput(
|
|
248
|
+
item.target.full,
|
|
249
|
+
item.data,
|
|
250
|
+
false,
|
|
251
|
+
mode,
|
|
252
|
+
ctx.signal,
|
|
253
|
+
mode,
|
|
254
|
+
);
|
|
198
255
|
}
|
|
199
|
-
names.push(name);
|
|
256
|
+
names.push(item.name);
|
|
257
|
+
}
|
|
258
|
+
const directories = finalDirectoryPlans(planned);
|
|
259
|
+
for (const item of directories) {
|
|
260
|
+
throwIfAborted(ctx.signal);
|
|
261
|
+
await chmodDirectoryNoFollow(
|
|
262
|
+
item.target.full,
|
|
263
|
+
safeDirectoryMode(Number(item.header.mode)),
|
|
264
|
+
);
|
|
200
265
|
}
|
|
201
266
|
return textResult("", { archive: input.archive, members: names });
|
|
202
267
|
},
|
|
@@ -207,16 +272,29 @@ async function collectMembers(
|
|
|
207
272
|
name: string,
|
|
208
273
|
members: Array<{ name: string; full: string; stats: Awaited<ReturnType<typeof fs.lstat>> }>,
|
|
209
274
|
excludes: ((value: string) => boolean) | undefined,
|
|
275
|
+
concurrency: number,
|
|
276
|
+
signal: AbortSignal,
|
|
210
277
|
): Promise<void> {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
278
|
+
throwIfAborted(signal);
|
|
279
|
+
const walked = await walkAll(full, {
|
|
280
|
+
recursive: true,
|
|
281
|
+
includeHidden: true,
|
|
282
|
+
concurrency,
|
|
283
|
+
signal,
|
|
284
|
+
});
|
|
285
|
+
const excludedDirs: string[] = [];
|
|
286
|
+
for (const entry of walked) {
|
|
287
|
+
if (excludedDirs.some((dir) => entry.path === dir || entry.path.startsWith(dir + path.sep))) {
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
const relative = path.relative(full, entry.path).split(path.sep).join("/");
|
|
214
291
|
/* v8 ignore next */
|
|
215
|
-
const member = name === "" ?
|
|
216
|
-
if (excludes !== undefined && excludes(member))
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
292
|
+
const member = name === "" ? relative : `${name}/${relative}`;
|
|
293
|
+
if (excludes !== undefined && excludes(member)) {
|
|
294
|
+
if (entry.stats.isDirectory()) excludedDirs.push(entry.path);
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
members.push({ name: member, full: entry.path, stats: entry.stats });
|
|
220
298
|
}
|
|
221
299
|
}
|
|
222
300
|
|
|
@@ -229,7 +307,4 @@ function safeMember(name: string, strip: number): string | undefined {
|
|
|
229
307
|
return stripped.length === 0 ? undefined : stripped.join(path.sep);
|
|
230
308
|
}
|
|
231
309
|
|
|
232
|
-
|
|
233
|
-
const rel = path.relative(root, target);
|
|
234
|
-
return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
|
|
235
|
-
}
|
|
310
|
+
|
package/src/tools/tee.ts
CHANGED
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
import { Type, type Static } from "@sinclair/typebox";
|
|
7
7
|
import { pathsFrom } from "../core/aliases.js";
|
|
8
8
|
import { FsError } from "../core/errors.js";
|
|
9
|
-
import {
|
|
9
|
+
import { outputBoundary, resolveOutputTarget, writeOutput } from "../core/safe-output.js";
|
|
10
|
+
import { collect } from "../core/stream.js";
|
|
10
11
|
import type { ToolSpec } from "../core/types.js";
|
|
11
12
|
|
|
12
13
|
const Schema = Type.Object(
|
|
@@ -40,12 +41,14 @@ export const teeTool: ToolSpec<Input> = {
|
|
|
40
41
|
|
|
41
42
|
const targets = [];
|
|
42
43
|
for (const requested of paths) {
|
|
43
|
-
const target = await ctx.sandbox
|
|
44
|
+
const target = await resolveOutputTarget(ctx.sandbox, requested);
|
|
44
45
|
targets.push({ requested, target });
|
|
45
46
|
}
|
|
46
|
-
await ctx.permission.assert(
|
|
47
|
+
await ctx.permission.assert(
|
|
48
|
+
targets.map(({ target }) => ({ target, boundary: outputBoundary(target.full) })),
|
|
49
|
+
);
|
|
47
50
|
for (const target of targets) {
|
|
48
|
-
await
|
|
51
|
+
await writeOutput(target.target.full, data, input.append === true, 0o666, ctx.signal);
|
|
49
52
|
}
|
|
50
53
|
return { stdout: data, exitCode: 0, metadata: { files: paths, append: input.append === true } };
|
|
51
54
|
},
|
package/src/tools/touch.ts
CHANGED
|
@@ -36,14 +36,21 @@ export const touchTool: ToolSpec<Input> = {
|
|
|
36
36
|
const base = await resolveTimes(ctx, input);
|
|
37
37
|
const changed: string[] = [];
|
|
38
38
|
for (const requested of input.paths) {
|
|
39
|
-
const target =
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
const target = input.noDereference === true
|
|
40
|
+
? await ctx.sandbox.resolveForCreate(requested)
|
|
41
|
+
: await ctx.sandbox.resolveForWrite(requested);
|
|
42
|
+
let stats: Awaited<ReturnType<typeof fs.lstat>> | undefined;
|
|
42
43
|
try {
|
|
43
44
|
stats = await ctx.sandbox.lstat(target.full, requested);
|
|
44
45
|
} catch (error) {
|
|
45
46
|
/* v8 ignore next */
|
|
46
47
|
if (!(error instanceof FsError) || error.code !== "not-found") throw error;
|
|
48
|
+
}
|
|
49
|
+
await ctx.permission.assert([{
|
|
50
|
+
target,
|
|
51
|
+
boundary: ctx.sandbox.boundaryFor(target.full, stats?.isDirectory() === true ? "directory" : "file"),
|
|
52
|
+
}]);
|
|
53
|
+
if (stats === undefined) {
|
|
47
54
|
if (input.noCreate === true) continue;
|
|
48
55
|
try {
|
|
49
56
|
const handle = await fs.open(target.full, "a");
|
|
@@ -78,7 +85,8 @@ async function resolveTimes(
|
|
|
78
85
|
input: Input,
|
|
79
86
|
): Promise<{ atime: Date; mtime: Date }> {
|
|
80
87
|
if (input.reference !== undefined) {
|
|
81
|
-
const target = await ctx.sandbox.
|
|
88
|
+
const target = await ctx.sandbox.resolveFollow(input.reference);
|
|
89
|
+
await ctx.permission.assert([{ target, boundary: ctx.sandbox.boundaryFor(target.full, "file") }]);
|
|
82
90
|
const stats = await ctx.sandbox.lstat(target.full, input.reference);
|
|
83
91
|
return { atime: stats.atime, mtime: stats.mtime };
|
|
84
92
|
}
|
package/src/tools/truncate.ts
CHANGED
|
@@ -36,21 +36,31 @@ export const truncateTool: ToolSpec<Input> = {
|
|
|
36
36
|
if (input.size === undefined && input.reference === undefined) {
|
|
37
37
|
throw new FsError("size or reference is required.", "invalid");
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
let referenceSize: number | undefined;
|
|
40
|
+
if (input.reference !== undefined) {
|
|
41
|
+
const reference = await ctx.sandbox.resolveFollow(input.reference);
|
|
42
|
+
await ctx.permission.assert([{ target: reference, boundary: ctx.sandbox.boundaryFor(reference.full, "file") }]);
|
|
43
|
+
const stats = await ctx.sandbox.lstat(reference.full, input.reference);
|
|
44
|
+
referenceSize = num(stats.size);
|
|
45
|
+
}
|
|
43
46
|
|
|
44
47
|
const changed: string[] = [];
|
|
45
48
|
for (const requested of input.paths) {
|
|
46
|
-
const target =
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
const target = input.noDereference === true
|
|
50
|
+
? await ctx.sandbox.resolveForCreate(requested)
|
|
51
|
+
: await ctx.sandbox.resolveForWrite(requested);
|
|
52
|
+
let stats: Awaited<ReturnType<typeof fs.lstat>> | undefined;
|
|
49
53
|
try {
|
|
50
54
|
stats = await ctx.sandbox.lstat(target.full, requested);
|
|
51
55
|
} catch (error) {
|
|
52
56
|
/* v8 ignore next 2 */
|
|
53
57
|
if (!(error instanceof FsError) || error.code !== "not-found") throw error;
|
|
58
|
+
}
|
|
59
|
+
await ctx.permission.assert([{
|
|
60
|
+
target,
|
|
61
|
+
boundary: ctx.sandbox.boundaryFor(target.full, stats?.isDirectory() === true ? "directory" : "file"),
|
|
62
|
+
}]);
|
|
63
|
+
if (stats === undefined) {
|
|
54
64
|
/* v8 ignore next */
|
|
55
65
|
if (input.noCreate === true) continue;
|
|
56
66
|
/* v8 ignore next 10 */
|