Fix the Panel account bug and harden what the second audit found

- Panel: changing a role or disabling an account always failed
  (db.transaction doesn't exist on node:sqlite); it uses tx() now
- Feeds and link previews are scanned with indexOf, not whole-document
  regexes, so a hostile feed or page can't freeze the server; entity
  decoding can't throw; escaped-HTML descriptions make clean excerpts
- Feed reads share one fetch per feed and serve the stale copy while
  a refresh runs
- Deliberate HTTP errors (the CSRF 403) keep their status instead of
  becoming logged 500s; a JSON null body is handled
- url columns read the scheme the way the URL parser does, so
  control characters can't hide javascript:
- Google sign-in state cookie is __Host- and its return page is
  checked again; more reserved ranges blocked for previews and feeds,
  and a 15 s cap on each whole fetch
- Log lines from visitor text are made single-line and short; list
  limits can't go negative; entity names can't hit object prototypes
- A clip title can't point the clip tool at another file
- Uploads over 60 MP are refused before decoding; the Keep original
  help says a photo's location stays in it
- admin-cli add defaults to the editor role

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-29 02:34:26 -05:00
parent fd40907ed6
commit be7e92417e
18 changed files with 226 additions and 59 deletions

View file

@ -36,7 +36,8 @@ entities.use("*", requireAuth);
const NO_STORE = { "Cache-Control": "no-store" };
function entityOr404(c) {
const entity = ENTITIES[c.req.param("entity")];
const name = c.req.param("entity");
const entity = Object.hasOwn(ENTITIES, name) ? ENTITIES[name] : null;
if (!entity) throw new HttpError(404, "Unknown entity.");
return entity;
}

View file

@ -53,7 +53,7 @@ admin.get("/feedback", (c) => {
params.push(Number(before));
}
const take = Math.min(Number(limit) || 50, MAX_LIMIT);
const take = Math.max(1, Math.min(Number(limit) || 50, MAX_LIMIT));
const rows = db
.prepare(
@ -99,7 +99,8 @@ admin.patch("/feedback/:id", requireRole("editor"), async (c) => {
let body;
try {
body = await c.req.json();
// `?? {}`: a body of JSON null parses fine and has no fields.
body = (await c.req.json()) ?? {};
} catch {
return c.json({ error: "Expected a JSON body." }, 400);
}

View file

@ -33,6 +33,7 @@ import { deleteCookie, getCookie, setCookie } from "hono/cookie";
import {
SECURE_COOKIE,
logSafe,
createSession,
destroySession,
currentUser,
@ -58,7 +59,8 @@ const NO_STORE = { "Cache-Control": "no-store" };
auth.post("/login", async (c) => {
let body;
try {
body = await c.req.json();
// `?? {}`: a body of JSON null parses fine and has no fields.
body = (await c.req.json()) ?? {};
} catch {
return c.json({ error: "Expected a JSON body." }, 400);
}
@ -72,7 +74,7 @@ auth.post("/login", async (c) => {
const wait = loginFailures.blockedFor(email);
if (wait) {
console.warn(`login locked for ${email}`);
console.warn(`login locked for ${logSafe(email)}`);
return c.json(
{ error: "Too many wrong passwords for this account. Try again in a few minutes." },
429,
@ -97,7 +99,7 @@ auth.post("/login", async (c) => {
if (!ok) {
loginFailures.fail(email);
console.warn(`login failed for ${email}`);
console.warn(`login failed for ${logSafe(email)}`);
return c.json({ error: "That email and password don't match." }, 401);
}
@ -134,12 +136,14 @@ auth.get("/me", (c) => {
// The state and PKCE verifier between the two legs. Scoped to the
// Google routes and gone in ten minutes; Lax so it comes back on
// the redirect from Google, which is a top-level navigation.
const OAUTH_COOKIE = "ngu_oauth";
// __Host- (Secure, path /, no Domain) when over https, like the
// session cookie, so a sibling subdomain can't plant its own state.
const OAUTH_COOKIE = SECURE_COOKIE ? "__Host-ngu_oauth" : "ngu_oauth";
const OAUTH_COOKIE_OPTIONS = {
httpOnly: true,
secure: SECURE_COOKIE,
sameSite: "Lax",
path: "/api/auth/google",
path: "/",
};
// Where to land afterwards: an admin page, never anywhere else.
@ -232,7 +236,8 @@ auth.get("/google/callback", async (c) => {
).run(identity.name, user.id);
console.log(`login ${user.email} (google)`);
return c.redirect(saved.next ?? "/admin/home", 302);
// Checked again: the cookie came back from the browser.
return c.redirect(safeNext(saved.next), 302);
});
export default auth;

View file

@ -52,7 +52,8 @@ function looksLikeEmail(value) {
feedback.post("/", async (c) => {
let body;
try {
body = await c.req.json();
// `?? {}`: a body of JSON null parses fine and has no fields.
body = (await c.req.json()) ?? {};
} catch {
return c.json({ error: "Expected a JSON body." }, 400);
}

View file

@ -165,7 +165,7 @@ media.post("/link-preview", requireRole("editor"), async (c) => {
const user = c.get("user");
let url;
try {
({ url } = await c.req.json());
({ url } = (await c.req.json()) ?? {});
} catch {
return c.json({ error: "Expected a JSON body." }, 400);
}

View file

@ -25,6 +25,7 @@
import { Hono } from "hono";
import { requireAuth, requireRole, ROLES } from "../auth.js";
import { tx } from "../db.js";
const panel = new Hono();
@ -116,7 +117,8 @@ panel.patch("/users/:id", async (c) => {
let body;
try {
body = await c.req.json();
// `?? {}`: a body of JSON null parses fine and has no fields.
body = (await c.req.json()) ?? {};
} catch {
return c.json({ error: "Expected a JSON body." }, 400);
}
@ -154,7 +156,8 @@ panel.patch("/users/:id", async (c) => {
if (sets.length === 0) return c.json({ error: "Nothing to change." }, 400);
const tx = db.transaction(() => {
// tx(), not db.transaction(): node:sqlite has no transaction().
tx(db, () => {
db.prepare(`UPDATE admin_users SET ${sets.join(", ")} WHERE id = ?`).run(
...params,
id,
@ -163,7 +166,6 @@ panel.patch("/users/:id", async (c) => {
// matches the row. Make them sign in again.
db.prepare("DELETE FROM sessions WHERE user_id = ?").run(id);
});
tx();
console.log(
`account ${target.email} updated by ${me.email}: ${JSON.stringify(body)}`,