// Arbitration Watch's only server code. Every request goes to the static files in dist/, // except POST /api/report: the "Report an error" form (/report/) and the "Request a // company" form (/report/request/, kind "request"). A report waits in // Cloudflare storage (KV) until the next run, when scripts/pull_reports.py files it as an // issue in the private data repo for the research agents and deletes it here. // // What it keeps: the entry, the kind of error, the text, the optional link and the date. // No name, email or IP address. Cloudflare's rate limiter counts requests per connection // for a minute, and the count isn't stored. No credentials: storage is a binding. // // It also emails the site's owner that a report is waiting, through Cloudflare Email // Routing (the NOTIFY binding), to the address in the NOTIFY_TO secret, which must be a // verified Email Routing destination. The email names only the entry and the kind of // error, never the visitor's text or link, and goes out at most once an hour. const KINDS = { address: "Wrong address or contact", deadline: "Wrong deadline", rating: "Wrong rating", quote: "Quote doesn't match the agreement", outdated: "The agreement has changed", other: "Something else", request: "Add a company", }; export default { async fetch(request, env, ctx) { const url = new URL(request.url); if (url.pathname === "/api/report") return report(request, env, url, ctx); return env.ASSETS.fetch(request); }, }; async function report(request, env, url, ctx) { const go = (path) => Response.redirect(new URL(path, url).toString(), 303); if (request.method !== "POST") { return new Response("Method not allowed", { status: 405, headers: { Allow: "POST" } }); } // Only the site's own form may post here. const origin = request.headers.get("Origin"); if (origin && origin !== url.origin) return new Response("Forbidden", { status: 403 }); if (!(request.headers.get("Content-Type") || "").startsWith("application/x-www-form-urlencoded")) { return new Response("Unsupported form", { status: 415 }); } if (Number(request.headers.get("Content-Length") || 0) > 8000) return go("/report/error/"); const form = await request.formData(); const field = (name) => String(form.get(name) || "").trim(); // A field people never see: anything in it is a bot. Pretend it worked. if (field("website")) return go("/report/sent/"); if (env.REPORT_LIMIT) { const { success } = await env.REPORT_LIMIT.limit({ key: request.headers.get("CF-Connecting-IP") || "unknown" }); if (!success) return go("/report/busy/"); } const entry = field("entry"); const kind = field("kind"); const details = field("details"); const link = field("link"); const company = field("company"); const isRequest = kind === "request"; if ((entry && !/^[a-z0-9-]{1,80}$/.test(entry)) || !(kind in KINDS) || details.length > 3000 || (!isRequest && details.length < 10) || (isRequest && (entry || company.length < 2 || company.length > 120)) || (!isRequest && company) || (link && (!/^https?:\/\/\S+$/.test(link) || link.length > 500))) { return go("/report/error/"); } if (!env.REPORTS) return go("/report/error/"); const received = new Date().toISOString(); // Unfiled reports expire after 180 days, in case runs stop. await env.REPORTS.put(`report/${received}/${crypto.randomUUID()}`, JSON.stringify({ received, entry, kind, company, details, link }), { expirationTtl: 180 * 86400 }); ctx.waitUntil(notify(env, entry, kind, received)); return go(isRequest ? "/report/request/sent/" : "/report/sent/"); } // At most one email an hour: later reports in that hour wait without one, and the next // run files them all. async function notify(env, entry, kind, received) { if (!env.NOTIFY || !env.NOTIFY_TO) return; if (await env.REPORTS.get("notify/last")) return; await env.REPORTS.put("notify/last", received, { expirationTtl: 3600 }); try { await env.NOTIFY.send({ to: env.NOTIFY_TO, from: { email: "reports@arbitrationwatch.org", name: "Arbitration Watch" }, subject: kind === "request" ? "New request to add a company" : `New error report: ${entry || "general"} (${KINDS[kind]})`, text: [ kind === "request" ? `A visitor asked for a company to be added.` : `A visitor sent a report with the "Report an error" form.`, ``, ...(kind === "request" ? [] : [`Entry: ${entry || "not about one entry"}`]), `Kind: ${KINDS[kind]}`, `Received: ${received}`, ``, `The visitor's text (and the company asked for) isn't in this email. To file waiting reports as issues in the`, `data repo, run: python3 site/scripts/pull_reports.py`, ``, `More reports in the next hour won't send another email.`, ].join("\n"), }); } catch (error) { console.log("notify failed", error && error.code, error && error.message); } }