diff --git a/gateway/src/index.ts b/gateway/src/index.ts index 4bb1cea..670afa5 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -1,5 +1,5 @@ import { createServer, type IncomingMessage, type ServerResponse } from "node:http"; -import { readFile, appendFile } from "node:fs/promises"; +import { readFile, appendFile, rename, stat } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { WebSocketServer, type WebSocket } from "ws"; @@ -75,9 +75,27 @@ const GITHUB_REPO = process.env.GITHUB_REPO ?? "Moepchi/webspeak3"; // image); mount a volume over it if you want submissions to survive a // container recreate. const FEEDBACK_LOG_FILE = process.env.FEEDBACK_LOG_FILE ?? path.resolve(process.cwd(), "feedback.log"); +// Simple size-based rotation: once the log crosses this size, the current +// file is moved to feedback.log.1 (overwriting any previous one) and a +// fresh file is started. Keeps disk usage bounded on a plain JSON-lines +// file with no other retention policy. +const FEEDBACK_LOG_MAX_BYTES = 5 * 1024 * 1024; const FEEDBACK_CATEGORIES = new Set(["bug", "idea", "other"]); const FEEDBACK_MESSAGE_MAX_LENGTH = 4000; +async function rotateFeedbackLogIfNeeded(): Promise { + try { + const { size } = await stat(FEEDBACK_LOG_FILE); + if (size < FEEDBACK_LOG_MAX_BYTES) return; + await rename(FEEDBACK_LOG_FILE, `${FEEDBACK_LOG_FILE}.1`); + } catch (err) { + // ENOENT just means there's no log yet - nothing to rotate. + if ((err as NodeJS.ErrnoException).code !== "ENOENT") { + console.error("[feedback] Failed to rotate feedback log:", err); + } + } +} + // Small in-memory rate limit: a handful of submissions per IP per hour is // far more than any real user needs, and keeps one abusive client from // spamming the log file or (worse) the GitHub issue tracker. In-memory is @@ -120,7 +138,9 @@ async function createGithubIssue(payload: { "Content-Type": "application/json", "User-Agent": "webspeak3-gateway", }, - body: JSON.stringify({ title, body, labels: ["feedback"] }), + // GitHub auto-creates labels that don't already exist in the repo, so + // the category label needs no manual setup on first use. + body: JSON.stringify({ title, body, labels: ["feedback", payload.category] }), }); if (!res.ok) { console.error(`[feedback] GitHub issue creation failed (${res.status}): ${await res.text().catch(() => "")}`); @@ -168,7 +188,7 @@ async function handleFeedback(req: IncomingMessage, res: ServerResponse) { } } - let body: { category?: unknown; message?: unknown; email?: unknown; publishAsIssue?: unknown }; + let body: { category?: unknown; message?: unknown; email?: unknown; publishAsIssue?: unknown; website?: unknown }; try { body = JSON.parse(raw || "{}"); } catch { @@ -177,6 +197,16 @@ async function handleFeedback(req: IncomingMessage, res: ServerResponse) { return; } + // Honeypot: a field named to look attractive to form-filling bots, kept + // hidden from real users via CSS. Any value here means a bot filled it in + // blindly - report success without actually logging or filing anything, + // so the bot has no signal to adapt on. + if (typeof body.website === "string" && body.website.trim() !== "") { + res.writeHead(200, { "Content-Type": "application/json" }); + res.end(JSON.stringify({ ok: true })); + return; + } + const category = typeof body.category === "string" && FEEDBACK_CATEGORIES.has(body.category) ? body.category : "other"; const message = typeof body.message === "string" ? body.message.trim().slice(0, FEEDBACK_MESSAGE_MAX_LENGTH) : ""; @@ -190,6 +220,7 @@ async function handleFeedback(req: IncomingMessage, res: ServerResponse) { } try { + await rotateFeedbackLogIfNeeded(); const entry = { at: new Date().toISOString(), category, message, email }; await appendFile(FEEDBACK_LOG_FILE, JSON.stringify(entry) + "\n", "utf-8"); } catch (err) { diff --git a/web/src/App.css b/web/src/App.css index 0fef4d3..f48e6b6 100644 --- a/web/src/App.css +++ b/web/src/App.css @@ -3457,6 +3457,15 @@ max-width: 90vw; } +.ts-feedback-honeypot { + position: absolute; + left: -5000px; + width: 1px; + height: 1px; + opacity: 0; + pointer-events: none; +} + .ts-update-toast { position: fixed; left: 50%; diff --git a/web/src/App.tsx b/web/src/App.tsx index dbf1c47..f6e87ce 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -1678,6 +1678,10 @@ function FeedbackDialog({ onClose }: { onClose: () => void }) { const [message, setMessage] = useState(""); const [email, setEmail] = useState(""); const [publishAsIssue, setPublishAsIssue] = useState(false); + // Honeypot: real users never see or fill this (hidden via CSS, no label, + // excluded from tab order); bots that blindly fill every field do. Kept + // in sync with the gateway's "website" field name. + const [website, setWebsite] = useState(""); const [status, setStatus] = useState<"idle" | "sending" | "sent" | "error">("idle"); const [issueUrl, setIssueUrl] = useState(undefined); @@ -1694,12 +1698,22 @@ function FeedbackDialog({ onClose }: { onClose: () => void }) { message: trimmed, email: email.trim() || undefined, publishAsIssue, + website, }), }); if (!res.ok) throw new Error(String(res.status)); const json = (await res.json()) as { ok?: boolean; githubIssueUrl?: string }; setIssueUrl(json.githubIssueUrl); setStatus("sent"); + // Someone who just sent feedback doesn't need the nudge bubble asking + // them to do exactly that again for a while - push its cooldown out + // from this moment rather than leaving it on whatever schedule it was + // already on. + try { + localStorage.setItem(FEEDBACK_BUBBLE_STORAGE_KEY, String(Date.now())); + } catch { + // ignore + } } catch { setStatus("error"); } @@ -1731,6 +1745,16 @@ function FeedbackDialog({ onClose }: { onClose: () => void }) { ) : ( <>

{t("feedback.dialog.intro")}

+ setWebsite(e.target.value)} + className="ts-feedback-honeypot" + tabIndex={-1} + autoComplete="off" + aria-hidden="true" + />