File size: 9,024 Bytes
6062397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | # Security
Security architecture for production deployment.
## Overview
FixMyNeighborhood uses a **hybrid security model**:
| Layer | Responsibility | Why |
|-------|---------------|-----|
| **Python** | Rate limiting, input validation, prompt injection | Deterministic, fast, can't be jailbroken |
| **LLM** | Business validation, user interaction | Flexible, contextual, intelligent |
```
User Input
β
βΌ
βββββββββββββββββββββββββββββββββββββββ
β PYTHON SECURITY LAYER β
β β
β 1. Rate Limiting (abuse prevention)β
β 2. Input Validation (sanitization) β
β 3. Prompt Injection (detection) β
β β
β Blocks: Abuse, garbage, attacks β
β Passes: Clean input to LLM β
βββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββ
β LLM BUSINESS LAYER β
β β
β 1. Is this infrastructure? β
β 2. Is location valid? β
β 3. What priority? β
β 4. What follow-up questions? β
β β
β Full autonomy for decisions β
βββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββ
β OUTPUT SECURITY LAYER β
β β
β 1. PII Masking (logs) β
β 2. Audit Trail (compliance) β
β β
βββββββββββββββββββββββββββββββββββββββ
```
## Rate Limiting
**File**: `security/rate_limiter.py`
Sliding window rate limiting per session:
```python
class RateLimiter:
def __init__(
self,
max_requests: int = 10,
window_seconds: int = 60,
min_interval_ms: int = 2000,
):
self.max_requests = max_requests
self.window_seconds = window_seconds
self.min_interval_ms = min_interval_ms
```
**Configuration**:
| Parameter | Default | Description |
|-----------|---------|-------------|
| `max_requests` | 10 | Max requests per window |
| `window_seconds` | 60 | Sliding window duration |
| `min_interval_ms` | 2000 | Minimum time between requests |
**Usage in app.py**:
```python
try:
rate_limiter.check_rate_limit(session_id)
except RateLimitExceeded as e:
# Return friendly message, don't process
return f"Please wait {e.wait_seconds} seconds"
```
## Input Validation
**File**: `security/input_validator.py`
Validates and sanitizes user input:
```python
class InputValidator:
def validate_text(self, text: str, context: str = "message") -> ValidationResult:
# Check length
if len(text) > self.max_length:
return ValidationResult(is_valid=False, error="too_long")
# Check for HTML/XSS
if self._contains_html(text):
sanitized = self._strip_html(text)
return ValidationResult(
is_valid=True,
sanitized_value=sanitized,
warning="html_stripped"
)
```
**Checks**:
| Check | Action |
|-------|--------|
| Empty input | Block (unless image provided) |
| Too long (>5000 chars) | Block |
| HTML/script tags | Strip and warn |
| Excessive whitespace | Normalize |
## Prompt Injection Protection
**File**: `security/prompt_guard.py`
Detects and blocks prompt injection attempts:
```python
class PromptGuard:
PATTERNS = {
"role_impersonation": [
r"you are now",
r"ignore (?:all )?(?:previous|above)",
r"disregard (?:all )?(?:previous|above)",
r"forget (?:all )?(?:previous|above)",
],
"instruction_override": [
r"your (?:new )?instructions are",
r"system prompt:",
r"admin override",
],
"jailbreak": [
r"DAN mode",
r"developer mode",
r"pretend you",
r"act as if",
],
}
```
**Threat Levels**:
| Level | Action | Example |
|-------|--------|---------|
| `none` | Allow | "pothole on Broadway" |
| `low` | Allow, log | Contains "ignore" but in context |
| `medium` | Allow, sanitize | Suspicious but not malicious |
| `high` | Block | Clear jailbreak attempt |
**Response for blocked input**:
```python
if not guard_result.is_safe:
return "Please describe your infrastructure issue normally."
```
## Geographic Validation
Geographic validation (NYC bounds checking) is handled by the **MCP tools** rather than a separate Python security layer:
- `validate_address` - Validates addresses via NYC GeoSearch API
- `geo_search_address` - Reverse geocoding via Photon API
This approach lets the LLM make intelligent decisions about location validation, using real NYC API data rather than static bounding box checks.
## Cross-User Isolation
**Mechanism**: Gradio `gr.State`
Each user session has isolated state:
```python
# In app.py
session_logs = gr.State([]) # Per-session logs
session_orchestrator = gr.State(None) # Per-session orchestrator
```
**Why this works**:
- `gr.State` is tied to browser session
- No shared mutable state between users
- Orchestrator maintains per-session conversation history
**What's isolated**:
| Component | Isolation |
|-----------|-----------|
| Conversation history | Per-session |
| Rate limit counters | Per-session |
| Uploaded images | Unique filenames |
| Logs | Per-session capture |
## Output Masking
**File**: `security/output_masker.py`
Masks PII in logs and audit trails:
```python
class OutputMasker:
PATTERNS = {
"email": r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}",
"phone": r"\b\d{3}[-.]?\d{3}[-.]?\d{4}\b",
"ssn": r"\b\d{3}-\d{2}-\d{4}\b",
}
def mask(self, text: str) -> str:
for pattern_name, pattern in self.PATTERNS.items():
text = re.sub(pattern, f"[{pattern_name.upper()}_MASKED]", text)
return text
```
**Masked in logs**:
- Email addresses β `[EMAIL_MASKED]`
- Phone numbers β `[PHONE_MASKED]`
- SSN patterns β `[SSN_MASKED]`
## Error Handling
**File**: `security/error_handler.py`
Converts technical errors to user-friendly messages:
```python
class ErrorHandler:
ERROR_MESSAGES = {
"rate_limit": "You're sending requests too quickly. Please wait a moment.",
"api_error": "We're having trouble connecting to our services. Please try again.",
"validation": "Please check your input and try again.",
}
def handle(self, error: Exception, context: str = "") -> FriendlyError:
# Map technical error to friendly message
# Log full error for debugging
# Return safe message for user
```
**Principles**:
- Never expose stack traces to users
- Log full errors for debugging
- Provide actionable user messages
## HTTPS
All external calls use HTTPS:
| Service | URL | Protocol |
|---------|-----|----------|
| MCP Server | `https://...hf.space` | HTTPS |
| Weather.gov | `https://api.weather.gov` | HTTPS |
| NYC GeoSearch | `https://geosearch.planninglabs.nyc` | HTTPS |
| Photon API | `https://photon.komoot.io` | HTTPS |
| NYC Open Data | `https://data.cityofnewyork.us` | HTTPS |
| Resend API | `https://api.resend.com` | HTTPS |
## Audit Trail
**File**: `observability/audit_trail.py`
Logs all requests for compliance:
```python
class AuditTrail:
def start_request(self, session_id: str, input_preview: str, has_image: bool) -> str:
"""Start tracking a request."""
request_id = str(uuid.uuid4())[:8]
self._log({
"event": "request_start",
"request_id": request_id,
"session_id": session_id[:8], # Truncated for privacy
"has_image": has_image,
"timestamp": time.time(),
})
return request_id
```
**Logged events**:
- Request start/end
- Rate limit hits
- Validation failures
- Security blocks
- Errors
## Security Checklist
| Requirement | Implementation | Status |
|-------------|----------------|--------|
| Rate limiting | `security/rate_limiter.py` | β
|
| Input validation | `security/input_validator.py` | β
|
| Prompt injection | `security/prompt_guard.py` | β
|
| Cross-user isolation | `gr.State` per-session | β
|
| HTTPS for APIs | All external calls | β
|
| PII masking | `security/output_masker.py` | β
|
| Error handling | `security/error_handler.py` | β
|
| Audit logging | `observability/audit_trail.py` | β
|
|