Consider a Node.js web app that accepts user input. Why must the app validate this input before processing?
Think about what happens if bad data reaches your database or server.
Input validation stops harmful data from entering the system, protecting against attacks like SQL injection or cross-site scripting.
Imagine a Node.js server that serves pages over HTTP only. What is the main security risk?
Think about what happens when data travels without encryption.
Without HTTPS, data is sent in plain text, making it easy for attackers to spy or tamper with it.
Look at this code that handles user login. What security issue does it have?
app.post('/login', (req, res) => { const { username, password } = req.body; if (username === 'admin' && password === 'password123') { res.send('Welcome admin!'); } else { res.send('Invalid credentials'); } });
Think about storing passwords safely.
Hardcoding passwords is unsafe because anyone reading the code knows the password. Passwords should be hashed and stored securely.
Choose the code that properly sets security headers using Express.js.
Look for headers that prevent content sniffing.
Setting 'X-Content-Type-Options' to 'nosniff' tells browsers not to guess content types, improving security.
Consider a Node.js app with async functions that do not catch errors. What security risk does this cause?
Think about what happens if the server stops responding.
Not handling errors can cause the server to crash, making the app unavailable and vulnerable to denial of service attacks.