📖 Scenario: You are building a simple Node.js server that handles user input and serves data. To keep your server safe, you need to understand common security problems and how to avoid them.
🎯 Goal: Build a basic Node.js server that demonstrates common security vulnerabilities and how to fix them step-by-step.
📋 What You'll Learn
Create a basic Express server with a user input route
Add a configuration variable to control input validation
Implement input validation to prevent injection attacks
Add security headers using Helmet middleware
💡 Why This Matters
🌍 Real World
Web servers often receive input from users. Without validation and security headers, they can be vulnerable to attacks like injection or cross-site scripting.
💼 Career
Understanding and fixing common Node.js security vulnerabilities is essential for backend developers to build safe and reliable applications.
Progress0 / 4 steps
1
Set up a basic Express server
Create a file called server.js. Import express and create an app using const app = express(). Add a route /submit that accepts POST requests and reads user input from req.body.input. Use express.json() middleware to parse JSON bodies. Finally, listen on port 3000.
Node.js
Hint
Remember to use express.json() middleware to parse JSON input.
2
Add input validation configuration
Add a constant called validateInput and set it to true. This will control whether the server validates user input before processing.
Node.js
Hint
This variable will help us turn input validation on or off easily.
3
Add input validation to prevent injection
Inside the /submit route, add a check that runs only if validateInput is true. Validate that userInput is a string and contains only letters and numbers using a regular expression /^[a-zA-Z0-9]+$/. If invalid, respond with status 400 and message 'Invalid input'. Otherwise, continue to send the received input.
Node.js
Hint
Use a regular expression to allow only letters and numbers.
4
Add Helmet middleware for security headers
Import helmet at the top. Use app.use(helmet()) before your routes to add security headers that protect against common vulnerabilities.
Node.js
Hint
Helmet helps add important security headers automatically.
Practice
(1/5)
1. Which of the following is a common security vulnerability in Node.js applications?
easy
A. Memory Leak
B. SQL Injection
C. Slow Network
D. Syntax Error
Solution
Step 1: Understand common vulnerabilities
SQL Injection is a well-known security risk where attackers inject malicious SQL code.
Step 2: Identify relevant options
Memory leaks and slow networks are performance issues, syntax errors are coding mistakes, not security vulnerabilities.
Final Answer:
SQL Injection -> Option B
Quick Check:
Common Node.js security risk = SQL Injection [OK]
Hint: Security risks often involve data injection or unauthorized access [OK]
Common Mistakes:
Confusing performance issues with security vulnerabilities
Thinking syntax errors are security problems
Ignoring injection attacks
2. Which syntax correctly prevents Cross-Site Scripting (XSS) in a Node.js Express app when rendering user input in a template?
easy
A. res.send(`
${escape(userInput)}
`);
B. res.render('page', { data: userInput });
C. res.send(`
${userInput}
`);
D. res.send(userInput);
Solution
Step 1: Identify safe rendering method
Escaping user input before inserting into HTML prevents XSS attacks.
Step 2: Compare options
Only res.send(`
${escape(userInput)}`); uses an escape function to sanitize input before sending.
Final Answer:
res.send(`
${escape(userInput)}
`); -> Option A
Quick Check:
Escape user input to prevent XSS = res.send(`
${escape(userInput)}
`); [OK]
Hint: Always escape user input before rendering in HTML [OK]
Common Mistakes:
Sending raw user input directly in HTML
Assuming template rendering auto-escapes input
Not using escape functions
3. What will be the output of this Node.js code snippet vulnerable to prototype pollution?