Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix(site): share limits count an IPv6 household as one visitor, and the whole site has a daily review budget

wquguru952a170d8900
diff --git a/server/src/shares.js b/server/src/shares.js
index 46123b7..40199ba 100644
--- a/server/src/shares.js
+++ b/server/src/shares.js
@@ -63,6 +63,13 @@ export async function create(request, env) {
const characters = JSON.stringify(doc).length;
const wait = await limit(env, "review:" + who.device, REVIEW_CHARS_PER_DAY, 86400, characters);
if (wait) return fail(429, "rate_limited", { retryAfter: wait });
+ // The whole site's review budget a day (Jev is paid per token): past it, sharing waits rather
+ // than skipping the review. Many devices from many addresses can't run up the bill past this.
+ const budget = Number(env.REVIEW_CHARS_PER_DAY_ALL || 50_000_000);
+ if (await limit(env, "review-all", budget, 86400, characters)) {
+ console.error("review: daily budget used up", budget);
+ return fail(503, "review_unavailable", { retryAfter: 3600 });
+ }
let result;
try {
result = await review(env, doc);
diff --git a/server/src/util.js b/server/src/util.js
index 721b0a2..a7c78fc 100644
--- a/server/src/util.js
+++ b/server/src/util.js
@@ -44,9 +44,22 @@ export function now() {
return Math.floor(Date.now() / 1000);
}
-/** The visitor's address, hashed with a salt that changes every day: enough to rate-limit, not to track. */
+/**
+ * What a rate limit counts as one visitor: an IPv4 address, or an IPv6 /64. One home or phone
+ * usually gets a whole /64, so counting single IPv6 addresses would let anyone step past every
+ * per-address limit by picking another address from their own block.
+ */
+export function ipGroup(ip) {
+ if (!ip || !ip.includes(":")) return ip || "local";
+ const [head, tail = ""] = ip.toLowerCase().split("::");
+ const a = head ? head.split(":") : [], b = tail ? tail.split(":") : [];
+ const groups = ip.includes("::") ? [...a, ...Array(Math.max(0, 8 - a.length - b.length)).fill("0"), ...b] : a;
+ return groups.slice(0, 4).map((g) => g.replace(/^0+(?=.)/, "")).join(":") + "::/64";
+}
+
+/** The visitor's address (group), hashed with a salt that changes every day: enough to rate-limit, not to track. */
export async function ipKey(request, env) {
- const ip = request.headers.get("CF-Connecting-IP") || "local";
+ const ip = ipGroup(request.headers.get("CF-Connecting-IP"));
const day = new Date().toISOString().slice(0, 10);
return hex((await sha256(`${env.IP_SALT || "cicada"}|${day}|${ip}`)).slice(0, 12));
}