React Security की Best Practices
In this page:
// Safe: React escapes the value
<p>{userInput}</p>
// Dangerous: only use with sanitized HTML
<div dangerouslySetInnerHTML={{ __html: sanitizedHtml }} />
React का Built-In XSS Protection
जब आप JSX के अंदर {userInput} जैसी एक value render करते हैं, React इसे DOM में insert करने से पहले automatically escape करता है, मतलब even अगर userInput में HTML/script tags हैं, यह code के रूप में execute होने के बजाय plain text के रूप में displayed होता है।
यह default behavior cross-site scripting (XSS) attacks की एक बड़ी class से automatically protect करता है।
उदाहरण: React's Built-In XSS Protection
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function App() {
const userInput = "<img src=x onerror=alert('hacked')>";
return <p>{userInput}</p>; // Rendered as literal text, NOT executed as HTML
}
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
</script>
</body>
</html>
dangerouslySetInnerHTML का Danger
dangerouslySetInnerHTML आपको directly DOM में raw HTML inject करने देता है, React की automatic escaping को पूरी तरह bypass करते हुए। अगर वह HTML user input (या किसी untrusted source) से बिना sanitize किए आता है, यह attacker के लिए आपके page पर arbitrary JavaScript run करने का एक direct path खोल देता है।
उदाहरण: The Danger of dangerouslySetInnerHTML
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function App() {
const trustedContent = "<strong>This is safe because WE wrote it, not a user</strong>";
return <div dangerouslySetInnerHTML={{ __html: trustedContent }} />;
// Never pass raw, unsanitized USER input here without a sanitizer like DOMPurify first
}
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
</script>
</body>
</html>
Client-Side Validation Real Security नहीं है
Browser में एक form field validate करना (जैसे submit से पहले email format check करना) instant feedback देकर user experience improve करता है, लेकिन एक malicious user सारी client-side JavaScript पूरी तरह bypass कर सकता है और directly आपके server को जो भी data चाहे भेज सकता है।
Real security और validation हमेशा server-side पर भी होनी चाहिए।
उदाहरण: Client-Side Validation Isn't Real Security
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function App() {
const [email, setEmail] = React.useState("");
const isValidLooking = email.includes("@"); // UX nicety only
return (
<div>
<input value={email} onChange={e => setEmail(e.target.value)} />
<p>{isValidLooking ? "Looks valid (still re-check on the server!)" : "Enter an email"}</p>
</div>
);
}
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
</script>
</body>
</html>
- Unsanitized user-provided content के साथ dangerouslySetInnerHTML उपयोग करना, cross-site scripting (XSS) का door खोलते हुए।
- Sensitive tokens (जैसे auth tokens) localStorage में store करना, जो page पर run होने वाली किसी भी JavaScript द्वारा readable है, injected malicious scripts सहित।
- Client-side-only validation को real security मानकर trust करना, जब एक malicious user client-side JavaScript पूरी तरह bypass कर सकता है।
- React JSX में rendered values को automatically escape करता है, ज़्यादातर normal usage में XSS से protect करते हुए।
- dangerouslySetInnerHTML इस protection को bypass करता है और सिर्फ उस content के साथ उपयोग होना चाहिए जिसे आपने explicitly sanitize किया है।
- Sensitive data (tokens, secrets) localStorage में store नहीं होना चाहिए, जिसे कोई भी page script पढ़ सकता है।
- Client-side validation एक UX convenience है, real server-side validation और authorization का substitute कभी नहीं।
कोई specific React version requirement नहीं — ये general web security practices हैं जो React context में apply होती हैं।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Error Boundaries
- React Portals
- Modals using Portals (practical use)
- React Suspense
- Code Splitting with React.lazy
- Introduction to Server Components
- Introduction to Next.js (server-side React)
- Using React with TypeScript
- Scalable Folder Architecture
- Common React Design Patterns
- Component Documentation with Storybook
- Accessibility (a11y) in React
- i18n with react-i18next
- React Security Best Practices