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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Security posture and governance endpoints.

Provides visibility into security rules, threats blocked, and OWASP alignment.
Security by default, visibility by design.
"""

from fastapi import APIRouter, Depends
from pydantic import BaseModel
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func
from datetime import datetime, timedelta

from backend.db.session import get_db
from backend.db.models import LLMRequestTrace, TraceDecision


router = APIRouter(prefix="/security")


# ============================================================================
# Schemas
# ============================================================================


class SecurityRule(BaseModel):
    """Security rule configuration."""

    name: str
    category: str
    action: str  # "block" | "warn"
    scope: str  # "global" | "environment" | "application"
    mandatory: bool
    enabled: bool
    description: str
    owasp_mapping: str | None = None


class ThreatMetrics(BaseModel):
    """Metrics for blocked threats."""

    total_blocked: int
    by_category: dict[str, int]
    trend_7d: list[dict]  # Daily counts for last 7 days


class OWASPAlignment(BaseModel):
    """OWASP Top 10 for LLM alignment status."""

    id: str
    name: str
    status: str  # "full" | "partial" | "na"
    coverage: str


class SecurityPosture(BaseModel):
    """Overall security posture."""

    status: str  # "protected" | "warning" | "critical"
    message: str
    active_rules: int
    total_rules: int
    owasp_coverage: list[OWASPAlignment]


class SecurityReport(BaseModel):
    """Complete security report."""

    posture: SecurityPosture
    threats: ThreatMetrics
    rules: list[SecurityRule]


# ============================================================================
# Default Security Rules (OWASP-aligned)
# ============================================================================

DEFAULT_SECURITY_RULES = [
    # LLM01 - Prompt Injection
    SecurityRule(
        name="Block Prompt Injection",
        category="Prompt Injection",
        action="block",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Detects and blocks prompt injection attempts (system override, role confusion, instruction manipulation)",
        owasp_mapping="LLM01",
    ),
    SecurityRule(
        name="Detect Indirect Injection",
        category="Prompt Injection",
        action="warn",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Detects indirect prompt injection from external sources",
        owasp_mapping="LLM01",
    ),
    # LLM02 - Insecure Output Handling
    SecurityRule(
        name="Enforce JSON Schema",
        category="Output Validation",
        action="block",
        scope="application",
        mandatory=False,
        enabled=True,
        description="Validates LLM outputs against defined JSON schemas",
        owasp_mapping="LLM02",
    ),
    SecurityRule(
        name="Validate Response Size",
        category="Output Validation",
        action="block",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Blocks excessively large responses that could indicate abuse",
        owasp_mapping="LLM02",
    ),
    # LLM04 - Model Denial of Service
    SecurityRule(
        name="Rate Limiting",
        category="DoS Prevention",
        action="block",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Prevents abuse through request rate limiting per user/app",
        owasp_mapping="LLM04",
    ),
    SecurityRule(
        name="Detect Retry Storms",
        category="DoS Prevention",
        action="block",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Detects and blocks retry storms and infinite loops",
        owasp_mapping="LLM04",
    ),
    SecurityRule(
        name="Token Explosion Control",
        category="DoS Prevention",
        action="block",
        scope="environment",
        mandatory=True,
        enabled=True,
        description="Enforces maximum token limits per request to prevent resource exhaustion",
        owasp_mapping="LLM04",
    ),
    SecurityRule(
        name="Budget Hard Stop",
        category="DoS Prevention",
        action="block",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Hard budget limits prevent cost-based DoS attacks",
        owasp_mapping="LLM04",
    ),
    # LLM05 - Supply Chain Vulnerabilities
    SecurityRule(
        name="Restrict Models by Environment",
        category="Model Governance",
        action="block",
        scope="environment",
        mandatory=True,
        enabled=True,
        description="Whitelists approved models per environment (dev/staging/prod)",
        owasp_mapping="LLM05",
    ),
    SecurityRule(
        name="Block Deprecated Models",
        category="Model Governance",
        action="block",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Prevents use of deprecated or insecure model versions",
        owasp_mapping="LLM05",
    ),
    # LLM06 - Sensitive Information Disclosure
    SecurityRule(
        name="Block Secrets & API Keys",
        category="Data Exfiltration",
        action="block",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Detects and blocks API keys, passwords, tokens, and private keys",
        owasp_mapping="LLM06",
    ),
    SecurityRule(
        name="Detect PII",
        category="Data Exfiltration",
        action="warn",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Detects personally identifiable information (emails, SSN, phone numbers)",
        owasp_mapping="LLM06",
    ),
    SecurityRule(
        name="Warn on Data Exfiltration",
        category="Data Exfiltration",
        action="warn",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Detects patterns indicating potential data exfiltration attempts",
        owasp_mapping="LLM06",
    ),
    # LLM08 - Excessive Agency
    SecurityRule(
        name="Feature Allowlisting",
        category="Excessive Agency",
        action="block",
        scope="application",
        mandatory=True,
        enabled=True,
        description="Restricts model capabilities to explicitly allowed features per application",
        owasp_mapping="LLM08",
    ),
    SecurityRule(
        name="Environment-based Restrictions",
        category="Excessive Agency",
        action="block",
        scope="environment",
        mandatory=True,
        enabled=True,
        description="Enforces stricter controls in production environments",
        owasp_mapping="LLM08",
    ),
    # LLM09 - Overreliance
    SecurityRule(
        name="Output Validation & Warnings",
        category="Overreliance Prevention",
        action="warn",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Validates outputs and warns on potentially unsafe responses",
        owasp_mapping="LLM09",
    ),
    # LLM10 - Model Theft
    SecurityRule(
        name="Abuse Detection",
        category="Model Theft Prevention",
        action="block",
        scope="global",
        mandatory=True,
        enabled=True,
        description="Detects suspicious patterns that may indicate model extraction attempts",
        owasp_mapping="LLM10",
    ),
]


OWASP_ALIGNMENT = [
    OWASPAlignment(
        id="LLM01",
        name="Prompt Injection",
        status="full",
        coverage="Direct & indirect injection detection with blocking",
    ),
    OWASPAlignment(
        id="LLM02",
        name="Insecure Output Handling",
        status="full",
        coverage="JSON schema validation, size limits, content filtering",
    ),
    OWASPAlignment(
        id="LLM03",
        name="Training Data Poisoning",
        status="na",
        coverage="Out of scope for API gateway (provider responsibility)",
    ),
    OWASPAlignment(
        id="LLM04",
        name="Model Denial of Service",
        status="full",
        coverage="Rate limiting, retry storm detection, token limits, budget controls",
    ),
    OWASPAlignment(
        id="LLM05",
        name="Supply Chain Vulnerabilities",
        status="partial",
        coverage="Model whitelisting, deprecated model blocking",
    ),
    OWASPAlignment(
        id="LLM06",
        name="Sensitive Information Disclosure",
        status="full",
        coverage="Secrets detection, PII detection, exfiltration warnings",
    ),
    OWASPAlignment(
        id="LLM07",
        name="Insecure Plugin Design",
        status="na",
        coverage="Not applicable (no plugin execution)",
    ),
    OWASPAlignment(
        id="LLM08",
        name="Excessive Agency",
        status="full",
        coverage="Feature allowlisting, environment-based restrictions",
    ),
    OWASPAlignment(
        id="LLM09",
        name="Overreliance",
        status="partial",
        coverage="Output validation and explainability features",
    ),
    OWASPAlignment(
        id="LLM10",
        name="Model Theft",
        status="partial",
        coverage="Rate limiting and abuse detection patterns",
    ),
]


# ============================================================================
# Endpoints
# ============================================================================


@router.get("/report", response_model=SecurityReport)
async def get_security_report(
    period: str = "7d",
    db: AsyncSession = Depends(get_db),
):
    """
    Get complete security report.

    Shows:
    - Overall security posture
    - Threats blocked
    - Active security rules
    - OWASP alignment
    """
    # Calculate time range
    days = int(period.replace("d", ""))
    start_date = datetime.utcnow() - timedelta(days=days)

    # Get blocked requests
    result = await db.execute(
        select(func.count(LLMRequestTrace.id))
        .where(LLMRequestTrace.decision == TraceDecision.BLOCK)
        .where(LLMRequestTrace.timestamp_start >= start_date)
    )
    total_blocked = result.scalar() or 0

    # Get all blocked requests to analyze decision_reasons
    blocked_traces = await db.execute(
        select(LLMRequestTrace)
        .where(LLMRequestTrace.decision == TraceDecision.BLOCK)
        .where(LLMRequestTrace.timestamp_start >= start_date)
    )

    by_category = {}
    for trace in blocked_traces.scalars():
        # Parse category from decision_reasons (JSON array)
        reasons = trace.decision_reasons if trace.decision_reasons else []
        for reason in reasons:
            reason_str = str(reason).lower()
            if "injection" in reason_str:
                category = "Prompt Injection"
            elif "secret" in reason_str or "key" in reason_str or "pii" in reason_str:
                category = "Data Exfiltration"
            elif "token" in reason_str or "budget" in reason_str:
                category = "DoS Prevention"
            elif "model" in reason_str:
                category = "Model Governance"
            else:
                category = "Other"

            by_category[category] = by_category.get(category, 0) + 1
            break  # Count each trace only once

    # Get daily trend for last 7 days
    trend_7d = []
    for i in range(7):
        day_start = datetime.utcnow().replace(
            hour=0, minute=0, second=0, microsecond=0
        ) - timedelta(days=6 - i)
        day_end = day_start + timedelta(days=1)

        result = await db.execute(
            select(func.count(LLMRequestTrace.id))
            .where(LLMRequestTrace.decision == TraceDecision.BLOCK)
            .where(LLMRequestTrace.timestamp_start >= day_start)
            .where(LLMRequestTrace.timestamp_start < day_end)
        )
        count = result.scalar() or 0

        trend_7d.append(
            {
                "date": day_start.strftime("%Y-%m-%d"),
                "blocked": count,
            }
        )

    # Build security posture
    active_rules = len([r for r in DEFAULT_SECURITY_RULES if r.enabled])
    total_rules = len(DEFAULT_SECURITY_RULES)

    posture = SecurityPosture(
        status="protected",
        message="All critical protections are enabled",
        active_rules=active_rules,
        total_rules=total_rules,
        owasp_coverage=OWASP_ALIGNMENT,
    )

    threats = ThreatMetrics(
        total_blocked=total_blocked,
        by_category=by_category,
        trend_7d=trend_7d,
    )

    return SecurityReport(
        posture=posture,
        threats=threats,
        rules=DEFAULT_SECURITY_RULES,
    )


@router.get("/posture", response_model=SecurityPosture)
async def get_security_posture():
    """Get security posture summary."""
    active_rules = len([r for r in DEFAULT_SECURITY_RULES if r.enabled])
    total_rules = len(DEFAULT_SECURITY_RULES)

    return SecurityPosture(
        status="protected",
        message="All critical protections are enabled",
        active_rules=active_rules,
        total_rules=total_rules,
        owasp_coverage=OWASP_ALIGNMENT,
    )


@router.get("/rules", response_model=list[SecurityRule])
async def get_security_rules():
    """Get all security rules (read-only in V1)."""
    return DEFAULT_SECURITY_RULES


@router.get("/threats", response_model=ThreatMetrics)
async def get_threat_metrics(
    period: str = "7d",
    db: AsyncSession = Depends(get_db),
):
    """Get threat blocking metrics."""
    days = int(period.replace("d", ""))
    start_date = datetime.utcnow() - timedelta(days=days)

    # Total blocked
    result = await db.execute(
        select(func.count(LLMRequestTrace.id))
        .where(LLMRequestTrace.decision == TraceDecision.BLOCK)
        .where(LLMRequestTrace.timestamp_start >= start_date)
    )
    total_blocked = result.scalar() or 0

    # By category
    blocked_traces = await db.execute(
        select(LLMRequestTrace)
        .where(LLMRequestTrace.decision == TraceDecision.BLOCK)
        .where(LLMRequestTrace.timestamp_start >= start_date)
    )

    by_category = {}
    for trace in blocked_traces.scalars():
        reasons = trace.decision_reasons if trace.decision_reasons else []
        for reason in reasons:
            reason_str = str(reason).lower()
            if "injection" in reason_str:
                category = "Prompt Injection"
            elif "secret" in reason_str or "key" in reason_str or "pii" in reason_str:
                category = "Data Exfiltration"
            elif "token" in reason_str or "budget" in reason_str:
                category = "DoS Prevention"
            elif "model" in reason_str:
                category = "Model Governance"
            else:
                category = "Other"

            by_category[category] = by_category.get(category, 0) + 1
            break

    # Trend
    trend_7d = []
    for i in range(7):
        day_start = datetime.utcnow().replace(
            hour=0, minute=0, second=0, microsecond=0
        ) - timedelta(days=6 - i)
        day_end = day_start + timedelta(days=1)

        result = await db.execute(
            select(func.count(LLMRequestTrace.id))
            .where(LLMRequestTrace.decision == TraceDecision.BLOCK)
            .where(LLMRequestTrace.timestamp_start >= day_start)
            .where(LLMRequestTrace.timestamp_start < day_end)
        )
        count = result.scalar() or 0

        trend_7d.append(
            {
                "date": day_start.strftime("%Y-%m-%d"),
                "blocked": count,
            }
        )

    return ThreatMetrics(
        total_blocked=total_blocked,
        by_category=by_category,
        trend_7d=trend_7d,
    )