JOHN SILLVESTOR commited on
Commit
d0225fe
·
verified ·
1 Parent(s): 5211983

Limit anonymous ticket guesses and accept POST only

Browse files
Files changed (1) hide show
  1. server.js +60 -15
server.js CHANGED
@@ -511,6 +511,28 @@ function limited(ip) {
511
  return n > 360;
512
  }
513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514
  function isBlogOrigin(o) {
515
  o = String(o || "").trim();
516
  if (!o || o.toLowerCase() === "null") return false;
@@ -2055,13 +2077,23 @@ async function readBody(req, max) {
2055
  return Buffer.concat(chunks);
2056
  }
2057
 
2058
- async function handleGenerate(req, res, url) {
 
 
 
 
2059
  if (!aesKey) return json(res, 503, { error: "misconfigured" });
2060
  if (!destOk(req, ["empty"])) return json(res, 403, { error: "forbidden" });
2061
  if (denyHot(req, res)) return;
2062
  const ip = clientIp(req);
2063
- let path = url.searchParams.get("path") || url.searchParams.get("p") || "";
2064
- let hash = (url.searchParams.get("hash") || url.searchParams.get("h") || "").toLowerCase();
 
 
 
 
 
 
2065
  if (req.method === "POST") {
2066
  try {
2067
  const raw = JSON.parse((await readBody(req, 4096)).toString("utf8") || "{}");
@@ -2092,13 +2124,29 @@ async function handleGenerate(req, res, url) {
2092
  if (!path && raw && raw.p) path = String(raw.p);
2093
  if (!hash && raw && raw.h) hash = String(raw.h).toLowerCase();
2094
  } catch {
 
 
 
 
2095
  return json(res, 400, { error: "bad_request" });
2096
  }
2097
  }
2098
  const m = /\/stream\/(\d+)/.exec(path);
2099
- if (!m || !/^[0-9a-f]{6}$/.test(hash)) return json(res, 400, { error: "bad_request" });
 
 
 
 
 
 
2100
  const msgId = Number(m[1]);
2101
- if (!Number.isInteger(msgId) || msgId < 1 || msgId > 1e15) return json(res, 400, { error: "bad_request" });
 
 
 
 
 
 
2102
  const rawAuth = String(req.headers.authorization || "").trim();
2103
  if (!rawAuth) {
2104
  const gk = path + "|" + hash + "|a|" + ipTag(ip) + "|" + uaTag(req.headers["user-agent"]);
@@ -2112,10 +2160,6 @@ async function handleGenerate(req, res, url) {
2112
  return json(res, 200, body);
2113
  }
2114
  }
2115
- if (limited(ip)) {
2116
- res.setHeader("Retry-After", "2");
2117
- return json(res, 429, { error: "rate_limited" });
2118
- }
2119
  const authJob = (async () => {
2120
  if (!rawAuth) return { uid: null, lvl: "anonymous" };
2121
  try {
@@ -2139,7 +2183,13 @@ async function handleGenerate(req, res, url) {
2139
  const lvl = auth.lvl;
2140
  const bot = got.bot;
2141
  const rec = got.rec;
2142
- if (rec.hash && rec.hash !== hash) return json(res, 400, { error: "hash" });
 
 
 
 
 
 
2143
  warmHot(bot, rec, msgId);
2144
  const hid = String(msgId);
2145
  if (durCache.get(hid) > 1) rec.duration = durCache.get(hid);
@@ -2258,11 +2308,6 @@ async function handleStream(req, res, url, msgId, kHex, ticketObj) {
2258
  }
2259
  let held = false;
2260
  try {
2261
- if (limited(ip)) {
2262
- res.setHeader("Retry-After", "2");
2263
- res.writeHead(429);
2264
- return res.end();
2265
- }
2266
  if (!takeStream(ip, ticket.u, msgId)) {
2267
  res.setHeader("Retry-After", "1");
2268
  res.writeHead(429);
 
511
  return n > 360;
512
  }
513
 
514
+ function slotOver(key, max) {
515
+ const slot = Math.floor(Date.now() / 60000);
516
+ const k = key + ":" + slot;
517
+ const n = (rateMap.get(k) || 0) + 1;
518
+ rateMap.set(k, n);
519
+ return n > max;
520
+ }
521
+
522
+ function mintLimited(ip) {
523
+ const s = String(ip || "0");
524
+ return slotOver("mint:" + s, 90) || slotOver("mintn:" + ipNet(s), 600);
525
+ }
526
+
527
+ function mintBlocked(ip) {
528
+ const slot = Math.floor(Date.now() / 60000);
529
+ return (rateMap.get("badm:" + String(ip || "0") + ":" + slot) || 0) > 20;
530
+ }
531
+
532
+ function mintFailed(ip) {
533
+ return slotOver("badm:" + String(ip || "0"), 20);
534
+ }
535
+
536
  function isBlogOrigin(o) {
537
  o = String(o || "").trim();
538
  if (!o || o.toLowerCase() === "null") return false;
 
2077
  return Buffer.concat(chunks);
2078
  }
2079
 
2080
+ async function handleGenerate(req, res) {
2081
+ if (req.method !== "POST") {
2082
+ res.setHeader("Allow", "POST");
2083
+ return json(res, 405, { error: "method" });
2084
+ }
2085
  if (!aesKey) return json(res, 503, { error: "misconfigured" });
2086
  if (!destOk(req, ["empty"])) return json(res, 403, { error: "forbidden" });
2087
  if (denyHot(req, res)) return;
2088
  const ip = clientIp(req);
2089
+ if (mintBlocked(ip) || mintLimited(ip)) {
2090
+ res.setHeader("Retry-After", "3");
2091
+ return json(res, 429, { error: "rate_limited" });
2092
+ }
2093
+ const ctype = String(req.headers["content-type"] || "");
2094
+ if (!/^application\/json(?:\s*;|$)/i.test(ctype)) return json(res, 415, { error: "type" });
2095
+ let path = "";
2096
+ let hash = "";
2097
  if (req.method === "POST") {
2098
  try {
2099
  const raw = JSON.parse((await readBody(req, 4096)).toString("utf8") || "{}");
 
2124
  if (!path && raw && raw.p) path = String(raw.p);
2125
  if (!hash && raw && raw.h) hash = String(raw.h).toLowerCase();
2126
  } catch {
2127
+ if (mintFailed(ip)) {
2128
+ res.setHeader("Retry-After", "3");
2129
+ return json(res, 429, { error: "rate_limited" });
2130
+ }
2131
  return json(res, 400, { error: "bad_request" });
2132
  }
2133
  }
2134
  const m = /\/stream\/(\d+)/.exec(path);
2135
+ if (!m || !/^[0-9a-f]{6}$/.test(hash)) {
2136
+ if (mintFailed(ip)) {
2137
+ res.setHeader("Retry-After", "3");
2138
+ return json(res, 429, { error: "rate_limited" });
2139
+ }
2140
+ return json(res, 400, { error: "bad_request" });
2141
+ }
2142
  const msgId = Number(m[1]);
2143
+ if (!Number.isInteger(msgId) || msgId < 1 || msgId > 1e15) {
2144
+ if (mintFailed(ip)) {
2145
+ res.setHeader("Retry-After", "3");
2146
+ return json(res, 429, { error: "rate_limited" });
2147
+ }
2148
+ return json(res, 400, { error: "bad_request" });
2149
+ }
2150
  const rawAuth = String(req.headers.authorization || "").trim();
2151
  if (!rawAuth) {
2152
  const gk = path + "|" + hash + "|a|" + ipTag(ip) + "|" + uaTag(req.headers["user-agent"]);
 
2160
  return json(res, 200, body);
2161
  }
2162
  }
 
 
 
 
2163
  const authJob = (async () => {
2164
  if (!rawAuth) return { uid: null, lvl: "anonymous" };
2165
  try {
 
2183
  const lvl = auth.lvl;
2184
  const bot = got.bot;
2185
  const rec = got.rec;
2186
+ if (rec.hash && rec.hash !== hash) {
2187
+ if (mintFailed(ip)) {
2188
+ res.setHeader("Retry-After", "3");
2189
+ return json(res, 429, { error: "rate_limited" });
2190
+ }
2191
+ return json(res, 400, { error: "hash" });
2192
+ }
2193
  warmHot(bot, rec, msgId);
2194
  const hid = String(msgId);
2195
  if (durCache.get(hid) > 1) rec.duration = durCache.get(hid);
 
2308
  }
2309
  let held = false;
2310
  try {
 
 
 
 
 
2311
  if (!takeStream(ip, ticket.u, msgId)) {
2312
  res.setHeader("Retry-After", "1");
2313
  res.writeHead(429);