Key Takeaways
- Input validation and output encoding prevent most injection attacks.
- Use parameterized queries—never concatenate SQL.
- Implement proper session management and authentication.
- Defense in depth—multiple security layers.
- Security testing should be continuous, not just pre-release.
- Keep frameworks and dependencies updated.
Table of Contents
1. Web Security Fundamentals
Web application security protects web applications from attacks that exploit vulnerabilities in code, configuration, or design. With web applications handling sensitive data and business logic, security failures can lead to data breaches, financial loss, and reputational damage.
The core principle is: never trust user input. All data from users, APIs, and external sources should be validated, sanitized, and encoded before use.
2. OWASP Top 10 (2021)
| # | Vulnerability | Description |
|---|---|---|
| A01 | Broken Access Control | Users acting outside permissions |
| A02 | Cryptographic Failures | Weak/missing encryption |
| A03 | Injection | SQL, NoSQL, OS command, LDAP injection |
| A04 | Insecure Design | Missing security in design phase |
| A05 | Security Misconfiguration | Insecure defaults, incomplete configs |
| A06 | Vulnerable Components | Outdated libraries/frameworks |
| A07 | Auth Failures | Weak authentication/session management |
| A08 | Software/Data Integrity | CI/CD tampering, insecure deserialization |
| A09 | Logging Failures | Insufficient logging and monitoring |
| A10 | SSRF | Server-side request forgery |
3. Injection Prevention
3.1 SQL Injection Prevention
# VULNERABLE - Never do this
query = "SELECT * FROM users WHERE id = " + user_input
# SAFE - Parameterized query (Python/SQLAlchemy)
result = db.execute(
text("SELECT * FROM users WHERE id = :id"),
{"id": user_input}
)
# SAFE - Prepared statement (PHP/PDO)
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_input]);
3.2 XSS Prevention
# Output encoding (context-specific)
# HTML context:
< → <
> → >
& → &
# JavaScript context requires different encoding
# URL context requires URL encoding
# Use framework auto-escaping:
# React: automatically escaped
# Django: {{ variable }} auto-escaped
# Rails: <%= variable %> auto-escaped
Context Matters
XSS encoding must match the context. HTML encoding in a JavaScript context doesn't prevent XSS. Use Content Security Policy (CSP) as defense-in-depth against XSS that escapes encoding.
4. Authentication & Sessions
4.1 Best Practices
- Use established frameworks—don't build custom auth
- Implement MFA for sensitive operations
- Use secure, random session IDs
- Set appropriate session timeouts
- Regenerate session ID after login
- Use secure cookie flags (HttpOnly, Secure, SameSite)
4.2 Cookie Security
# Secure cookie settings
Set-Cookie: session=abc123;
HttpOnly; // No JavaScript access
Secure; // HTTPS only
SameSite=Strict; // CSRF protection
Path=/; // Scope
Max-Age=3600 // 1 hour
5. Security Headers
# Essential security headers
Content-Security-Policy: default-src 'self'; script-src 'self'
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Strict-Transport-Security: max-age=31536000; includeSubDomains
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: geolocation=(), microphone=()
6. Security Testing
| Type | When | Tools |
|---|---|---|
| SAST | Development | SonarQube, Semgrep, CodeQL |
| DAST | Staging/Prod | OWASP ZAP, Burp Suite, Nikto |
| SCA | CI/CD | Snyk, Dependabot, OWASP Dependency-Check |
| Pen Test | Pre-release | Manual testing by experts |
7. Web Application Firewalls
WAFs filter malicious traffic based on rules. They provide defense-in-depth but don't replace secure coding.
- Cloudflare WAF: Cloud-based, easy setup
- AWS WAF: For AWS environments
- ModSecurity: Open source, self-hosted
Defense in Depth
Layer security controls: secure coding + input validation + WAF + security headers + monitoring. No single control is sufficient. Defense in depth means an attacker must bypass multiple layers to succeed.
8. Frequently Asked Questions
Conclusion
Web application security starts with secure coding practices—validate input, encode output, use parameterized queries, and implement proper authentication. Layer defenses with security headers, WAFs, and continuous testing. Stay current with OWASP guidelines and keep dependencies updated. Security is an ongoing process, not a one-time effort.
Continue Learning:
XSS Prevention
SQL Injection