File size: 25,336 Bytes
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
daf23ba
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
772635a
 
 
824bf31
772635a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
824bf31
772635a
 
 
 
 
 
 
 
 
 
 
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5765075
e63663c
 
 
 
ac4a861
 
 
be83f73
 
 
 
 
 
e63663c
824bf31
5765075
 
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
71ebb65
 
 
 
 
772635a
 
 
 
 
 
824bf31
 
71ebb65
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
"""
Module: api.middleware.security
Description: Advanced security middleware for comprehensive protection
Author: Anderson H. Silva
Date: 2025-01-15
License: Proprietary - All rights reserved
"""

import time
import re
import ipaddress
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Set, Tuple
from collections import defaultdict, deque
import hashlib
import hmac
import secrets

from fastapi import Request, HTTPException, status
from fastapi.responses import JSONResponse
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.types import ASGIApp

from src.core import get_logger, settings
from src.core.audit import audit_logger, AuditEventType, AuditSeverity, AuditContext
from src.services.rate_limit_service import get_rate_limiter


class SecurityConfig:
    """Security middleware configuration."""
    
    # Rate limiting
    RATE_LIMIT_REQUESTS_PER_MINUTE = 60
    RATE_LIMIT_REQUESTS_PER_HOUR = 1000
    RATE_LIMIT_BURST_SIZE = 10
    
    # IP blocking
    MAX_FAILED_ATTEMPTS = 5
    BLOCK_DURATION_MINUTES = 30
    SUSPICIOUS_ACTIVITY_THRESHOLD = 20
    
    # Request validation
    MAX_REQUEST_SIZE = 10 * 1024 * 1024  # 10MB
    MAX_HEADER_SIZE = 8192  # 8KB
    MAX_URL_LENGTH = 2048
    
    # Content security
    ALLOWED_CONTENT_TYPES = {
        "application/json",
        "application/x-www-form-urlencoded",
        "multipart/form-data",
        "text/plain"
    }
    
    # Security headers
    SECURITY_HEADERS = {
        "X-Content-Type-Options": "nosniff",
        "X-Frame-Options": "DENY",
        "X-XSS-Protection": "1; mode=block",
        "Strict-Transport-Security": "max-age=31536000; includeSubDomains",
        "Referrer-Policy": "strict-origin-when-cross-origin",
        "Permissions-Policy": "camera=(), microphone=(), geolocation=()"
    }
    
    # Suspicious patterns
    SUSPICIOUS_PATTERNS = [
        # XSS patterns
        r"<script[^>]*>.*?</script>",  # XSS script tags
        r"javascript:",  # XSS javascript protocol
        r"on\w+\s*=",  # Event handlers
        r"<iframe[^>]*>",  # Iframe injection
        r"<object[^>]*>",  # Object injection
        r"<embed[^>]*>",  # Embed injection
        r"vbscript:",  # VBScript protocol
        r"data:.*base64",  # Data URI with base64
        
        # SQL injection patterns
        r"union\s+select",  # SQL union select
        r"union\s+all\s+select",  # SQL union all select
        r"drop\s+table",  # SQL drop table
        r"drop\s+database",  # SQL drop database
        r"insert\s+into",  # SQL insert
        r"delete\s+from",  # SQL delete
        r"update\s+\w+\s+set",  # SQL update
        r"select\s+.*\s+from",  # SQL select
        r"or\s+1\s*=\s*1",  # SQL boolean injection
        r"or\s+'1'\s*=\s*'1'",  # SQL boolean injection quoted
        r";\s*--",  # SQL comment injection
        r"xp_cmdshell",  # SQL Server command execution
        r"exec\s+sp_",  # SQL Server stored procedures
        
        # Command injection patterns
        r"exec\s*\(",  # Command execution
        r"system\s*\(",  # System command
        r"eval\s*\(",  # Code evaluation
        r"`[^`]+`",  # Backtick command substitution
        r"\$\([^)]+\)",  # Command substitution
        r"&&",  # Command chaining
        r"\|\|",  # Command chaining
        r";\s*\w+",  # Command separator
        r"\|",  # Pipe character
        
        # Path traversal patterns
        r"\.\./",  # Unix path traversal
        r"\.\.\\",  # Windows path traversal
        r"/etc/passwd",  # Common target file
        r"c:\\windows",  # Windows system path
        r"/proc/self",  # Linux proc access
        
        # File inclusion patterns
        r"file://",  # Local file protocol
        r"ftp://",  # FTP protocol
        r"gopher://",  # Gopher protocol
        r"dict://",  # Dict protocol
        r"php://",  # PHP wrappers
        r"data://",  # Data protocol
        
        # XXE patterns
        r"<!ENTITY",  # XML entity
        r"SYSTEM\s+[\"']",  # System entity
        
        # LDAP injection
        r"\(\w+\s*=\s*\*\)",  # LDAP wildcard
        r"\(\w+\s*=\s*\)",  # LDAP empty
    ]
    
    # Endpoints that are exempt from suspicious pattern checks
    PATTERN_CHECK_EXEMPT_PATHS = {
        "/docs",
        "/redoc",
        "/openapi.json",
        "/api/v1/debug",
        "/api/v1/agents/status",
        "/health",
        "/metrics"
    }


class IPBlockList:
    """IP address blocking management."""
    
    def __init__(self):
        self.blocked_ips: Dict[str, datetime] = {}
        self.failed_attempts: Dict[str, List[datetime]] = defaultdict(list)
        self.whitelist: Set[str] = {
            "127.0.0.1", "::1",  # Localhost
            "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"  # Private networks
        }
    
    def is_whitelisted(self, ip: str) -> bool:
        """Check if IP is whitelisted."""
        try:
            ip_addr = ipaddress.ip_address(ip)
            for whitelist_entry in self.whitelist:
                if "/" in whitelist_entry:
                    if ip_addr in ipaddress.ip_network(whitelist_entry, strict=False):
                        return True
                elif ip == whitelist_entry:
                    return True
            return False
        except ValueError:
            return False
    
    def is_blocked(self, ip: str) -> bool:
        """Check if IP is currently blocked."""
        if self.is_whitelisted(ip):
            return False
        
        if ip in self.blocked_ips:
            if datetime.utcnow() - self.blocked_ips[ip] < timedelta(minutes=SecurityConfig.BLOCK_DURATION_MINUTES):
                return True
            else:
                # Unblock expired IPs
                del self.blocked_ips[ip]
        
        return False
    
    def record_failed_attempt(self, ip: str):
        """Record a failed attempt from IP."""
        if self.is_whitelisted(ip):
            return
        
        now = datetime.utcnow()
        
        # Clean old attempts (older than 1 hour)
        self.failed_attempts[ip] = [
            attempt for attempt in self.failed_attempts[ip]
            if now - attempt < timedelta(hours=1)
        ]
        
        # Add new attempt
        self.failed_attempts[ip].append(now)
        
        # Check if should block
        if len(self.failed_attempts[ip]) >= SecurityConfig.MAX_FAILED_ATTEMPTS:
            self.blocked_ips[ip] = now
    
    def get_failed_attempts_count(self, ip: str, window_minutes: int = 60) -> int:
        """Get number of failed attempts in time window."""
        if ip not in self.failed_attempts:
            return 0
        
        cutoff = datetime.utcnow() - timedelta(minutes=window_minutes)
        return sum(1 for attempt in self.failed_attempts[ip] if attempt > cutoff)


class RateLimiter:
    """Advanced rate limiting with multiple windows."""
    
    def __init__(self):
        self.requests: Dict[str, deque] = defaultdict(deque)
        self.burst_tokens: Dict[str, int] = defaultdict(lambda: SecurityConfig.RATE_LIMIT_BURST_SIZE)
        self.last_refill: Dict[str, datetime] = defaultdict(lambda: datetime.utcnow())
    
    def is_allowed(self, identifier: str) -> Tuple[bool, Dict[str, any]]:
        """Check if request is allowed for identifier."""
        now = datetime.utcnow()
        
        # Refill burst tokens (token bucket algorithm)
        time_since_refill = (now - self.last_refill[identifier]).total_seconds()
        tokens_to_add = int(time_since_refill * SecurityConfig.RATE_LIMIT_REQUESTS_PER_MINUTE / 60)
        
        if tokens_to_add > 0:
            self.burst_tokens[identifier] = min(
                SecurityConfig.RATE_LIMIT_BURST_SIZE,
                self.burst_tokens[identifier] + tokens_to_add
            )
            self.last_refill[identifier] = now
        
        # Check burst limit
        if self.burst_tokens[identifier] <= 0:
            return False, {"reason": "burst_limit_exceeded"}
        
        # Clean old requests
        cutoff_minute = now - timedelta(minutes=1)
        cutoff_hour = now - timedelta(hours=1)
        
        while self.requests[identifier] and self.requests[identifier][0] < cutoff_hour:
            self.requests[identifier].popleft()
        
        # Count requests in windows
        requests_last_minute = sum(1 for req_time in self.requests[identifier] if req_time > cutoff_minute)
        requests_last_hour = len(self.requests[identifier])
        
        # Check limits
        if requests_last_minute >= SecurityConfig.RATE_LIMIT_REQUESTS_PER_MINUTE:
            return False, {"reason": "minute_limit_exceeded", "requests_last_minute": requests_last_minute}
        
        if requests_last_hour >= SecurityConfig.RATE_LIMIT_REQUESTS_PER_HOUR:
            return False, {"reason": "hour_limit_exceeded", "requests_last_hour": requests_last_hour}
        
        # Allow request
        self.requests[identifier].append(now)
        self.burst_tokens[identifier] -= 1
        
        return True, {
            "requests_last_minute": requests_last_minute + 1,
            "requests_last_hour": requests_last_hour + 1,
            "burst_tokens": self.burst_tokens[identifier]
        }


class RequestValidator:
    """Request validation and security scanning."""
    
    def __init__(self):
        self.suspicious_patterns = [re.compile(pattern, re.IGNORECASE) for pattern in SecurityConfig.SUSPICIOUS_PATTERNS]
    
    def validate_request_size(self, request: Request) -> bool:
        """Validate request size."""
        content_length = request.headers.get("content-length")
        if content_length:
            try:
                size = int(content_length)
                return size <= SecurityConfig.MAX_REQUEST_SIZE
            except ValueError:
                return False
        return True
    
    def validate_headers(self, request: Request) -> Tuple[bool, Optional[str]]:
        """Validate request headers."""
        
        # Check header size
        headers_size = sum(len(k) + len(v) for k, v in request.headers.items())
        if headers_size > SecurityConfig.MAX_HEADER_SIZE:
            return False, "Headers too large"
        
        # Check for suspicious headers (skip user-agent and common headers)
        skip_headers = {
            "user-agent", "accept", "accept-language", "accept-encoding", 
            "referer", "origin", "x-direct-url", "x-forwarded-for", 
            "x-forwarded-proto", "x-forwarded-host", "x-real-ip",
            "host", "connection", "upgrade-insecure-requests",
            "authorization", "x-api-key", "content-type", "content-length",
            "cookie", "cache-control", "pragma", "expires",
            "if-none-match", "if-modified-since",
            # Chrome client hints headers
            "sec-ch-ua", "sec-ch-ua-mobile", "sec-ch-ua-platform",
            "sec-fetch-dest", "sec-fetch-mode", "sec-fetch-site", "sec-fetch-user",
            # Other security headers
            "dnt", "x-requested-with", "x-csrf-token"
        }
        for name, value in request.headers.items():
            if name.lower() in skip_headers:
                continue
            if any(pattern.search(value) for pattern in self.suspicious_patterns):
                return False, f"Suspicious content in header {name}"
        
        return True, None
    
    def validate_url(self, request: Request) -> Tuple[bool, Optional[str]]:
        """Validate request URL."""
        
        url = str(request.url)
        
        # Check URL length
        if len(url) > SecurityConfig.MAX_URL_LENGTH:
            return False, "URL too long"
        
        # Only check path and query for suspicious patterns, not the full URL
        path_and_query = request.url.path
        if request.url.query:
            path_and_query += "?" + request.url.query
            
        # Check if path is exempt from pattern checking
        if request.url.path not in SecurityConfig.PATTERN_CHECK_EXEMPT_PATHS:
            # Check for suspicious patterns in path and query only
            for pattern in self.suspicious_patterns:
                if pattern.search(path_and_query):
                    return False, "Suspicious pattern in URL"
        
        # Check for double encoding
        if "%25" in path_and_query:
            return False, "Double URL encoding detected"
        
        return True, None
    
    def validate_content_type(self, request: Request) -> bool:
        """Validate content type."""
        content_type = request.headers.get("content-type", "").split(";")[0].strip()
        
        if not content_type:
            return True  # Allow requests without content-type
        
        return content_type.lower() in SecurityConfig.ALLOWED_CONTENT_TYPES
    
    async def scan_request_body(self, body: bytes) -> Tuple[bool, Optional[str]]:
        """Scan request body for suspicious content."""
        if not body:
            return True, None
        
        try:
            body_text = body.decode("utf-8", errors="ignore")
            
            for pattern in self.suspicious_patterns:
                if pattern.search(body_text):
                    return False, "Suspicious pattern in request body"
            
            return True, None
            
        except Exception:
            return False, "Invalid request body encoding"


class SecurityMiddleware(BaseHTTPMiddleware):
    """Comprehensive security middleware."""
    
    def __init__(self, app: ASGIApp):
        super().__init__(app)
        self.logger = get_logger(__name__)
        self.ip_blocklist = IPBlockList()
        self.rate_limiter = RateLimiter()
        self.request_validator = RequestValidator()
    
    async def dispatch(self, request: Request, call_next):
        """Process request through security checks."""
        
        start_time = time.time()
        client_ip = self._get_client_ip(request)
        
        # Create audit context
        audit_context = AuditContext(
            ip_address=client_ip,
            user_agent=request.headers.get("user-agent"),
            host=request.headers.get("host"),
            referer=request.headers.get("referer")
        )
        
        try:
            # 1. IP blocking check
            if self.ip_blocklist.is_blocked(client_ip):
                await self._log_security_event(
                    "IP address blocked",
                    AuditEventType.UNAUTHORIZED_ACCESS,
                    AuditSeverity.HIGH,
                    {"ip": client_ip, "reason": "blocked_ip"},
                    audit_context
                )
                return JSONResponse(
                    status_code=status.HTTP_403_FORBIDDEN,
                    content={"detail": "Access denied"}
                )
            
            # 2. Rate limiting
            allowed, rate_info = self.rate_limiter.is_allowed(client_ip)
            if not allowed:
                self.ip_blocklist.record_failed_attempt(client_ip)
                await self._log_security_event(
                    "Rate limit exceeded",
                    AuditEventType.RATE_LIMIT_EXCEEDED,
                    AuditSeverity.MEDIUM,
                    {"ip": client_ip, **rate_info},
                    audit_context
                )
                return JSONResponse(
                    status_code=status.HTTP_429_TOO_MANY_REQUESTS,
                    content={"detail": "Rate limit exceeded"},
                    headers={
                        "Retry-After": "60",
                        "X-RateLimit-Limit": str(SecurityConfig.RATE_LIMIT_REQUESTS_PER_MINUTE),
                        "X-RateLimit-Remaining": "0"
                    }
                )
            
            # 3. Request validation
            validation_result = await self._validate_request(request, audit_context)
            if validation_result:
                return validation_result
            
            # 4. Process request
            response = await call_next(request)
            
            # 5. Add security headers
            self._add_security_headers(response)
            
            # 6. Log successful request
            processing_time = time.time() - start_time
            
            if processing_time > 5.0:  # Log slow requests
                await self._log_security_event(
                    "Slow request detected",
                    AuditEventType.SUSPICIOUS_ACTIVITY,
                    AuditSeverity.LOW,
                    {
                        "ip": client_ip,
                        "path": request.url.path,
                        "method": request.method,
                        "processing_time": processing_time
                    },
                    audit_context
                )
            
            # Add rate limit headers
            response.headers["X-RateLimit-Limit"] = str(SecurityConfig.RATE_LIMIT_REQUESTS_PER_MINUTE)
            response.headers["X-RateLimit-Remaining"] = str(rate_info.get("burst_tokens", 0))
            
            return response
            
        except Exception as e:
            # Log security middleware errors
            await self._log_security_event(
                f"Security middleware error: {str(e)}",
                AuditEventType.SYSTEM_STARTUP,  # Using system event for internal errors
                AuditSeverity.HIGH,
                {"ip": client_ip, "error": str(e)},
                audit_context
            )
            
            # Continue with request (fail open for availability)
            response = await call_next(request)
            self._add_security_headers(response)
            return response
    
    def _get_client_ip(self, request: Request) -> str:
        """Get client IP address considering proxies."""
        
        # Check X-Forwarded-For header (reverse proxy)
        forwarded_for = request.headers.get("x-forwarded-for")
        if forwarded_for:
            # Take the first IP (original client)
            ip = forwarded_for.split(",")[0].strip()
            try:
                ipaddress.ip_address(ip)
                return ip
            except ValueError:
                pass
        
        # Check X-Real-IP header (nginx)
        real_ip = request.headers.get("x-real-ip")
        if real_ip:
            try:
                ipaddress.ip_address(real_ip)
                return real_ip
            except ValueError:
                pass
        
        # Fall back to client address
        if hasattr(request.client, "host"):
            return request.client.host
        
        return "unknown"
    
    async def _validate_request(self, request: Request, audit_context: AuditContext) -> Optional[JSONResponse]:
        """Validate request and return error response if invalid."""
        
        # Validate request size
        if not self.request_validator.validate_request_size(request):
            await self._log_security_event(
                "Request size too large",
                AuditEventType.SUSPICIOUS_ACTIVITY,
                AuditSeverity.MEDIUM,
                {"ip": audit_context.ip_address, "path": request.url.path},
                audit_context
            )
            return JSONResponse(
                status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
                content={"detail": "Request too large"}
            )
        
        # Validate headers
        headers_valid, header_error = self.request_validator.validate_headers(request)
        if not headers_valid:
            await self._log_security_event(
                f"Invalid headers: {header_error}",
                AuditEventType.SUSPICIOUS_ACTIVITY,
                AuditSeverity.MEDIUM,
                {"ip": audit_context.ip_address, "error": header_error},
                audit_context
            )
            return JSONResponse(
                status_code=status.HTTP_400_BAD_REQUEST,
                content={"detail": "Invalid request headers"}
            )
        
        # Validate URL
        url_valid, url_error = self.request_validator.validate_url(request)
        if not url_valid:
            await self._log_security_event(
                f"Invalid URL: {url_error}",
                AuditEventType.SUSPICIOUS_ACTIVITY,
                AuditSeverity.HIGH,
                {"ip": audit_context.ip_address, "url": str(request.url), "error": url_error},
                audit_context
            )
            return JSONResponse(
                status_code=status.HTTP_400_BAD_REQUEST,
                content={"detail": "Invalid request URL"}
            )
        
        # Validate content type
        if not self.request_validator.validate_content_type(request):
            await self._log_security_event(
                "Unsupported content type",
                AuditEventType.SUSPICIOUS_ACTIVITY,
                AuditSeverity.MEDIUM,
                {
                    "ip": audit_context.ip_address,
                    "content_type": request.headers.get("content-type")
                },
                audit_context
            )
            return JSONResponse(
                status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
                content={"detail": "Unsupported content type"}
            )
        
        # Validate request body for POST/PUT requests
        if request.method in ["POST", "PUT", "PATCH"]:
            try:
                body = await request.body()
                body_valid, body_error = await self.request_validator.scan_request_body(body)
                
                if not body_valid:
                    await self._log_security_event(
                        f"Suspicious request body: {body_error}",
                        AuditEventType.SUSPICIOUS_ACTIVITY,
                        AuditSeverity.HIGH,
                        {"ip": audit_context.ip_address, "error": body_error},
                        audit_context
                    )
                    return JSONResponse(
                        status_code=status.HTTP_400_BAD_REQUEST,
                        content={"detail": "Invalid request content"}
                    )
                    
            except Exception as e:
                await self._log_security_event(
                    f"Request body validation error: {str(e)}",
                    AuditEventType.SUSPICIOUS_ACTIVITY,
                    AuditSeverity.MEDIUM,
                    {"ip": audit_context.ip_address, "error": str(e)},
                    audit_context
                )
        
        return None
    
    def _add_security_headers(self, response):
        """Add security headers to response."""
        for header, value in SecurityConfig.SECURITY_HEADERS.items():
            response.headers[header] = value
        
        # Add CSP header
        csp_policy = (
            "default-src 'self'; "
            "script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net; "
            "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; "
            "font-src 'self' https://fonts.gstatic.com; "
            "img-src 'self' data: https:; "
            "connect-src 'self' https://api.portaldatransparencia.gov.br; "
            "frame-ancestors 'none';"
        )
        response.headers["Content-Security-Policy"] = csp_policy
    
    async def _log_security_event(
        self,
        message: str,
        event_type: AuditEventType,
        severity: AuditSeverity,
        details: Dict,
        context: AuditContext
    ):
        """Log security event to audit system."""
        
        await audit_logger.log_event(
            event_type=event_type,
            message=message,
            severity=severity,
            details=details,
            context=context,
            success=False
        )


class CSRFProtection:
    """CSRF protection middleware."""
    
    def __init__(self):
        self.secret_key = settings.secret_key.get_secret_value()
    
    def generate_token(self, session_id: str) -> str:
        """Generate CSRF token for session."""
        timestamp = str(int(time.time()))
        message = f"{session_id}:{timestamp}"
        signature = hmac.new(
            self.secret_key.encode(),
            message.encode(),
            hashlib.sha256
        ).hexdigest()
        return f"{timestamp}:{signature}"
    
    def validate_token(self, token: str, session_id: str, max_age: int = 3600) -> bool:
        """Validate CSRF token."""
        try:
            timestamp_str, signature = token.split(":", 1)
            timestamp = int(timestamp_str)
            
            # Check token age
            if time.time() - timestamp > max_age:
                return False
            
            # Verify signature
            message = f"{session_id}:{timestamp_str}"
            expected_signature = hmac.new(
                self.secret_key.encode(),
                message.encode(),
                hashlib.sha256
            ).hexdigest()
            
            return hmac.compare_digest(signature, expected_signature)
            
        except (ValueError, IndexError):
            return False


# Global instances
csrf_protection = CSRFProtection()