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:
parent
0bb4b8685f
commit
f99010252f
18 changed files with 229 additions and 96 deletions
|
|
@ -1,10 +1,9 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
EVENT DATA
|
||||
|
||||
One request, filtered per band. The Retreats page has three
|
||||
bands of events, and all three call this hook, the cache in
|
||||
api.ts keys on the path, so they share a single fetch and each
|
||||
narrows the result to what it shows.
|
||||
Events, narrowed by the server. Each filter is part of the path,
|
||||
so the cache in api.ts gives each its own entry and callers
|
||||
asking the same thing share one fetch.
|
||||
|
||||
useEvents({ scope: "national" }) one band
|
||||
useEvents({ host: "northwest" }) one host's events
|
||||
|
|
@ -24,47 +23,33 @@
|
|||
No fallback. An empty list on a failed request would read as
|
||||
"nothing scheduled" when the truth is "the server is down", so
|
||||
the error comes back and each caller says so.
|
||||
|
||||
Filtering here rather than in the query keeps the endpoint to
|
||||
one cached response. At a few dozen events that's the right
|
||||
trade; if the list ever runs to hundreds, move the filters into
|
||||
the URL and let each become its own cache entry.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { useMemo } from "react";
|
||||
|
||||
import { useResource } from "./useResource.ts";
|
||||
|
||||
/* One shared empty list, so a memo keyed on `scopes` doesn't
|
||||
/* Shared empty lists, so a memo keyed on them doesn't
|
||||
restart on every render before the data arrives. */
|
||||
const NO_SCOPES: any[] = [];
|
||||
const NO_EVENTS: any[] = [];
|
||||
|
||||
export function useEvents({ scope, host, status, type }: any = {}) {
|
||||
const { data, error, loading } = useResource("/events");
|
||||
/* The path for a filter, as the server reads it (routes/content.js). */
|
||||
export function eventsPath({ scope, host, status, type }: any = {}) {
|
||||
const query = new URLSearchParams();
|
||||
if (scope) query.set("scope", scope);
|
||||
// An organization or a person slug: co-hosting puts one event on
|
||||
// both hosts' lists, which is the point.
|
||||
if (host) query.set("host", host);
|
||||
if (status) query.set("status", status);
|
||||
const types = Array.isArray(type) ? type.join(",") : type;
|
||||
if (types) query.set("type", types);
|
||||
const qs = query.toString();
|
||||
return qs ? `/events?${qs}` : "/events";
|
||||
}
|
||||
|
||||
const all = data?.events;
|
||||
export function useEvents(filter: any = {}) {
|
||||
const { data, error, loading } = useResource(eventsPath(filter));
|
||||
const events = data?.events ?? NO_EVENTS;
|
||||
const scopes = data?.scopes ?? NO_SCOPES;
|
||||
|
||||
/* An array prop is a new identity on every render, which would
|
||||
restart the memo each time. Joining it gives the dependency
|
||||
list something stable to compare. */
|
||||
const typeKey = Array.isArray(type) ? type.join(",") : (type ?? "");
|
||||
|
||||
const events = useMemo(() => {
|
||||
let list = all ?? [];
|
||||
if (scope) list = list.filter(e => e.scope_id === scope);
|
||||
// `host` is an organization or a person slug, and an event can
|
||||
// have several of either, co-hosting puts one event on both
|
||||
// hosts' lists, which is the point.
|
||||
if (host) list = list.filter(e => e.hosts?.some(h => h.id === host));
|
||||
if (status) list = list.filter(e => e.status === status);
|
||||
if (typeKey) {
|
||||
const wanted = new Set(typeKey.split(","));
|
||||
list = list.filter(e => wanted.has(e.event_type));
|
||||
}
|
||||
return list;
|
||||
}, [all, scope, host, status, typeKey]);
|
||||
|
||||
return { events, scopes, loading, error };
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -254,7 +254,7 @@ export type OrganizationRecord = {
|
|||
|
||||
/** One row of GET /organizations: the card surface and details,
|
||||
* without the sections only the org's own page loads. */
|
||||
export type OrganizationListItem = Omit<OrganizationRecord, 'teams' | 'awards' | 'events'> & {
|
||||
export type OrganizationListItem = Omit<OrganizationRecord, 'blocks' | 'leadership' | 'teams' | 'awards' | 'events'> & {
|
||||
sort_order: number
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -77,12 +77,12 @@ const BANDS: Record<FrontPageSectionKey, (props: BandProps) => ReactNode> = {
|
|||
export default function Home() {
|
||||
const { data: page, error, reload } = useFrontPage()
|
||||
|
||||
// The bands below ask for events and the timeline once the front
|
||||
// page has said which of them to show. Start both now, alongside it:
|
||||
// api.ts hands the same request to the bands when they mount, so
|
||||
// the page waits for one round trip instead of two.
|
||||
// The bands below ask for the timeline once the front page has said
|
||||
// which of them to show. Start it now, alongside it: api.ts hands the
|
||||
// same request to the band when it mounts, so the page waits for one
|
||||
// round trip instead of two. Events aren't fetched ahead: each band
|
||||
// asks the server for only its own, so there's no one list to start.
|
||||
useEffect(() => {
|
||||
get('/events').catch(() => {})
|
||||
get('/history').catch(() => {})
|
||||
}, [])
|
||||
|
||||
|
|
|
|||
|
|
@ -208,7 +208,16 @@ function RequestRecord({ row }) {
|
|||
setError(null);
|
||||
try {
|
||||
const params = new URLSearchParams({ limit: "500", ...(type.preset ?? {}) });
|
||||
const { rows } = await get(`/admin/${type.entity}?${params}`, { ttl: 0 });
|
||||
// The records matching the request's name, however far down the
|
||||
// list they sort, then the first 500 for picking by hand.
|
||||
const search = new URLSearchParams(params);
|
||||
search.set("q", String(row.subject ?? "").trim().slice(0, 100));
|
||||
const [matches, all] = await Promise.all([
|
||||
search.get("q") ? get(`/admin/${type.entity}?${search}`, { ttl: 0 }) : { rows: [] },
|
||||
get(`/admin/${type.entity}?${params}`, { ttl: 0 }),
|
||||
]);
|
||||
const seen = new Set();
|
||||
const rows = [...matches.rows, ...all.rows].filter((r) => !seen.has(r.id) && seen.add(r.id));
|
||||
const wanted = String(row.subject ?? "").toLowerCase().trim();
|
||||
const name = (r) => String(r[type.heading] ?? r.id);
|
||||
// The closest name first: exact, then containing, then the rest.
|
||||
|
|
|
|||
|
|
@ -109,8 +109,8 @@ export default function AdminNewsletter() {
|
|||
const cards = frontPage?.featured ?? []
|
||||
|
||||
useEffect(() => {
|
||||
get('/admin/blog_posts?limit=200', { ttl: 0 })
|
||||
.then((data: { rows: PostRow[] }) => setPosts(data.rows.filter((row) => row.is_published && row.is_listed)))
|
||||
get('/admin/blog_posts?is_published=1&is_listed=1&limit=200', { ttl: 0 })
|
||||
.then((data: { rows: PostRow[] }) => setPosts(data.rows))
|
||||
.catch(() => setPosts([]))
|
||||
}, [])
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ export default function EntityList() {
|
|||
|
||||
const [params, setParams] = useSearchParams();
|
||||
const [rows, setRows] = useState<any[]>([]);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [loadingMore, setLoadingMore] = useState(false);
|
||||
const [options, setOptions] = useState<any>({});
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<any>(null);
|
||||
|
|
@ -53,6 +55,7 @@ export default function EntityList() {
|
|||
get("/admin/options", { ttl: 60_000 }),
|
||||
]);
|
||||
setRows(list.rows);
|
||||
setTotal(list.total);
|
||||
setOptions(opts.options);
|
||||
} catch (err) {
|
||||
if (isUnauthorized(err)) return navigate("/admin/login", { replace: true });
|
||||
|
|
@ -66,6 +69,22 @@ export default function EntityList() {
|
|||
load();
|
||||
}, [load]);
|
||||
|
||||
// The server sends a page at a time; the next one goes under it.
|
||||
async function loadMore() {
|
||||
setLoadingMore(true);
|
||||
try {
|
||||
const next = new URLSearchParams(params);
|
||||
next.set("offset", String(rows.length));
|
||||
const list = await get(`/admin/${manifest.key}?${next}`, { ttl: 0 });
|
||||
setRows((current) => [...current, ...list.rows]);
|
||||
setTotal(list.total);
|
||||
} catch (err) {
|
||||
setError(err instanceof ApiError ? err.message : "Couldn't reach the server.");
|
||||
} finally {
|
||||
setLoadingMore(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (!manifest) {
|
||||
return <p className="text-muted">No such thing to edit.</p>;
|
||||
}
|
||||
|
|
@ -211,6 +230,22 @@ export default function EntityList() {
|
|||
)}
|
||||
{loading && <p className="px-4 py-8 text-center text-muted">Loading…</p>}
|
||||
</div>
|
||||
|
||||
{!loading && rows.length < total && (
|
||||
<div className="mt-4 flex items-center gap-4">
|
||||
<button
|
||||
type="button"
|
||||
onClick={loadMore}
|
||||
disabled={loadingMore}
|
||||
className="btn border border-muted/25 px-4 py-1.5 text-sm text-muted hover:border-brand/50 disabled:opacity-60"
|
||||
>
|
||||
{loadingMore ? "Loading…" : "Show more"}
|
||||
</button>
|
||||
<p className="text-sm text-muted">
|
||||
{rows.length} of {total}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { CaretRight, X } from '@phosphor-icons/react';
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useCommunity } from "../../lib/useCommunity.ts";
|
||||
import { useOrganization } from "../../lib/useContent.ts";
|
||||
import { Link } from "react-router-dom";
|
||||
import ArrowLink from "../../components/ArrowLink.tsx";
|
||||
import OrgLogo from "../../components/OrgLogo.tsx";
|
||||
|
|
@ -522,9 +523,13 @@ function GroupCard({ group, color, open, onOpen }) {
|
|||
}
|
||||
|
||||
function GroupDetail({ group, region, onClose }) {
|
||||
// The list leaves out each group's body and leadership; this
|
||||
// panel fetches them for the one group it shows.
|
||||
const { data: full } = useOrganization(group.id);
|
||||
|
||||
/* "Led by" comes from affiliations rather than a text field, so
|
||||
it lists real people and stays empty until they exist. */
|
||||
const leads = (group.leadership ?? [])
|
||||
const leads = (full?.leadership ?? [])
|
||||
.map(person =>
|
||||
person.title ? `${person.display_name} (${person.title})` : person.display_name
|
||||
)
|
||||
|
|
@ -561,7 +566,7 @@ function GroupDetail({ group, region, onClose }) {
|
|||
</button>
|
||||
</div>
|
||||
|
||||
<Blocks blocks={group.blocks} color={colorOf(region)} />
|
||||
<Blocks blocks={full?.blocks} color={colorOf(region)} />
|
||||
|
||||
{rows.length > 0 && (
|
||||
<dl className="mt-4 grid grid-cols-1 sm:grid-cols-2 gap-x-8 gap-y-1 text-sm">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue