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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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
"""
Security Middleware
Implements rate limiting, input sanitization, CSRF protection, and security headers

SECURITY REQUIREMENTS FOR NEW FEATURES:
========================================
1. INPUT VALIDATION:
   - Always use sanitize_input() for text fields (titles, descriptions, comments)
   - Use sanitize_dict() for JSON payloads
   - Validate file uploads: check file type, size, and scan content
   - Validate ObjectIds before database queries with validate_object_id()

2. AUTHENTICATION & AUTHORIZATION:
   - Use Depends(get_current_user) for user-only endpoints
   - Use Depends(get_current_admin) for admin-only endpoints
   - Never expose user data without authentication
   - Check ownership before allowing updates/deletes

3. RATE LIMITING:
   - Apply @limiter.limit() to all write endpoints
   - Use stricter limits for sensitive operations (login, registration, money transfers)
   - Example: @limiter.limit("5/minute") for login

4. DATA MASKING:
   - Use mask_email(), mask_phone(), mask_sensitive_data() for PII
   - Default to masked view, require explicit permission for full data
   - Log access to unmasked sensitive data

5. ERROR HANDLING:
   - Never expose stack traces or internal details to users
   - Use HTTPException with sanitized messages
   - Log full errors server-side with request ID
   - Return consistent error format

6. LOGGING:
   - Log all security events (failed logins, access denials, suspicious activity)
   - Include request ID in all logs for tracing
   - Never log passwords, tokens, or sensitive PII

7. NEW FEATURE CHECKLIST:
   ☐ Input sanitization implemented
   ☐ Authentication/authorization configured
   ☐ Rate limiting applied
   ☐ PII data masked
   ☐ Error handling prevents information leakage
   ☐ Security logging added
   ☐ Unit tests for security scenarios written
   ☐ Penetration testing performed

EXAMPLES:
---------
Chat/Comments Feature:
  - Sanitize message content: sanitize_input(message.content)
  - Rate limit: @limiter.limit("10/minute") for sending messages
  - Authenticate: current_user = Depends(get_current_user)
  - Validate: max message length, blocked words list
  - Mask: user email/phone in chat metadata

File Upload Feature:
  - Validate file type: allowed_types = ['pdf', 'jpg', 'png']
  - Validate file size: max_size = 10 * 1024 * 1024  # 10MB
  - Scan content: virus scan, malicious code detection
  - Sanitize filename: remove path traversal characters
  - Store securely: use IPFS or encrypted storage
"""
from fastapi import Request, HTTPException, status
from fastapi.responses import JSONResponse
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.datastructures import Headers
import bleach
import re
from typing import Dict, Any
import secrets
from datetime import datetime, timedelta
import uuid
import logging

# Initialize rate limiter
limiter = Limiter(key_func=get_remote_address)

# Configure logger
logger = logging.getLogger(__name__)

# Failed login attempts tracking (in-memory - replace with Redis in production)
failed_login_attempts: Dict[str, Dict[str, Any]] = {}

# CSRF token storage (in-memory - replace with Redis in production)
csrf_tokens: Dict[str, datetime] = {}


class SecurityHeadersMiddleware(BaseHTTPMiddleware):
    """Add security headers to all responses"""
    
    async def dispatch(self, request: Request, call_next):
        response = await call_next(request)
        
        # Security Headers
        response.headers["X-Content-Type-Options"] = "nosniff"
        # Allow iframe embedding on Hugging Face Spaces
        response.headers["X-Frame-Options"] = "ALLOWALL"
        response.headers["X-XSS-Protection"] = "1; mode=block"
        response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
        response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
        
        # Content Security Policy - allow HF Spaces iframe
        csp = (
            "default-src 'self'; "
            "script-src 'self' 'unsafe-inline' 'unsafe-eval'; "
            "style-src 'self' 'unsafe-inline'; "
            "img-src 'self' data: https:; "
            "font-src 'self' data:; "
            "connect-src 'self' https://s.altnet.rippletest.net:51234 https://testnet.xrpl.org https://*.huggingface.co https://*.hf.space; "
            "frame-ancestors 'self' https://huggingface.co https://*.huggingface.co https://*.hf.space;"
        )
        response.headers["Content-Security-Policy"] = csp
        
        return response


class RequestIDMiddleware(BaseHTTPMiddleware):
    """Add unique request ID to each request for tracking and debugging"""
    
    async def dispatch(self, request: Request, call_next):
        # Generate unique request ID
        request_id = str(uuid.uuid4())
        
        # Store request ID in request state for access in route handlers
        request.state.request_id = request_id
        
        # Log request details
        logger.info(f"[{request_id}] {request.method} {request.url.path} - Client: {request.client.host if request.client else 'unknown'}")
        
        try:
            response = await call_next(request)
            # Add request ID to response headers for client tracking
            response.headers["X-Request-ID"] = request_id
            logger.info(f"[{request_id}] Response status: {response.status_code}")
            return response
        except Exception as e:
            # Log error with request ID
            logger.error(f"[{request_id}] Error: {str(e)}", exc_info=True)
            # Return error response without exposing internal details
            return JSONResponse(
                status_code=500,
                content={
                    "detail": "Internal server error",
                    "request_id": request_id
                },
                headers={"X-Request-ID": request_id}
            )


def sanitize_input(text: str) -> str:
    """Sanitize user input to prevent XSS attacks"""
    if not isinstance(text, str):
        return text
    
    # Remove HTML tags and attributes
    cleaned = bleach.clean(
        text,
        tags=[],  # No HTML tags allowed
        attributes={},
        strip=True
    )
    
    return cleaned.strip()


def sanitize_dict(data: dict) -> dict:
    """Recursively sanitize all string values in a dictionary"""
    if not isinstance(data, dict):
        return data
    
    sanitized = {}
    for key, value in data.items():
        if isinstance(value, str):
            sanitized[key] = sanitize_input(value)
        elif isinstance(value, dict):
            sanitized[key] = sanitize_dict(value)
        elif isinstance(value, list):
            sanitized[key] = [
                sanitize_input(item) if isinstance(item, str) else item
                for item in value
            ]
        else:
            sanitized[key] = value
    
    return sanitized


def validate_object_id(id_string: str) -> bool:
    """Validate MongoDB ObjectId format"""
    if not isinstance(id_string, str):
        return False
    
    # ObjectId is 24 character hexadecimal string
    pattern = re.compile(r'^[0-9a-fA-F]{24}$')
    return bool(pattern.match(id_string))


def validate_name(name: str) -> tuple[bool, str]:
    """
    Validate and sanitize user name
    Returns: (is_valid, error_message or sanitized_name)
    """
    if not name:
        return False, "Name is required"
    
    # Sanitize first
    name = sanitize_input(name).strip()
    
    # Check length
    if len(name) < 2:
        return False, "Name must be at least 2 characters"
    if len(name) > 100:
        return False, "Name must not exceed 100 characters"
    
    # Only allow letters, spaces, hyphens, and apostrophes (strict)
    pattern = re.compile(r"^[a-zA-Z\s\-']+$")
    if not pattern.match(name):
        return False, "Name can only contain letters, spaces, hyphens, and apostrophes"
    
    # Check for excessive spaces
    if '  ' in name:
        return False, "Name cannot contain multiple consecutive spaces"
    
    return True, name


def validate_phone(phone: str) -> tuple[bool, str]:
    """
    Validate and sanitize phone number (REQUIRED, exactly 10 digits)
    Returns: (is_valid, error_message or sanitized_phone)
    """
    if not phone:
        return False, "Phone number is required"
    
    # Sanitize first
    phone = sanitize_input(phone).strip()
    
    # Remove any non-digit characters for validation
    digits_only = re.sub(r'\D', '', phone)
    
    # Must be exactly 10 digits
    if len(digits_only) != 10:
        return False, "Phone number must be exactly 10 digits"
    
    # Only allow pure digits (no formatting characters)
    pattern = re.compile(r'^[0-9]{10}$')
    if not pattern.match(phone):
        return False, "Phone number must contain only 10 digits (no spaces or special characters)"
    
    return True, phone


def validate_date(date_str: str) -> tuple[bool, str]:
    """
    Validate date string (YYYY-MM-DD format)
    Returns: (is_valid, error_message or sanitized_date)
    """
    if not date_str:
        return True, None  # Date is optional
    
    # Sanitize first
    date_str = sanitize_input(date_str).strip()
    
    # Check format
    pattern = re.compile(r'^\d{4}-\d{2}-\d{2}$')
    if not pattern.match(date_str):
        return False, "Date must be in YYYY-MM-DD format"
    
    # Try to parse date
    try:
        date_obj = datetime.strptime(date_str, '%Y-%m-%d')
        
        # Check if date is not in future
        if date_obj > datetime.now():
            return False, "Date cannot be in the future"
        
        return True, date_str
    except ValueError:
        return False, "Invalid date"


def validate_gender(gender: str) -> tuple[bool, str]:
    """
    Validate gender selection
    Returns: (is_valid, error_message or sanitized_gender)
    """
    if not gender:
        return True, None  # Gender is optional
    
    # Sanitize first
    gender = sanitize_input(gender).strip()
    
    # Must be from predefined list
    valid_genders = ['Male', 'Female', 'Other']
    if gender not in valid_genders:
        return False, f"Gender must be one of: {', '.join(valid_genders)}"
    
    return True, gender


def validate_address(address: str) -> tuple[bool, str]:
    """
    Validate and sanitize address
    Returns: (is_valid, error_message or sanitized_address)
    """
    if not address:
        return True, ""  # Address is optional
    
    # Sanitize first
    address = sanitize_input(address).strip()
    
    # Check length
    if len(address) > 500:
        return False, "Address must not exceed 500 characters"
    
    # Only allow letters, numbers, spaces, and common address characters
    pattern = re.compile(r"^[a-zA-Z0-9\s,.\-'#/()\n]+$")
    if not pattern.match(address):
        return False, "Address contains invalid characters"
    
    return True, address


def validate_redirect_url(url: str, allowed_domains: list = None) -> bool:
    """
    Validate redirect URL to prevent open redirect attacks
    Only allows relative URLs or URLs from whitelisted domains
    Blocks javascript:, data:, and other dangerous schemes
    """
    if not url:
        return False
    
    # Block dangerous schemes
    dangerous_schemes = ['javascript:', 'data:', 'vbscript:', 'file:', 'about:']
    url_lower = url.lower().strip()
    for scheme in dangerous_schemes:
        if url_lower.startswith(scheme):
            return False
    
    # Default allowed domains (localhost and local dev)
    if allowed_domains is None:
        allowed_domains = [
            'localhost',
            '127.0.0.1',
            'localhost:5173',
            'localhost:5174',
            'localhost:5175',
            '127.0.0.1:5173',
            '127.0.0.1:5174',
            '127.0.0.1:5175'
        ]
    
    # Check if URL is relative (starts with /)
    if url.startswith('/') and not url.startswith('//'):
        return True
    
    # Check if URL starts with allowed domain
    for domain in allowed_domains:
        if url.startswith(f'http://{domain}') or url.startswith(f'https://{domain}'):
            return True
    
    # Reject all other URLs (external domains)
    return False


def generate_csrf_token() -> str:
    """Generate a CSRF token"""
    token = secrets.token_urlsafe(32)
    csrf_tokens[token] = datetime.utcnow() + timedelta(hours=1)
    return token


def validate_csrf_token(token: str) -> bool:
    """Validate CSRF token"""
    if not token or token not in csrf_tokens:
        return False
    
    # Check if token is expired
    if csrf_tokens[token] < datetime.utcnow():
        del csrf_tokens[token]
        return False
    
    return True


def check_rate_limit(ip: str, endpoint: str, max_attempts: int = 5, window_minutes: int = 15) -> bool:
    """
    Check if IP has exceeded rate limit for failed login attempts
    Returns True if allowed, False if blocked
    """
    key = f"{ip}:{endpoint}"
    now = datetime.utcnow()
    
    if key not in failed_login_attempts:
        failed_login_attempts[key] = {
            'count': 0,
            'first_attempt': now,
            'locked_until': None
        }
    
    attempt_data = failed_login_attempts[key]
    
    # Check if currently locked
    if attempt_data['locked_until'] and attempt_data['locked_until'] > now:
        return False
    
    # Reset if window has passed
    if now - attempt_data['first_attempt'] > timedelta(minutes=window_minutes):
        failed_login_attempts[key] = {
            'count': 0,
            'first_attempt': now,
            'locked_until': None
        }
        return True
    
    # Check if exceeded max attempts
    if attempt_data['count'] >= max_attempts:
        # Lock for 15 minutes
        attempt_data['locked_until'] = now + timedelta(minutes=window_minutes)
        return False
    
    return True


def record_failed_attempt(ip: str, endpoint: str):
    """Record a failed login attempt"""
    key = f"{ip}:{endpoint}"
    now = datetime.utcnow()
    
    if key not in failed_login_attempts:
        failed_login_attempts[key] = {
            'count': 1,
            'first_attempt': now,
            'locked_until': None
        }
    else:
        failed_login_attempts[key]['count'] += 1


def reset_failed_attempts(ip: str, endpoint: str):
    """Reset failed attempts after successful login"""
    key = f"{ip}:{endpoint}"
    if key in failed_login_attempts:
        del failed_login_attempts[key]


def mask_sensitive_data(data: str, mask_char: str = "*", visible_chars: int = 4) -> str:
    """
    Mask sensitive data showing only last N characters
    Shows fixed-length mask (12 chars) for consistent display
    """
    if not data or len(data) <= visible_chars:
        return data
    
    # Use fixed mask length for consistent display (prevents length-based attacks)
    fixed_mask_length = 12
    return mask_char * fixed_mask_length + data[-visible_chars:]


def mask_email(email: str) -> str:
    """Mask email address preserving domain"""
    if not email or '@' not in email:
        return email
    
    local, domain = email.split('@', 1)
    if len(local) <= 2:
        return f"{local}@{domain}"
    
    masked_local = local[0] + '*' * (len(local) - 2) + local[-1]
    return f"{masked_local}@{domain}"


def mask_phone(phone: str) -> str:
    """Mask phone number showing only last 4 digits"""
    if not phone:
        return phone
    
    # Remove non-numeric characters
    digits_only = re.sub(r'\D', '', phone)
    if len(digits_only) <= 4:
        return phone
    
    return '*' * (len(digits_only) - 4) + digits_only[-4:]