The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { createHash } from "node:crypto"; |
| 2 | import { gzipSync } from "node:zlib"; |
| 3 | import { packageWebsiteArtifact, stageWebsiteArtifact } from "@alchemy.run/neon/Website/Artifact"; |
| 4 | import { expect, it } from "@alchemy.run/test"; |
| 5 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 11 | import * as Result from "effect/Result"; |
| 12 | import * as Stream from "effect/Stream"; |
| 13 | import { unzipSync } from "fflate"; |
| 14 | |
| 15 | const fixture = Effect.gen(function* () { |
| 16 | const fs = yield* FileSystem.FileSystem; |
| 17 | const path = yield* Path.Path; |
| 18 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "neon-website-test-" }); |
| 19 | const dist = path.join(root, "dist"); |
| 20 | yield* fs.makeDirectory(dist); |
| 21 | yield* fs.writeFileString(path.join(dist, "index.html"), "<h1>Neon</h1>"); |
| 22 | return { fs, path, root, dist }; |
| 23 | }); |
| 24 | |
| 25 | it.effect( |
| 26 | "static archives are deterministic and omit env and source maps", |
| 27 | () => |
| 28 | Effect.gen(function* () { |
| 29 | const { fs, path, root, dist } = yield* fixture; |
| 30 | yield* fs.writeFileString(path.join(dist, ".env.production"), "TOKEN=do-not-package"); |
| 31 | yield* fs.writeFileString(path.join(dist, "app.js.map"), "source-secret"); |
| 32 | const props = { root, distDir: dist, static: { notFoundHandling: "spa" as const } }; |
| 33 | const first = yield* packageWebsiteArtifact(props); |
| 34 | const second = yield* packageWebsiteArtifact(props); |
| 35 | expect(first.hash).toBe(second.hash); |
| 36 | const files = yield* Effect.sync(() => unzipSync(first.archive)); |
| 37 | expect(files["index.mjs"]).toBeDefined(); |
| 38 | expect( |
| 39 | Object.keys(files).some((name) => name.includes(".env") || name.endsWith(".map")), |
| 40 | ).toBe(false); |
| 41 | const source = yield* Effect.sync(() => new TextDecoder().decode(files["index.mjs"])); |
| 42 | expect(source).toContain("export default"); |
| 43 | expect(source).not.toContain(".listen("); |
| 44 | yield* fs.writeFileString(path.join(dist, "index.html"), "<h1>Updated</h1>"); |
| 45 | expect((yield* packageWebsiteArtifact(props)).hash).not.toBe(first.hash); |
| 46 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 47 | { tags: ["unit", "provider:neon", "provider:neon:website", "local"] }, |
| 48 | ); |
| 49 | |
| 50 | for (const kind of [ |
| 51 | "escape", |
| 52 | "native", |
| 53 | "executable", |
| 54 | "secret-alias", |
| 55 | "cycle", |
| 56 | "error-page", |
| 57 | ] as const) { |
| 58 | it.effect( |
| 59 | `rejects unsafe ${kind} output`, |
| 60 | () => |
| 61 | Effect.gen(function* () { |
| 62 | const { fs, path, root, dist } = yield* fixture; |
| 63 | if (kind === "escape") { |
| 64 | yield* fs.writeFileString(path.join(root, "secret.txt"), "private"); |
| 65 | yield* fs.symlink("../secret.txt", path.join(dist, "leak.txt")); |
| 66 | } else if (kind === "native") |
| 67 | yield* fs.writeFileString(path.join(dist, "addon.node"), "native"); |
| 68 | else if (kind === "executable") |
| 69 | yield* fs.writeFile(path.join(dist, "binary"), new Uint8Array([0xcf, 0xfa, 0xed, 0xfe])); |
| 70 | else if (kind === "secret-alias") { |
| 71 | yield* fs.writeFileString(path.join(dist, ".env"), "SECRET=hidden"); |
| 72 | yield* fs.symlink(".env", path.join(dist, "public.txt")); |
| 73 | } else if (kind === "cycle") yield* fs.symlink(".", path.join(dist, "cycle")); |
| 74 | const result = yield* stageWebsiteArtifact({ |
| 75 | root, |
| 76 | distDir: dist, |
| 77 | static: { errorPage: kind === "error-page" ? "../secret.txt" : undefined }, |
| 78 | }).pipe(Effect.result); |
| 79 | expect(Result.isFailure(result)).toBe(true); |
| 80 | if (Result.isFailure(result)) expect(result.failure._tag).toBe("WebsiteArtifactError"); |
| 81 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 82 | { tags: ["unit", "provider:neon", "provider:neon:website", "local"] }, |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | for (const sensitive of [".env.production", ".alchemy/state file.json", "private.pem"]) |
| 87 | for (const alias of [false, true]) |
| 88 | it.effect( |
| 89 | `rejects traced ${JSON.stringify(sensitive)}${alias ? " through a symlink" : ""} with a sanitized filename`, |
| 90 | () => |
| 91 | Effect.gen(function* () { |
| 92 | const { fs, path, root, dist } = yield* fixture; |
| 93 | const secret = path.join(root, sensitive); |
| 94 | yield* fs.makeDirectory(path.dirname(secret), { recursive: true }); |
| 95 | yield* fs.writeFileString(secret, "fixture-secret-do-not-log"); |
| 96 | const dependency = alias ? "runtime-data.txt" : sensitive; |
| 97 | if (alias) yield* fs.symlink(secret, path.join(root, dependency)); |
| 98 | const serverEntry = path.join(dist, "serve.mjs"); |
| 99 | yield* fs.writeFileString( |
| 100 | serverEntry, |
| 101 | `import { readFileSync } from "node:fs"; |
| 102 | const value = readFileSync(new URL(${JSON.stringify(`../${dependency}`)}, import.meta.url), "utf8"); |
| 103 | export default { fetch: () => new Response(value) };`, |
| 104 | ); |
| 105 | const result = yield* stageWebsiteArtifact({ root, distDir: dist, serverEntry }).pipe( |
| 106 | Effect.result, |
| 107 | ); |
| 108 | expect(Result.isFailure(result)).toBe(true); |
| 109 | if (Result.isFailure(result)) { |
| 110 | expect(result.failure._tag).toBe("WebsiteArtifactError"); |
| 111 | expect(result.failure.message).toBe( |
| 112 | `A traced dependency selects a sensitive file: ${sensitive.replace(/[^a-zA-Z0-9_./@+-]/g, "_")}`, |
| 113 | ); |
| 114 | expect(result.failure.message).not.toContain(root); |
| 115 | expect(result.failure.message).not.toContain("fixture-secret-do-not-log"); |
| 116 | } |
| 117 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 118 | { tags: ["unit", "provider:neon", "provider:neon:website", "local"] }, |
| 119 | ); |
| 120 | |
| 121 | it.effect( |
| 122 | "traces referenced source data without selecting neighboring state or secrets", |
| 123 | () => |
| 124 | Effect.gen(function* () { |
| 125 | const { fs, path, root, dist } = yield* fixture; |
| 126 | yield* fs.makeDirectory(path.join(root, "src")); |
| 127 | yield* fs.makeDirectory(path.join(root, ".alchemy")); |
| 128 | yield* fs.writeFileString(path.join(root, "src/guide.mdx"), "# Guide"); |
| 129 | yield* fs.writeFileString(path.join(root, ".env"), "fixture-secret"); |
| 130 | yield* fs.writeFileString(path.join(root, "private.key"), "fixture-key"); |
| 131 | yield* fs.writeFileString( |
| 132 | path.join(root, ".alchemy/state.json"), |
| 133 | '{"private":"fixture-state"}', |
| 134 | ); |
| 135 | const serverEntry = path.join(dist, "serve.mjs"); |
| 136 | yield* fs.writeFileString( |
| 137 | serverEntry, |
| 138 | 'import { readFileSync } from "node:fs"; const guide = readFileSync(new URL("../src/guide.mdx", import.meta.url), "utf8"); export default { fetch: () => new Response(guide) };', |
| 139 | ); |
| 140 | const props = { root, distDir: dist, serverEntry }; |
| 141 | const first = yield* packageWebsiteArtifact(props); |
| 142 | const archive = yield* Effect.sync(() => unzipSync(first.archive)); |
| 143 | expect(Object.keys(archive).sort()).toEqual([ |
| 144 | "files/dist/index.html", |
| 145 | "files/dist/serve.mjs", |
| 146 | "files/src/guide.mdx", |
| 147 | "index.mjs", |
| 148 | ]); |
| 149 | yield* fs.writeFileString(path.join(root, "src/guide.mdx"), "# Updated guide"); |
| 150 | expect((yield* packageWebsiteArtifact(props)).hash).not.toBe(first.hash); |
| 151 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 152 | { tags: ["unit", "provider:neon", "provider:neon:website", "local"] }, |
| 153 | ); |
| 154 | |
| 155 | for (const alias of [false, true]) |
| 156 | it.effect( |
| 157 | `rejects a traced workspace escape${alias ? " through a symlink" : ""}`, |
| 158 | () => |
| 159 | Effect.gen(function* () { |
| 160 | const { fs, path, root, dist } = yield* fixture; |
| 161 | const outside = yield* fs.makeTempDirectoryScoped(); |
| 162 | const dependency = path.join(outside, "runtime-data.txt"); |
| 163 | yield* fs.writeFileString(dependency, "outside-workspace"); |
| 164 | const selected = alias ? path.join(root, "runtime-data.txt") : dependency; |
| 165 | if (alias) yield* fs.symlink(dependency, selected); |
| 166 | const serverEntry = path.join(dist, "serve.mjs"); |
| 167 | yield* fs.writeFileString( |
| 168 | serverEntry, |
| 169 | `import { readFileSync } from "node:fs"; const value = readFileSync(${JSON.stringify(selected)}, "utf8"); export default { fetch: () => new Response(value) };`, |
| 170 | ); |
| 171 | const result = yield* stageWebsiteArtifact({ root, distDir: dist, serverEntry }).pipe( |
| 172 | Effect.result, |
| 173 | ); |
| 174 | expect(Result.isFailure(result)).toBe(true); |
| 175 | if (Result.isFailure(result)) { |
| 176 | expect(result.failure._tag).toBe("WebsiteArtifactError"); |
| 177 | expect(result.failure.message).toBe( |
| 178 | "A traced dependency escapes the application workspace.", |
| 179 | ); |
| 180 | } |
| 181 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 182 | { tags: ["unit", "provider:neon", "provider:neon:website", "local"] }, |
| 183 | ); |
| 184 | |
| 185 | for (const mode of ["import", "require"] as const) |
| 186 | it.effect( |
| 187 | `resolves pnpm ${mode} aliases without copying untraced dependencies`, |
| 188 | () => |
| 189 | Effect.gen(function* () { |
| 190 | const { fs, path, root, dist } = yield* fixture; |
| 191 | const store = path.join( |
| 192 | root, |
| 193 | "node_modules", |
| 194 | ".pnpm", |
| 195 | "answer@1", |
| 196 | "node_modules", |
| 197 | "answer", |
| 198 | ); |
| 199 | yield* fs.makeDirectory(store, { recursive: true }); |
| 200 | yield* fs.writeFileString(path.join(root, "package.json"), '{"type":"module"}'); |
| 201 | yield* fs.writeFileString( |
| 202 | path.join(store, "package.json"), |
| 203 | '{"name":"answer","type":"module","exports":"./index.js"}', |
| 204 | ); |
| 205 | yield* fs.writeFileString(path.join(store, "index.js"), "export const answer = 42;"); |
| 206 | yield* fs.writeFileString(path.join(store, "unused.txt"), "not-traced"); |
| 207 | yield* fs.symlink( |
| 208 | ".pnpm/answer@1/node_modules/answer", |
| 209 | path.join(root, "node_modules", "answer"), |
| 210 | ); |
| 211 | const other = path.join(root, "node_modules/.pnpm/answer@2/node_modules/answer"); |
| 212 | yield* fs.makeDirectory(other, { recursive: true }); |
| 213 | yield* fs.writeFileString( |
| 214 | path.join(other, "package.json"), |
| 215 | '{"name":"answer","type":"module","exports":"./index.js"}', |
| 216 | ); |
| 217 | yield* fs.writeFileString(path.join(other, "index.js"), "export const answer = 99;"); |
| 218 | yield* fs.makeDirectory(path.join(root, "node_modules/.pnpm/node_modules")); |
| 219 | yield* fs.symlink( |
| 220 | "../answer@2/node_modules/answer", |
| 221 | path.join(root, "node_modules/.pnpm/node_modules/answer"), |
| 222 | ); |
| 223 | yield* fs.writeFileString( |
| 224 | path.join(dist, "other.mjs"), |
| 225 | 'import "../node_modules/.pnpm/node_modules/answer/index.js";', |
| 226 | ); |
| 227 | const serverEntry = path.join(dist, "serve-neon.mjs"); |
| 228 | yield* fs.writeFileString( |
| 229 | serverEntry, |
| 230 | (mode === "import" |
| 231 | ? 'import { answer } from "answer";' |
| 232 | : 'import { createRequire } from "node:module"; const require = createRequire(import.meta.url); const { answer } = require("answer");') + |
| 233 | " export default { fetch() { return new Response(String(answer)); } };", |
| 234 | ); |
| 235 | const props = { root, distDir: dist, serverEntry }; |
| 236 | const first = yield* packageWebsiteArtifact(props); |
| 237 | const files = yield* Effect.sync(() => unzipSync(first.archive)); |
| 238 | expect( |
| 239 | Object.keys(files).some((name) => name.endsWith("node_modules/answer/index.js")), |
| 240 | ).toBe(true); |
| 241 | expect(Object.keys(files).some((name) => name.endsWith("unused.txt"))).toBe(false); |
| 242 | const staged = yield* stageWebsiteArtifact(props); |
| 243 | const proc = yield* ChildProcess.make("node", [ |
| 244 | "--input-type=module", |
| 245 | "-e", |
| 246 | `const {default: handler} = await import(${JSON.stringify(path.join(staged.directory, "index.mjs"))}); console.log(await handler.fetch(new Request("http://localhost/")).text());`, |
| 247 | ]); |
| 248 | const [code, stdout, stderr] = yield* Effect.all( |
| 249 | [ |
| 250 | proc.exitCode, |
| 251 | proc.stdout.pipe(Stream.decodeText, Stream.mkString), |
| 252 | proc.stderr.pipe(Stream.decodeText, Stream.mkString), |
| 253 | ], |
| 254 | { concurrency: "unbounded" }, |
| 255 | ); |
| 256 | expect({ code, stderr }).toEqual({ code: 0, stderr: "" }); |
| 257 | expect(stdout.trim()).toBe("42"); |
| 258 | yield* fs.writeFileString(path.join(store, "index.js"), "export const answer = 43;"); |
| 259 | expect((yield* packageWebsiteArtifact(props)).hash).not.toBe(first.hash); |
| 260 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 261 | { tags: ["unit", "provider:neon", "provider:neon:website", "local"] }, |
| 262 | ); |
| 263 | |
| 264 | const elf = (machine = 183) => { |
| 265 | const bytes = new Uint8Array(64); |
| 266 | bytes.set([0x7f, 0x45, 0x4c, 0x46, 2, 1, 1]); |
| 267 | bytes[16] = 3; |
| 268 | bytes[18] = machine; |
| 269 | return bytes; |
| 270 | }; |
| 271 | |
| 272 | for (const machine of [183, 62]) |
| 273 | it.effect( |
| 274 | `validates ELF target architecture ${machine}`, |
| 275 | () => |
| 276 | Effect.gen(function* () { |
| 277 | const { fs, path, root, dist } = yield* fixture; |
| 278 | yield* fs.writeFile(path.join(dist, "addon.node"), yield* Effect.sync(() => elf(machine))); |
| 279 | const result = yield* stageWebsiteArtifact({ root, distDir: dist, static: {} }).pipe( |
| 280 | Effect.result, |
| 281 | ); |
| 282 | expect(Result.isSuccess(result)).toBe(machine === 183); |
| 283 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 284 | { tags: ["unit", "provider:neon", "provider:neon:website", "local"] }, |
| 285 | ); |
| 286 | |
| 287 | it.effect( |
| 288 | "Sharp metadata-only traces do not materialize native packages", |
| 289 | () => |
| 290 | Effect.gen(function* () { |
| 291 | const { fs, path, root, dist } = yield* fixture; |
| 292 | const sharp = path.join(root, "node_modules/sharp"); |
| 293 | yield* fs.makeDirectory(sharp, { recursive: true }); |
| 294 | yield* fs.writeFileString( |
| 295 | path.join(sharp, "package.json"), |
| 296 | JSON.stringify({ name: "sharp", version: "0.35.4" }), |
| 297 | ); |
| 298 | const serverEntry = path.join(dist, "serve.mjs"); |
| 299 | yield* fs.writeFileString( |
| 300 | serverEntry, |
| 301 | 'import manifest from "sharp/package.json" with { type: "json" }; export default { fetch: () => new Response(manifest.version) };', |
| 302 | ); |
| 303 | const artifact = yield* stageWebsiteArtifact({ root, distDir: dist, serverEntry }); |
| 304 | expect( |
| 305 | yield* fs.exists(path.join(artifact.directory, "files/node_modules/sharp/package.json")), |
| 306 | ).toBe(true); |
| 307 | expect(yield* fs.exists(path.join(artifact.directory, "files/node_modules/@img"))).toBe( |
| 308 | false, |
| 309 | ); |
| 310 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 311 | { tags: ["unit", "provider:neon", "provider:neon:website", "local"] }, |
| 312 | ); |
| 313 | |
| 314 | // Minimal ustar fixtures exercise the registry boundary without downloading packages. |
| 315 | const tarball = (files: [string, Uint8Array, string?][]) => { |
| 316 | const blocks: Buffer[] = []; |
| 317 | for (const [name, content, type = "0"] of files) { |
| 318 | const header = Buffer.alloc(512); |
| 319 | header.write(name, 0); |
| 320 | header.write("0000644\0", 100); |
| 321 | header.write(content.length.toString(8).padStart(11, "0") + "\0", 124); |
| 322 | header.fill(32, 148, 156); |
| 323 | header.write(type, 156); |
| 324 | header.write("ustar\0", 257); |
| 325 | const checksum = header.reduce((sum: number, byte: number) => sum + byte, 0); |
| 326 | header.write(checksum.toString(8).padStart(6, "0") + "\0 ", 148); |
| 327 | blocks.push(header, Buffer.from(content), Buffer.alloc((512 - (content.length % 512)) % 512)); |
| 328 | } |
| 329 | return gzipSync(Buffer.concat([...blocks, Buffer.alloc(1024)])); |
| 330 | }; |
| 331 | |
| 332 | for (const scenario of [ |
| 333 | "valid", |
| 334 | "integrity", |
| 335 | "identity", |
| 336 | "origin", |
| 337 | "traversal", |
| 338 | "symlink", |
| 339 | "architecture", |
| 340 | "musl", |
| 341 | ] as const) |
| 342 | it.effect( |
| 343 | `Sharp platform replacement verifies ${scenario}`, |
| 344 | () => |
| 345 | Effect.gen(function* () { |
| 346 | const { fs, path, root, dist } = yield* fixture; |
| 347 | const sharp = path.join(root, "node_modules/sharp"); |
| 348 | yield* fs.makeDirectory(sharp, { recursive: true }); |
| 349 | yield* fs.writeFileString( |
| 350 | path.join(sharp, "package.json"), |
| 351 | JSON.stringify({ |
| 352 | name: "sharp", |
| 353 | version: "0.34.5", |
| 354 | main: "index.js", |
| 355 | optionalDependencies: { |
| 356 | "@img/sharp-linux-arm64": "0.34.5", |
| 357 | "@img/sharp-libvips-linux-arm64": "1.2.4", |
| 358 | }, |
| 359 | }), |
| 360 | ); |
| 361 | yield* fs.writeFileString(path.join(sharp, "index.js"), "module.exports = 42;"); |
| 362 | const serverEntry = path.join(dist, "serve.mjs"); |
| 363 | yield* fs.writeFileString( |
| 364 | serverEntry, |
| 365 | 'import sharp from "sharp"; export default { fetch: () => new Response(String(sharp)) };', |
| 366 | ); |
| 367 | const requested: string[] = []; |
| 368 | const fetch = ((input: string | URL | Request) => |
| 369 | Effect.runPromise( |
| 370 | Effect.sync(() => { |
| 371 | const url = String(input); |
| 372 | requested.push(url); |
| 373 | const name = url.includes("libvips") |
| 374 | ? "@img/sharp-libvips-linux-arm64" |
| 375 | : "@img/sharp-linux-arm64"; |
| 376 | const version = name.includes("libvips") ? "1.2.4" : "0.34.5"; |
| 377 | const manifest = { |
| 378 | name, |
| 379 | version, |
| 380 | os: ["linux"], |
| 381 | cpu: ["arm64"], |
| 382 | libc: [scenario === "musl" ? "musl" : "glibc"], |
| 383 | }; |
| 384 | const binary = name.includes("libvips") |
| 385 | ? "lib/libvips.so.42" |
| 386 | : "lib/sharp-linux-arm64.node"; |
| 387 | const archive = tarball([ |
| 388 | ["package/package.json", Buffer.from(JSON.stringify(manifest))], |
| 389 | [ |
| 390 | scenario === "traversal" ? "package/../escape" : `package/${binary}`, |
| 391 | elf(scenario === "architecture" ? 62 : 183), |
| 392 | scenario === "symlink" ? "2" : "0", |
| 393 | ], |
| 394 | ]); |
| 395 | if (url.endsWith(".tgz")) return new Response(archive); |
| 396 | return Response.json({ |
| 397 | ...manifest, |
| 398 | version: scenario === "identity" ? "99.0.0" : version, |
| 399 | dist: { |
| 400 | integrity: `sha512-${createHash("sha512") |
| 401 | .update(scenario === "integrity" ? "wrong" : archive) |
| 402 | .digest("base64")}`, |
| 403 | tarball: `${scenario === "origin" ? "https://untrusted.invalid" : "https://registry.npmjs.org"}/${name}/-/${name.split("/")[1]}-${version}.tgz`, |
| 404 | }, |
| 405 | }); |
| 406 | }), |
| 407 | )) as typeof globalThis.fetch; |
| 408 | const result = yield* stageWebsiteArtifact({ root, distDir: dist, serverEntry }).pipe( |
| 409 | Effect.provideService(FetchHttpClient.Fetch, fetch), |
| 410 | Effect.result, |
| 411 | ); |
| 412 | expect(Result.isSuccess(result)).toBe(scenario === "valid"); |
| 413 | expect(requested.length).toBeGreaterThan(0); |
| 414 | if (Result.isFailure(result)) expect(result.failure._tag).toBe("WebsiteArtifactError"); |
| 415 | if (Result.isSuccess(result)) { |
| 416 | const packageFile = path.join( |
| 417 | result.success.directory, |
| 418 | "files/node_modules/@img/sharp-linux-arm64/package.json", |
| 419 | ); |
| 420 | expect(JSON.parse(yield* fs.readFileString(packageFile)).version).toBe("0.34.5"); |
| 421 | expect( |
| 422 | JSON.parse(yield* fs.readFileString(path.join(sharp, "package.json"))).version, |
| 423 | ).toBe("0.34.5"); |
| 424 | } |
| 425 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 426 | { tags: ["unit", "provider:neon", "provider:neon:website", "local"] }, |
| 427 | ); |