diff --git a/src/q+a/backend.ts b/src/q+a/backend.ts index 1c7e6aa9ee2ae348c5f87040abb1c052355675e5..ce49a15ea8970206092ceb28a1fcdbc86b1d679a 100644 --- a/src/q+a/backend.ts +++ b/src/q+a/backend.ts @@ -1,4 +1,5 @@ const PROXYCHECK_API_KEY = process.env.PROXYCHECK_API_KEY; +const console = log.scoped("q+a"); export const app = new Hono(); @@ -12,28 +13,64 @@ app.get("/q+a", async (c) => { // Submit form app.post("/q+a", async (c) => { - const form = await c.req.formData(); + // Every submission is logged once, whatever happened to it, along with what + // is known about the request. The ip address is only ever written here, not + // stored with the question. `qmid` and `id` find the question it became. + const trace: Record = { + ip: c.req.header("x-forwarded-for"), + userAgent: c.req.header("user-agent"), + language: c.req.header("accept-language"), + referer: c.req.header("referer"), + origin: c.req.header("origin"), + accept: c.req.header("accept"), + contentType: c.req.header("content-type"), + }; + const respond = (outcome: string, response: T) => { + console.info(`submit ${outcome} ${JSON.stringify(trace)}`); + return response; + }; + + let form; + try { + form = await c.req.formData(); + } catch { + return respond("bad-form", await questionFailure(c, 400, "Bad Request")); + } + trace.fields = [...new Set(form.keys())]; let text = form.get("text"); if (typeof text !== "string") { - return questionFailure(c, 400, "Bad Request"); + return respond("bad-text", await questionFailure(c, 400, "Bad Request")); } text = text.trim(); + trace.textLength = text.length; + // The values of the honeypot fields are kept to see what bots send. `null` + // is a field that was not sent at all. They are cut short, since nothing + // limits what a bot posts here. + const honeypot = (name: string) => { + const value = form.get(name); + return typeof value === "string" ? value.slice(0, 200) : null; + }; const input = { date: new Date(), prompt: text, sourceName: "unknown", sourceLocation: "unknown", - sourceVPN: null, + sourceVPN: null as string | null, + sourceRisk: null as number | null, + robot: honeypot("robot"), + email: honeypot("email"), }; + trace.robot = input.robot; + trace.email = input.email; input.date.setMilliseconds(0); if (text.length <= 0) { - return questionFailure(c, 400, "Content is too short", text); + return respond("too-short", await questionFailure(c, 400, "Content is too short", text)); } if (text.length > 16000) { - return questionFailure(c, 400, "Content is too long", text); + return respond("too-long", await questionFailure(c, 400, "Content is too long", text)); } // Ban patterns @@ -42,7 +79,7 @@ app.post("/q+a", async (c) => { ) { // To prevent known automatic spam-bots from noticing something automatic is // happening, pretend that the question was successfully submitted. - return sendSuccess(c, new Date()); + return respond("banned", await sendSuccess(c, new Date())); } const ipAddr = c.req.header("x-forwarded-for"); @@ -50,9 +87,23 @@ app.post("/q+a", async (c) => { input.sourceName = uniqueNamesGenerator({ dictionaries: [adjectives, colors, animals], separator: "-", - seed: ipAddr + PROXYCHECK_API_KEY, + // names were once seeded with `ipAddr + PROXYCHECK_API_KEY`, which was + // never set in production. the suffix keeps those names unchanged. + seed: ipAddr + "undefined", }); } + trace.sourceName = input.sourceName; + + // Honeypots. The form shows an "i am a robot" checkbox which a person leaves + // alone, and an "email" input hidden by css which a person never sees. A bot + // that fills in every field trips them. Like the ban patterns, pretend it was + // submitted. The text is kept as a rejected question in case of a false hit. + if (form.has("robot") || form.get("email")) { + const date = questions.create("reject", JSON.stringify(input), input.date); + trace.qmid = date.getTime() / 60_000; + trace.id = formatQuestionId(date); + return respond("honeypot", await sendSuccess(c, date)); + } if (ipAddr && PROXYCHECK_API_KEY) { const proxyCheck = await fetch( @@ -67,18 +118,26 @@ app.post("/q+a", async (c) => { ).then((res) => res.json()); if (ipAddr && proxyCheck[ipAddr]) { + trace.proxy = proxyCheck[ipAddr].proxy; + trace.risk = proxyCheck[ipAddr].risk; + const risk = Number(proxyCheck[ipAddr].risk); + if (Number.isFinite(risk)) input.sourceRisk = risk; if (proxyCheck[ipAddr].proxy === "yes") { input.sourceVPN = proxyCheck[ipAddr].operator?.name ?? proxyCheck[ipAddr].organisation ?? proxyCheck[ipAddr].provider ?? "unknown"; + trace.sourceVPN = input.sourceVPN; } if (Number(proxyCheck[ipAddr].risk) > 78) { - return questionFailure( - c, - 403, - "This IP address has been flagged as a high risk IP address. If " - + "you are using a VPN/Proxy, please disable it and try again.", - text, + return respond( + "high-risk", + await questionFailure( + c, + 403, + "This IP address has been flagged as a high risk IP address. If " + + "you are using a VPN/Proxy, please disable it and try again.", + text, + ), ); } } @@ -89,7 +148,9 @@ app.post("/q+a", async (c) => { JSON.stringify(input), input.date, ); - await sendSuccess(c, date); + trace.qmid = date.getTime() / 60_000; + trace.id = formatQuestionId(date); + return respond("accepted", await sendSuccess(c, date)); }); async function sendSuccess(c: Context, date: Date) { if (c.req.header("Accept")?.includes("application/json")) { @@ -219,6 +280,7 @@ async function questionFailure( import { serveAsset } from "#sitegen/assets"; import * as view from "#sitegen/view"; +import * as log from "@clo/lib/log"; import { type Context, Hono } from "hono"; import type { ContentfulStatusCode } from "hono/utils/http-status"; import { adjectives, animals, colors, uniqueNamesGenerator } from "unique-names-generator"; diff --git a/src/q+a/models/questions.ts b/src/q+a/models/questions.ts index efb6bb9196ae087ac1f6f8c5a4732344d1ee090e..7b5d2e9703e18dc9e10223180c007b25108ab591 100644 --- a/src/q+a/models/questions.ts +++ b/src/q+a/models/questions.ts @@ -32,6 +32,12 @@ export type PendingData = { sourceName: string | "unknown"; sourceLocation: string | "unknown"; sourceVPN: string | null; + /** proxycheck risk score, 0-100. absent before 2026-10-24, and when proxycheck is not configured */ + sourceRisk?: number | null; + /** value of the "i am a robot" honeypot checkbox, null when not sent. absent before 2026-10-24 */ + robot?: string | null; + /** value of the hidden "email" honeypot input, null when not sent. absent before 2026-10-24 */ + email?: string | null; }; function toQuestion(q: typeof dbQuestions.$inferSelect) { diff --git a/src/q+a/questions.css b/src/q+a/questions.css index 16011ca20dead6477750fbb101c620845e580c79..c08b4c53389e447340c3edd79fbee5a6b134f8e9 100644 --- a/src/q+a/questions.css +++ b/src/q+a/questions.css @@ -178,6 +178,14 @@ align-self: flex-start; } } + form .email { + display: none; + } + form .robot { + margin-top: 0.5rem; + align-self: flex-start; + cursor: pointer; + } textarea { width: 100%; max-width: calc(60ch); @@ -195,7 +203,8 @@ } } &:not(:placeholder-shown) ~ .disabled-button, - &:placeholder-shown ~ button { + &:placeholder-shown ~ button, + &:placeholder-shown ~ .robot { display: none; } } diff --git a/src/q+a/tags/question-form.marko b/src/q+a/tags/question-form.marko index a455da0d24e2f86b213c5de36b336dc78d6ee7f7..93aec3aaebd26cd8021764d5076b2b1a079a1014 100644 --- a/src/q+a/tags/question-form.marko +++ b/src/q+a/tags/question-form.marko @@ -7,6 +7,18 @@ maxlength="10000" />
ask a question
+ +
send
diff --git a/src/q+a/views/backend-inbox.marko b/src/q+a/views/backend-inbox.marko index 6599d6369304de86dbe2f5934510e04d03160972..c96415a35319112978fb3d25277ce192418f4b1a 100644 --- a/src/q+a/views/backend-inbox.marko +++ b/src/q+a/views/backend-inbox.marko @@ -21,7 +21,9 @@ export const meta = { title: "question answer inbox" };
${q.sourceName}${ q.sourceLocation !== "unknown" ? `from ${q.sourceLocation}` : "" - }${q.sourceVPN ? `(VPN: ${q.sourceVPN})` : ""} + }${q.sourceVPN ? `(VPN: ${q.sourceVPN})` : ""}${ + q.sourceRisk ? ` (risk: ${q.sourceRisk})` : "" + }

${q.prompt}