miscellaneous
Security, Code Quality, and Linting — Building Trustworthy and Maintainable React Apps
Master security best practices, enforce code quality with ESLint and Prettier, and set up CI pipelines for professional React development.
Security, Code Quality, and Linting — Building Trustworthy and Maintainable React Apps
Learning Objectives
By the end of this article, you will:
- Recognize and address top security threats in React apps—preventing XSS, CSRF, and injection attacks.
- Enforce code quality using best practices, strong linting, and formatting.
- Learn to set up and combine tools like ESLint, Prettier, and CI pipelines for automated code health checks.
- Practice writing clean, secure, and professional React code that passes peer and industry review.
Why Security and Code Quality Matter
With the rapid adoption of digital products across India’s fintech, healthcare, and e-commerce sectors, security is not a bonus—it’s a requirement.
Users trust your apps with their sensitive data; flaws can hurt both them and your company’s reputation.
At the same time, code quality is your “insurance policy”—clean code reduces bugs, speeds up feature dev, ensures smooth onboarding for new team members, and keeps maintenance costs low.
Core Security Principles for React Apps
1. Cross-Site Scripting (XSS)
- Safe by default: React’s JSX escapes dynamic data when you use
{}
—so basic rendering prevents XSS (React Security Best Practices 2025, React Security Vulnerabilities).
<div>{userInput}</div> // safe: auto-escaped
- Be careful with dangerouslySetInnerHTML: Never inject raw HTML from users, and if you must (for example, with a CMS), sanitize with libraries like DOMPurify before rendering.
import DOMPurify from "dompurify";
<div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(rawHtml) }} />;
- Avoid third-party scripts or libraries that manipulate the DOM directly.
2. Cross-Site Request Forgery (CSRF)
- For apps with authentication, always use CSRF tokens in API requests, especially for state-changing operations (React Security Practices, Vulnerabilities & Solutions, Application Security Best Practices).
- Secure cookies with
SameSite
,HttpOnly
, andSecure
attributes.
3. Secure Local Storage and Sensitive Data
- Never store sensitive data (tokens, passwords, user details) unencrypted in localStorage/sessionStorage (LinkedIn Security Guide, Application Security Best Practices).
- Use secure HTTP (HTTPS, TLS) always—especially with APIs.
4. Protecting Against Insecure Dependencies
- Update npm/yarn packages regularly; run
npm audit
or use GitHub Dependabot for alerts (React Security Best Practices 2025, LinkedIn Security Guide). - Avoid using deprecated or untrusted third-party packages.
5. Input Validation
- Validate all user input on both client and server.
- For URLs, allow only safe protocols (http, https); sanitize or validate all dynamically generated links (React Security Best Practices 2025).
6. Secure Authentication
- Use strong authentication protocols (OAuth2, OpenID).
- Encourage strong passwords, multi-factor authentication, and secure password storage (LinkedIn Security Guide, Application Security Best Practices).
- Never expose secrets, API keys, or sensitive logic in the client code.
Writing Reliable, Maintainable Code — Linting and Formatting
Why Linting?
- Linters (like ESLint) scan your code for potential errors, anti-patterns, and style issues (ReactJS Best Practices, Writing Clean Code, Setup ESLint & Prettier, How to Configure ESLint & Prettier).
- Enforcing consistent code style helps teams collaborate and review faster.
- Linting reduces the risk of introducing subtle bugs and security issues.
ESLint: The Industry Standard
Install:
npm install eslint --save-dev
npx eslint --init
React Integration:
When prompted, extend from plugin:react/recommended
for React-specific rules.
Prettier Integration: Optionally, add Prettier for auto-formatting:
npm install --save-dev prettier eslint-config-prettier eslint-plugin-prettier
In .eslintrc
:
{
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:prettier/recommended"
]
}
Best Practices
- Run lint and format in CI: Prevent poor quality or unsafe code from being merged.
- Use named exports instead of
export default
to improve maintainability and enable better tree-shaking (Writing Clean Code). - Keep code DRY; prefer small reusable functions/components.
Code Quality in CI/CD Pipelines
- Set up automated tasks in CI (like GitHub Actions) to run
npm test
,npm run lint
, andnpm run format
on every pull request. - Block merges if linting or tests fail (ReactJS Best Practices, React CI/CD Guide).
- Optionally, use static code analysis tools for deeper security and code coverage checks.
Checklist: Keeping React Apps Secure and Clean
- All dynamic content in JSX uses curly braces (escapes by default).
- No untrusted HTML with
dangerouslySetInnerHTML
unless sanitized. - Components and utilities are validated by ESLint and Prettier.
- Package dependencies are reviewed and updated regularly.
- No secrets or sensitive data are present in the client bundle.
- User authentication follows modern best practices.
- All input is sanitized and validated.
Practice Exercises
- XSS Challenge: Write a component that accepts user input and try to inject a
<script>
tag. See how React escapes it. Now try to break it (and fix with DOMPurify). - ESLint & Prettier Setup: Add both tools to your project. Run with
npm run lint
andnpm run format
. Fix all reported issues. - Secure Form Handling: Add validation for a contact form; sanitize all user input.
- CI Quality Gate: Configure your GitHub Actions pipeline to block PRs if lint, format, or tests fail.
Project Idea
Professional Upgrade: Take your main application and:
- Implement a full ESLint + Prettier setup.
- Add security checks (code reviews, dependency audit, and automated CI).
- Make sure all forms validate inputs and never expose sensitive info in localStorage or client-side code.
Further Reading & Resources
- React Security Best Practices 2025
- React.js Security Guide
- OWASP Top 10 Security Risks
- ESLint Docs
- Prettier Docs
- React Security Practices
- Setup ESLint & Prettier
Final Thoughts: Professionalism from First Line to Last
“Move fast and break things” is good—until broken code or security leaks cost real money or trust. Adopt the right security and code quality practices before crunch time. They’ll help you ship confidently, collaborate smoothly, and deliver apps ready for the demands of India’s tech landscape in 2025 and beyond.
Next: Cap the series off by mastering industry-standard React tools—like Router and styled-components—and learn which anti-patterns could hold your projects back!