mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 01:42:50 +08:00
Harden and polish the feedback endpoint
- Honeypot field ("website", hidden via CSS + tabIndex -1): any
non-empty value silently short-circuits to a fake success response
without logging or filing anything, at near-zero cost to real users.
- Tag created GitHub issues with the feedback category (bug/idea/
other) alongside the existing "feedback" label, for easier triage -
GitHub auto-creates labels that don't exist yet.
- Rotate feedback.log once it crosses 5MB (move to feedback.log.1) so
a busy instance doesn't grow the file unbounded.
- Push the nudge bubble's cooldown out from the moment someone
actually submits feedback, so it doesn't ask again shortly after
they just did.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5b5f4caf79
commit
7776a71e84
3 files changed
+67
-3
No files matched your search
+34
-3
@@ -1,5 +1,5 @@
|
|||||||
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
|
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 path from "node:path";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
import { WebSocketServer, type WebSocket } from "ws";
|
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
|
// image); mount a volume over it if you want submissions to survive a
|
||||||
// container recreate.
|
// container recreate.
|
||||||
const FEEDBACK_LOG_FILE = process.env.FEEDBACK_LOG_FILE ?? path.resolve(process.cwd(), "feedback.log");
|
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_CATEGORIES = new Set(["bug", "idea", "other"]);
|
||||||
const FEEDBACK_MESSAGE_MAX_LENGTH = 4000;
|
const FEEDBACK_MESSAGE_MAX_LENGTH = 4000;
|
||||||
|
|
||||||
|
async function rotateFeedbackLogIfNeeded(): Promise<void> {
|
||||||
|
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
|
// 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
|
// 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
|
// 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",
|
"Content-Type": "application/json",
|
||||||
"User-Agent": "webspeak3-gateway",
|
"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) {
|
if (!res.ok) {
|
||||||
console.error(`[feedback] GitHub issue creation failed (${res.status}): ${await res.text().catch(() => "")}`);
|
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 {
|
try {
|
||||||
body = JSON.parse(raw || "{}");
|
body = JSON.parse(raw || "{}");
|
||||||
} catch {
|
} catch {
|
||||||
@@ -177,6 +197,16 @@ async function handleFeedback(req: IncomingMessage, res: ServerResponse) {
|
|||||||
return;
|
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 category = typeof body.category === "string" && FEEDBACK_CATEGORIES.has(body.category) ? body.category : "other";
|
||||||
const message =
|
const message =
|
||||||
typeof body.message === "string" ? body.message.trim().slice(0, FEEDBACK_MESSAGE_MAX_LENGTH) : "";
|
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 {
|
try {
|
||||||
|
await rotateFeedbackLogIfNeeded();
|
||||||
const entry = { at: new Date().toISOString(), category, message, email };
|
const entry = { at: new Date().toISOString(), category, message, email };
|
||||||
await appendFile(FEEDBACK_LOG_FILE, JSON.stringify(entry) + "\n", "utf-8");
|
await appendFile(FEEDBACK_LOG_FILE, JSON.stringify(entry) + "\n", "utf-8");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -3457,6 +3457,15 @@
|
|||||||
max-width: 90vw;
|
max-width: 90vw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.ts-feedback-honeypot {
|
||||||
|
position: absolute;
|
||||||
|
left: -5000px;
|
||||||
|
width: 1px;
|
||||||
|
height: 1px;
|
||||||
|
opacity: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
.ts-update-toast {
|
.ts-update-toast {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
left: 50%;
|
left: 50%;
|
||||||
|
|||||||
@@ -1678,6 +1678,10 @@ function FeedbackDialog({ onClose }: { onClose: () => void }) {
|
|||||||
const [message, setMessage] = useState("");
|
const [message, setMessage] = useState("");
|
||||||
const [email, setEmail] = useState("");
|
const [email, setEmail] = useState("");
|
||||||
const [publishAsIssue, setPublishAsIssue] = useState(false);
|
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 [status, setStatus] = useState<"idle" | "sending" | "sent" | "error">("idle");
|
||||||
const [issueUrl, setIssueUrl] = useState<string | undefined>(undefined);
|
const [issueUrl, setIssueUrl] = useState<string | undefined>(undefined);
|
||||||
|
|
||||||
@@ -1694,12 +1698,22 @@ function FeedbackDialog({ onClose }: { onClose: () => void }) {
|
|||||||
message: trimmed,
|
message: trimmed,
|
||||||
email: email.trim() || undefined,
|
email: email.trim() || undefined,
|
||||||
publishAsIssue,
|
publishAsIssue,
|
||||||
|
website,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
if (!res.ok) throw new Error(String(res.status));
|
if (!res.ok) throw new Error(String(res.status));
|
||||||
const json = (await res.json()) as { ok?: boolean; githubIssueUrl?: string };
|
const json = (await res.json()) as { ok?: boolean; githubIssueUrl?: string };
|
||||||
setIssueUrl(json.githubIssueUrl);
|
setIssueUrl(json.githubIssueUrl);
|
||||||
setStatus("sent");
|
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 {
|
} catch {
|
||||||
setStatus("error");
|
setStatus("error");
|
||||||
}
|
}
|
||||||
@@ -1731,6 +1745,16 @@ function FeedbackDialog({ onClose }: { onClose: () => void }) {
|
|||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<p className="ts-feedback-intro">{t("feedback.dialog.intro")}</p>
|
<p className="ts-feedback-intro">{t("feedback.dialog.intro")}</p>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
name="website"
|
||||||
|
value={website}
|
||||||
|
onChange={(e) => setWebsite(e.target.value)}
|
||||||
|
className="ts-feedback-honeypot"
|
||||||
|
tabIndex={-1}
|
||||||
|
autoComplete="off"
|
||||||
|
aria-hidden="true"
|
||||||
|
/>
|
||||||
<label className="ts-dialog-field">
|
<label className="ts-dialog-field">
|
||||||
{t("feedback.dialog.category")}
|
{t("feedback.dialog.category")}
|
||||||
<select value={category} onChange={(e) => setCategory(e.target.value as FeedbackCategory)}>
|
<select value={category} onChange={(e) => setCategory(e.target.value as FeedbackCategory)}>
|
||||||
|
|||||||
Reference in new issue
Block a user