Get a Pentest and security assessment of your IT network.

Cyber Security

Secure JavaScript & REST API Web App

TL;DR

Protect your web app with strong input validation on both client and server, secure authentication (JWTs are good), HTTPS everywhere, regular updates, Content Security Policy, and careful handling of sensitive data. Don’t trust the client!

1. Secure Your REST API Back End

  1. Authentication & Authorisation: Use a robust system like JSON Web Tokens (JWTs). Don’t store passwords directly; use hashing algorithms (bcrypt, Argon2).
    // Example JWT verification (Node.js with express-jwt)
    const jwt = require('express-jwt');
    app.use(jwt({ secret: 'your_secret_key', algorithm: 'HS256' }));
  2. Input Validation: Always validate all data received from the client. This prevents injection attacks (SQL, NoSQL, command injection).
    // Example input validation (Node.js)
    app.post('/users', (req, res) => {
      if (!req.body.username || !req.body.email) {
        return res.status(400).send('Missing username or email');
      }
    });
  3. HTTPS: Use HTTPS to encrypt all communication between the client and server.
  4. Rate Limiting: Protect against brute-force attacks by limiting the number of requests from a single IP address.
  5. Regular Updates: Keep your frameworks, libraries, and operating system up to date with the latest security patches.
  6. CORS Configuration: Configure Cross-Origin Resource Sharing (CORS) carefully to only allow requests from trusted origins. Avoid using * in production.
    // Example CORS configuration (Node.js)
    const cors = require('cors');
    app.use(cors({ origin: 'https://yourfrontenddomain.com' }));

2. Secure Your JavaScript Front End

  1. Input Validation (Client-Side): Validate user input *before* sending it to the server, but do not rely on this alone. Client-side validation is easily bypassed.
  2. Content Security Policy (CSP): Implement a strong CSP to prevent cross-site scripting (XSS) attacks. This tells the browser which sources are allowed for loading resources.
    // Example CSP header:
    Content-Security-Policy: default-src 'self'; script-src 'self' https://trustedcdn.com; style-src 'self' https://trustedcdn.com
  3. HTTPS: Ensure your front end is served over HTTPS.
  4. Avoid Storing Sensitive Data: Don’t store sensitive information (passwords, API keys) in client-side code or local storage.
  5. Sanitize User Input: If you display user input on the page, sanitize it to prevent XSS attacks. Use a library designed for this purpose.
  6. Regular Updates: Keep your JavaScript frameworks and libraries up-to-date with the latest security patches.
  7. Subresource Integrity (SRI): Use SRI tags when including external scripts to verify their integrity. This prevents malicious code from being injected if a CDN is compromised.
    <script src="https://trustedcdn.com/script.js" integrity="sha384-examplehash" crossorigin="anonymous"></script>

3. Communication Between Front End and Back End

  1. Use Secure HTTP Methods: Use the correct HTTP methods (GET, POST, PUT, DELETE) for their intended purpose.
  2. Protect API Keys: Never expose your API keys in client-side code. Store them securely on the server and access them through environment variables.
  3. Handle Errors Gracefully: Don’t display sensitive error messages to users. Log errors on the server for debugging purposes.

4. General Best Practices

  • Principle of Least Privilege: Grant only the necessary permissions to your users and applications.
  • Regular Security Audits: Conduct regular security audits to identify vulnerabilities in your code and infrastructure.
  • Penetration Testing: Consider performing penetration testing to simulate real-world attacks.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation