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 @@...@@ -1,4 +1,5 @@
1const PROXYCHECK_API_KEY = process.env.PROXYCHECK_API_KEY;1const PROXYCHECK_API_KEY = process.env.PROXYCHECK_API_KEY;
2const console = log.scoped("q+a");
23
3export const app = new Hono();4export const app = new Hono();
45
...@@ -12,28 +13,64 @@ app.get("/q+a", async (c) => {...@@ -12,28 +13,64 @@ app.get("/q+a", async (c) => {
1213
13// Submit form14// Submit form
14app.post("/q+a", async (c) => {15app.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 let text = form.get("text");40 let text = form.get("text");
17 if (typeof text !== "string") {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 text = text.trim();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 const input = {53 const input = {
22 date: new Date(),54 date: new Date(),
23 prompt: text,55 prompt: text,
24 sourceName: "unknown",56 sourceName: "unknown",
25 sourceLocation: "unknown",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;
2865
29 input.date.setMilliseconds(0);66 input.date.setMilliseconds(0);
3067
31 if (text.length <= 0) {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 }
3471
35 if (text.length > 16000) {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 }
3875
39 // Ban patterns76 // Ban patterns
...@@ -42,7 +79,7 @@ app.post("/q+a", async (c) => {...@@ -42,7 +79,7 @@ app.post("/q+a", async (c) => {
42 ) {79 ) {
43 // To prevent known automatic spam-bots from noticing something automatic is80 // To prevent known automatic spam-bots from noticing something automatic is
44 // happening, pretend that the question was successfully submitted.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 }
4784
48 const ipAddr = c.req.header("x-forwarded-for");85 const ipAddr = c.req.header("x-forwarded-for");
...@@ -50,9 +87,23 @@ app.post("/q+a", async (c) => {...@@ -50,9 +87,23 @@ app.post("/q+a", async (c) => {
50 input.sourceName = uniqueNamesGenerator({87 input.sourceName = uniqueNamesGenerator({
51 dictionaries: [adjectives, colors, animals],88 dictionaries: [adjectives, colors, animals],
52 separator: "-",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 }
56107
57 if (ipAddr && PROXYCHECK_API_KEY) {108 if (ipAddr && PROXYCHECK_API_KEY) {
58 const proxyCheck = await fetch(109 const proxyCheck = await fetch(
...@@ -67,18 +118,26 @@ app.post("/q+a", async (c) => {...@@ -67,18 +118,26 @@ app.post("/q+a", async (c) => {
67 ).then((res) => res.json());118 ).then((res) => res.json());
68119
69 if (ipAddr && proxyCheck[ipAddr]) {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 if (proxyCheck[ipAddr].proxy === "yes") {125 if (proxyCheck[ipAddr].proxy === "yes") {
71 input.sourceVPN = proxyCheck[ipAddr].operator?.name126 input.sourceVPN = proxyCheck[ipAddr].operator?.name
72 ?? proxyCheck[ipAddr].organisation127 ?? proxyCheck[ipAddr].organisation
73 ?? proxyCheck[ipAddr].provider ?? "unknown";128 ?? proxyCheck[ipAddr].provider ?? "unknown";
129 trace.sourceVPN = input.sourceVPN;
74 }130 }
75 if (Number(proxyCheck[ipAddr].risk) > 78) {131 if (Number(proxyCheck[ipAddr].risk) > 78) {
76 return questionFailure(132 return respond(
77 c,133 "high-risk",
78 403,134 await questionFailure(
79 "This IP address has been flagged as a high risk IP address. If "135 c,
80 + "you are using a VPN/Proxy, please disable it and try again.",136 403,
81 text,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,7 +148,9 @@ app.post("/q+a", async (c) => {
89 JSON.stringify(input),148 JSON.stringify(input),
90 input.date,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});
94async function sendSuccess(c: Context, date: Date) {155async function sendSuccess(c: Context, date: Date) {
95 if (c.req.header("Accept")?.includes("application/json")) {156 if (c.req.header("Accept")?.includes("application/json")) {
...@@ -219,6 +280,7 @@ async function questionFailure(...@@ -219,6 +280,7 @@ async function questionFailure(
219280
220import { serveAsset } from "#sitegen/assets";281import { serveAsset } from "#sitegen/assets";
221import * as view from "#sitegen/view";282import * as view from "#sitegen/view";
283import * as log from "@clo/lib/log";
222import { type Context, Hono } from "hono";284import { type Context, Hono } from "hono";
223import type { ContentfulStatusCode } from "hono/utils/http-status";285import type { ContentfulStatusCode } from "hono/utils/http-status";
224import { adjectives, animals, colors, uniqueNamesGenerator } from "unique-names-generator";286import { adjectives, animals, colors, uniqueNamesGenerator } from "unique-names-generator";
src/q+a/models/questions.ts+6
...@@ -32,6 +32,12 @@ export type PendingData = {...@@ -32,6 +32,12 @@ export type PendingData = {
32 sourceName: string | "unknown";32 sourceName: string | "unknown";
33 sourceLocation: string | "unknown";33 sourceLocation: string | "unknown";
34 sourceVPN: string | null;34 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;
35};41};
3642
37function toQuestion(q: typeof dbQuestions.$inferSelect) {43function toQuestion(q: typeof dbQuestions.$inferSelect) {
src/q+a/questions.css+10-1
...@@ -178,6 +178,14 @@...@@ -178,6 +178,14 @@
178 align-self: flex-start;178 align-self: flex-start;
179 }179 }
180 }180 }
181 form .email {
182 display: none;
183 }
184 form .robot {
185 margin-top: 0.5rem;
186 align-self: flex-start;
187 cursor: pointer;
188 }
181 textarea {189 textarea {
182 width: 100%;190 width: 100%;
183 max-width: calc(60ch);191 max-width: calc(60ch);
...@@ -195,7 +203,8 @@...@@ -195,7 +203,8 @@
195 }203 }
196 }204 }
197 &:not(:placeholder-shown) ~ .disabled-button,205 &:not(:placeholder-shown) ~ .disabled-button,
198 &:placeholder-shown ~ button {206 &:placeholder-shown ~ button,
207 &:placeholder-shown ~ .robot {
199 display: none;208 display: none;
200 }209 }
201 }210 }
src/q+a/tags/question-form.marko+12
...@@ -7,6 +7,18 @@...@@ -7,6 +7,18 @@
7 maxlength="10000"7 maxlength="10000"
8 />8 />
9 <div aria-hidden class="title">ask a question</div>9 <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 />
10 <button type="submit">send</button>22 <button type="submit">send</button>
11 <div class="disabled-button">send</div>23 <div class="disabled-button">send</div>
12</form>24</form>
src/q+a/views/backend-inbox.marko+3-1
...@@ -21,7 +21,9 @@ export const meta = { title: "question answer inbox" };...@@ -21,7 +21,9 @@ export const meta = { title: "question answer inbox" };
21 <div style="color: dodgerblue; margin-bottom: 0.25rem">21 <div style="color: dodgerblue; margin-bottom: 0.25rem">
22 ${q.sourceName}${22 ${q.sourceName}${
23 q.sourceLocation !== "unknown" ? `from ${q.sourceLocation}` : ""23 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 }
25 </div>27 </div>
26 <p style="white-space: pre-wrap">${q.prompt}</p>28 <p style="white-space: pre-wrap">${q.prompt}</p>
27 <p>29 <p>