Skip to content

Curated website design inspiration gallery, open zero-auth MCP server for AI agents, and shareable moodboard lists.

Harden API rate limits, Redis TTLs, catalog ID checks, and Vercel client IP extraction

Ethanbd643c1fc13d
diff --git a/api/favorites.js b/api/favorites.js
index 5a91aeb..929ac1d 100644
--- a/api/favorites.js
+++ b/api/favorites.js
@@ -1,10 +1,12 @@
import { createHmac } from "node:crypto";
+import sites from "../apps/gallery/sites.json" with { type: "json" };
const FAVORITES_ZSET = "designtaste:favorites";
const VOTERS_PREFIX = "designtaste:fav:voters:";
const RATE_WINDOW_MS = 10 * 60 * 1000;
const RATE_MAX_TOGGLES = 60;
const SITE_ID_RE = /^[a-z0-9][a-z0-9-]{0,80}$/;
+const KNOWN_SITE_IDS = new Set(sites.map((s) => s.id));
const memoryVoters = new Map();
const rateBuckets = new Map();
@@ -154,7 +156,7 @@ export async function recordFavoriteToggle(
const id = String(body.id || "").trim();
const action = String(body.action || "").trim();
- if (!SITE_ID_RE.test(id)) {
+ if (!SITE_ID_RE.test(id) || !KNOWN_SITE_IDS.has(id)) {
return {
status: 400,
body: { ok: false, message: "Invalid site identifier." },
@@ -261,10 +263,15 @@ export default async function handler(req, res) {
return res.status(403).json({ ok: false, message: "Origin not allowed." });
}
- const forwarded = String(req.headers?.["x-forwarded-for"] || "")
+ const realIp = String(
+ req.headers?.["x-real-ip"] ||
+ req.headers?.["x-vercel-forwarded-for"] ||
+ req.headers?.["x-forwarded-for"] ||
+ "",
+ )
.split(",")[0]
.trim();
- const clientKey = forwarded || req.socket?.remoteAddress || "anonymous";
+ const clientKey = realIp || req.socket?.remoteAddress || "anonymous";
try {
const body = await readJsonBody(req);
diff --git a/api/lists.js b/api/lists.js
index 8b722de..cafdae2 100644
--- a/api/lists.js
+++ b/api/lists.js
@@ -7,6 +7,9 @@ const LIST_ID_RE = /^dt_[a-z0-9]{6,16}$/;
const MAX_SITES_PER_LIST = 50;
const RATE_WINDOW_MS = 15 * 60 * 1000;
const RATE_MAX_CREATES = 15;
+const GLOBAL_WINDOW_MS = 60 * 60 * 1000;
+const GLOBAL_MAX_CREATES = 250;
+const LIST_TTL_SECONDS = 365 * 24 * 60 * 60;
const ALPHABET = "abcdefghjkmnpqrstuvwxyz23456789";
export const sitesById = new Map(sites.map((s) => [s.id, s]));
@@ -115,15 +118,22 @@ async function checkCreateRate(
if (redis) {
try {
const win = Math.floor(Date.now() / RATE_WINDOW_MS);
+ const gWin = Math.floor(Date.now() / GLOBAL_WINDOW_MS);
const key = `designtaste:lists:rate:${safeKey}:${win}`;
- const [count] = await redisPipeline(
+ const gKey = `designtaste:lists:global:${gWin}`;
+ const [count, , globalCount] = await redisPipeline(
[
["INCR", key],
["EXPIRE", key, String(Math.ceil(RATE_WINDOW_MS / 1000) + 60)],
+ ["INCR", gKey],
+ ["EXPIRE", gKey, String(Math.ceil(GLOBAL_WINDOW_MS / 1000) + 60)],
],
redis,
fetchImpl,
);
+ if (Number(globalCount) > GLOBAL_MAX_CREATES) {
+ return { allowed: false, retryAfter: 300 };
+ }
if (Number(count) > RATE_MAX_CREATES) {
return { allowed: false, retryAfter: 300 };
}
@@ -226,7 +236,15 @@ export async function createSavedList(
if (redis) {
try {
await redisPipeline(
- [["SET", LIST_KEY_PREFIX + id, JSON.stringify(record)]],
+ [
+ [
+ "SET",
+ LIST_KEY_PREFIX + id,
+ JSON.stringify(record),
+ "EX",
+ String(LIST_TTL_SECONDS),
+ ],
+ ],
redis,
fetchImpl,
);
@@ -372,10 +390,15 @@ export default async function handler(req, res) {
return res.status(403).json({ ok: false, message: "Origin not allowed." });
}
- const forwarded = String(req.headers?.["x-forwarded-for"] || "")
+ const realIp = String(
+ req.headers?.["x-real-ip"] ||
+ req.headers?.["x-vercel-forwarded-for"] ||
+ req.headers?.["x-forwarded-for"] ||
+ "",
+ )
.split(",")[0]
.trim();
- const clientKey = forwarded || req.socket?.remoteAddress || "anonymous";
+ const clientKey = realIp || req.socket?.remoteAddress || "anonymous";
try {
const body = await readJsonBody(req);
diff --git a/api/mcp.js b/api/mcp.js
index a0d066e..776d5a6 100644
--- a/api/mcp.js
+++ b/api/mcp.js
@@ -1074,10 +1074,15 @@ export default async function handler(req, res) {
return res.status(405).json(rpcError(null, -32600, "Method not allowed."));
}
- const forwarded = String(req.headers?.["x-forwarded-for"] || "")
+ const realIp = String(
+ req.headers?.["x-real-ip"] ||
+ req.headers?.["x-vercel-forwarded-for"] ||
+ req.headers?.["x-forwarded-for"] ||
+ "",
+ )
.split(",")[0]
.trim();
- const clientKey = forwarded || req.socket?.remoteAddress || "anonymous";
+ const clientKey = realIp || req.socket?.remoteAddress || "anonymous";
try {
const payload = await readJsonBody(req);
diff --git a/api/submit.js b/api/submit.js
index fd2cb64..7b918b8 100644
--- a/api/submit.js
+++ b/api/submit.js
@@ -408,10 +408,15 @@ export default async function handler(req, res) {
return res.status(403).json({ ok: false, message: "Origin not allowed." });
}
- const forwarded = String(req.headers?.["x-forwarded-for"] || "")
+ const realIp = String(
+ req.headers?.["x-real-ip"] ||
+ req.headers?.["x-vercel-forwarded-for"] ||
+ req.headers?.["x-forwarded-for"] ||
+ "",
+ )
.split(",")[0]
.trim();
- const clientKey = forwarded || req.socket?.remoteAddress || "anonymous";
+ const clientKey = realIp || req.socket?.remoteAddress || "anonymous";
try {
const body = await readJsonBody(req);