Q52: How do experimental_taintUniqueValue and experimental_taintObjectReference enforce data security boundaries?
React Taint APIs prevent sensitive server-side objects or raw string values (such as database tokens, user passwords, or API secrets) from accidentally leaking into Client Component props or serialization boundaries. If a tainted object or value passes across a Server-to-Client boundary, React throws a critical runtime boundary error during rendering.
Tainting Sensitive Objects in Data Layer
import { experimental_taintObjectReference } from 'react';
export async function getUserSession(id) {
const user = await db.user.findUnique({ where: { id } });
// Flags object reference; throws error if returned to Client Component
experimental_taintObjectReference(
'Do not pass raw user session objects directly to Client Components!',
user
);
return user;
}💡 If a developer attempts to pass
user into a Client Component prop, React halts compilation and throws the specified security message.Key Takeaways
experimental_taintObjectReferenceflags entire object references to block serialization to client bundles.experimental_taintUniqueValueflags specific string values (like private keys) to prevent transmission.
Knowledge Check
What happens if a Server Component attempts to pass a tainted object prop into a Client Component?