Common Web Security Vulnerabilities and How to Protect Against Them
Web applications are often targets for attackers seeking to steal data, disrupt services, or gain unauthorized access. Understanding common security vulnerabilities—and how to prevent them—is critical for building secure applications.
Below is a list of the most common web security vulnerabilities, based on the OWASP Top 10, and how you can protect your application from them.
๐ 1. SQL Injection (SQLi)
What is it?
SQL Injection occurs when user input is inserted directly into a SQL query without proper validation or escaping, allowing attackers to manipulate the database.
Example:
SELECT * FROM users WHERE username = 'admin' AND password = '1234';
If user input isn't sanitized, an attacker could input:
' OR '1'='1
How to protect:
Use parameterized queries or ORMs (e.g., SQLAlchemy, Django ORM).
Never concatenate user input directly into SQL queries.
Validate and sanitize all user input.
Python Example (safe way using SQLite + parameterized query):
cursor.execute("SELECT * FROM users WHERE username = ? AND password = ?", (username, password))
๐ 2. Cross-Site Scripting (XSS)
What is it?
XSS allows attackers to inject malicious JavaScript into web pages viewed by other users.
Example:
<script>alert('You have been hacked!');</script>
How to protect:
Escape output in HTML, JavaScript, and URLs.
Use frameworks that auto-escape output (e.g., Django, Flask’s Jinja2).
Sanitize user input using libraries like Bleach in Python.
Implement Content Security Policy (CSP) headers.
๐ 3. Cross-Site Request Forgery (CSRF)
What is it?
CSRF tricks users into performing actions they didn't intend, like submitting a form or changing their password.
How to protect:
Use CSRF tokens in all forms and API requests.
Use frameworks with built-in CSRF protection (Django, Flask-WTF).
Check referer headers on sensitive requests.
Example in Flask:
from flask_wtf.csrf import CSRFProtect
csrf = CSRFProtect(app)
๐ 4. Broken Authentication
What is it?
Weak login systems (e.g., no account lockout, weak passwords) allow attackers to gain access using brute-force attacks.
How to protect:
Use strong password hashing (e.g., bcrypt).
Enforce password strength requirements.
Enable account lockout after multiple failed attempts.
Use multi-factor authentication (MFA).
Secure session management (use HttpOnly, Secure, and SameSite cookies).
๐ 5. Insecure Direct Object References (IDOR)
What is it?
Occurs when users can access data by modifying a reference in the URL or request (e.g., accessing another user's account by changing user_id=123).
How to protect:
Always check user authorization on every request.
Don’t expose internal IDs directly—use hashed or UUID-based references.
Use access control checks on the server side.
๐ 6. Security Misconfiguration
What is it?
Leaving debug mode on, using default passwords, exposing sensitive data in error messages, or misconfigured headers.
How to protect:
Disable debug mode in production.
Use secure headers (e.g., Content-Security-Policy, X-Frame-Options).
Remove unnecessary services and ports.
Regularly patch and update your dependencies.
๐ 7. Sensitive Data Exposure
What is it?
Sensitive data (like passwords, credit card numbers, or API keys) is stored or transmitted without encryption.
How to protect:
Use HTTPS for all communications.
Encrypt sensitive data at rest and in transit.
Don’t log or expose passwords or tokens.
Use strong algorithms for encryption (e.g., AES-256).
๐ 8. Using Components with Known Vulnerabilities
What is it?
Outdated or vulnerable libraries, frameworks, or tools used in your application.
How to protect:
Keep dependencies updated.
Use tools like pip-audit, npm audit, or safety for Python.
Subscribe to security advisories for the tools you use.
๐ 9. Insufficient Logging & Monitoring
What is it?
Without proper logging and alerts, security incidents go unnoticed.
How to protect:
Log authentication attempts, access control violations, and errors.
Store logs securely and monitor them regularly.
Set up alerts for suspicious activity.
๐ 10. Broken Access Control
What is it?
When users can perform actions or access data they shouldn't.
How to protect:
Enforce authorization on the server side for every route.
Follow the principle of least privilege.
Avoid relying on the client side for access control decisions.
✅ General Security Best Practices
Always validate and sanitize all user input.
Use strong password policies and secure storage (bcrypt, argon2).
Keep your framework and libraries up to date.
Use HTTPS and configure secure HTTP headers.
Run regular security audits and penetration testing.
Limit user permissions (least privilege principle).
Back up data and have an incident response plan.
Conclusion
Web security is not a one-time setup—it's an ongoing process. By understanding common vulnerabilities and applying best practices, you can build safer and more resilient applications.
Protecting your app starts with knowing the threats—and coding defensively against them.
Learn Fullstack Python Training in Hyderabad
Read More
Encrypting Sensitive Data in Full Stack Python Apps
At Our Quality Thought Training Institute in Hyderabad