Main vulnerabilities in web applications:
// Protection examples:
// Data escaping
// Token validation
// Input validationVulnerabilities — like holes in a fence: if not closed, intruders will get in! 🏚️🚧
XSS — like forging a bank letter: malicious code disguised as normal text.
// ❌ Dangerous — no data validation
document.innerHTML = userInput; // If userInput = "<script>data theft</script>"
// ✅ Safe — escape data
const safeText = escapeHtml(userInput);
document.innerHTML = safeText;How it works:
CSRF — like if someone forces you to sign an important document when you don’t want to.
// ❌ Dangerous — no verification who sent
<form action="/transfer" method="POST">
<input name="to" value="hacker" />
<input name="amount" value="10000" />
</form>
// ✅ Safe — verification token
<form action="/transfer" method="POST">
<input name="csrf-token" value="unique_token" />
<input name="to" value="hacker" />
<input name="amount" value="10000" />
</form>How it works:
SQL injections — like if someone puts poison in soup at a restaurant.
// ❌ Dangerous — insert directly
const query = "SELECT * FROM users WHERE id = " + userId;
// ✅ Safe — parameterized queries
const query = "SELECT * FROM users WHERE id = ?";How it works:
Data leaks — like leaving house keys in a visible place.
// ❌ Dangerous — show everything
console.log("Password: " + password); // Visible in console!
// ✅ Safe — nothing extra
// Don't log anythingCommon causes:
Insufficient authorization — like if anyone can enter office.
// ❌ Dangerous — no verification
app.get('/admin', (req, res) => {
res.send('Admin panel');
});
// ✅ Safe — check rights
app.get('/admin', checkAuth, checkRole('admin'), (req, res) => {
res.send('Admin panel');
});How it works:
// ❌ Mistake — "First make it, then think about security"
// Harder to refactor later!
// ✅ Correct — think about security from the beginning// ❌ Mistake — trust all input data
const data = req.body; // May contain malicious code!
// ✅ Correct — validate everything
const data = validate(req.body); // Only safe dataSecurity — like seatbelt in car: can save life if something goes wrong! 🚗💨
Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪