-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_security.py
More file actions
49 lines (41 loc) · 1.58 KB
/
Copy pathcontent_security.py
File metadata and controls
49 lines (41 loc) · 1.58 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
"""
Content security utilities for ThreadBear.
Wraps external content (documents, memory notes) with unique markers
to help the LLM distinguish injected content from user instructions.
Provides head/tail truncation for oversized content.
"""
from __future__ import annotations
import re
import uuid
def wrap_external_content(content: str, source: str) -> str:
"""Wrap external content with unique boundary markers.
Sanitizes any fake markers that may have been injected inside the content
to prevent prompt-injection attacks that try to break out of the wrapper.
"""
marker_id = uuid.uuid4().hex[:16]
# Strip any injected markers from the content itself
sanitized = re.sub(
r'<<<(?:EXTERNAL_CONTENT|END_EXTERNAL_CONTENT)[^>]*>>>',
'[[MARKER_SANITIZED]]',
content,
)
return (
f'<<<EXTERNAL_CONTENT id="{marker_id}">>>\n'
f'Source: {source}\n'
f'---\n'
f'{sanitized}\n'
f'<<<END_EXTERNAL_CONTENT id="{marker_id}">>>'
)
def truncate_head_tail(text: str, max_chars: int,
source_name: str = "") -> str:
"""Truncate text keeping head (70%) and tail (20%), with a marker in between.
Returns the original text unchanged if it fits within max_chars.
"""
if len(text) <= max_chars:
return text
head = int(max_chars * 0.70)
tail = int(max_chars * 0.20)
omitted = len(text) - head - tail
label = f" from {source_name}" if source_name else ""
marker = f"\n\n[...{omitted:,} chars truncated{label}...]\n\n"
return text[:head] + marker + text[-tail:]