Node.js Security Best Practices Quiz Quiz

Explore essential security best practices for Node.js applications with this easy quiz. Enhance your understanding of secure coding techniques, common vulnerabilities, and effective protective measures tailored for Node.js environments.

  1. Understanding Environment Variables

    Which is the safest way to manage secret keys and passwords in a Node.js application?

    1. Store them in environment variables
    2. Write them in plain text in the README file
    3. Save them in a public folder
    4. Hard-code them in the source code

    Explanation: Using environment variables keeps secret keys and passwords out of your source code and repository, reducing the risk of accidental exposure. Hard-coding secrets or storing them in public folders and documentation files makes them easily accessible to attackers. All secrets should be managed securely, and environment variables provide a practical solution for Node.js apps.

  2. Using Secure HTTP Headers

    What is the main purpose of setting HTTP security headers like Content-Security-Policy in a Node.js server?

    1. To increase available RAM
    2. To store user session data
    3. To speed up page load times
    4. To help prevent cross-site scripting (XSS) attacks

    Explanation: HTTP security headers such as Content-Security-Policy restrict which resources can be loaded or executed, helping prevent XSS and other attacks. These headers do not affect hardware performance, such as RAM, or page load speed directly, nor are they used for session data storage. The main benefit comes from improved security against common web vulnerabilities.

  3. Dependency Management

    Why is it important to regularly update dependencies in your Node.js project?

    1. To avoid using environment variables
    2. To make the user interface more colorful
    3. To reduce energy consumption
    4. To patch known security vulnerabilities

    Explanation: Updating dependencies helps patch known security flaws that may exist in older library versions. It has no effect on the system's energy use, does not alter the UI appearance, and is not related to the use of environment variables. Ignoring updates can leave your project open to exploitation.

  4. Preventing NoSQL Injection

    What technique helps protect a Node.js application from NoSQL injection when using a database?

    1. Storing queries in browser cookies
    2. Sending queries as plain text
    3. Sanitizing all user inputs before database queries
    4. Disabling all forms in the application

    Explanation: Sanitizing user inputs helps prevent injection attacks by removing harmful characters or patterns before they reach the database. Disabling all forms is not practical, while sending queries as plain text or storing them in cookies does not offer any protection and can make the application more vulnerable.

  5. Session Management

    What is the most secure way to store user session data in a Node.js web app?

    1. Save it in the browser's local storage
    2. Embed it in the page URL
    3. Store it on the server with secure cookies
    4. Print it in the page footer

    Explanation: Storing session data on the server and using secure cookies is the safest method, protecting against a variety of attacks. Local storage and URLs are easily accessible and can be stolen, while printing session data on the page exposes it publicly. Secure cookies help keep session data confidential and tied to server-side logic.

  6. Avoiding Information Leakage

    What is a common mistake that can reveal sensitive internal details about a Node.js application to attackers?

    1. Returning detailed error messages to users
    2. Disabling unused routes
    3. Minimizing error messages in logs
    4. Using HTTPS

    Explanation: Exposing detailed error messages may reveal stack traces, code structure, or database details useful to attackers. Using HTTPS, minimizing log errors, or disabling unused routes does not cause information leakage; in fact, these steps can improve overall security. Error information should always be kept generic for users.

  7. Mitigating Cross-Site Request Forgery (CSRF)

    How can you reduce the risk of CSRF attacks in a Node.js-powered web app?

    1. Disable all user input fields
    2. Store CSRF tokens in JavaScript comments
    3. Delay all server responses by 10 seconds
    4. Include unique CSRF tokens in forms

    Explanation: CSRF tokens are unique per user session and help ensure that form submissions were intended by the user, blocking unauthorized actions. Disabling input fields or delaying server responses does not prevent CSRF attacks, while storing tokens in comments is insecure as attackers could access them easily.

  8. Securing File Uploads

    What should you always do when handling file uploads in a Node.js application?

    1. Save every uploaded file as-is in the root folder
    2. Rename files to random names only
    3. Restrict allowed file types and check content
    4. Ignore the original file extension

    Explanation: Restricting file types and verifying content prevent executable or malicious files from being uploaded and run on the server. Simply renaming files or saving them in root does not guarantee safety, and ignoring extensions can be very dangerous. Ensuring the upload matches expected formats is essential for security.

  9. Defending Against Denial of Service (DoS) Attacks

    Which practice helps protect a Node.js server from simple DoS attacks?

    1. Add more comments to the code
    2. Reduce the number of dependencies
    3. Use only GET methods in the API
    4. Rate limit incoming API requests

    Explanation: Rate limiting restricts how many requests a user or client can make in a given time, preventing server overload. More code comments do not affect security, using only GET methods does not address DoS, and dependency count is unrelated to request volume management. Rate limiting is a standard countermeasure for DoS.

  10. Protecting Sensitive Data in Transit

    What is the primary reason for enabling HTTPS in a Node.js web server?

    1. To make URLs shorter
    2. To allow access without passwords
    3. To increase JavaScript execution speed
    4. To encrypt sensitive information sent between client and server

    Explanation: HTTPS encrypts all data transferred, protecting it from interception or tampering. It does not change URL length, speed up script execution, or modify access controls. Secure data transmission is critical for authentication, transactions, and user privacy.