fix(security): audit completo — secondo gate admin, hardening slug, XSS, CSP/HSTS, update CVE

Audit di sicurezza su tutta l'app. Report in .planning/SECURITY-SCAN.md (codice),
.planning/SECURITY-AUDIT-INFRA.md (dipendenze/segreti/deploy) e piano in
.planning/SECURITY-REMEDIATION-PLAN.md.

CRITICO — l'autorizzazione admin era un unico punto di rottura: nessuna delle 21
pagine /admin controllava la sessione e admin/layout.tsx renderizzava comunque i
figli quando mancava. L'unico guard era proxy.ts, su un Next.js affetto da
GHSA-6gpp-xcg3-4w24 (proxy bypass). Ora il layout è un secondo gate indipendente;
proxy.ts marca il path con un token derivato da NEXTAUTH_SECRET, così il gate non
è aggirabile forgiando header e fallisce chiuso se il proxy non gira.

ALTO — gli slug cliente avevano 4 caratteri casuali da Math.random() (~20 bit,
1.7M tentativi) e risolvono prima del token: ora 12 caratteri via nanoid
(CSPRNG, ~62 bit). Aggiunto rate limit al ramo /client/, che ne era privo.

ALTO — src/lib/quote-actions.ts esponeva due server action pubbliche senza
autenticazione, una delle quali scriveva su DB. Codice morto, zero chiamanti:
rimosso.

MEDIO — i quattro dangerouslySetInnerHTML nelle sezioni proposta rendevano output
AI come HTML grezzo su pagina pubblica, alimentato da transcript di terzi. Sostituiti
con RichText (whitelist di emphasis, nessun HTML al DOM). I transcript ora sono
recintati in tag che il system prompt dichiara essere dati, non istruzioni.

Inoltre: next 16.2.6 -> 16.2.12 e next-auth 4.24.14 -> 4.24.15 (chiude 9 CVE Next
piu GHSA-xmf8-cvqr-rfgj su getToken, raggiungibile dal proxy); HSTS e CSP;
potatura della Map di rate-limit.ts, che cresceva senza limite; espunta la password
Postgres di produzione dai due 07-01-SUMMARY.md.

Verificato: tsc pulito, build OK, smoke test su login/redirect/header forgiati.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-27 23:35:36 +02:00
parent dd2d148457
commit e2bd1d95ed
20 changed files with 768 additions and 189 deletions
@@ -0,0 +1,43 @@
import { Fragment } from "react";
/**
* Renders AI-generated proposal strings with emphasis, without ever handing raw
* HTML to the DOM.
*
* These strings come from Claude (src/lib/proposal/agent.ts), which builds its
* prompt from client transcripts — third-party text. They were previously
* rendered with dangerouslySetInnerHTML on the public /preventivo/[slug] page,
* so a successful prompt injection became stored XSS against the prospect
* (C-4 in .planning/SECURITY-SCAN.md). The prompt never asks for HTML in the
* first place; the only markup worth keeping is emphasis.
*
* Recognises **bold** and <strong>/<b> and turns them into real React elements.
* Anything else — including <img onerror>, <script>, stray angle brackets — is
* emitted as text by React's normal escaping.
*/
// No dotAll flag: emphasis is not expected to span lines, and the project's
// TS target predates es2018.
const PATTERN = /\*\*(.+?)\*\*|<(?:strong|b)>(.+?)<\/(?:strong|b)>/gi;
export function RichText({ children }: { children: string }) {
const parts: React.ReactNode[] = [];
let cursor = 0;
for (const match of children.matchAll(PATTERN)) {
const at = match.index;
if (at > cursor) parts.push(children.slice(cursor, at));
parts.push(<strong key={at}>{match[1] ?? match[2]}</strong>);
cursor = at + match[0].length;
}
if (cursor < children.length) parts.push(children.slice(cursor));
return (
<span>
{parts.map((p, i) => (
<Fragment key={i}>{p}</Fragment>
))}
</span>
);
}