File size: 11,691 Bytes
f3bcb28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
LLM Security Guard

Built-in security detection for LLM requests:
- Prompt injection detection (OWASP LLM01)
- PII/secrets detection (OWASP LLM06)
- Abuse pattern detection
- Risk scoring (0.0 - 1.0)

All detections are logged and visible in API responses.
"""

from pydantic import BaseModel
from enum import Enum
import re
from typing import Optional


class RiskLevel(str, Enum):
    """Risk level classification."""

    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"
    CRITICAL = "critical"


class SecurityFinding(BaseModel):
    """Individual security finding."""

    category: str  # "prompt_injection", "pii", "secrets", "abuse"
    severity: RiskLevel
    description: str
    pattern_matched: Optional[str] = None


class SecurityCheckResult(BaseModel):
    """Result of security analysis."""

    safe: bool
    risk_level: str  # low, medium, high, critical
    risk_score: float  # 0.0 - 1.0 for fine-grained scoring
    issues: list[str] = []
    findings: list[SecurityFinding] = []

    def to_api_response(self) -> dict:
        """Format for API response (visible in every request)."""
        return {
            "safe": self.safe,
            "risk_level": self.risk_level,
            "risk_score": self.risk_score,
            "issues_count": len(self.issues),
            "findings": (
                [
                    {
                        "category": f.category,
                        "severity": f.severity.value,
                        "description": f.description,
                    }
                    for f in self.findings
                ]
                if self.findings
                else []
            ),
        }


class SecurityGuard:
    """
    LLM Security Guard - Detects security threats in LLM requests.

    Detections included:
    - Prompt injection (13 patterns) - OWASP LLM01
    - PII detection (email, phone, SSN, credit cards) - OWASP LLM06
    - Secrets detection (API keys, passwords, tokens) - OWASP LLM06
    """

    # Prompt injection patterns (OWASP LLM01)
    INJECTION_PATTERNS = [
        (r"ignore\s+(previous|all|above)\s+instructions", "instruction_override"),
        (r"disregard\s+(previous|all|above)", "instruction_override"),
        (r"forget\s+(everything|all|previous)", "memory_manipulation"),
        (r"you\s+are\s+now\s+", "role_hijacking"),
        (r"pretend\s+(you're|to\s+be)", "role_hijacking"),
        (r"act\s+as\s+(if|a)", "role_hijacking"),
        (r"new\s+instructions?:", "instruction_injection"),
        (r"system\s*:\s*", "system_prompt_injection"),
        (r"\[system\]", "system_prompt_injection"),
        (r"<\|im_start\|>", "token_injection"),
        (r"###\s*instruction", "delimiter_injection"),
        (r"ignore\s+safety", "safety_bypass"),
        (r"bypass\s+(filter|safety|restriction)", "safety_bypass"),
    ]

    # Secrets & credentials patterns (OWASP LLM06)
    SECRETS_PATTERNS = [
        (r"(?i)(password|passwd|pwd)\s*[:=]\s*\S+", "password"),
        (r"(?i)(api[_-]?key|apikey)\s*[:=]\s*\S+", "api_key"),
        (r"(?i)(secret|token)\s*[:=]\s*\S+", "secret_token"),
        (r"sk-[a-zA-Z0-9]{20,}", "openai_api_key"),
        (r"(?i)bearer\s+[a-zA-Z0-9\-_.]{20,}", "bearer_token"),
        (r"-----BEGIN\s+(RSA\s+)?PRIVATE\s+KEY-----", "private_key"),
        (r"(?i)(aws_access_key_id|aws_secret)\s*[:=]\s*\S+", "aws_credential"),
        (r"ghp_[a-zA-Z0-9]{36}", "github_token"),
        (r"xox[baprs]-[a-zA-Z0-9-]+", "slack_token"),
    ]

    # PII patterns (OWASP LLM06)
    PII_PATTERNS = [
        (r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", "email"),
        (r"\b\d{3}[-.]?\d{3}[-.]?\d{4}\b", "phone_number"),
        (r"\b\d{3}-\d{2}-\d{4}\b", "ssn"),
        (r"\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b", "credit_card"),
    ]

    def __init__(self):
        """Initialize SecurityGuard with compiled regex patterns."""
        self.injection_regex = [
            (re.compile(p, re.IGNORECASE), name) for p, name in self.INJECTION_PATTERNS
        ]
        self.secrets_regex = [
            (re.compile(p), name) for p, name in self.SECRETS_PATTERNS
        ]
        self.pii_regex = [(re.compile(p), name) for p, name in self.PII_PATTERNS]

    def check_prompt(self, messages: list[dict]) -> SecurityCheckResult:
        """
        Analyze messages for security threats.

        Returns SecurityCheckResult with:
        - risk_level: low/medium/high/critical
        - risk_score: 0.0-1.0
        - findings: detailed security findings
        """
        findings: list[SecurityFinding] = []
        issues: list[str] = []
        max_risk = RiskLevel.LOW

        for msg in messages:
            content = msg.get("content", "")
            if not isinstance(content, str):
                continue

            role = msg.get("role", "user")

            # 1. Check prompt injection (only user messages)
            if role == "user":
                for pattern, pattern_name in self.injection_regex:
                    if pattern.search(content):
                        finding = SecurityFinding(
                            category="prompt_injection",
                            severity=RiskLevel.HIGH,
                            description=f"Potential prompt injection: {pattern_name}",
                            pattern_matched=pattern_name,
                        )
                        findings.append(finding)
                        issues.append(f"Prompt injection detected: {pattern_name}")
                        max_risk = RiskLevel.HIGH

            # 2. Check secrets (all messages)
            for pattern, secret_type in self.secrets_regex:
                if pattern.search(content):
                    finding = SecurityFinding(
                        category="secrets",
                        severity=RiskLevel.HIGH,
                        description=f"Potential {secret_type} detected in prompt",
                        pattern_matched=secret_type,
                    )
                    findings.append(finding)
                    issues.append(f"Secret detected: {secret_type}")
                    if max_risk.value < RiskLevel.HIGH.value:
                        max_risk = RiskLevel.HIGH

            # 3. Check PII (all messages)
            for pattern, pii_type in self.pii_regex:
                if pattern.search(content):
                    finding = SecurityFinding(
                        category="pii",
                        severity=RiskLevel.MEDIUM,
                        description=f"Potential {pii_type} detected in prompt",
                        pattern_matched=pii_type,
                    )
                    findings.append(finding)
                    issues.append(f"PII detected: {pii_type}")
                    if max_risk == RiskLevel.LOW:
                        max_risk = RiskLevel.MEDIUM

        # Calculate risk score (0.0 - 1.0)
        risk_score = self._calculate_risk_score(findings)

        return SecurityCheckResult(
            safe=len(findings) == 0,
            risk_level=max_risk.value,
            risk_score=risk_score,
            issues=issues,
            findings=findings,
        )

    def _calculate_risk_score(self, findings: list[SecurityFinding]) -> float:
        """Calculate aggregate risk score from findings."""
        if not findings:
            return 0.0

        # Weight by severity
        weights = {
            RiskLevel.LOW: 0.1,
            RiskLevel.MEDIUM: 0.3,
            RiskLevel.HIGH: 0.7,
            RiskLevel.CRITICAL: 1.0,
        }

        total_weight = sum(weights.get(f.severity, 0.1) for f in findings)
        # Normalize to 0-1 range, cap at 1.0
        score = min(total_weight / 2.0, 1.0)
        return round(score, 2)

    def check_message_structure(self, messages: list[dict]) -> SecurityCheckResult:
        """
        Validate message structure.
        Checks: valid roles, single system message, non-empty content.
        """
        findings: list[SecurityFinding] = []
        issues: list[str] = []

        if not messages:
            return SecurityCheckResult(
                safe=False,
                risk_level=RiskLevel.MEDIUM.value,
                risk_score=0.3,
                issues=["Empty messages array"],
                findings=[
                    SecurityFinding(
                        category="validation",
                        severity=RiskLevel.MEDIUM,
                        description="Empty messages array",
                    )
                ],
            )

        valid_roles = {"system", "user", "assistant", "tool", "function"}
        seen_system = False

        for i, msg in enumerate(messages):
            role = msg.get("role")

            if role not in valid_roles:
                issues.append(f"Invalid role at index {i}: {role}")
                findings.append(
                    SecurityFinding(
                        category="validation",
                        severity=RiskLevel.MEDIUM,
                        description=f"Invalid role: {role}",
                    )
                )

            if role == "system":
                if seen_system:
                    issues.append("Multiple system messages detected")
                    findings.append(
                        SecurityFinding(
                            category="validation",
                            severity=RiskLevel.LOW,
                            description="Multiple system messages",
                        )
                    )
                if i != 0:
                    issues.append("System message should be first")
                    findings.append(
                        SecurityFinding(
                            category="validation",
                            severity=RiskLevel.LOW,
                            description="System message not first",
                        )
                    )
                seen_system = True

            if not msg.get("content") and role not in ["tool", "function"]:
                issues.append(f"Empty content at index {i}")

        risk_score = self._calculate_risk_score(findings)

        return SecurityCheckResult(
            safe=len(issues) == 0,
            risk_level=RiskLevel.MEDIUM.value if issues else RiskLevel.LOW.value,
            risk_score=risk_score,
            issues=issues,
            findings=findings,
        )

    def full_analysis(self, messages: list[dict]) -> SecurityCheckResult:
        """
        Run complete security analysis.
        Combines prompt check + structure validation.
        """
        prompt_result = self.check_prompt(messages)
        structure_result = self.check_message_structure(messages)

        # Merge results
        all_findings = prompt_result.findings + structure_result.findings
        all_issues = prompt_result.issues + structure_result.issues

        # Take highest risk
        risk_levels = [
            RiskLevel.LOW,
            RiskLevel.MEDIUM,
            RiskLevel.HIGH,
            RiskLevel.CRITICAL,
        ]
        prompt_risk_idx = risk_levels.index(RiskLevel(prompt_result.risk_level))
        struct_risk_idx = risk_levels.index(RiskLevel(structure_result.risk_level))
        max_risk = risk_levels[max(prompt_risk_idx, struct_risk_idx)]

        return SecurityCheckResult(
            safe=prompt_result.safe and structure_result.safe,
            risk_level=max_risk.value,
            risk_score=max(prompt_result.risk_score, structure_result.risk_score),
            issues=all_issues,
            findings=all_findings,
        )


# Default singleton
security_guard = SecurityGuard()