authoravatarclover caruso<git@paperclover.net>2026-10-24 19:10:33-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-25 02:41:46-07:00
log57b1c450e19e70424b38b959cccd575e625b5bfe
treee4c12fcb95418878b7b82f3137b649065ed6a4f7
parent249d9e27c0bfc53fcf7685acb0006570d635a011
signature Signed by SSH key SHA256:xbd+BjjhyBfwk7GVoURf9Yx0gzDerHbvYv7SddNWmAs

feat: more q+a blocking


5 files changed, 107 insertions(+), 16 deletions(-)

src/q+a/backend.ts+76-14
......@@ -1,4 +1,5 @@
11const PROXYCHECK_API_KEY = process.env.PROXYCHECK_API_KEY;
2const console = log.scoped("q+a");
23
34export const app = new Hono();
45
......@@ -12,28 +13,64 @@ app.get("/q+a", async (c) => {
1213
1314// Submit form
1415app.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())];
1640 let text = form.get("text");
1741 if (typeof text !== "string") {
18 return questionFailure(c, 400, "Bad Request");
42 return respond("bad-text", await questionFailure(c, 400, "Bad Request"));
1943 }
2044 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 };
2153 const input = {
2254 date: new Date(),
2355 prompt: text,
2456 sourceName: "unknown",
2557 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"),
2762 };
63 trace.robot = input.robot;
64 trace.email = input.email;
2865
2966 input.date.setMilliseconds(0);
3067
3168 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));
3370 }
3471
3572 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));
3774 }
3875
3976 // Ban patterns
......@@ -42,7 +79,7 @@ app.post("/q+a", async (c) => {
4279 ) {
4380 // To prevent known automatic spam-bots from noticing something automatic is
4481 // happening, pretend that the question was successfully submitted.
45 return sendSuccess(c, new Date());
82 return respond("banned", await sendSuccess(c, new Date()));
4683 }
4784
4885 const ipAddr = c.req.header("x-forwarded-for");
......@@ -50,9 +87,23 @@ app.post("/q+a", async (c) => {
5087 input.sourceName = uniqueNamesGenerator({
5188 dictionaries: [adjectives, colors, animals],
5289 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",
5493 });
5594 }
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 }
56107
57108 if (ipAddr && PROXYCHECK_API_KEY) {
58109 const proxyCheck = await fetch(
......@@ -67,18 +118,26 @@ app.post("/q+a", async (c) => {
67118 ).then((res) => res.json());
68119
69120 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;
70125 if (proxyCheck[ipAddr].proxy === "yes") {
71126 input.sourceVPN = proxyCheck[ipAddr].operator?.name
72127 ?? proxyCheck[ipAddr].organisation
73128 ?? proxyCheck[ipAddr].provider ?? "unknown";
129 trace.sourceVPN = input.sourceVPN;
74130 }
75131 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 ),
82141 );
83142 }
84143 }
......@@ -89,7 +148,9 @@ app.post("/q+a", async (c) => {
89148 JSON.stringify(input),
90149 input.date,
91150 );
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));
93154});
94155async function sendSuccess(c: Context, date: Date) {
95156 if (c.req.header("Accept")?.includes("application/json")) {
......@@ -219,6 +280,7 @@ async function questionFailure(
219280
220281import { serveAsset } from "#sitegen/assets";
221282import * as view from "#sitegen/view";
283import * as log from "@clo/lib/log";
222284import { type Context, Hono } from "hono";
223285import type { ContentfulStatusCode } from "hono/utils/http-status";
224286import { adjectives, animals, colors, uniqueNamesGenerator } from "unique-names-generator";
src/q+a/models/questions.ts+6
......@@ -32,6 +32,12 @@ export type PendingData = {
3232 sourceName: string | "unknown";
3333 sourceLocation: string | "unknown";
3434 sourceVPN: string | null;
35 /** proxycheck risk score, 0-100. absent before 2026-10-24, and when proxycheck is not configured */
36 sourceRisk?: number | null;
37 /** value of the "i am a robot" honeypot checkbox, null when not sent. absent before 2026-10-24 */
38 robot?: string | null;
39 /** value of the hidden "email" honeypot input, null when not sent. absent before 2026-10-24 */
40 email?: string | null;
3541};
3642
3743function toQuestion(q: typeof dbQuestions.$inferSelect) {
src/q+a/questions.css+10-1
......@@ -178,6 +178,14 @@
178178 align-self: flex-start;
179179 }
180180 }
181 form .email {
182 display: none;
183 }
184 form .robot {
185 margin-top: 0.5rem;
186 align-self: flex-start;
187 cursor: pointer;
188 }
181189 textarea {
182190 width: 100%;
183191 max-width: calc(60ch);
......@@ -195,7 +203,8 @@
195203 }
196204 }
197205 &:not(:placeholder-shown) ~ .disabled-button,
198 &:placeholder-shown ~ button {
206 &:placeholder-shown ~ button,
207 &:placeholder-shown ~ .robot {
199208 display: none;
200209 }
201210 }
src/q+a/tags/question-form.marko+12
......@@ -7,6 +7,18 @@
77 maxlength="10000"
88 />
99 <div aria-hidden class="title">ask a question</div>
10 <label class="robot">
11 <input type="checkbox" name="robot" />
12 i am a robot
13 </label>
14 <input
15 class="email"
16 type="text"
17 name="email"
18 tabindex="-1"
19 autocomplete="off"
20 aria-hidden="true"
21 />
1022 <button type="submit">send</button>
1123 <div class="disabled-button">send</div>
1224</form>
src/q+a/views/backend-inbox.marko+3-1
......@@ -21,7 +21,9 @@ export const meta = { title: "question answer inbox" };
2121 <div style="color: dodgerblue; margin-bottom: 0.25rem">
2222 ${q.sourceName}${
2323 q.sourceLocation !== "unknown" ? `from ${q.sourceLocation}` : ""
24 }${q.sourceVPN ? `(VPN: ${q.sourceVPN})` : ""}
24 }${q.sourceVPN ? `(VPN: ${q.sourceVPN})` : ""}${
25 q.sourceRisk ? ` (risk: ${q.sourceRisk})` : ""
26 }
2527 </div>
2628 <p style="white-space: pre-wrap">${q.prompt}</p>
2729 <p>