Skip to content

armgit / fuobe

Public Imported from GitHub
0

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.

Generations API: a LoRA link only from Hugging Face, civitai, fal or Replicate

Claudea1ec3c05349e
diff --git a/src/app/api/generations/route.ts b/src/app/api/generations/route.ts
index 8b64ad8b..8e5c309f 100644
--- a/src/app/api/generations/route.ts
+++ b/src/app/api/generations/route.ts
@@ -9,6 +9,23 @@ import { SignatureSchema } from "@/server/palettes";
/** A point on a picture as fractions of its width and height. */
const Focus = z.object({ x: z.number().min(0).max(1), y: z.number().min(0).max(1) });
+/**
+ * Where a LoRA named in a request may be downloaded from. A file the server doesn't know is fetched from its
+ * link before a ComfyUI run (or by fal / Replicate), so an open link would let any token holder make the
+ * server fetch an internal address into models/loras. Every research LoRA is on Hugging Face or civitai;
+ * trained and mirrored copies live in fal's or Replicate's file stores.
+ */
+const LORA_HOSTS = ["huggingface.co", "hf.co", "civitai.com", "fal.media", "replicate.delivery"];
+function loraHostOk(link: string): boolean {
+ try {
+ const u = new URL(link);
+ const host = u.hostname.toLowerCase();
+ return u.protocol === "https:" && !u.username && !u.password && LORA_HOSTS.some((h) => host === h || host.endsWith(`.${h}`));
+ } catch {
+ return false;
+ }
+}
+
const RequestSchema = z.object({
feature: z.string().min(1),
presetId: z.string().optional(),
@@ -95,7 +112,13 @@ const RequestSchema = z.object({
// Brackets too: Redmond's Z-Image LoRAs are called "[ZImage.Turbo]FilmStill_Redmond.safetensors".
.regex(/^[\w ()[\]+,.@-]+(\/[\w ()[\]+,.@-]+)*$/, "a file name in ComfyUI's models/loras")
.refine((f) => !f.split("/").includes(".."), "a file name in ComfyUI's models/loras"),
- url: z.string().max(2000).regex(/^https:\/\//, "an https:// link").optional(),
+ // Only the public model hubs and the cloud providers' file stores: the server (or a provider) may fetch it.
+ url: z
+ .string()
+ .max(2000)
+ .regex(/^https:\/\//, "an https:// link")
+ .refine(loraHostOk, "a LoRA link on Hugging Face, civitai, fal or Replicate")
+ .optional(),
strength: z.number().min(-4).max(4).optional(),
trigger: z.string().max(200).optional(),
target: z.enum(["high", "low", "both"]).optional(),
diff --git a/tests/eval-api.test.ts b/tests/eval-api.test.ts
index 593b7295..f47fe458 100644
--- a/tests/eval-api.test.ts
+++ b/tests/eval-api.test.ts
@@ -47,6 +47,11 @@ describe("POST /api/generations with a recipe's LoRAs", () => {
{ file: "/abs.safetensors" },
{ file: "ok.safetensors", url: "http://insecure.example/x.safetensors" },
{ file: "ok.safetensors", url: "file:///etc/passwd" },
+ // Only the model hubs and the providers' file stores: the server may fetch an unknown file's link.
+ { file: "ok.safetensors", url: "https://internal.host/x.safetensors" },
+ { file: "ok.safetensors", url: "https://10.0.0.5/x.safetensors" },
+ { file: "ok.safetensors", url: "https://[email protected]/x.safetensors" },
+ { file: "ok.safetensors", url: "https://huggingface.co.evil.example/x.safetensors" },
{ file: "ok.safetensors", strength: 9 },
{ file: "ok.safetensors", target: "middle" },
]) {
@@ -57,10 +62,22 @@ describe("POST /api/generations with a recipe's LoRAs", () => {
expect((await post({ ...soul, loras: Array.from({ length: 9 }, (_, i) => ({ file: `l${i}.safetensors` })) })).status).toBe(400);
});
+ it("takes LoRA links on Hugging Face, civitai, fal and Replicate", async () => {
+ for (const url of ["https://huggingface.co/a/b/resolve/main/x.safetensors", "https://civitai.com/api/download/models/2463391", "https://v3.fal.media/files/x/lora.safetensors", "https://replicate.delivery/pbxt/x/lora.safetensors"]) {
+ expect((await post({ ...soul, loras: [{ file: "ok.safetensors", url }] })).status, url).toBe(200);
+ }
+ });
+
it("takes every LoRA file name in the research (the harness sends them as they are)", async () => {
const research = JSON.parse(fs.readFileSync(path.join(process.cwd(), "docs/research/fuobe-originals-recipes-5.json"), "utf8"));
type L = { file?: string };
const files = [...new Set<string>([...research.presets, ...research.motionsEffects].flatMap((e: { loras?: L[] }) => (e.loras ?? []).map((l) => l.file ?? "")).filter(Boolean))];
+ // And every link: they're all on hosts the API takes.
+ const urls = [...new Set<string>([...research.presets, ...research.motionsEffects].flatMap((e: { loras?: (L & { url?: string })[] }) => (e.loras ?? []).map((l) => l.url ?? "")).filter(Boolean))];
+ for (let i = 0; i < urls.length; i += 8) {
+ const r = await post({ ...soul, loras: urls.slice(i, i + 8).map((url, j) => ({ file: `l${j}.safetensors`, url })) });
+ expect(r.status, r.body.error).toBe(200);
+ }
expect(files.length).toBeGreaterThan(50);
for (let i = 0; i < files.length; i += 8) {
const r = await post({ ...soul, loras: files.slice(i, i + 8).map((file) => ({ file })) });