Security and Trust Boundaries in React
React escapes ordinary text by default, but React itself is not your complete security boundary. Secure applications still need explicit validation, authentication, authorization, output controls, secret isolation, dependency hygiene, and observability.
Start with a trust mapโ
For every boundary ask: what can the caller control, who are they, what may they do, what must be validated, what may cross back to the client, and what must never be logged or serialized?
JSX text is escaped; raw HTML is differentโ
function Comment({ message }) {
return <p>{message}</p>;
}
Ordinary text values are escaped rather than interpreted as markup.
dangerouslySetInnerHTML deliberately bypasses that safe text-rendering path:
<div dangerouslySetInnerHTML={{ __html: trustedHtml }} />
Prefer structured content when possibleโ
A structured CMS/document model often gives you more control than arbitrary HTML.
Server Functions are server entry points reachable from clientsโ
'use server';
export async function deleteProject(rawProjectId) {
const user = await requireAuthenticatedUser();
const projectId = parseProjectId(rawProjectId);
const project = await db.project.findUniqueOrThrow({ where: { id: projectId } });
if (project.ownerId !== user.id) {
throw new UnauthorizedError();
}
await db.project.delete({ where: { id: projectId } });
}
Hiding a button in the client does not authorize the server mutation.
Authentication and authorization are differentโ
A logged-in user still must not be able to mutate another user's record by changing an ID.
Validation and authorization are also differentโ
TypeScript annotations do not validate runtime input. Data from forms, URLs, network requests, storage, and Server Function arguments still needs runtime checks.
Never trust hidden/client-provided ownership fieldsโ
<input type="hidden" name="ownerId" value={user.id} />
The browser can modify submitted values. Derive authority from authenticated server identity and server-owned records.
Server/client serialization is a data-exposure boundaryโ
Do not pass private tokens, internal authorization metadata, or secrets simply because a Server Component could access them.
'use client' expands the client module graphโ
Framework tooling can help, but architecture should make server-only modules difficult to import from client code.
URLs and redirects are untrusted inputโ
Attackers can control query strings, path parameters, redirect targets, and CMS-provided resource URLs.
Use structured URL parsing instead of string-prefix security checks.
Browser storage is not a secret vaultโ
Anything available to client JavaScript should be treated as client-accessible. Minimize sensitive tokens in browser storage and follow the authentication framework's cookie/session protections.
Third-party code expands the trust surfaceโ
Review permissions, update policy, CSP/resource loading, secret handling, and failure isolation for critical integrations.
Telemetry must respect trust boundariesโ
Do not dump whole props, state, storage, headers, form bodies, or Server Function arguments into logs.
Security review lifecycleโ
Security is strongest when trust boundaries are explicit in architecture instead of scattered across UI conditionals and assumptions.