The best way to control Claude Code, Codex and more from any computer or phone

main
ts 343 lines 10.8 KB
Raw
1 import { createWriteStream } from "node:fs";
2 import { lstat, opendir } from "node:fs/promises";
3 import { posix } from "node:path";
4 import { Readable, Transform } from "node:stream";
5 import { pipeline } from "node:stream/promises";
6 import { createGzip } from "node:zlib";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import { verifiedFileChunks, type VerifiedFile } from "./ArtifactFile.ts";
10
11 /**
12 * Node/Bun boundary for filesystem operations that Effect's portable
13 * FileSystem service cannot currently express: incremental directory handles
14 * and streaming a verified file descriptor through gzip.
15 */
16
17 export interface ArchiveTarFileEntry {
18 readonly type: "file";
19 readonly name: string;
20 readonly mode: number;
21 readonly file: VerifiedFile;
22 }
23
24 export interface ArchiveTarSymlinkEntry {
25 readonly type: "symlink";
26 readonly name: string;
27 readonly mode: number;
28 readonly linkname: string;
29 }
30
31 export type ArchiveTarEntry = ArchiveTarFileEntry | ArchiveTarSymlinkEntry;
32
33 export interface SecureDirectoryEntry {
34 readonly name: string;
35 readonly type: "Directory" | "File" | "SymbolicLink" | "Other";
36 }
37
38 interface CloseableDirectoryHandle {
39 readonly close: () => void | Promise<void>;
40 }
41
42 export const closeDirectoryHandle = async (handle: CloseableDirectoryHandle) => {
43 try {
44 await handle.close();
45 } catch {
46 // Async directory iteration closes the handle when it exits. Ignore both
47 // Node's already-closed rejection and Bun's equivalent synchronous error.
48 }
49 };
50
51 export class ArchivePlatformError extends Data.TaggedError("ArchivePlatformError")<{
52 readonly message: string;
53 readonly cause?: unknown;
54 }> {}
55
56 const toArchivePlatformError = (error: unknown) =>
57 new ArchivePlatformError({
58 message: error instanceof Error ? error.message : String(error),
59 cause: error,
60 });
61
62 const entryLimitError = (maxEntries: number) =>
63 new Error(
64 `Compute artifact exceeds the ${maxEntries} entry safety limit. Narrow the artifact directory or add ignore patterns.`,
65 );
66
67 export const readDirectoryEntriesSecure = (options: {
68 readonly directory: string;
69 readonly relativePrefix: string;
70 readonly ignore: readonly RegExp[];
71 readonly entriesAlreadyObserved: number;
72 readonly maxEntries: number;
73 }) =>
74 Effect.tryPromise({
75 try: async () => {
76 const before = await lstat(options.directory, { bigint: true });
77 if (!before.isDirectory() || before.isSymbolicLink()) {
78 throw new Error(
79 `Compute artifact directory changed before traversal: ${options.relativePrefix || "."}`,
80 );
81 }
82 const handle = await opendir(options.directory);
83 const entries: SecureDirectoryEntry[] = [];
84 let observedEntries = 0;
85 try {
86 for await (const entry of handle) {
87 if (entry.name.includes("\\")) {
88 throw new Error(
89 `Compute artifact entry contains an unsupported backslash: ${options.relativePrefix || "."}`,
90 );
91 }
92 const name = entry.name;
93 if (name.length === 0) continue;
94 const relativeName = options.relativePrefix ? `${options.relativePrefix}/${name}` : name;
95 observedEntries += 1;
96 if (options.entriesAlreadyObserved + observedEntries > options.maxEntries) {
97 throw entryLimitError(options.maxEntries);
98 }
99 if (options.ignore.some((pattern) => pattern.test(relativeName))) {
100 continue;
101 }
102 entries.push({
103 name,
104 type: entry.isSymbolicLink()
105 ? "SymbolicLink"
106 : entry.isDirectory()
107 ? "Directory"
108 : entry.isFile()
109 ? "File"
110 : "Other",
111 });
112 }
113 } finally {
114 await closeDirectoryHandle(handle);
115 }
116 const after = await lstat(options.directory, { bigint: true });
117 if (
118 !after.isDirectory() ||
119 after.isSymbolicLink() ||
120 before.dev !== after.dev ||
121 before.ino !== after.ino ||
122 before.mtimeNs !== after.mtimeNs ||
123 before.ctimeNs !== after.ctimeNs
124 ) {
125 throw new Error(
126 `Compute artifact directory changed during traversal: ${options.relativePrefix || "."}`,
127 );
128 }
129 return {
130 entries: entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)),
131 observedEntries,
132 };
133 },
134 catch: toArchivePlatformError,
135 });
136
137 export const writeCompressedArchiveSecure = (
138 archivePath: string,
139 entries: readonly ArchiveTarEntry[],
140 manifest: Uint8Array,
141 maxCompressedBytes: number,
142 ) =>
143 Effect.tryPromise({
144 try: (signal) => {
145 let compressedBytes = 0;
146 const compressedByteLimit = new Transform({
147 transform(chunk: Buffer, _encoding, callback) {
148 compressedBytes += chunk.byteLength;
149 if (compressedBytes > maxCompressedBytes) {
150 callback(
151 new Error(
152 `Prisma compute archive exceeds the ${maxCompressedBytes} byte compressed upload safety limit.`,
153 ),
154 );
155 return;
156 }
157 callback(null, chunk);
158 },
159 });
160 return pipeline(
161 Readable.from(tarChunks(entries, manifest), { objectMode: false }),
162 createGzip(),
163 compressedByteLimit,
164 createWriteStream(archivePath, { flags: "w", mode: 0o600 }),
165 { signal },
166 );
167 },
168 catch: toArchivePlatformError,
169 });
170
171 export const isArchivedRegularFile = (
172 entries: readonly ArchiveTarEntry[],
173 entrypointName: string,
174 ) => {
175 const byName = new Map(entries.map((entry) => [entry.name, entry]));
176 const visited = new Set<string>();
177 let currentName = entrypointName;
178 while (!visited.has(currentName)) {
179 visited.add(currentName);
180 const entry = byName.get(currentName);
181 if (entry?.type === "file") return true;
182 if (entry?.type !== "symlink") return false;
183 const target = posix.normalize(posix.join(posix.dirname(currentName), entry.linkname));
184 if (target === ".." || target.startsWith("../") || target.startsWith("/")) {
185 return false;
186 }
187 currentName = target;
188 }
189 return false;
190 };
191
192 async function* tarChunks(
193 entries: readonly ArchiveTarEntry[],
194 manifest: Uint8Array,
195 ): AsyncGenerator<Uint8Array> {
196 for (const entry of entries) {
197 if (entry.type === "symlink") {
198 yield* createEntryHeaders({
199 name: entry.name,
200 mode: entry.mode,
201 size: 0,
202 type: "symlink",
203 linkname: entry.linkname,
204 });
205 continue;
206 }
207
208 yield* createEntryHeaders({
209 name: entry.name,
210 mode: entry.mode,
211 size: entry.file.size,
212 type: "file",
213 });
214 let emitted = 0;
215 for await (const chunk of verifiedFileChunks(entry.file)) {
216 emitted += chunk.byteLength;
217 yield chunk;
218 }
219 if (emitted !== entry.file.size) {
220 throw new Error(
221 `Compute artifact file '${entry.name}' changed while the archive was being created.`,
222 );
223 }
224 const padding = paddingLength(entry.file.size);
225 if (padding > 0) yield new Uint8Array(padding);
226 }
227
228 yield createHeader({
229 name: "compute.manifest.json",
230 mode: 0o644,
231 size: manifest.byteLength,
232 type: "file",
233 });
234 yield manifest;
235 const manifestPadding = paddingLength(manifest.byteLength);
236 if (manifestPadding > 0) yield new Uint8Array(manifestPadding);
237 yield new Uint8Array(1024);
238 }
239
240 interface TarHeader {
241 readonly name: string;
242 readonly mode: number;
243 readonly size: number;
244 readonly type: "file" | "symlink" | "pax";
245 readonly linkname?: string;
246 }
247
248 const paxRecord = (key: string, value: string) => {
249 const body = ` ${key}=${value}\n`;
250 let length = byteLength(body) + 1;
251 while (byteLength(String(length)) + byteLength(body) !== length) {
252 length = byteLength(String(length)) + byteLength(body);
253 }
254 return `${length}${body}`;
255 };
256
257 function* createEntryHeaders(entry: TarHeader): Generator<Uint8Array> {
258 const longPath = splitTarName(entry.name) === undefined;
259 const longLink = entry.linkname !== undefined && byteLength(entry.linkname) > 100;
260 if (longPath || longLink) {
261 const data = new TextEncoder().encode(
262 (longPath ? paxRecord("path", entry.name) : "") +
263 (longLink ? paxRecord("linkpath", entry.linkname!) : ""),
264 );
265 yield createHeader({
266 name: "PaxHeaders/entry",
267 mode: 0o644,
268 size: data.byteLength,
269 type: "pax",
270 });
271 yield data;
272 const padding = paddingLength(data.byteLength);
273 if (padding > 0) yield new Uint8Array(padding);
274 }
275 yield createHeader({
276 ...entry,
277 name: longPath ? "PaxEntry" : entry.name,
278 linkname: longLink ? "PaxLink" : entry.linkname,
279 });
280 }
281
282 const createHeader = (entry: TarHeader) => {
283 const header = new Uint8Array(512);
284 const { name, prefix } = splitTarName(entry.name)!;
285 if (entry.linkname && byteLength(entry.linkname) > 100) {
286 throw new Error(`Archive symlink target is too long for tar header: ${entry.linkname}`);
287 }
288
289 writeString(header, 0, 100, name);
290 writeOctal(header, 100, 8, entry.mode);
291 writeOctal(header, 108, 8, 0);
292 writeOctal(header, 116, 8, 0);
293 writeOctal(header, 124, 12, entry.size);
294 writeOctal(header, 136, 12, 0);
295 header.fill(0x20, 148, 156);
296 writeString(header, 156, 1, entry.type === "pax" ? "x" : entry.type === "symlink" ? "2" : "0");
297 if (entry.linkname) writeString(header, 157, 100, entry.linkname);
298 writeString(header, 257, 6, "ustar");
299 writeString(header, 263, 2, "00");
300 writeString(header, 265, 32, "alchemy");
301 writeString(header, 297, 32, "alchemy");
302 if (prefix) writeString(header, 345, 155, prefix);
303
304 const checksum = header.reduce((sum, value) => sum + value, 0);
305 writeChecksum(header, checksum);
306 return header;
307 };
308
309 const splitTarName = (name: string): { name: string; prefix?: string } | undefined => {
310 if (byteLength(name) <= 100) return { name };
311 const slashIndexes = Array.from(name.matchAll(/\//g), (match) => match.index);
312 for (const index of slashIndexes.reverse()) {
313 if (index === undefined) continue;
314 const prefix = name.slice(0, index);
315 const suffix = name.slice(index + 1);
316 if (byteLength(prefix) <= 155 && byteLength(suffix) <= 100) {
317 return { name: suffix, prefix };
318 }
319 }
320 return undefined;
321 };
322
323 const byteLength = (value: string) => new TextEncoder().encode(value).length;
324
325 const writeString = (buffer: Uint8Array, offset: number, length: number, value: string) => {
326 const bytes = new TextEncoder().encode(value);
327 buffer.set(bytes.slice(0, length), offset);
328 };
329
330 const writeOctal = (buffer: Uint8Array, offset: number, length: number, value: number) => {
331 const text = value
332 .toString(8)
333 .padStart(length - 1, "0")
334 .slice(0, length - 1);
335 writeString(buffer, offset, length, `${text}\0`);
336 };
337
338 const writeChecksum = (buffer: Uint8Array, checksum: number) => {
339 const text = checksum.toString(8).padStart(6, "0").slice(0, 6);
340 writeString(buffer, 148, 8, `${text}\0 `);
341 };
342
343 const paddingLength = (size: number) => (512 - (size % 512)) % 512;