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)

<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)

3. Secure Local Storage and Sensitive Data

4. Protecting Against Insecure Dependencies

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

Writing Reliable, Maintainable Code — Linting and Formatting

Why Linting?

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, and npm 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

  1. 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).
  2. ESLint & Prettier Setup: Add both tools to your project. Run with npm run lint and npm run format. Fix all reported issues.
  3. Secure Form Handling: Add validation for a contact form; sanitize all user input.
  4. 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

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!