Design: Web Application Security: SQL Injection and XSS Prevention
Focus on design of input validation, sanitization, and secure coding practices to prevent SQL injection and XSS. Out of scope: detailed cryptography, network security layers, or physical security.
Functional Requirements
FR1: Prevent attackers from injecting malicious SQL commands through user inputs
FR2: Prevent attackers from injecting malicious scripts (XSS) that run in users' browsers
FR3: Ensure all user inputs are validated and sanitized before processing
FR4: Provide secure authentication and authorization to protect sensitive data
FR5: Log and monitor suspicious activities related to injection attempts
Non-Functional Requirements
NFR1: Handle up to 10,000 concurrent users securely
NFR2: API response latency p99 under 200ms including security checks
NFR3: Availability target of 99.9% uptime
NFR4: Support modern browsers and mobile clients
NFR5: Compliance with OWASP Top 10 security standards
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Input validation and sanitization module
Parameterized queries or prepared statements
Web application firewall (WAF)
Content Security Policy (CSP) headers
Security logging and monitoring system
Design Patterns
Parameterized queries to prevent SQL injection
Output encoding to prevent XSS
Whitelist input validation
Use of security headers (CSP, X-Content-Type-Options)
Defense in depth with layered security controls
Reference Architecture
Client Browser
|
| HTTP Requests with user inputs
v
Web Application Server
|-- Input Validation & Sanitization Module
|-- Parameterized Query Executor
|-- Output Encoding Module
|-- Security Headers Middleware (CSP, etc.)
|
v
Database Server
Security Logging & Monitoring System (receives logs from Web App Server)
Web Application Firewall (WAF) sits in front of Web Application Server
Components
Input Validation & Sanitization Module
Custom code or security libraries
Check and clean user inputs to remove or reject malicious content
But users report that some scripts still run. What is the likely cause?
medium
A. The replace method does not work on strings
B. The function should remove all HTML tags instead of replacing
C. The code should use double quotes instead of single quotes
D. Only replacing < and > is insufficient; other characters like quotes must be escaped
Solution
Step 1: Analyze current escaping
The code replaces only < and > but leaves quotes and other characters unescaped.
Step 2: Understand XSS vectors
Attackers can use quotes or other entities to break out of attributes or inject scripts.
Final Answer:
Only replacing < and > is insufficient; other characters like quotes must be escaped -> Option D
Quick Check:
Partial escaping = incomplete XSS prevention [OK]
Hint: Escape all special HTML chars, not just < and > [OK]
Common Mistakes:
Thinking replacing < and > is enough
Believing string replace fails silently
Ignoring quotes and ampersands in input
5. You are designing a web app that stores user comments and displays them safely. Which combination of techniques best prevents both SQL injection and XSS attacks?
hard
A. Use parameterized queries for database and escape HTML output with a Content Security Policy (CSP)
B. Sanitize user input by removing all special characters before storing
C. Use client-side JavaScript to validate input and disable scripts in browser settings
D. Store raw input and rely on HTTPS to secure data transmission
Solution
Step 1: Prevent SQL injection
Use parameterized queries to safely handle user input in database commands.
Step 2: Prevent XSS attacks
Escape HTML output when rendering user content and enforce a Content Security Policy to restrict script execution.
Final Answer:
Use parameterized queries for database and escape HTML output with a Content Security Policy (CSP) -> Option A