);
}
```
**Important:** Client-side validation is for UX only. **Always validate on the server** - client-side validation can be bypassed.
## Attack Scenarios & Protection
### Attack 1: XSS via Comment
**Attack:**
```javascript
POST /api/comment
{
"content": ""
}
```
**Protection:**
```typescript
const validation = validateRequest(safeLongTextSchema, body);
// Result: content = "alert(document.cookie)"
// < and > removed, script harmless
```
### Attack 2: SQL Injection Attempt
**Attack:**
```javascript
POST /api/search
{
"query": "'; DROP TABLE users; --"
}
```
**Protection:**
```typescript
const validation = validateRequest(safeTextSchema, body);
// Result: query = "'; DROP TABLE users; --"
// Still contains SQL, but parameterized queries prevent execution
// Additionally, input length limited, special chars sanitized
```
**Note:** Use parameterized queries in database layer for full SQL injection protection.
### Attack 3: Buffer Overflow via Long Input
**Attack:**
```javascript
POST /api/profile
{
"bio": "A".repeat(1000000) // 1 million characters
}
```
**Protection:**
```typescript
const validation = validateRequest(updateProfileSchema, body);
// Result: Validation fails
// Error: "Bio must be at most 5000 characters"
// HTTP 400 returned before processing
```
### Attack 4: Script Injection in Multiple Fields
**Attack:**
```javascript
POST /api/contact
{
"name": "",
"email": "attacker@evil.com",
"subject": "