| ... | ... | @@ -1,4 +1,5 @@ |
| 1 | 1 | const PROXYCHECK_API_KEY = process.env.PROXYCHECK_API_KEY; |
| 2 | const console = log.scoped("q+a"); |
| 2 | 3 | |
| 3 | 4 | export const app = new Hono(); |
| 4 | 5 | |
| ... | ... | @@ -12,28 +13,64 @@ app.get("/q+a", async (c) => { |
| 12 | 13 | |
| 13 | 14 | // Submit form |
| 14 | 15 | app.post("/q+a", async (c) => { |
| 15 | | const form = await c.req.formData(); |
| 16 | // Every submission is logged once, whatever happened to it, along with what |
| 17 | // is known about the request. The ip address is only ever written here, not |
| 18 | // stored with the question. `qmid` and `id` find the question it became. |
| 19 | const trace: Record<string, unknown> = { |
| 20 | ip: c.req.header("x-forwarded-for"), |
| 21 | userAgent: c.req.header("user-agent"), |
| 22 | language: c.req.header("accept-language"), |
| 23 | referer: c.req.header("referer"), |
| 24 | origin: c.req.header("origin"), |
| 25 | accept: c.req.header("accept"), |
| 26 | contentType: c.req.header("content-type"), |
| 27 | }; |
| 28 | const respond = <T>(outcome: string, response: T) => { |
| 29 | console.info(`submit ${outcome} ${JSON.stringify(trace)}`); |
| 30 | return response; |
| 31 | }; |
| 32 | |
| 33 | let form; |
| 34 | try { |
| 35 | form = await c.req.formData(); |
| 36 | } catch { |
| 37 | return respond("bad-form", await questionFailure(c, 400, "Bad Request")); |
| 38 | } |
| 39 | trace.fields = [...new Set(form.keys())]; |
| 16 | 40 | let text = form.get("text"); |
| 17 | 41 | if (typeof text !== "string") { |
| 18 | | return questionFailure(c, 400, "Bad Request"); |
| 42 | return respond("bad-text", await questionFailure(c, 400, "Bad Request")); |
| 19 | 43 | } |
| 20 | 44 | text = text.trim(); |
| 45 | trace.textLength = text.length; |
| 46 | // The values of the honeypot fields are kept to see what bots send. `null` |
| 47 | // is a field that was not sent at all. They are cut short, since nothing |
| 48 | // limits what a bot posts here. |
| 49 | const honeypot = (name: string) => { |
| 50 | const value = form.get(name); |
| 51 | return typeof value === "string" ? value.slice(0, 200) : null; |
| 52 | }; |
| 21 | 53 | const input = { |
| 22 | 54 | date: new Date(), |
| 23 | 55 | prompt: text, |
| 24 | 56 | sourceName: "unknown", |
| 25 | 57 | sourceLocation: "unknown", |
| 26 | | sourceVPN: null, |
| 58 | sourceVPN: null as string | null, |
| 59 | sourceRisk: null as number | null, |
| 60 | robot: honeypot("robot"), |
| 61 | email: honeypot("email"), |
| 27 | 62 | }; |
| 63 | trace.robot = input.robot; |
| 64 | trace.email = input.email; |
| 28 | 65 | |
| 29 | 66 | input.date.setMilliseconds(0); |
| 30 | 67 | |
| 31 | 68 | if (text.length <= 0) { |
| 32 | | return questionFailure(c, 400, "Content is too short", text); |
| 69 | return respond("too-short", await questionFailure(c, 400, "Content is too short", text)); |
| 33 | 70 | } |
| 34 | 71 | |
| 35 | 72 | if (text.length > 16000) { |
| 36 | | return questionFailure(c, 400, "Content is too long", text); |
| 73 | return respond("too-long", await questionFailure(c, 400, "Content is too long", text)); |
| 37 | 74 | } |
| 38 | 75 | |
| 39 | 76 | // Ban patterns |
| ... | ... | @@ -42,7 +79,7 @@ app.post("/q+a", async (c) => { |
| 42 | 79 | ) { |
| 43 | 80 | // To prevent known automatic spam-bots from noticing something automatic is |
| 44 | 81 | // happening, pretend that the question was successfully submitted. |
| 45 | | return sendSuccess(c, new Date()); |
| 82 | return respond("banned", await sendSuccess(c, new Date())); |
| 46 | 83 | } |
| 47 | 84 | |
| 48 | 85 | const ipAddr = c.req.header("x-forwarded-for"); |
| ... | ... | @@ -50,9 +87,23 @@ app.post("/q+a", async (c) => { |
| 50 | 87 | input.sourceName = uniqueNamesGenerator({ |
| 51 | 88 | dictionaries: [adjectives, colors, animals], |
| 52 | 89 | separator: "-", |
| 53 | | seed: ipAddr + PROXYCHECK_API_KEY, |
| 90 | // names were once seeded with `ipAddr + PROXYCHECK_API_KEY`, which was |
| 91 | // never set in production. the suffix keeps those names unchanged. |
| 92 | seed: ipAddr + "undefined", |
| 54 | 93 | }); |
| 55 | 94 | } |
| 95 | trace.sourceName = input.sourceName; |
| 96 | |
| 97 | // Honeypots. The form shows an "i am a robot" checkbox which a person leaves |
| 98 | // alone, and an "email" input hidden by css which a person never sees. A bot |
| 99 | // that fills in every field trips them. Like the ban patterns, pretend it was |
| 100 | // submitted. The text is kept as a rejected question in case of a false hit. |
| 101 | if (form.has("robot") || form.get("email")) { |
| 102 | const date = questions.create("reject", JSON.stringify(input), input.date); |
| 103 | trace.qmid = date.getTime() / 60_000; |
| 104 | trace.id = formatQuestionId(date); |
| 105 | return respond("honeypot", await sendSuccess(c, date)); |
| 106 | } |
| 56 | 107 | |
| 57 | 108 | if (ipAddr && PROXYCHECK_API_KEY) { |
| 58 | 109 | const proxyCheck = await fetch( |
| ... | ... | @@ -67,18 +118,26 @@ app.post("/q+a", async (c) => { |
| 67 | 118 | ).then((res) => res.json()); |
| 68 | 119 | |
| 69 | 120 | if (ipAddr && proxyCheck[ipAddr]) { |
| 121 | trace.proxy = proxyCheck[ipAddr].proxy; |
| 122 | trace.risk = proxyCheck[ipAddr].risk; |
| 123 | const risk = Number(proxyCheck[ipAddr].risk); |
| 124 | if (Number.isFinite(risk)) input.sourceRisk = risk; |
| 70 | 125 | if (proxyCheck[ipAddr].proxy === "yes") { |
| 71 | 126 | input.sourceVPN = proxyCheck[ipAddr].operator?.name |
| 72 | 127 | ?? proxyCheck[ipAddr].organisation |
| 73 | 128 | ?? proxyCheck[ipAddr].provider ?? "unknown"; |
| 129 | trace.sourceVPN = input.sourceVPN; |
| 74 | 130 | } |
| 75 | 131 | if (Number(proxyCheck[ipAddr].risk) > 78) { |
| 76 | | return questionFailure( |
| 77 | | c, |
| 78 | | 403, |
| 79 | | "This IP address has been flagged as a high risk IP address. If " |
| 80 | | + "you are using a VPN/Proxy, please disable it and try again.", |
| 81 | | text, |
| 132 | return respond( |
| 133 | "high-risk", |
| 134 | await questionFailure( |
| 135 | c, |
| 136 | 403, |
| 137 | "This IP address has been flagged as a high risk IP address. If " |
| 138 | + "you are using a VPN/Proxy, please disable it and try again.", |
| 139 | text, |
| 140 | ), |
| 82 | 141 | ); |
| 83 | 142 | } |
| 84 | 143 | } |
| ... | ... | @@ -89,7 +148,9 @@ app.post("/q+a", async (c) => { |
| 89 | 148 | JSON.stringify(input), |
| 90 | 149 | input.date, |
| 91 | 150 | ); |
| 92 | | await sendSuccess(c, date); |
| 151 | trace.qmid = date.getTime() / 60_000; |
| 152 | trace.id = formatQuestionId(date); |
| 153 | return respond("accepted", await sendSuccess(c, date)); |
| 93 | 154 | }); |
| 94 | 155 | async function sendSuccess(c: Context, date: Date) { |
| 95 | 156 | if (c.req.header("Accept")?.includes("application/json")) { |
| ... | ... | @@ -219,6 +280,7 @@ async function questionFailure( |
| 219 | 280 | |
| 220 | 281 | import { serveAsset } from "#sitegen/assets"; |
| 221 | 282 | import * as view from "#sitegen/view"; |
| 283 | import * as log from "@clo/lib/log"; |
| 222 | 284 | import { type Context, Hono } from "hono"; |
| 223 | 285 | import type { ContentfulStatusCode } from "hono/utils/http-status"; |
| 224 | 286 | import { adjectives, animals, colors, uniqueNamesGenerator } from "unique-names-generator"; |