Third audit: security fixes and server-side scalability

Security:
- docx unzip reads each part once (a repeated entry was a zip bomb)
- request files must be named for their type (no PDF named .html)
- team roster hidden when its organization is unpublished
- /people?ids= hides draft organizations, takes 200 ids
- v_events takes the first published host and reads event_hosts
  directly (migration 059, plus an index on person_awards.event_id)
- group accounts refused while access groups are off; group sync
  can't undo a superadmin's disable
- 64 KB body limit on login, feedback and csp-report
- Drive pick-up races no longer 500; random upload boundary

Scalability:
- /events filters by scope, host, status and type on the server;
  Home no longer prefetches every event
- /organizations leaves out bodies and leadership; the map's group
  panel fetches its own
- admin lists page with Show more and a real count
- content request record search and the newsletter's post list
  ask the server

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-10-01 18:29:26 -05:00
parent 0bb4b8685f
commit f99010252f
18 changed files with 229 additions and 96 deletions

View file

@ -262,8 +262,11 @@ auth.get("/google/callback", async (c) => {
}
// A group account's role follows its groups, checked every time.
// One a superadmin disabled stays disabled whatever its groups say.
if (user.access_source === "group" && user.role !== "superadmin" && accessEnabled(db)) {
// One a superadmin disabled stays disabled whatever its groups say,
// and with access groups switched off none gets in (it can't be
// rechecked, so it would keep its last role for good).
if (user.access_source === "group" && user.role !== "superadmin") {
if (!accessEnabled(db)) return backToLogin(c, "not_in_group");
if (user.is_active !== 1 && user.group_disabled !== 1) return backToLogin(c, "disabled");
const found = await roleFromGroups();
if (found.failed) return backToLogin(c, "groups_unreachable");

View file

@ -442,8 +442,8 @@ function attachAwards(db, orgs) {
}
/* ── Events ────────────────────────────────────────────────────
Flat, with the event scopes alongside. Retreats.tsx owns the
band titles and colors and filters this list by scope_id.
Flat, with the event scopes alongside, narrowed by the query
(below). Each band asks for only what it shows.
Oldest first, undated last. The carousel shows the list as it
comes and opens on the first upcoming event, so past events
@ -461,13 +461,26 @@ content.get("/events", (c) => {
.prepare(`SELECT id, name, sort_order FROM event_scopes ORDER BY sort_order`)
.all();
// Optional filters, each narrowing the list (src/lib/eventData.ts):
// ?scope=national ?host=<org or person slug> ?status=upcoming
// ?type=class,workshop
const where = [`is_published = 1`, `is_listed = 1`];
const params = [];
const scope = c.req.query("scope");
if (scope) where.push(`scope_id = ?`), params.push(scope);
const host = c.req.query("host");
if (host) {
where.push(`EXISTS (SELECT 1 FROM event_hosts h WHERE h.event_id = v_events.id AND (h.org_id = ? OR h.person_id = ?))`);
params.push(host, host);
}
const status = c.req.query("status");
if (status) where.push(`effective_status = ?`), params.push(status);
const types = (c.req.query("type") ?? "").split(",").filter(Boolean).slice(0, 20);
if (types.length) where.push(`event_type IN (${marks(types.length)})`), params.push(...types);
const rows = db
.prepare(
`SELECT * FROM v_events
WHERE is_published = 1 AND is_listed = 1
ORDER BY ${EVENT_ORDER}`,
)
.all();
.prepare(`SELECT * FROM v_events WHERE ${where.join(" AND ")} ORDER BY ${EVENT_ORDER}`)
.all(...params);
const ids = rows.map((row) => row.id);
const links = loadLinks(db, "event", ids);
@ -576,20 +589,17 @@ content.get("/organizations", (c) => {
const ids = rows.map((row) => row.id);
const links = loadLinks(db, "organization", ids);
const cards = loadBlocks(db, "organization", ids, "card");
const bodies = loadBlocks(db, "organization", ids, "body");
const organizations = rows.map((row) =>
shapeOrganization(
row,
links.get(row.id) ?? [],
cards.get(row.id) ?? [],
bodies.get(row.id) ?? [],
),
);
// No body blocks or leadership: they grow with every organization
// and only a group's own detail panel or page shows them, which
// fetch /organizations/:id.
const organizations = rows.map((row) => {
const { blocks, leadership, ...org } = shapeOrganization(row, links.get(row.id) ?? [], cards.get(row.id) ?? [], []);
return org;
});
attachRegionDetails(db, organizations);
attachGroupDetails(db, organizations);
attachLeadership(db, organizations);
return json(c, { organizations });
});

View file

@ -86,9 +86,15 @@ export const inDrive = (folder) => (folder ? `${FOLDER}/${folder}` : FOLDER);
Returns what the list shows. Also how the clip tool's Save to Drive
lands (routes/clips.js). */
export async function recordDriveUpload(db, uploaded, { name, mime, bytes, folder, userId }) {
const id = db
.prepare(`INSERT INTO drive_files (drive_id, name, mime, bytes, url, folder, uploaded_by) VALUES (?, ?, ?, ?, ?, ?, ?)`)
.run(uploaded.id, name, mime, bytes, uploaded.webViewLink, folder, userId).lastInsertRowid;
// A pick-up may have found it in Drive before this insert: ours wins.
const { id } = db
.prepare(
`INSERT INTO drive_files (drive_id, name, mime, bytes, url, folder, uploaded_by) VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (drive_id) DO UPDATE SET name = excluded.name, mime = excluded.mime, bytes = excluded.bytes,
url = excluded.url, folder = excluded.folder, uploaded_by = excluded.uploaded_by
RETURNING id`,
)
.get(uploaded.id, name, mime, bytes, uploaded.webViewLink, folder, userId);
await fetchPreview(db, db.prepare(`${SELECT} WHERE f.id = ?`).get(id), userId);
return shape(db.prepare(`${SELECT} WHERE f.id = ?`).get(id));
}
@ -201,9 +207,13 @@ async function pickUp(db, force = false) {
continue;
}
refused.delete(file.id);
const id = db
.prepare(`INSERT INTO drive_files (drive_id, name, mime, bytes, url, folder) VALUES (?, ?, ?, ?, ?, ?)`)
.run(file.id, safeName(file.name), file.mimeType, Number(file.size ?? 0), file.webViewLink, file.folder).lastInsertRowid;
// OR IGNORE: another pick-up, or an upload between Drive and its
// own insert, may have recorded it meanwhile.
const res = db
.prepare(`INSERT OR IGNORE INTO drive_files (drive_id, name, mime, bytes, url, folder) VALUES (?, ?, ?, ?, ?, ?)`)
.run(file.id, safeName(file.name), file.mimeType, Number(file.size ?? 0), file.webViewLink, file.folder);
if (!res.changes) continue;
const id = res.lastInsertRowid;
console.log(`files: ${file.name} picked up from Drive`);
// In the background: a folder of new files shouldn't hold the list.
fetchPreview(db, db.prepare(`SELECT * FROM drive_files WHERE id = ?`).get(id), null);

View file

@ -26,8 +26,9 @@ import { postsBy } from "./blog.js";
const people = new Hono();
/* How many slugs one request may name. Keeps the URL sane. */
const MAX_IDS = 50;
/* How many slugs one request may name. Keeps the URL sane (a few
KB) while covering any hand-picked list. */
const MAX_IDS = 200;
function shapePerson(row) {
return {
@ -69,9 +70,10 @@ people.get("/teams/:id/people", (c) => {
const team = db
.prepare(
`SELECT id, org_id, name, tagline, color, logo
FROM teams
WHERE id = ? AND is_published = 1`,
`SELECT t.id, t.org_id, t.name, t.tagline, t.color, t.logo
FROM teams t
JOIN organizations o ON o.id = t.org_id AND o.is_published = 1
WHERE t.id = ? AND t.is_published = 1`,
)
.get(id);
@ -125,7 +127,7 @@ people.get("/people", (c) => {
o.id AS primary_org_id,
o.name AS primary_org_name
FROM people p
LEFT JOIN organizations o ON o.id = p.primary_org_id
LEFT JOIN organizations o ON o.id = p.primary_org_id AND o.is_published = 1
WHERE p.id IN (${marks(ids.length)})
AND p.is_published = 1`,
)

View file

@ -79,15 +79,17 @@ export const TYPE_FOLDERS = {
export const MAX_FILES = 3;
export const MAX_FILE_BYTES = 10 * 1024 * 1024;
/* What a file is, from its first bytes. A zip is only taken as a
Word document when it's named like one. */
/* What a file is, from its first bytes, and only when its name says
the same: a PDF named .html is refused, so a download is always
what it says. */
const DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
function kindOf(bytes, name) {
const ext = String(name).toLowerCase().match(/\.([a-z0-9]+)$/)?.[1];
const starts = (...sig) => sig.every((b, i) => bytes[i] === b);
if (starts(0x25, 0x50, 0x44, 0x46, 0x2d)) return "application/pdf";
if (starts(0xff, 0xd8, 0xff)) return "image/jpeg";
if (starts(0x89, 0x50, 0x4e, 0x47)) return "image/png";
if (starts(0x50, 0x4b, 0x03, 0x04) && /\.docx$/i.test(name)) return DOCX;
if (ext === "pdf" && starts(0x25, 0x50, 0x44, 0x46, 0x2d)) return "application/pdf";
if ((ext === "jpg" || ext === "jpeg") && starts(0xff, 0xd8, 0xff)) return "image/jpeg";
if (ext === "png" && starts(0x89, 0x50, 0x4e, 0x47)) return "image/png";
if (ext === "docx" && starts(0x50, 0x4b, 0x03, 0x04)) return DOCX;
return null;
}