Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(plugins): rate-limit manifest import, publish and install counting
nirholas559dc8706a35
diff --git a/api/_lib/rate-limit.js b/api/_lib/rate-limit.jsindex 0d365165e..1f268f252 100644--- a/api/_lib/rate-limit.js+++ b/api/_lib/rate-limit.js@@ -1190,6 +1190,13 @@ export const limits = {// still stops a scripted flood. Page-load-critical clusters additionally get// their own dedicated buckets below so one surface can never starve another.publicIp: (ip) => getLimiter('public:ip', { limit: 240, window: '1 m', local: true }).limit(ip),+ // Fiat onramp checkout links (/api/onramp/link). Unlike the generic public read,+ // each configured call mints a single-use session token against Coinbase's CDP+ // API, so an unauthenticated flood here is upstream quota burn, not just our CPU.+ // 20 per 5 min per IP covers a user reopening the Add funds overlay and switching+ // between USDC and SOL several times, and nothing legitimate needs more.+ onrampLinkIp: (ip) =>+ getLimiter('onramp:link:ip', { limit: 20, window: '5 m', local: true }).limit(ip),// /irl coordinate reads (pins nearby, drops nearby, world-lines nearby). These// are the only public reads that reveal WHERE another user placed something, so// their ceiling is a privacy budget, not a flood guard, and it must not drift@@ -1308,6 +1315,27 @@ export const limits = {// Skills marketplace browse — isolated bucket so traffic on other public endpoints// can't starve the skills list. 60/min per IP.skillsBrowse: (ip) => getLimiter('skills:browse', { limit: 60, window: '1 m' }).limit(ip),+ // Plugin manifest import (POST /api/plugins/import). Every call makes the+ // server fetch a caller-supplied URL, so this is the one plugin lane that can+ // be turned into an egress amplifier: 600/min (the generic browse ceiling) is+ // 600 outbound requests a minute per IP pointed wherever the caller likes.+ // 20 per 5 min is more imports than any human performs and still lets a+ // developer iterate on a manifest they are debugging.+ pluginImportIp: (ip) =>+ getLimiter('plugin:import:ip', { limit: 20, window: '5 m' }).limit(ip),+ // Plugin publish (POST /api/plugins/publish). Keyed on the authenticated user+ // rather than the IP because the row it writes is owned by that account.+ // Re-publishing the same identifier is an upsert, so the ceiling only bounds+ // how many DISTINCT plugin rows one account can create per hour.+ pluginPublishUser: (userId) =>+ getLimiter('plugin:publish:user', { limit: 30, window: '1 h' }).limit(userId),+ // Install-counter dedupe (POST /api/plugins/:id/install), same shape as+ // avatarViewIp: one counted install per (IP, plugin) per 30 minutes so a user+ // who reinstalls a plugin four times moves install_count by one. NOT local:+ // a per-instance counter would let the same caller count once per warm Cloud+ // Run instance, which is exactly the inflation this bucket exists to stop.+ pluginInstallDedupe: (ipAndPlugin) =>+ getLimiter('plugin:install:dedupe', { limit: 1, window: '30 m' }).limit(ipAndPlugin),// Marketplace agent preview chat — anonymous "try before fork" flow on the// agent detail page. Strict per-IP and per-agent caps so one client can't// drain LLM credits and one agent can't starve the global pool.diff --git a/api/plugins/[action].js b/api/plugins/[action].jsindex a85b28e70..0e4fef26b 100644--- a/api/plugins/[action].js+++ b/api/plugins/[action].js@@ -14,12 +14,23 @@ import { sql } from '../_lib/db.js';import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';import { cors, error, json, method, readJson, wrap, rateLimited } from '../_lib/http.js';import { limits, clientIp } from '../_lib/rate-limit.js';-import { fetchSafePublicUrl, SsrfBlockedError } from '../_lib/ssrf-guard.js';+import {+ fetchSafePublicUrlPinned,+ MaxBytesExceededError,+ SsrfBlockedError,+} from '../_lib/ssrf-guard.js';+import { requireCsrf } from '../_lib/csrf.js';import { isUuid } from '../_lib/validate.js';const VALID_SORTS = new Set(['popular', 'new', 'az']);const MAX_MANIFEST_BYTES = 64 * 1024; // 64 KBconst FETCH_TIMEOUT_MS = 8000;+// identifier is half of the (identifier, author_id) unique key and is echoed+// back in every listing, so it gets a length ceiling rather than inheriting the+// unbounded `text` column. api[] is capped for the same reason: the manifest is+// stored whole and re-served on every detail read.+const MAX_IDENTIFIER_LENGTH = 128;+const MAX_TOOLS = 100;// ── Manifest validation — LobeHub/pai-chat ToolManifest format ───────────────// Required: identifier, meta.title, api[]@@ -31,9 +42,12 @@ function validateManifest(json) {throw new Error('Missing identifier');if (!/^[a-z0-9._-]+$/i.test(json.identifier))throw new Error('identifier must be alphanumeric with dots, hyphens, or underscores');+ if (json.identifier.length > MAX_IDENTIFIER_LENGTH)+ throw new Error(`identifier must be ${MAX_IDENTIFIER_LENGTH} characters or fewer`);if (!json.meta?.title) throw new Error('Missing meta.title');if (!Array.isArray(json.api) || !json.api.length)throw new Error('api must be a non-empty array');+ if (json.api.length > MAX_TOOLS) throw new Error(`api must declare at most ${MAX_TOOLS} tools`);for (const tool of json.api) {if (!tool.name || !tool.description)throw new Error(`Tool "${tool.name || '?'}" missing name or description`);@@ -142,7 +156,13 @@ async function handleList(req, res, url) {const sort = VALID_SORTS.has(sortParam) ? sortParam : 'popular';const cursor = url.searchParams.get('cursor') || null;const limit = Math.min(40, Math.max(1, Number(url.searchParams.get('limit')) || 20));- const offset = cursor ? Math.max(0, Number(cursor)) : 0;+ // The cursor is this endpoint's own opaque offset, handed back as next_cursor.+ // A hand-typed one still has to be a non-negative integer: Number('abc') is+ // NaN, and NaN reaching OFFSET made Postgres throw, which surfaced as a 500+ // for what is plainly a client fault.+ const offset = cursor === null ? 0 : Number(cursor);+ if (!Number.isInteger(offset) || offset < 0)+ return error(res, 400, 'validation_error', 'cursor must be a non-negative integer');const qLike = q ? `%${q}%` : null;const fetchLimit = limit + 1;@@ -186,6 +206,14 @@ async function handleDetail(req, res, id) {const rl = await limits.widgetRead(clientIp(req));if (!rl.success) return rateLimited(res, rl);+ // `is_public: false` is a real publish option, and list/categories both honor+ // it. Detail used to read by id alone, so anyone holding the UUID could pull+ // an unpublished plugin's full manifest. Gate it the same way: public to+ // everyone, private to its author only. An anonymous reader has a null viewer+ // id, and `author_id = NULL` is never true, so the row simply 404s for them.+ const auth = await resolveAuth(req);+ const viewerId = auth?.userId ?? null;+const [row] = await sql`SELECT p.*, u.display_name AS author_display_name,ap.amount AS asset_price_amount,@@ -196,7 +224,9 @@ async function handleDetail(req, res, id) {LEFT JOIN users u ON u.id = p.author_idLEFT JOIN asset_prices apON ap.item_type = 'plugin' AND ap.item_id = p.id AND ap.is_active = true- WHERE p.id = ${id} AND p.deleted_at IS NULL+ WHERE p.id = ${id}+ AND p.deleted_at IS NULL+ AND (p.is_public = true OR p.author_id = ${viewerId}::uuid)`;if (!row) return error(res, 404, 'not_found', 'plugin not found');@@ -209,7 +239,7 @@ async function handleImport(req, res) {if (cors(req, res, { methods: 'POST,OPTIONS', credentials: true })) return;if (!method(req, res, ['POST'])) return;- const rl = await limits.widgetRead(clientIp(req));+ const rl = await limits.pluginImportIp(clientIp(req));if (!rl.success) return rateLimited(res, rl);const body = await readJson(req);@@ -225,32 +255,44 @@ async function handleImport(req, res) {if (!['https:', 'http:'].includes(parsed.protocol))return error(res, 400, 'validation_error', 'manifest_url must be http or https');- // Fetch the manifest server-side to avoid CORS issues. fetchSafePublicUrl- // DNS-resolves the host (and every redirect hop) and rejects private,- // loopback, link-local, and cloud-metadata ranges so a user-supplied- // manifest_url can't be used to probe internal services (SSRF).+ // Fetch the manifest server-side to avoid CORS issues. The pinned variant+ // DNS-resolves the host (and every redirect hop), rejects private, loopback,+ // link-local, and cloud-metadata ranges, and connects straight to the address+ // it validated so a hostile resolver cannot rebind between the check and the+ // socket. It is the right variant here because the fetched body is handed+ // back to the caller rather than merely rendered.+ //+ // maxBytes aborts the transfer the instant it crosses the manifest ceiling,+ // from the advertised content-length up front and from the streamed bytes+ // after that. Reading the body first and measuring it afterwards, as this+ // used to, let a hostile host stream gigabytes into the instance before the+ // size check ever ran.let manifest;try {const ac = new AbortController();const timer = setTimeout(() => ac.abort(), FETCH_TIMEOUT_MS);let resp;try {- resp = await fetchSafePublicUrl(+ resp = await fetchSafePublicUrlPinned(manifestUrl,{ signal: ac.signal },- { allowHttp: true },+ { allowHttp: true, maxBytes: MAX_MANIFEST_BYTES },);} finally {clearTimeout(timer);}if (!resp.ok) throw new Error(`HTTP ${resp.status}`);- const text = await resp.text();- if (text.length > MAX_MANIFEST_BYTES)- throw new Error(`Manifest exceeds ${MAX_MANIFEST_BYTES / 1024}KB limit`);- manifest = JSON.parse(text);+ manifest = JSON.parse(await resp.text());} catch (err) {if (err instanceof SsrfBlockedError)return error(res, 400, 'validation_error', `manifest_url rejected: ${err.message}`);+ if (err instanceof MaxBytesExceededError)+ return error(+ res,+ 422,+ 'fetch_failed',+ `Manifest exceeds the ${MAX_MANIFEST_BYTES / 1024}KB limit`,+ );return error(res, 422, 'fetch_failed', `Could not fetch manifest: ${err.message}`);}@@ -282,8 +324,12 @@ async function handlePublish(req, res) {const auth = await resolveAuth(req);if (!auth) return error(res, 401, 'unauthorized', 'authentication required');+ // Cookie sessions are attached by the browser on any cross-site POST, so the+ // only thing separating a real publish from one a hostile page triggered is+ // the double-submit token. Bearer callers are exempt inside requireCsrf.+ if (!(await requireCsrf(req, res, auth.userId))) return;- const rl = await limits.widgetRead(clientIp(req));+ const rl = await limits.pluginPublishUser(auth.userId);if (!rl.success) return rateLimited(res, rl);const raw = await readJson(req);@@ -308,7 +354,10 @@ async function handlePublish(req, res) {? manifest.meta.tags.slice(0, 20).map((t) => String(t).slice(0, 40)): [];- // Upsert on identifier + author so re-publishing updates the record+ // Upsert on identifier + author so re-publishing updates the record.+ // The RETURNING row carries author_id but no author_display_name, so the+ // author's name is joined back on afterwards: without it this endpoint answered+ // with a half-populated `author` object that list and detail both fill in.const [row] = await sql`INSERT INTO plugins (author_id, identifier, manifest_url, manifest_json, name, description, category, tags, is_public)VALUES (