Skip to main content

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โ€‹

Every network/runtime boundary changes what can be trusted
Browser/user input
fully attacker-controlled
Client Component
still untrusted from the server's perspective
Server Function / API
validate ยท authenticate ยท authorize
Service/domain layer
business invariants
Database / providers
protected resources

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 }} />
Do you need raw HTML?
No โ€” content can be structured as React data/elementsPrefer structured rendering
Yes โ€” source is fully trusted by your security modelKeep the trust decision explicit and narrow
Yes โ€” content can contain user/CMS inputSanitize with an audited HTML sanitizer before rendering
Plan is regex-based sanitizationDo not do this โ€” HTML parsing/security is more complex

Prefer structured content when possibleโ€‹

Structured rendering narrows the allowed surface
Untrusted content model
paragraph ยท text ยท link ยท emphasis
Known React components
controlled semantics + attributes

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 } });
}
A Server Function crosses a real network trust boundary
Client arguments
fully controllable by caller
Server Function
parse + authenticate + authorize
Mutation
domain/database operation

Hiding a button in the client does not authorize the server mutation.

Authentication and authorization are differentโ€‹

Authentication
Who is this caller?
Authorization
May this caller perform this action on this resource?

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โ€‹

Validation
Is this input structurally/domain-wise acceptable?
Authorization
Does this identity have permission for this resource/action?

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โ€‹

Anything serialized to a Client Component becomes browser-visible
Server-only data
DB record ยท secrets ยท privileged metadata
Serializable client props
only what UI needs
User-inspectable
assume client can read it

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โ€‹

Client boundaries are dependency boundaries
'use client' module
interactive entry point
Client module graph
must not include DB clients, signing keys, filesystem secrets, privileged SDKs

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.

Can the user influence this URL?
Internal redirect targetAllow only valid internal paths or an explicit origin allow-list
External link/resourceParse URL and enforce allowed schemes/origins
javascript: or unexpected schemeReject it

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โ€‹

Third-party code executes inside your application's risk envelope
Dependency
npm/package supply chain
Script/SDK
runtime data + DOM access
Widget/provider
network + iframe/API integration

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.

What should telemetry contain?
Release, route, trace ID, error class, safe feature IDsUseful diagnostic context
Secrets, tokens, payment details, private messagesNever log
User data needed only occasionallyRedact/aggregate/minimize according to policy

Security review lifecycleโ€‹

1Map trust boundaries
2Validate input
3Authenticate identity
4Authorize resource/action
5Minimize output
6Observe safely

Security is strongest when trust boundaries are explicit in architecture instead of scattered across UI conditionals and assumptions.