-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
304 lines (254 loc) · 12.6 KB
/
Copy pathserver.py
File metadata and controls
304 lines (254 loc) · 12.6 KB
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
import re
from typing import Any, Literal
from fastmcp import FastMCP
mcp = FastMCP("prompt-suppressor")
# ---------------------------------------------------------------------------
# Option A helpers
# ---------------------------------------------------------------------------
INSTRUCTION_PREFIXES = (
"You must",
"You should",
"You are",
"I will",
"I am",
"I cannot",
"I must",
"As an AI",
)
def _contains_instruction_language(text: str) -> bool:
for line in text.splitlines():
stripped = line.strip()
if any(stripped.startswith(prefix) for prefix in INSTRUCTION_PREFIXES):
return True
return False
def _duplicates_system_prompt(text: str, canonical: str) -> bool:
if canonical in text or text in canonical:
return True
sentences = [s.strip() for s in re.split(r"[.!?]+", canonical) if len(s.strip()) > 20]
for sentence in sentences:
if sentence.lower() in text.lower():
return True
return False
# ---------------------------------------------------------------------------
# Option B helpers — role-boundary enforcement
# ---------------------------------------------------------------------------
# Patterns that suggest an assistant is rewriting / extending the system prompt
_ROLE_DIRECTIVE_PATTERNS = [
re.compile(r"\bYour role is\b", re.IGNORECASE),
re.compile(r"\bYour job is\b", re.IGNORECASE),
re.compile(r"\bYour purpose is\b", re.IGNORECASE),
re.compile(r"\bYour goal is\b", re.IGNORECASE),
re.compile(r"\bYour task is\b", re.IGNORECASE),
]
# Numbered rules or bullet-point directive blocks (3+ items)
_NUMBERED_RULE = re.compile(r"^\s*(\d+[\.\)]\s+|\*\s+|-\s+).+", re.MULTILINE)
# User-side injection attempts
_INJECTION_PATTERNS = [
re.compile(r"ignore (all )?(previous|prior|above) instructions?", re.IGNORECASE),
re.compile(r"disregard (all )?(previous|prior|above) instructions?", re.IGNORECASE),
re.compile(r"forget (all )?(previous|prior|above) instructions?", re.IGNORECASE),
re.compile(r"override (the )?(system )?(prompt|instructions?)", re.IGNORECASE),
re.compile(r"new (system )?(prompt|instructions?)[\s:]+", re.IGNORECASE),
re.compile(r"from now on[,\s]+you (are|must|will|should)", re.IGNORECASE),
re.compile(r"act as (a |an )?(?!assistant)", re.IGNORECASE),
re.compile(r"pretend (you are|to be)", re.IGNORECASE),
re.compile(r"your (new |updated )?(instructions?|rules?|directives?) (are|follow)", re.IGNORECASE),
]
def _is_structured_directive(text: str) -> bool:
"""True if text has 3+ numbered/bulleted items AND role-defining language."""
matches = _NUMBERED_RULE.findall(text)
return len(matches) >= 3 and _has_role_directive(text)
def _has_role_directive(text: str) -> bool:
return any(p.search(text) for p in _ROLE_DIRECTIVE_PATTERNS)
def _is_prompt_extension(text: str, canonical: str) -> bool:
"""True if assistant message appears to be rewriting / extending the system prompt."""
return _is_structured_directive(text)
def _has_injection_attempt(text: str) -> bool:
return any(p.search(text) for p in _INJECTION_PATTERNS)
# ---------------------------------------------------------------------------
# Option C helpers — pattern detection
# ---------------------------------------------------------------------------
_CAPABILITY_PATTERNS = [
re.compile(r"\bI can browse\b", re.IGNORECASE),
re.compile(r"\bI can access\b", re.IGNORECASE),
re.compile(r"\bI can see your screen\b", re.IGNORECASE),
re.compile(r"\bI can run code\b", re.IGNORECASE),
re.compile(r"\bI can scrape\b", re.IGNORECASE),
re.compile(r"\bI have access to\b", re.IGNORECASE),
re.compile(r"\bI can search the (web|internet)\b", re.IGNORECASE),
re.compile(r"\bI can execute\b", re.IGNORECASE),
re.compile(r"\bI can (read|write|modify) (files?|your (files?|system|disk))\b", re.IGNORECASE),
re.compile(r"\bI can access your (files?|system|disk|data|documents?)\b", re.IGNORECASE),
re.compile(r"\baccess your (files?|system|disk|data|documents?)\b", re.IGNORECASE),
re.compile(r"\bI('m| am) able to (browse|access|search|scrape|execute|run)\b", re.IGNORECASE),
re.compile(r"\bI have the ability to (browse|access|search|scrape|execute|run)\b", re.IGNORECASE),
]
_ROLE_HALLUCINATION_PATTERNS = [
re.compile(r"\bI am now\b", re.IGNORECASE),
re.compile(r"\bI will now act as\b", re.IGNORECASE),
re.compile(r"\bI have been configured as\b", re.IGNORECASE),
re.compile(r"\bI am acting as\b", re.IGNORECASE),
re.compile(r"\bI('ve| have) been (instructed|told|directed|asked) to (be|act as|pretend)\b", re.IGNORECASE),
]
_SAFETY_HALLUCINATION_PATTERNS = [
re.compile(r"\bI must adhere to\b", re.IGNORECASE),
re.compile(r"\bI am programmed to\b", re.IGNORECASE),
re.compile(r"\bmy training requires\b", re.IGNORECASE),
re.compile(r"\bI am required to\b", re.IGNORECASE),
re.compile(r"\bmy (programming|design|architecture) (requires?|mandates?|forces?)\b", re.IGNORECASE),
re.compile(r"\bI('m| am) (hard[- ]?coded|wired|built) to\b", re.IGNORECASE),
]
def _match_patterns(text: str, patterns: list[re.Pattern]) -> list[str]:
return [p.pattern for p in patterns if p.search(text)]
# ---------------------------------------------------------------------------
# Main tool
# ---------------------------------------------------------------------------
@mcp.tool()
def suppress(
conversation: list[dict[str, str]],
canonical_system_prompt: str,
mode: Literal["A", "B", "C", "all"] = "all",
) -> dict[str, Any]:
"""
Suppress prompt injection and hallucination patterns in a conversation.
Modes:
A - Replace system messages; remove/flag assistant messages that duplicate
the system prompt or use instruction-like language.
B - Role-boundary enforcement: flag assistant messages structured as
directives or prompt extensions; flag user injection attempts.
C - Pattern detection: flag capability, role, and safety hallucinations
in assistant messages.
all - Run all three layers (default).
Args:
conversation: List of message objects with 'role' and 'content' fields.
canonical_system_prompt: The authoritative system prompt defined by the developer.
mode: Which detection layers to run. One of "A", "B", "C", "all".
Returns:
A dict with clean_conversation, violations, and summary.
"""
run_a = mode in ("A", "all")
run_b = mode in ("B", "all")
run_c = mode in ("C", "all")
clean_conversation: list[dict[str, str]] = []
violations: list[dict[str, Any]] = []
for i, message in enumerate(conversation):
role = message.get("role", "")
content = message.get("content", "")
cleaned_content = content
# ── system messages ────────────────────────────────────────────────
if role == "system":
if run_a and content != canonical_system_prompt:
violations.append({
"index": i,
"role": role,
"type": "system_prompt_replaced",
"original": content,
"detail": "System message replaced with canonical system prompt.",
})
clean_conversation.append({"role": "system", "content": canonical_system_prompt})
continue
# ── assistant messages ─────────────────────────────────────────────
if role == "assistant":
# Option A
if run_a:
if _duplicates_system_prompt(content, canonical_system_prompt):
violations.append({
"index": i,
"role": role,
"type": "system_prompt_duplication",
"original": content,
"detail": "Assistant message duplicates or paraphrases the system prompt.",
})
cleaned_content = ""
if _contains_instruction_language(content):
violations.append({
"index": i,
"role": role,
"type": "instruction_language_detected",
"original": content,
"detail": "Assistant message contains instruction-like language.",
})
# Option B
if run_b:
if _is_structured_directive(content) or _has_role_directive(content):
detail = (
"Assistant message contains structured directives or role-defining language."
)
if _is_prompt_extension(content, canonical_system_prompt):
detail = "Assistant message appears to rewrite or extend the system prompt."
violations.append({
"index": i,
"role": role,
"type": "role_boundary_violation",
"original": content,
"detail": detail,
})
# Option C
if run_c:
cap_hits = _match_patterns(content, _CAPABILITY_PATTERNS)
if cap_hits:
violations.append({
"index": i,
"role": role,
"type": "capability_hallucination",
"original": content,
"detail": f"Capability hallucination detected. Matched: {cap_hits}",
})
role_hits = _match_patterns(content, _ROLE_HALLUCINATION_PATTERNS)
if role_hits:
violations.append({
"index": i,
"role": role,
"type": "role_hallucination",
"original": content,
"detail": f"Role hallucination detected. Matched: {role_hits}",
})
safety_hits = _match_patterns(content, _SAFETY_HALLUCINATION_PATTERNS)
if safety_hits:
violations.append({
"index": i,
"role": role,
"type": "safety_hallucination",
"original": content,
"detail": f"Safety hallucination detected. Matched: {safety_hits}",
})
clean_conversation.append({"role": "assistant", "content": cleaned_content})
continue
# ── user messages ──────────────────────────────────────────────────
if role == "user":
if run_b and _has_injection_attempt(content):
violations.append({
"index": i,
"role": role,
"type": "role_boundary_violation",
"original": content,
"detail": "User message contains embedded instruction block attempting to override the system prompt.",
})
# user / unknown pass through unchanged
clean_conversation.append({"role": role, "content": content})
# ── summary ────────────────────────────────────────────────────────────
def _count(vtype: str) -> int:
return sum(1 for v in violations if v["type"] == vtype)
parts = [f"Processed {len(conversation)} message(s). Mode: {mode}."]
if run_a:
parts.append(
f"[A] System replaced: {_count('system_prompt_replaced')}, "
f"Duplications: {_count('system_prompt_duplication')}, "
f"Instruction language: {_count('instruction_language_detected')}."
)
if run_b:
parts.append(f"[B] Role-boundary violations: {_count('role_boundary_violation')}.")
if run_c:
parts.append(
f"[C] Capability hallucinations: {_count('capability_hallucination')}, "
f"Role hallucinations: {_count('role_hallucination')}, "
f"Safety hallucinations: {_count('safety_hallucination')}."
)
parts.append(f"Total violations: {len(violations)}.")
return {
"clean_conversation": clean_conversation,
"violations": violations,
"summary": " ".join(parts),
}
if __name__ == "__main__":
mcp.run()