Open source CapCut alternative.
TypeScript
chore(site): track Vercel firewall rate limits
David Lefcbe2e440a7d
diff --git a/site/firewall/README.md b/site/firewall/README.mdnew file mode 100644index 00000000..b8829e6c--- /dev/null+++ b/site/firewall/README.md@@ -0,0 +1,39 @@+# Site firewall++`all-apis.json` limits every method under `/api` to 100 requests per 10 seconds+per IP. `export-list-reads.json` adds a limit of 20 GET requests per 10 seconds+for project export lists. `frontend.json` limits non-API requests to 300 per+10 seconds per IP, excluding `/_next/static/` bundles. It covers page navigation,+server actions, dynamic images, and other asset requests. All rules apply across+this Vercel project's domains and deployments, with every HTTP method covered+by the API or frontend rule. Excess requests receive HTTP 429. Matching paths+share each rule's counter within a Vercel region. People sharing an IP share+the allowance; separate regions have separate counters.++The rules run before authentication, functions, and database access for every+hosted client, including older iOS builds. Auth, webhooks, cron routes, workers,+and MCP are covered too. Callers must handle 429 and back off. Browser-local+and Mac-local APIs stay local. Accepted requests incur normal compute charges;+these per-IP limits do not impose a total spending or concurrency cap.++From `site/`, with the Vercel CLI signed in to the Donkey Cut team:++```sh+npm run firewall:check+npm run firewall:stage+npx --yes [email protected] firewall publish --scope donkeycut --project donkey+npm run firewall:check+```++Edit the JSON, stage it, review the printed diff, then publish. Staging updates+existing rules by name and creates missing ones. Checking detects drift+from the published rules. Both commands stop when unpublished changes exist;+Vercel's publish command applies all pending firewall changes.++Firewall changes deploy separately from the site build. Keep credentials in+the Vercel CLI login. The CLI version is pinned in the script. Vercel bills+allowed matching requests at its rate-limiting price. Run+`node --test scripts/firewall.test.mjs` to check route coverage and the staging workflow.++[Vercel firewall CLI](https://vercel.com/docs/cli/firewall)+and [rate-limiting scope and pricing](https://vercel.com/docs/vercel-firewall/vercel-waf/rate-limiting).diff --git a/site/firewall/all-apis.json b/site/firewall/all-apis.jsonnew file mode 100644index 00000000..72c9111e--- /dev/null+++ b/site/firewall/all-apis.json@@ -0,0 +1,24 @@+{+ "name": "Limit API request bursts",+ "description": "Limit all API methods before functions and authentication run.",+ "active": true,+ "conditionGroup": [+ {+ "conditions": [+ { "type": "path", "op": "re", "value": "^/api(?:/|$)" }+ ]+ }+ ],+ "action": {+ "mitigate": {+ "action": "rate_limit",+ "rateLimit": {+ "algo": "fixed_window",+ "window": 10,+ "limit": 100,+ "keys": ["ip"],+ "action": "rate_limit"+ }+ }+ }+}diff --git a/site/firewall/export-list-reads.json b/site/firewall/export-list-reads.jsonnew file mode 100644index 00000000..87daea9e--- /dev/null+++ b/site/firewall/export-list-reads.json@@ -0,0 +1,25 @@+{+ "name": "Limit export listing bursts",+ "description": "Limit export list reads before authentication and database access.",+ "active": true,+ "conditionGroup": [+ {+ "conditions": [+ { "type": "path", "op": "re", "value": "^/api/cut-cloud/projects/[^/]+/exports/?$" },+ { "type": "method", "op": "eq", "value": "GET" }+ ]+ }+ ],+ "action": {+ "mitigate": {+ "action": "rate_limit",+ "rateLimit": {+ "algo": "fixed_window",+ "window": 10,+ "limit": 20,+ "keys": ["ip"],+ "action": "rate_limit"+ }+ }+ }+}diff --git a/site/firewall/frontend.json b/site/firewall/frontend.jsonnew file mode 100644index 00000000..a3df473b--- /dev/null+++ b/site/firewall/frontend.json@@ -0,0 +1,29 @@+{+ "name": "Limit frontend request bursts",+ "description": "Limit non-API requests before page routing and rendering; exclude Next.js static bundles.",+ "active": true,+ "conditionGroup": [+ {+ "conditions": [+ {+ "type": "path",+ "op": "re",+ "neg": true,+ "value": "^/(?:api(?:/|$)|_next/static(?:/|$))"+ }+ ]+ }+ ],+ "action": {+ "mitigate": {+ "action": "rate_limit",+ "rateLimit": {+ "algo": "fixed_window",+ "window": 10,+ "limit": 300,+ "keys": ["ip"],+ "action": "rate_limit"+ }+ }+ }+}diff --git a/site/package.json b/site/package.jsonindex 5dbc7e11..4a00d620 100644--- a/site/package.json+++ b/site/package.json@@ -12,6 +12,8 @@"build": "next build","start": "next start","lint": "node --max-old-space-size=8192 node_modules/eslint/bin/eslint.js",+ "firewall:check": "node scripts/firewall.mjs check",+ "firewall:stage": "node scripts/firewall.mjs stage","postinstall": "patch-package && node scripts/check-mediabunny-patch.mjs && prisma generate && node scripts/copy-mediapipe-assets.mjs && node scripts/fetch-cut-fonts.mjs && node scripts/stage-frame-synth.mjs","db:generate": "prisma generate","db:pull": "prisma db pull",diff --git a/site/scripts/firewall.mjs b/site/scripts/firewall.mjsnew file mode 100644index 00000000..bee5a75b--- /dev/null+++ b/site/scripts/firewall.mjs@@ -0,0 +1,61 @@+import { execFileSync } from "node:child_process";+import { readFileSync } from "node:fs";+import { fileURLToPath } from "node:url";+import { isDeepStrictEqual } from "node:util";++const site = fileURLToPath(new URL("../", import.meta.url));+const rules = ["export-list-reads", "all-apis", "frontend"].map((name) =>+ JSON.parse(readFileSync(new URL(`../firewall/${name}.json`, import.meta.url), "utf8")));+const cli = ["--yes", "[email protected]"];+const scope = ["--scope", "donkeycut", "--project", "donkey"];++function vercel(args, capture = false) {+ return execFileSync("npx", [...cli, "firewall", ...args, ...scope], {+ cwd: site,+ encoding: "utf8",+ stdio: capture ? ["ignore", "pipe", "inherit"] : "inherit",+ });+}++// Ignore provider IDs and unset optional fields; keep every effective condition.+function normalize(value) {+ if (Array.isArray(value)) return value.map(normalize);+ if (value !== null && typeof value === "object") {+ return Object.fromEntries(Object.entries(value)+ .filter(([key, item]) => key !== "id" && item != null && !(key === "neg" && item === false))+ .map(([key, item]) => [key, normalize(item)]));+ }+ return value;+}++function matches(rule, actual) {+ const fields = ["name", "description", "active", "conditionGroup", "action"];+ return isDeepStrictEqual(normalize(rule), normalize(Object.fromEntries(fields.map((key) => [key, actual[key]]))));+}++const mode = process.argv[2];+if (mode !== "check" && mode !== "stage") {+ throw new Error("Usage: node scripts/firewall.mjs check|stage");+}+const remote = JSON.parse(vercel(["rules", "list", "--json"], true));+if (remote.hasDraft) {+ throw new Error("Resolve pending Vercel firewall changes before checking or staging these rules.");+}+const changes = rules.flatMap((rule) => {+ const existing = remote.rules.filter((entry) => entry.name === rule.name);+ if (existing.length > 1) throw new Error(`Multiple rules named ${rule.name}; resolve the duplicate in Vercel.`);+ return existing.length === 1 && matches(rule, existing[0]) ? [] : [{ rule, existing: existing[0] }];+});+if (changes.length === 0) {+ console.log("Published firewall rules match the repository.");+ process.exit(0);+}+if (mode === "check") {+ throw new Error(`Firewall drift: ${changes.map(({ rule }) => rule.name).join(", ")}. Run npm run firewall:stage to prepare an update.`);+}+for (const { rule, existing } of changes) {+ const operation = existing ? ["edit", existing.id] : ["add"];+ vercel(["rules", ...operation, "--json", JSON.stringify(rule), "--yes"]);+}+vercel(["diff"]);+console.log("Review the draft, then publish: npx --yes [email protected] firewall publish --scope donkeycut --project donkey");diff --git a/site/scripts/firewall.test.mjs b/site/scripts/firewall.test.mjsnew file mode 100644index 00000000..e4362bb0--- /dev/null+++ b/site/scripts/firewall.test.mjs@@ -0,0 +1,116 @@+import assert from "node:assert/strict";+import { execFileSync, spawnSync } from "node:child_process";+import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { test } from "node:test";+import { fileURLToPath } from "node:url";++const site = fileURLToPath(new URL("../", import.meta.url));+const rules = ["export-list-reads", "all-apis", "frontend"].map((name) =>+ JSON.parse(readFileSync(new URL(`../firewall/${name}.json`, import.meta.url), "utf8")));+const live = rules.map((rule, index) => ({ ...rule, id: `rule_${index}`, _status: "live" }));++function run(mode, remote) {+ const directory = mkdtempSync(join(tmpdir(), "donkey-firewall-test-"));+ try {+ const executable = join(directory, "npx");+ const calls = join(directory, "calls.jsonl");+ writeFileSync(executable, `#!${process.execPath}\n` ++ 'const fs = require("node:fs"); const args = process.argv.slice(2);\n' ++ 'fs.appendFileSync(process.env.FIREWALL_TEST_CALLS, JSON.stringify(args) + "\\n");\n' ++ 'if (args.includes("list")) process.stdout.write(process.env.FIREWALL_TEST_REMOTE);\n');+ chmodSync(executable, 0o755);+ const result = spawnSync(process.execPath, ["scripts/firewall.mjs", mode], {+ cwd: site,+ encoding: "utf8",+ env: {+ ...process.env,+ PATH: `${directory}:${process.env.PATH}`,+ FIREWALL_TEST_CALLS: calls,+ FIREWALL_TEST_REMOTE: JSON.stringify(remote),+ },+ });+ return { ...result, calls: readFileSync(calls, "utf8").trim().split("\n").map(JSON.parse) };+ } finally {+ rmSync(directory, { recursive: true, force: true });+ }+}++test("the broad rule covers every API route and method without including pages", () => {+ const condition = rules[1].conditionGroup[0].conditions;+ assert.equal(condition.length, 1);+ assert.equal(condition[0].type, "path");+ const pattern = new RegExp(condition[0].value);+ const paths = execFileSync("git", ["ls-files", "src/app/api/**/route.ts"], { cwd: site, encoding: "utf8" }).trim().split("\n");+ assert.ok(paths.length > 50);+ for (const path of paths) assert.ok(pattern.test(path.replace("src/app", "").replace("/route.ts", "")), path);+ assert.ok(pattern.test("/api"));+ for (const path of ["/apiary", "/app", "/sign-in"]) assert.equal(pattern.test(path), false);+ const [exportPath, method] = rules[0].conditionGroup[0].conditions;+ assert.equal(method.value, "GET");+ assert.ok(new RegExp(exportPath.value).test("/api/cut-cloud/projects/example/exports"));+ assert.equal(new RegExp(exportPath.value).test("/api/cut-cloud/projects/example/exports/file"), false);+});++test("matching published rules are a no-op, including provider metadata", () => {+ const remote = structuredClone(live);+ remote[0].action.mitigate.actionDuration = null;+ remote[0].conditionGroup[0].conditions[0].neg = false;+ for (const mode of ["check", "stage"]) {+ const result = run(mode, { rules: remote, hasDraft: false });+ assert.equal(result.status, 0, result.stderr);+ assert.equal(result.calls.length, 1);+ }+});++test("frontend protection covers page and dynamic asset requests across hosts and methods", () => {+ const conditions = rules[2].conditionGroup[0].conditions;+ assert.equal(conditions.length, 1);+ const condition = conditions[0];+ assert.equal(condition.type, "path");+ assert.equal(condition.neg, true);+ const excluded = new RegExp(condition.value);+ for (const path of ["/", "/app", "/app/p/example", "/sign-in", "/s/example/card/image.png", "/_next/image", "/_next/data/build/app.json"]) {+ assert.equal(excluded.test(path), false, path);+ }+ for (const path of ["/api", "/api/auth/get-session", "/api/inference/responses", "/_next/static/chunks/app.js", "/_next/static/css/app.css"]) {+ assert.equal(excluded.test(path), true, path);+ }+});++test("pending drafts prevent both checks and mutations", () => {+ for (const mode of ["check", "stage"]) {+ const result = run(mode, { rules: live, hasDraft: true });+ assert.notEqual(result.status, 0);+ assert.equal(result.calls.length, 1);+ }+});++test("condition negation is detected as drift", () => {+ const remote = structuredClone(live);+ remote[1].conditionGroup[0].conditions[0].neg = true;+ const result = run("check", { rules: remote, hasDraft: false });+ assert.notEqual(result.status, 0);+ assert.match(result.stderr, /Firewall drift/);+});++test("staging creates only the missing rule and leaves publication to the operator", () => {+ const result = run("stage", { rules: [live[0], live[2]], hasDraft: false });+ assert.equal(result.status, 0, result.stderr);+ assert.equal(result.calls.length, 3);+ const create = result.calls[1];+ assert.ok(create.includes("add"));+ assert.deepEqual(JSON.parse(create[create.indexOf("--json") + 1]), rules[1]);+ assert.ok(result.calls[2].includes("diff"));+ assert.equal(result.calls.some((args) => args.includes("publish")), false);+});++test("staging updates an existing rule by ID", () => {+ const remote = structuredClone(live);+ remote[1].action.mitigate.rateLimit.limit = 999;+ const result = run("stage", { rules: remote, hasDraft: false });+ assert.equal(result.status, 0, result.stderr);+ assert.ok(result.calls[1].includes("edit"));+ assert.ok(result.calls[1].includes("rule_1"));+});