Open source CapCut alternative.
TypeScript
fix(site): fal key only ever reaches the queue host
David Le1ec25d5039f4
diff --git a/site/src/lib/inference/adapters/fal-matte.test.ts b/site/src/lib/inference/adapters/fal-matte.test.tsnew file mode 100644index 00000000..58c04035--- /dev/null+++ b/site/src/lib/inference/adapters/fal-matte.test.ts@@ -0,0 +1,64 @@+import { describe, expect, test } from "bun:test";++import { createFalMatteProvider } from "./fal-matte";+import { falMatteModels } from "@/lib/inference/matte-models";++const generation = (providerPollingUrl: string) => ({+ id: "gen-1",+ kind: "matte" as const,+ provider: "fal",+ model: falMatteModels.segmenter,+ providerJobId: "req-1",+ providerGenerationId: "req-1",+ providerPollingUrl,+ outputs: [],+ metadata: {},+});++describe("fal matte provider", () => {+ test("a poll address off the queue host never receives the key", async () => {+ const calls: string[] = [];+ const provider = createFalMatteProvider({ FAL_KEY: "secret" }, (async (+ url: string | URL | Request,+ ) => {+ calls.push(String(url));+ return new Response("{}", { status: 200 });+ }) as typeof fetch);++ let refused = "";+ try {+ await provider.refreshAsset!(+ generation("https://attacker.example/collect"),+ );+ } catch (e) {+ refused = e instanceof Error ? e.message : String(e);+ }+ expect(refused).toContain("off the queue host");+ expect(calls).toEqual([]);+ });++ test("a poll address on the queue host is polled with the key", async () => {+ const seen: { url: string; auth: string | null }[] = [];+ const provider = createFalMatteProvider({ FAL_KEY: "secret" }, (async (+ url: string | URL | Request,+ init?: RequestInit,+ ) => {+ seen.push({+ url: String(url),+ auth: new Headers(init?.headers).get("authorization"),+ });+ return Response.json({ status: "IN_PROGRESS" });+ }) as typeof fetch);++ const result = await provider.refreshAsset!(+ generation("https://queue.fal.run/fal-ai/model/requests/req-1"),+ );+ expect(result.status).toBe("in_progress");+ expect(seen).toEqual([+ {+ url: "https://queue.fal.run/fal-ai/model/requests/req-1/status",+ auth: "Key secret",+ },+ ]);+ });+});diff --git a/site/src/lib/inference/adapters/fal-matte.ts b/site/src/lib/inference/adapters/fal-matte.tsindex b6eb9d6f..f0adc5bd 100644--- a/site/src/lib/inference/adapters/fal-matte.ts+++ b/site/src/lib/inference/adapters/fal-matte.ts@@ -115,7 +115,17 @@ export function createFalMatteProvider(return model as FalMatteModel;}+ // The key goes to the queue host and nowhere else. A poll URL arrives on+ // the refresh request as a client field, so the address is checked here,+ // at the one place the key is attached, and an address off the queue is+ // refused before any request goes out.async function api(url: string, init?: RequestInit): Promise<JsonValue> {+ if (new URL(url).origin !== new URL(QUEUE_BASE).origin) {+ throw new InferenceProviderError("The matte poll address is off the queue host.", {+ statusCode: 400,+ code: "matte_poll_url_rejected",+ });+ }const res = await fetchImpl(url, {...init,headers: {