The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Security: the 3D finish reads only the model it was given
Adarshfd09201e155e
diff --git a/scripts/model3d/finish-glb.mjs b/scripts/model3d/finish-glb.mjsindex dff21aa0..c2dea108 100644--- a/scripts/model3d/finish-glb.mjs+++ b/scripts/model3d/finish-glb.mjs@@ -57,10 +57,32 @@ const FINISH = {};const t0 = Date.now();+// Only a self-contained binary glTF (version 2): every buffer and image inside the file or in a data: URI. A model+// made on someone else's ComfyUI must never make this script read another file on this machine (a buffer or image+// URI such as "../../auth.json" would be loaded from disk and baked into the result). Exit 3 means refused.+const input = fs.readFileSync(a.in);+function refuse(why) {+ console.error(why);+ process.exit(3);+}+if (input.length < 20 || input.toString("latin1", 0, 4) !== "glTF" || input.readUInt32LE(4) !== 2) refuse("not a binary glTF 2 file");+{+ const jsonLength = input.readUInt32LE(12);+ if (input.readUInt32LE(16) !== 0x4e4f534a || 20 + jsonLength > input.length) refuse("not a binary glTF 2 file");+ let gltf;+ try {+ gltf = JSON.parse(input.toString("utf8", 20, 20 + jsonLength));+ } catch {+ refuse("not a binary glTF 2 file");+ }+ const uris = [...(gltf.buffers ?? []), ...(gltf.images ?? [])].map((x) => x?.uri).filter((u) => u !== undefined && u !== null);+ if (uris.some((u) => typeof u !== "string" || !u.startsWith("data:"))) refuse("external file reference");+}await Promise.all([MeshoptDecoder.ready, MeshoptEncoder.ready]);const io = new NodeIO().registerExtensions(ALL_EXTENSIONS).registerDependencies({ "meshopt.decoder": MeshoptDecoder, "meshopt.encoder": MeshoptEncoder });-const bytesIn = fs.statSync(a.in).size;-const doc = await io.read(a.in);+const bytesIn = input.length;+// From the bytes, never from the path: nothing beside the file is ever looked up.+const doc = await io.readBinary(new Uint8Array(input.buffer, input.byteOffset, input.byteLength));// stdout carries only the JSON result line.doc.setLogger(new Logger(Logger.Verbosity.ERROR));const root = doc.getRoot();diff --git a/src/app/api/generations/route.ts b/src/app/api/generations/route.tsindex 3ba30030..062af4cc 100644--- a/src/app/api/generations/route.ts+++ b/src/app/api/generations/route.ts@@ -1,5 +1,5 @@import { connection } from "next/server";-import { CONNECT_GPU_HEADER } from "@/lib/guest-access";+import { CONNECT_GPU_HEADER, OWN_GPU_TOOL_MESSAGE } from "@/lib/guest-access";import { accountGpu, requestAccount, requestViewer } from "@/server/auth";import { OPERATION_LABELS } from "@/lib/catalog/types";import { accountRun, asVisitor, createGenerations, listGenerations, outputKind, previewGeneration } from "@/server/generations";@@ -60,6 +60,11 @@ export async function POST(request: Request) {// createGenerations says what's wrong with the request.}if (planned) {+ // A 3D model from someone else's ComfyUI is finished on this server (decimate, web GLB, turntable): accounts+ // don't get Image to 3D until that finish runs under an account's own limits.+ if (account && outputKind(planned.op, planned.model) === "model3d") {+ return Response.json({ error: OWN_GPU_TOOL_MESSAGE, code: "own-gpu-tool" }, { status: 403 });+ }const skip = (body as { gpuCheck?: unknown } | null)?.gpuCheck === "skip";const gate = await gateGeneration(gpuTargetFor(request, "read"), { request: req, route: planned.route, op: planned.op, modelId: planned.model.id, skip, actor: account?.id ?? "owner" });if (!gate.ok) return Response.json({ error: gate.message, code: "gpu-setup", ref: gate.ref, plan: gate.plan }, { status: 409, headers: { [GPU_SETUP_HEADER]: "1" } });diff --git a/src/server/generations.ts b/src/server/generations.tsindex dc5b923b..0f95207e 100644--- a/src/server/generations.ts+++ b/src/server/generations.ts@@ -53,7 +53,7 @@ import { setResolver, watchGenerations, type LiveEvent } from "./live";import { provider, PROVIDERS, RETIRED_PROVIDER_ERROR } from "./providers";import { comfyLoraFiles, onOwnGpu, outputFetch, probeComfy } from "./providers/comfyui";import { accountGpu } from "./auth";-import { CONNECT_GPU_MESSAGE } from "@/lib/guest-access";+import { CONNECT_GPU_MESSAGE, OWN_GPU_TOOL_MESSAGE } from "@/lib/guest-access";import { graphLoras } from "@/lib/catalog/character-swap";import { ensureLorasOnComfy, prepareCloudLoras, withoutUnreachableCivitai } from "./loras";import { ACCESS_PLACE } from "@/lib/model-access";@@ -729,6 +729,8 @@ export function createGenerations(asked: GenerationRequest, opts: CreateOptionsconst catalogModel = getModel(original.modelId);const found = catalogModel && offeredFor(catalogModel, original.engine);const shotModel = found && dualEngine(found) ? onEngine(found, engineFor(found, original.engine)) : found;+ // An account's 3D model would be finished on this server, outside its own limits: not yet (the route refuses first).+ if (original.account && shotModel?.kind === "model3d") throw new Error(OWN_GPU_TOOL_MESSAGE);if (!opts.dependsOn && shotModel?.options.multiShot?.mode === "concat" && (original.params?.shots?.filter((x) => x.prompt.trim()).length ?? 0) > 1) {return createShotFilm(original, shotModel);}diff --git a/tests/glb-finish-safety.test.ts b/tests/glb-finish-safety.test.tsnew file mode 100644index 00000000..a645c623--- /dev/null+++ b/tests/glb-finish-safety.test.ts@@ -0,0 +1,94 @@+import { spawnSync } from "node:child_process";+import fs from "node:fs";+import os from "node:os";+import path from "node:path";+import { afterAll, describe, expect, it } from "vitest";++const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fuobe-glb-safety-"));+process.env.FUOBE_DATA_DIR = path.join(dir, "data");+const { createGenerations } = await import("@/server/generations");+const { OWN_GPU_TOOL_MESSAGE } = await import("@/lib/guest-access");+afterAll(() => {+ try {+ fs.rmSync(dir, { recursive: true, force: true });+ } catch {}+});++const SCRIPT = path.join(process.cwd(), "scripts", "model3d", "finish-glb.mjs");+const MARKER = "FAKE_SECRET_MARKER_0123456789";++/** A one-triangle GLB; `image` adds an image (and a texture using it) with that URI. */+function glb(image?: string): Buffer {+ const bin = Buffer.alloc(36);+ [0, 0, 0, 1, 0, 0, 0, 1, 0].forEach((v, i) => bin.writeFloatLE(v, i * 4));+ const json: Record<string, unknown> = {+ asset: { version: "2.0" },+ buffers: [{ byteLength: 36 }],+ bufferViews: [{ buffer: 0, byteOffset: 0, byteLength: 36 }],+ accessors: [{ bufferView: 0, componentType: 5126, count: 3, type: "VEC3", min: [0, 0, 0], max: [1, 1, 0] }],+ meshes: [{ primitives: [{ attributes: { POSITION: 0 }, ...(image && { material: 0 }) }] }],+ nodes: [{ mesh: 0 }],+ scenes: [{ nodes: [0] }],+ scene: 0,+ ...(image && {+ images: [{ uri: image, mimeType: "image/png" }],+ textures: [{ source: 0 }],+ materials: [{ emissiveTexture: { index: 0 }, emissiveFactor: [1, 1, 1] }],+ }),+ };+ let j = Buffer.from(JSON.stringify(json));+ j = Buffer.concat([j, Buffer.alloc((4 - (j.length % 4)) % 4, 0x20)]);+ const header = Buffer.alloc(12);+ header.writeUInt32LE(0x46546c67, 0);+ header.writeUInt32LE(2, 4);+ header.writeUInt32LE(12 + 8 + j.length + 8 + bin.length, 8);+ const jh = Buffer.alloc(8);+ jh.writeUInt32LE(j.length, 0);+ jh.writeUInt32LE(0x4e4f534a, 4);+ const bh = Buffer.alloc(8);+ bh.writeUInt32LE(bin.length, 0);+ bh.writeUInt32LE(0x004e4942, 4);+ return Buffer.concat([header, jh, j, bh, bin]);+}++const run = (input: string, output: string) => spawnSync(process.execPath, [SCRIPT, "--in", input, "--out", output], { encoding: "utf8", timeout: 60_000, cwd: dir });++describe("the 3D finish reads only the model it was given", () => {+ it("refuses a GLB whose image points at another file, and never reads it", () => {+ const work = fs.mkdtempSync(path.join(dir, "job-"));+ fs.writeFileSync(path.join(dir, "marker.txt"), MARKER);+ const raw = path.join(work, "raw.glb");+ const out = path.join(work, "web.glb");+ fs.writeFileSync(raw, glb("../marker.txt"));+ const r = run(raw, out);+ expect(r.status).toBe(3);+ expect(r.stderr).toMatch(/external file reference/);+ expect(fs.existsSync(out)).toBe(false);+ expect(r.stdout).not.toContain(MARKER);+ });++ it("refuses a .gltf (JSON) model and anything that isn't a binary glTF 2", () => {+ const work = fs.mkdtempSync(path.join(dir, "job-"));+ const gltf = path.join(work, "model.gltf");+ fs.writeFileSync(gltf, JSON.stringify({ asset: { version: "2.0" }, buffers: [{ uri: "../marker.txt", byteLength: 10 }] }));+ const r = run(gltf, path.join(work, "web.glb"));+ expect(r.status).toBe(3);+ expect(r.stderr).toMatch(/not a binary glTF 2/);+ });++ it("finishes a self-contained GLB as before", () => {+ const work = fs.mkdtempSync(path.join(dir, "job-"));+ const raw = path.join(work, "raw.glb");+ const out = path.join(work, "web.glb");+ fs.writeFileSync(raw, glb());+ const r = run(raw, out);+ expect(r.status, r.stderr).toBe(0);+ expect(fs.existsSync(out)).toBe(true);+ });+});++describe("accounts don't run 3D models yet", () => {+ it("refuses an account's Image to 3D run before anything is queued", () => {+ expect(() => createGenerations({ feature: "image-to-3d", modelId: "oss-pixal3d", op: "image-to-3d", prompt: "", inputs: { images: ["data:image/png;base64,AA=="] }, account: "acct-1", engine: "open" })).toThrow(OWN_GPU_TOOL_MESSAGE);+ });+});