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
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: EPL-2.0
|
|
3
|
+
* license-wizard managed-header v1 EPL-2.0 short 73897e75828f
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as fs from "node:fs/promises";
|
|
7
|
+
import * as path from "node:path";
|
|
8
|
+
import { constants } from "node:fs";
|
|
9
|
+
import { FsError, errnoCode, fromErrno, isMissing } from "./errors.js";
|
|
10
|
+
import type { PermissionGate } from "./permissions.js";
|
|
11
|
+
import type { ResolvedTarget, Sandbox } from "./sandbox.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Resolve the path a following write actually reaches. Existing final
|
|
15
|
+
* symlinks (including a dangling link's would-be target) are followed so the
|
|
16
|
+
* permission gate sees the real destination, not only the link path.
|
|
17
|
+
*/
|
|
18
|
+
export async function resolveOutputTarget(sandbox: Sandbox, target: string): Promise<ResolvedTarget> {
|
|
19
|
+
return sandbox.resolveForWrite(target);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Permission boundary for a file output is its containing directory. */
|
|
23
|
+
export function outputBoundary(full: string): string {
|
|
24
|
+
return path.dirname(full);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Open a file for a direct write without following a final symlink. The caller
|
|
29
|
+
* resolves and gates the canonical target first; `O_NOFOLLOW` closes the gap
|
|
30
|
+
* where the final component is swapped for a link after that check.
|
|
31
|
+
*/
|
|
32
|
+
export async function openOutput(
|
|
33
|
+
full: string,
|
|
34
|
+
append = false,
|
|
35
|
+
mode = 0o666,
|
|
36
|
+
): Promise<fs.FileHandle> {
|
|
37
|
+
const flags = append
|
|
38
|
+
? constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW
|
|
39
|
+
: constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC | constants.O_NOFOLLOW;
|
|
40
|
+
try {
|
|
41
|
+
return await fs.open(full, flags, mode);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
throw fromErrno(error);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function throwIfAborted(signal?: AbortSignal): void {
|
|
48
|
+
if (signal?.aborted === true) throw new FsError("Operation interrupted.", "interrupted");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Write every byte of a buffer, retrying partial writes. A zero-progress write
|
|
53
|
+
* is a hard I/O error, never a silent truncation.
|
|
54
|
+
*/
|
|
55
|
+
export async function writeFully(
|
|
56
|
+
handle: fs.FileHandle,
|
|
57
|
+
data: Buffer,
|
|
58
|
+
position?: number,
|
|
59
|
+
signal?: AbortSignal,
|
|
60
|
+
): Promise<void> {
|
|
61
|
+
let offset = 0;
|
|
62
|
+
while (offset < data.length) {
|
|
63
|
+
throwIfAborted(signal);
|
|
64
|
+
let bytesWritten: number;
|
|
65
|
+
try {
|
|
66
|
+
({ bytesWritten } = await handle.write(
|
|
67
|
+
data,
|
|
68
|
+
offset,
|
|
69
|
+
data.length - offset,
|
|
70
|
+
position === undefined ? undefined : position + offset,
|
|
71
|
+
));
|
|
72
|
+
} catch (error) {
|
|
73
|
+
throw fromErrno(error);
|
|
74
|
+
}
|
|
75
|
+
if (bytesWritten <= 0) throw new FsError("Short write: no bytes written.", "io");
|
|
76
|
+
offset += bytesWritten;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Write a buffer or stream to an already-resolved output path. */
|
|
81
|
+
export async function writeOutput(
|
|
82
|
+
full: string,
|
|
83
|
+
data: Buffer | AsyncIterable<Buffer>,
|
|
84
|
+
append = false,
|
|
85
|
+
mode = 0o666,
|
|
86
|
+
signal?: AbortSignal,
|
|
87
|
+
finalMode?: number,
|
|
88
|
+
): Promise<void> {
|
|
89
|
+
throwIfAborted(signal);
|
|
90
|
+
let handle: fs.FileHandle | undefined;
|
|
91
|
+
try {
|
|
92
|
+
handle = await openOutput(full, append, mode);
|
|
93
|
+
if (Buffer.isBuffer(data)) {
|
|
94
|
+
await writeFully(handle, data, undefined, signal);
|
|
95
|
+
} else {
|
|
96
|
+
for await (const chunk of data) await writeFully(handle, chunk, undefined, signal);
|
|
97
|
+
}
|
|
98
|
+
if (finalMode !== undefined) await handle.chmod(finalMode);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (error instanceof FsError) throw error;
|
|
101
|
+
throw fromErrno(error);
|
|
102
|
+
} finally {
|
|
103
|
+
await handle?.close();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Change a directory mode through an O_DIRECTORY|O_NOFOLLOW handle. An
|
|
109
|
+
* unreadable directory (EACCES on O_RDONLY) falls back to path chmod only
|
|
110
|
+
* after lstat confirms a real, non-symlink directory. That narrow fallback
|
|
111
|
+
* retains the final-component swap race; ordinary readable directories stay
|
|
112
|
+
* on the handle path.
|
|
113
|
+
*/
|
|
114
|
+
export async function chmodDirectoryNoFollow(full: string, mode: number): Promise<void> {
|
|
115
|
+
let handle: fs.FileHandle | undefined;
|
|
116
|
+
try {
|
|
117
|
+
handle = await fs.open(
|
|
118
|
+
full,
|
|
119
|
+
constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW,
|
|
120
|
+
);
|
|
121
|
+
await handle.chmod(mode);
|
|
122
|
+
return;
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if (errnoCode(error) !== "EACCES") throw fromErrno(error);
|
|
125
|
+
try {
|
|
126
|
+
const stats = await fs.lstat(full);
|
|
127
|
+
/* v8 ignore next 3 -- final-component swap guard cannot be made deterministic */
|
|
128
|
+
if (!stats.isDirectory()) {
|
|
129
|
+
throw new FsError(`Blocked directory mode: "${full}" is not a directory.`, "archive");
|
|
130
|
+
}
|
|
131
|
+
await fs.chmod(full, mode);
|
|
132
|
+
} catch (fallbackError) {
|
|
133
|
+
/* v8 ignore next 2 -- fallback OS errors are environment-dependent */
|
|
134
|
+
if (fallbackError instanceof FsError) throw fallbackError;
|
|
135
|
+
/* v8 ignore next -- fallback OS errors are environment-dependent */
|
|
136
|
+
throw fromErrno(fallbackError);
|
|
137
|
+
}
|
|
138
|
+
} finally {
|
|
139
|
+
await handle?.close();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export type ArchiveEntryKind = "file" | "directory" | "symlink";
|
|
144
|
+
|
|
145
|
+
/** File bits safe to apply: strip suid/sgid/sticky, then respect umask. */
|
|
146
|
+
export function safeMode(mode: number, umask = process.umask()): number {
|
|
147
|
+
return (mode & 0o777) & ~umask;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Directory bits: strip suid/sgid, preserve sticky, then respect umask. */
|
|
151
|
+
export function safeDirectoryMode(mode: number, umask = process.umask()): number {
|
|
152
|
+
return (mode & 0o1000) | ((mode & 0o777) & ~umask);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Resolve an archive extraction destination in its canonical parent, refusing
|
|
157
|
+
* an existing final symlink. A target that leaves the sandbox is allowed only
|
|
158
|
+
* through the later bulk `external_directory` gate; one that stays inside the
|
|
159
|
+
* sandbox but escapes the extraction root is refused here.
|
|
160
|
+
*/
|
|
161
|
+
export async function resolveArchiveTarget(
|
|
162
|
+
sandbox: Sandbox,
|
|
163
|
+
root: string,
|
|
164
|
+
destination: string,
|
|
165
|
+
kind: ArchiveEntryKind,
|
|
166
|
+
): Promise<ResolvedTarget> {
|
|
167
|
+
const target = await sandbox.resolveForCreate(destination);
|
|
168
|
+
let stats: Awaited<ReturnType<typeof fs.lstat>> | undefined;
|
|
169
|
+
try {
|
|
170
|
+
stats = await fs.lstat(target.full);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
/* v8 ignore next */
|
|
173
|
+
if (!isMissing(error)) throw fromErrno(error, "access");
|
|
174
|
+
}
|
|
175
|
+
if (stats !== undefined) {
|
|
176
|
+
if (stats.isSymbolicLink()) {
|
|
177
|
+
throw new FsError(`Blocked archive path: "${destination}" is a symbolic link.`, "archive");
|
|
178
|
+
}
|
|
179
|
+
if (kind === "symlink") {
|
|
180
|
+
throw new FsError(`Blocked archive path: "${destination}" already exists.`, "archive");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (!isWithinRoot(root, target.full) && !target.outside) {
|
|
184
|
+
throw new FsError(`Blocked archive path: "${destination}" escapes the extraction root.`, "archive");
|
|
185
|
+
}
|
|
186
|
+
return target;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface ArchivePlan {
|
|
190
|
+
readonly target: ResolvedTarget;
|
|
191
|
+
readonly kind: ArchiveEntryKind;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Assert one bulk permission request for all planned extraction targets. */
|
|
195
|
+
export async function assertArchivePlan(
|
|
196
|
+
gate: PermissionGate,
|
|
197
|
+
plans: readonly ArchivePlan[],
|
|
198
|
+
root: { target: ResolvedTarget; boundary: string },
|
|
199
|
+
): Promise<void> {
|
|
200
|
+
const entries = plans.map(({ target, kind }) => ({
|
|
201
|
+
target,
|
|
202
|
+
boundary: kind === "directory" ? target.full : outputBoundary(target.full),
|
|
203
|
+
}));
|
|
204
|
+
entries.push(root);
|
|
205
|
+
await gate.assert(entries);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Directory plans to apply modes for: canonical target deduplicated with the
|
|
210
|
+
* last archive entry winning, deepest path first.
|
|
211
|
+
*/
|
|
212
|
+
export function finalDirectoryPlans<T extends { target: ResolvedTarget; kind: ArchiveEntryKind }>(
|
|
213
|
+
plans: readonly T[],
|
|
214
|
+
): T[] {
|
|
215
|
+
const last = new Map<string, T>();
|
|
216
|
+
for (const plan of plans) {
|
|
217
|
+
if (plan.kind === "directory") last.set(plan.target.full, plan);
|
|
218
|
+
}
|
|
219
|
+
return [...last.values()].sort(
|
|
220
|
+
(a, b) => b.target.full.split(path.sep).length - a.target.full.split(path.sep).length,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Reject members that collide with, or live beneath, an archive-created
|
|
226
|
+
* symlink. Such descendants would otherwise be written through the newly
|
|
227
|
+
* created link during extraction.
|
|
228
|
+
*/
|
|
229
|
+
export function assertArchiveSymlinkPlan(plans: readonly ArchivePlan[]): void {
|
|
230
|
+
const links = new Set(
|
|
231
|
+
plans.filter((plan) => plan.kind === "symlink").map((plan) => plan.target.full),
|
|
232
|
+
);
|
|
233
|
+
if (links.size === 0) return;
|
|
234
|
+
const seenLinks = new Set<string>();
|
|
235
|
+
for (const plan of plans) {
|
|
236
|
+
const full = plan.target.full;
|
|
237
|
+
if (links.has(full)) {
|
|
238
|
+
if (plan.kind !== "symlink" || seenLinks.has(full)) {
|
|
239
|
+
throw new FsError(`Blocked archive path: "${full}" collides with a symlink member.`, "archive");
|
|
240
|
+
}
|
|
241
|
+
seenLinks.add(full);
|
|
242
|
+
}
|
|
243
|
+
for (let parent = path.dirname(full); parent !== path.dirname(parent); parent = path.dirname(parent)) {
|
|
244
|
+
if (links.has(parent)) {
|
|
245
|
+
throw new FsError(`Blocked archive path: "${full}" is beneath a symlink member.`, "archive");
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Safe member name for archive creation. Relative sources keep their safe
|
|
253
|
+
* relative layout; absolute sources are named relative to the base first, then
|
|
254
|
+
* the project root; unsafe or external sources fall back to the basename.
|
|
255
|
+
*/
|
|
256
|
+
export function archiveSourceName(requested: string, full: string, base: string, root: string): string {
|
|
257
|
+
if (!path.isAbsolute(requested)) {
|
|
258
|
+
const normalized = path.normalize(requested);
|
|
259
|
+
if (!path.isAbsolute(normalized) && !normalized.split(path.sep).includes("..")) {
|
|
260
|
+
return normalized;
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
const fromBase = relativeMember(base, full);
|
|
264
|
+
if (fromBase !== undefined) return fromBase;
|
|
265
|
+
const fromRoot = relativeMember(root, full);
|
|
266
|
+
if (fromRoot !== undefined) return fromRoot;
|
|
267
|
+
}
|
|
268
|
+
return path.basename(full);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function relativeMember(parent: string, child: string): string | undefined {
|
|
272
|
+
if (!isWithinRoot(parent, child)) return undefined;
|
|
273
|
+
const relative = path.relative(parent, child);
|
|
274
|
+
return relative === "" ? undefined : relative;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Reject archived symlink targets that are absolute, empty, or traversing. */
|
|
278
|
+
export function assertSafeLinkTarget(linkname: string): void {
|
|
279
|
+
if (linkname.length === 0 || linkname.includes("\0")) {
|
|
280
|
+
throw new FsError(`Blocked symlink target: ${JSON.stringify(linkname)}`, "archive");
|
|
281
|
+
}
|
|
282
|
+
if (path.isAbsolute(linkname) || linkname.split("/").includes("..")) {
|
|
283
|
+
throw new FsError(`Blocked symlink target: ${linkname}`, "archive");
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function isWithinRoot(root: string, target: string): boolean {
|
|
288
|
+
const rel = path.relative(root, target);
|
|
289
|
+
return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
|
|
290
|
+
}
|
package/src/core/sandbox.ts
CHANGED
|
@@ -150,6 +150,49 @@ export class Sandbox {
|
|
|
150
150
|
};
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Resolve the real path a following syscall will mutate. Existing final
|
|
155
|
+
* symlinks and chains are followed; a dangling link resolves to the path its
|
|
156
|
+
* target would create, so callers can gate that location before writing.
|
|
157
|
+
*/
|
|
158
|
+
async resolveForWrite(target: string): Promise<ResolvedTarget> {
|
|
159
|
+
this.assertTarget(target);
|
|
160
|
+
let node = path.resolve(this.root, target);
|
|
161
|
+
for (let hops = 0; ; hops++) {
|
|
162
|
+
let stats: Awaited<ReturnType<typeof fs.lstat>>;
|
|
163
|
+
try {
|
|
164
|
+
stats = await fs.lstat(node);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
/* v8 ignore next */
|
|
167
|
+
if (isMissing(error)) {
|
|
168
|
+
const created = await this.resolveForCreate(node);
|
|
169
|
+
return { ...created, requested: target };
|
|
170
|
+
}
|
|
171
|
+
/* v8 ignore next */
|
|
172
|
+
throw fromErrno(error, "access");
|
|
173
|
+
}
|
|
174
|
+
if (!stats.isSymbolicLink()) {
|
|
175
|
+
let real: string;
|
|
176
|
+
try {
|
|
177
|
+
real = await fs.realpath(node);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
/* v8 ignore next 2 */
|
|
180
|
+
throw fromErrno(error, "access");
|
|
181
|
+
}
|
|
182
|
+
return { full: real, outside: !this.isWithin(real), requested: target, realAncestor: real };
|
|
183
|
+
}
|
|
184
|
+
if (hops >= 40) throw new FsError(`Too many symbolic links: "${target}".`, "io");
|
|
185
|
+
let link: string;
|
|
186
|
+
try {
|
|
187
|
+
link = await fs.readlink(node);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
/* v8 ignore next */
|
|
190
|
+
throw fromErrno(error, "access");
|
|
191
|
+
}
|
|
192
|
+
node = path.resolve(path.dirname(node), link);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
153
196
|
/** Resolve the parent of a target that may be created or replaced. */
|
|
154
197
|
async resolveParent(target: string): Promise<ResolvedTarget> {
|
|
155
198
|
this.assertTarget(target);
|
package/src/index.ts
CHANGED
|
@@ -180,6 +180,17 @@ const JOB_SCHEMA = Type.Object(
|
|
|
180
180
|
{ additionalProperties: false },
|
|
181
181
|
);
|
|
182
182
|
|
|
183
|
+
/**
|
|
184
|
+
* Background jobs are session-scoped. The host must supply a non-empty session
|
|
185
|
+
* id; never fall back to the direct-call test owner in production.
|
|
186
|
+
*/
|
|
187
|
+
function sessionOwner(context: { sessionID: string }): string {
|
|
188
|
+
if (typeof context.sessionID !== "string" || context.sessionID.length === 0) {
|
|
189
|
+
throw new FsError("background jobs require a non-empty session id.", "invalid");
|
|
190
|
+
}
|
|
191
|
+
return context.sessionID;
|
|
192
|
+
}
|
|
193
|
+
|
|
183
194
|
export default Plugin.define({
|
|
184
195
|
id: "opencode-fs-plugin",
|
|
185
196
|
async setup(ctx) {
|
|
@@ -250,7 +261,8 @@ export default Plugin.define({
|
|
|
250
261
|
};
|
|
251
262
|
|
|
252
263
|
if ((parsed as { background?: unknown }).background === true) {
|
|
253
|
-
const
|
|
264
|
+
const owner = sessionOwner(context);
|
|
265
|
+
const job = await jobs.start(spec.name, runSpec, owner);
|
|
254
266
|
return {
|
|
255
267
|
content: JSON.stringify(
|
|
256
268
|
{
|
|
@@ -288,8 +300,9 @@ export default Plugin.define({
|
|
|
288
300
|
"Inspect, wait for, cancel, or list background fs jobs. Jobs are created by passing background: true to any fs tool. wait returns the original tool result when the job completes; a timeout returns the current status.",
|
|
289
301
|
input: JOB_SCHEMA,
|
|
290
302
|
options: { namespace: "fs", permission: "fs_job" },
|
|
291
|
-
execute: async (input,
|
|
303
|
+
execute: async (input, context) => {
|
|
292
304
|
try {
|
|
305
|
+
const owner = sessionOwner(context);
|
|
293
306
|
const parsed = parseSchema<{
|
|
294
307
|
id?: string;
|
|
295
308
|
wait?: boolean;
|
|
@@ -303,7 +316,7 @@ export default Plugin.define({
|
|
|
303
316
|
throw new FsError("list cannot be combined with id, wait, or cancel.", "invalid");
|
|
304
317
|
}
|
|
305
318
|
return {
|
|
306
|
-
content: JSON.stringify({ jobs: jobs.list() }, null, 2),
|
|
319
|
+
content: JSON.stringify({ jobs: jobs.list(owner) }, null, 2),
|
|
307
320
|
metadata: { ok: true },
|
|
308
321
|
};
|
|
309
322
|
}
|
|
@@ -314,7 +327,7 @@ export default Plugin.define({
|
|
|
314
327
|
if (parsed.wait === true) {
|
|
315
328
|
throw new FsError("cancel and wait are mutually exclusive.", "invalid");
|
|
316
329
|
}
|
|
317
|
-
const status = jobs.cancel(parsed.id);
|
|
330
|
+
const status = jobs.cancel(parsed.id, owner);
|
|
318
331
|
return {
|
|
319
332
|
content: JSON.stringify(status, null, 2),
|
|
320
333
|
metadata: { ok: true, jobId: status.id, state: status.state },
|
|
@@ -322,7 +335,11 @@ export default Plugin.define({
|
|
|
322
335
|
}
|
|
323
336
|
|
|
324
337
|
if (parsed.wait === true) {
|
|
325
|
-
const waited = await jobs.wait(
|
|
338
|
+
const waited = await jobs.wait(
|
|
339
|
+
parsed.id,
|
|
340
|
+
parsed.timeoutMs ?? 600000,
|
|
341
|
+
owner,
|
|
342
|
+
);
|
|
326
343
|
if (waited.result) {
|
|
327
344
|
return {
|
|
328
345
|
content: waited.result.content,
|
|
@@ -345,7 +362,7 @@ export default Plugin.define({
|
|
|
345
362
|
};
|
|
346
363
|
}
|
|
347
364
|
|
|
348
|
-
const status = jobs.status(parsed.id);
|
|
365
|
+
const status = jobs.status(parsed.id, owner);
|
|
349
366
|
return {
|
|
350
367
|
content: JSON.stringify(status, null, 2),
|
|
351
368
|
metadata: { ok: true, jobId: status.id, state: status.state },
|
package/src/tools/ar.ts
CHANGED
|
@@ -8,6 +8,16 @@ import * as path from "node:path";
|
|
|
8
8
|
import { Type, type Static } from "@sinclair/typebox";
|
|
9
9
|
import { FsError, fromErrno } from "../core/errors.js";
|
|
10
10
|
import { num } from "../core/fsutil.js";
|
|
11
|
+
import { mapLimit } from "../core/parallel.js";
|
|
12
|
+
import {
|
|
13
|
+
assertArchivePlan,
|
|
14
|
+
outputBoundary,
|
|
15
|
+
resolveArchiveTarget,
|
|
16
|
+
resolveOutputTarget,
|
|
17
|
+
safeMode,
|
|
18
|
+
throwIfAborted,
|
|
19
|
+
writeOutput,
|
|
20
|
+
} from "../core/safe-output.js";
|
|
11
21
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
12
22
|
|
|
13
23
|
const Schema = Type.Object(
|
|
@@ -45,9 +55,9 @@ export const arTool: ToolSpec<Input> = {
|
|
|
45
55
|
mutates: true,
|
|
46
56
|
async run(ctx, input) {
|
|
47
57
|
const archive = input.mode === "create"
|
|
48
|
-
? await ctx.sandbox
|
|
58
|
+
? await resolveOutputTarget(ctx.sandbox, input.archive)
|
|
49
59
|
: await ctx.sandbox.resolveFollow(input.archive);
|
|
50
|
-
await ctx.permission.assert([{ target: archive, boundary: archive.full }]);
|
|
60
|
+
await ctx.permission.assert([{ target: archive, boundary: outputBoundary(archive.full) }]);
|
|
51
61
|
|
|
52
62
|
if (input.mode === "create") {
|
|
53
63
|
if (input.paths === undefined || input.paths.length === 0) {
|
|
@@ -55,8 +65,9 @@ export const arTool: ToolSpec<Input> = {
|
|
|
55
65
|
}
|
|
56
66
|
const members: Member[] = [];
|
|
57
67
|
for (const requested of input.paths) {
|
|
68
|
+
throwIfAborted(ctx.signal);
|
|
58
69
|
const target = await ctx.sandbox.resolveFollow(requested);
|
|
59
|
-
await ctx.permission.assert([{ target, boundary: target.full }]);
|
|
70
|
+
await ctx.permission.assert([{ target, boundary: outputBoundary(target.full) }]);
|
|
60
71
|
const stats = await ctx.sandbox.stat(target.full, requested);
|
|
61
72
|
if (!stats.isFile()) throw new FsError(`Not a regular file: "${requested}".`, "not-file");
|
|
62
73
|
const name = path.basename(requested);
|
|
@@ -75,12 +86,8 @@ export const arTool: ToolSpec<Input> = {
|
|
|
75
86
|
}),
|
|
76
87
|
});
|
|
77
88
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
await fs.writeFile(archive.full, encode(members));
|
|
81
|
-
} catch (error) {
|
|
82
|
-
throw fromErrno(error);
|
|
83
|
-
}
|
|
89
|
+
throwIfAborted(ctx.signal);
|
|
90
|
+
await writeOutput(archive.full, encode(members), false, 0o666, ctx.signal);
|
|
84
91
|
return textResult("", { archive: input.archive, members: members.map((member) => member.name) });
|
|
85
92
|
}
|
|
86
93
|
|
|
@@ -99,23 +106,31 @@ export const arTool: ToolSpec<Input> = {
|
|
|
99
106
|
return textResult(`${selected.map((member) => member.name).join("\n")}\n`, { members: selected.length });
|
|
100
107
|
}
|
|
101
108
|
|
|
102
|
-
const
|
|
103
|
-
?
|
|
104
|
-
:
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
await
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
109
|
+
const root = input.directory === undefined
|
|
110
|
+
? await resolveOutputTarget(ctx.sandbox, ".")
|
|
111
|
+
: await resolveOutputTarget(ctx.sandbox, input.directory);
|
|
112
|
+
const planned = await mapLimit(selected, ctx.concurrency, async (member) => {
|
|
113
|
+
throwIfAborted(ctx.signal);
|
|
114
|
+
const target = await resolveArchiveTarget(
|
|
115
|
+
ctx.sandbox,
|
|
116
|
+
root.full,
|
|
117
|
+
path.join(root.full, safeName(member.name)),
|
|
118
|
+
"file",
|
|
119
|
+
);
|
|
120
|
+
return { member, target, kind: "file" as const };
|
|
121
|
+
}, ctx.signal);
|
|
122
|
+
throwIfAborted(ctx.signal);
|
|
123
|
+
await assertArchivePlan(ctx.permission, planned, { target: root, boundary: root.full });
|
|
124
|
+
for (const { member, target } of planned) {
|
|
125
|
+
throwIfAborted(ctx.signal);
|
|
126
|
+
/* v8 ignore next 4 -- write errors are environment-dependent */
|
|
112
127
|
try {
|
|
113
|
-
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
114
|
-
await fs.writeFile(target, member.data);
|
|
115
|
-
await fs.chmod(target, member.mode);
|
|
128
|
+
await fs.mkdir(path.dirname(target.full), { recursive: true });
|
|
116
129
|
} catch (error) {
|
|
117
130
|
throw fromErrno(error);
|
|
118
131
|
}
|
|
132
|
+
const mode = safeMode(member.mode);
|
|
133
|
+
await writeOutput(target.full, member.data, false, mode, ctx.signal, mode);
|
|
119
134
|
}
|
|
120
135
|
return textResult("", { extracted: selected.length });
|
|
121
136
|
},
|
|
@@ -149,26 +164,44 @@ function decode(data: Buffer): Member[] {
|
|
|
149
164
|
const header = data.subarray(offset, offset + 60).toString("ascii");
|
|
150
165
|
if (header.slice(58, 60) !== "`\n") throw new FsError("Corrupt ar header.", "archive");
|
|
151
166
|
const name = header.slice(0, 16).trim().replace(/\/$/, "");
|
|
152
|
-
const size =
|
|
167
|
+
const size = decimalField(header.slice(48, 58), "size");
|
|
153
168
|
const dataStart = offset + 60;
|
|
154
|
-
|
|
169
|
+
const next = dataStart + size + (size % 2);
|
|
170
|
+
if (dataStart + size > data.length || next <= offset) {
|
|
155
171
|
throw new FsError("Truncated ar member.", "archive");
|
|
156
172
|
}
|
|
157
173
|
if (name !== "" && name !== "/" && !name.startsWith("/")) {
|
|
158
174
|
members.push({
|
|
159
175
|
name,
|
|
160
|
-
mtime:
|
|
161
|
-
uid:
|
|
162
|
-
gid:
|
|
163
|
-
mode:
|
|
176
|
+
mtime: decimalField(header.slice(16, 28), "mtime", true),
|
|
177
|
+
uid: decimalField(header.slice(28, 34), "uid", true),
|
|
178
|
+
gid: decimalField(header.slice(34, 40), "gid", true),
|
|
179
|
+
mode: octalField(header.slice(40, 48)),
|
|
164
180
|
data: data.subarray(dataStart, dataStart + size),
|
|
165
181
|
});
|
|
166
182
|
}
|
|
167
|
-
offset =
|
|
183
|
+
offset = next;
|
|
168
184
|
}
|
|
169
185
|
return members;
|
|
170
186
|
}
|
|
171
187
|
|
|
188
|
+
function decimalField(field: string, label: string, blankDefault = false): number {
|
|
189
|
+
const text = field.trim();
|
|
190
|
+
if (text === "") {
|
|
191
|
+
if (blankDefault) return 0;
|
|
192
|
+
throw new FsError(`Invalid ar ${label} field.`, "archive");
|
|
193
|
+
}
|
|
194
|
+
if (!/^\d+$/.test(text)) throw new FsError(`Invalid ar ${label} field.`, "archive");
|
|
195
|
+
return Number(text);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function octalField(field: string): number {
|
|
199
|
+
const text = field.trim();
|
|
200
|
+
if (text === "") return 0o644;
|
|
201
|
+
if (!/^[0-7]+$/.test(text)) throw new FsError("Invalid ar mode field.", "archive");
|
|
202
|
+
return Number.parseInt(text, 8);
|
|
203
|
+
}
|
|
204
|
+
|
|
172
205
|
function safeName(name: string): string {
|
|
173
206
|
const normalized = name.replaceAll("\\", "/").replace(/^\/+/, "");
|
|
174
207
|
const parts = normalized.split("/").filter((part) => part !== "" && part !== ".");
|
package/src/tools/chmod.ts
CHANGED
|
@@ -38,36 +38,47 @@ export const chmodTool: ToolSpec<Input> = {
|
|
|
38
38
|
if (input.mode === undefined && input.reference === undefined) {
|
|
39
39
|
throw new FsError("mode or reference is required.", "invalid");
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
let referenceMode: number | undefined;
|
|
42
|
+
if (input.reference !== undefined) {
|
|
43
|
+
const reference = await ctx.sandbox.resolveFollow(input.reference);
|
|
44
|
+
await ctx.permission.assert([{ target: reference, boundary: ctx.sandbox.boundaryFor(reference.full, "file") }]);
|
|
45
|
+
const stats = await ctx.sandbox.lstat(reference.full, input.reference);
|
|
46
|
+
referenceMode = num(stats.mode) & 0o7777;
|
|
47
|
+
}
|
|
45
48
|
|
|
46
49
|
const changed: string[] = [];
|
|
47
50
|
for (const requested of input.paths) {
|
|
48
|
-
const root =
|
|
51
|
+
const root = input.noDereference === true
|
|
52
|
+
? await ctx.sandbox.resolveForCreate(requested)
|
|
53
|
+
: await ctx.sandbox.resolveFollow(requested);
|
|
49
54
|
ctx.sandbox.assertSafeToDelete(root.full);
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
const rootStats = await ctx.sandbox.lstat(root.full, requested);
|
|
56
|
+
const targets = [{ target: root, directory: rootStats.isDirectory() }];
|
|
57
|
+
if (input.recursive === true && rootStats.isDirectory()) {
|
|
58
|
+
for (const entry of await walkAll(root.full, {
|
|
59
|
+
recursive: true,
|
|
60
|
+
signal: ctx.signal,
|
|
61
|
+
concurrency: ctx.concurrency,
|
|
62
|
+
})) {
|
|
63
|
+
// Traversal symlinks are skipped: their targets were never resolved or gated.
|
|
64
|
+
if (input.noDereference !== true && entry.stats.isSymbolicLink()) continue;
|
|
65
|
+
targets.push({
|
|
66
|
+
target: {
|
|
60
67
|
full: entry.path,
|
|
61
68
|
outside: root.outside,
|
|
62
69
|
requested: entry.path,
|
|
63
70
|
realAncestor: root.realAncestor,
|
|
64
|
-
}
|
|
65
|
-
|
|
71
|
+
},
|
|
72
|
+
directory: entry.stats.isDirectory(),
|
|
73
|
+
});
|
|
66
74
|
}
|
|
67
75
|
}
|
|
68
|
-
await ctx.permission.assert(targets.map((
|
|
76
|
+
await ctx.permission.assert(targets.map(({ target, directory }) => ({
|
|
77
|
+
target,
|
|
78
|
+
boundary: ctx.sandbox.boundaryFor(target.full, directory ? "directory" : "file"),
|
|
79
|
+
})));
|
|
69
80
|
|
|
70
|
-
for (const target of targets) {
|
|
81
|
+
for (const { target } of targets) {
|
|
71
82
|
const stats = input.noDereference === true
|
|
72
83
|
? await ctx.sandbox.lstat(target.full, target.requested)
|
|
73
84
|
: await ctx.sandbox.stat(target.full, target.requested);
|