Send a toolbox through an encrypted link
Sharing a toolbox meant exporting a JSON file and passing it around by hand. A toolbox can now be sent from the site while staying local first: the browser compresses and encrypts the v2 export with AES-GCM, and the key lives only in the link fragment, so the server keeps for at most 24 hours a copy it cannot read. - Add the /api/shares service: upload, metadata, download and revoke, with files in SHARES_DIR, a purge every 10 minutes, and single-use links (the default) deleted only once fully downloaded. - Cap the server load: 50 MiB per share, total storage, free disk space, daily volume, concurrent and stalled uploads, reads per link, plus a wide per-connection cap kept in memory without storing IPs. - Limit each browser to 2 shares and 100 MiB per day and 2 active links, and show the existing link of a toolbox already shared. - Add the send dialog (card and game drawer) and the /receive page, which previews or directly imports through the existing import path. - Move shared HTTP helpers to services/http.js for both APIs. - Document the format, delivery, server layout and privacy wording. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
957bfdc86f
commit
bec9ea5b2f
43 changed files with 2349 additions and 59 deletions
|
|
@ -4,28 +4,15 @@ import { join, resolve } from "node:path";
|
|||
import { CardsDatabase } from "./database.js";
|
||||
import { loadCardsCatalog, publicCatalog } from "./catalog.js";
|
||||
import { CardsService, DEV_DISCORD_ID } from "./service.js";
|
||||
import { bool, createRateLimiter, json } from "../http.js";
|
||||
|
||||
export { createRateLimiter };
|
||||
|
||||
const JSON_LIMIT = 64 * 1024;
|
||||
const DISCORD_TIMEOUT_MS = 10_000;
|
||||
const MAINTENANCE_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
||||
const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
|
||||
function bool(value, fallback = false) {
|
||||
if (value == null || value === "") return fallback;
|
||||
return /^(1|true|yes|on)$/i.test(String(value));
|
||||
}
|
||||
|
||||
function json(res, status, body) {
|
||||
const payload = JSON.stringify(body);
|
||||
res.writeHead(status, {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
"Content-Length": Buffer.byteLength(payload),
|
||||
"Cache-Control": "no-store",
|
||||
"X-Content-Type-Options": "nosniff"
|
||||
});
|
||||
res.end(payload);
|
||||
}
|
||||
|
||||
function redirect(res, location, cookies = []) {
|
||||
const headers = { Location: location, "Cache-Control": "no-store" };
|
||||
if (cookies.length) headers["Set-Cookie"] = cookies;
|
||||
|
|
@ -106,25 +93,6 @@ function routeMatch(pathname, pattern) {
|
|||
}
|
||||
}
|
||||
|
||||
// Les compteurs ne valent que pour la minute en cours : ils sont tous oubliés à la suivante pour que la mémoire reste bornée.
|
||||
export function createRateLimiter(now) {
|
||||
const buckets = new Map();
|
||||
let currentMinute = null;
|
||||
const limiter = (req, scope, limit = 60) => {
|
||||
const key = `${String(req.headers["x-forwarded-for"] || req.socket?.remoteAddress || "unknown").split(",")[0]}:${scope}`;
|
||||
const minute = Math.floor(now() / 60_000);
|
||||
if (minute !== currentMinute) {
|
||||
buckets.clear();
|
||||
currentMinute = minute;
|
||||
}
|
||||
const count = (buckets.get(key) || 0) + 1;
|
||||
buckets.set(key, count);
|
||||
if (count > limit) throw Object.assign(new Error("Trop de tentatives. Réessayez dans quelques instants."), { status: 429, code: "RATE_LIMITED" });
|
||||
};
|
||||
limiter.bucketCount = () => buckets.size;
|
||||
return limiter;
|
||||
}
|
||||
|
||||
export function createCardsApiHandler({
|
||||
projectRoot = resolve("."),
|
||||
env = process.env,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue