forked from stephenschoettler/hermes-lcm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathingest_protection.py
More file actions
1142 lines (1021 loc) · 40.2 KB
/
Copy pathingest_protection.py
File metadata and controls
1142 lines (1021 loc) · 40.2 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
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Storage-boundary protection for payloads that should not live inline in SQLite.
Hermes core may hand LCM messages that already contain inline media/base64
payloads. LCM remains lossless by externalizing those payload strings and
storing compact placeholders in ``messages.content`` / ``messages.tool_calls``.
This avoids duplicating large/binary-ish payloads into SQLite rows, FTS shadow
structures, WAL files, and backups while keeping recovery available through LCM
externalized-payload tools.
"""
from __future__ import annotations
import hashlib
import json
import logging
import re
from collections import Counter
from typing import Any, Dict, List
from .externalize import (
externalize_ingest_payload,
extract_externalized_ref,
find_externalized_payload_for_message,
is_externalized_placeholder,
load_externalized_payload,
maybe_externalize_payload,
)
from .message_content import normalize_content_value
logger = logging.getLogger(__name__)
_MEDIA_TYPE_HINTS = ("image", "audio", "video")
_MEDIA_VALUE_KEYS = (
"image_url",
"input_image",
"output_image",
"audio_url",
"video_url",
"image",
"audio",
"video",
)
def _contains_media_payload(value: Any) -> bool:
if value is None:
return False
if isinstance(value, str):
return bool(_DATA_URI_BASE64_RE.search(value))
if isinstance(value, list):
return any(_contains_media_payload(item) for item in value)
if isinstance(value, dict):
block_type = str(value.get("type") or "").lower()
if any(hint in block_type for hint in _MEDIA_TYPE_HINTS):
return True
for key, nested in value.items():
key_text = str(key).lower()
if key_text in _MEDIA_VALUE_KEYS:
return True
if _contains_media_payload(nested):
return True
return False
def _externalization_kind_for_message(message: Dict[str, Any]) -> str:
role = str(message.get("role") or "unknown")
if role == "tool":
return "tool_result"
if _contains_media_payload(message.get("content")):
return "media_payload"
return "raw_payload"
# Any data URI base64 payload, not just image/audio/video. Keep the trailing
# payload alphabet conservative so we do not slurp surrounding JSON/markdown.
# Raw scans can see JSON-escaped slashes before decoding, including both `\/`
# and unicode escapes such as `\u002f` in duplicate-key argument strings.
_JSON_ESCAPED_SLASH_RE = r"(?:/|\\/|\\u002[fF])"
_DATA_URI_BASE64_RE = re.compile(
rf"data:(?:[A-Za-z0-9.+-]|{_JSON_ESCAPED_SLASH_RE})*"
rf"(?:;[A-Za-z0-9_.+%-]+=(?:[-A-Za-z0-9_.+%]|{_JSON_ESCAPED_SLASH_RE})*)*"
rf";base64,(?:[A-Za-z0-9+=]|{_JSON_ESCAPED_SLASH_RE}){{256,}}(?=$|[^A-Za-z0-9+/=])",
re.IGNORECASE,
)
_BASE64_RUN_RE = re.compile(r"(?<![A-Za-z0-9+/=_-])([A-Za-z0-9+/=_-]{4096,})(?![A-Za-z0-9+/=_-])")
_BASE64_ALPHABET_RE = re.compile(r"^[A-Za-z0-9+/=_\s-]+$")
_EXTERNALIZED_PLACEHOLDER_PREFIX = "[Externalized LCM ingest payload:"
_QUARANTINED_ASSISTANT_KIND = "quarantined_assistant_output"
_QUARANTINED_ASSISTANT_REASON = "high_repetition"
_QUARANTINED_ASSISTANT_MIN_CHARS = 65_536
_QUARANTINED_ASSISTANT_MIN_TOKENS = 1_000
_WORD_TOKEN_RE = re.compile(r"[A-Za-z0-9_]+")
_REPETITION_SEGMENT_SPLIT_RE = re.compile(r"(?:\n+|(?<=[.!?])\s+)")
_GENERIC_BASE64_MIN_CHARS = 4096
_INGEST_PLACEHOLDER_RE = re.compile(r"\[Externalized LCM ingest payload:.*?;\s*ref=([^;\]\s]+)\]")
_SENSITIVE_PLACEHOLDER_PREFIX = "[LCM sensitive redaction:"
_SENSITIVE_PATTERN_CATALOG: dict[str, re.Pattern[str]] = {
"api_key": re.compile(
r"(?P<prefix>\b(?:api[_-]?key|api[_-]?token|access[_-]?token|secret[_-]?key|client[_-]?secret)\b\s*[\"']?\s*[:=]\s*[\"']?)"
r"(?P<secret>[A-Za-z0-9._~+/=-]{12,})"
r"(?P<suffix>[\"']?)",
re.IGNORECASE,
),
"bearer_token": re.compile(
r"(?P<prefix>\bBearer\s+)"
r"(?P<secret>[A-Za-z0-9._~+/=-]{12,})",
re.IGNORECASE,
),
"password_assignment": re.compile(
r"(?P<prefix>\b(?:password|passwd|pwd|passphrase)\b\s*[\"']?\s*[:=]\s*)"
r"(?:(?P<quote>[\"'])(?P<secret_quoted>[^\r\n\]\}]{6,}?)(?P=quote)|"
r"(?P<secret_unquoted>[^\s,\"'\]}]{6,}))",
re.IGNORECASE,
),
"private_key": re.compile(
r"-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----.*?-----END [A-Z0-9 ]*PRIVATE KEY-----",
re.IGNORECASE | re.DOTALL,
),
}
def is_externalized_ingest_placeholder(text: str) -> bool:
return isinstance(text, str) and bool(_INGEST_PLACEHOLDER_RE.fullmatch(text.strip()))
def contains_data_uri_base64(text: str) -> bool:
return isinstance(text, str) and bool(_DATA_URI_BASE64_RE.search(text))
def contains_long_base64_run(text: str, *, min_chars: int = _GENERIC_BASE64_MIN_CHARS) -> bool:
if not isinstance(text, str) or len(text) < min_chars:
return False
return any(looks_like_long_base64(match.group(1), min_chars=min_chars) for match in _BASE64_RUN_RE.finditer(text))
def extract_ingest_externalized_refs(text: str) -> list[str]:
if not isinstance(text, str) or not text:
return []
refs: list[str] = []
for match in _INGEST_PLACEHOLDER_RE.finditer(text):
ref = match.group(1).strip()
if ref and ref not in refs:
refs.append(ref)
return refs
def sensitive_pattern_status(config) -> dict[str, Any]:
"""Return metadata-only status for opt-in sensitive redaction."""
configured, active, unknown = _configured_sensitive_pattern_names(config)
enabled = bool(getattr(config, "sensitive_patterns_enabled", False))
return {
"sensitive_patterns_enabled": enabled,
"enabled": enabled,
"sensitive_patterns": configured,
"patterns": configured,
"active_patterns": active if enabled else [],
"unknown_patterns": unknown,
"source": getattr(config, "sensitive_patterns_source", "default"),
"placeholder_format": "[LCM sensitive redaction: name=<pattern>; chars=<n>; bytes=<n>; sha256=<16 for non-password>]",
"lossless_recovery": False if enabled and active else None,
}
def _configured_sensitive_pattern_names(config) -> tuple[list[str], list[str], list[str]]:
raw = getattr(config, "sensitive_patterns", []) or []
if isinstance(raw, str):
names = [part.strip() for part in raw.split(",") if part.strip()]
else:
names = [str(part).strip() for part in raw if str(part).strip()]
if not names:
return [], [], []
configured: list[str] = []
active: list[str] = []
unknown: list[str] = []
for name in names:
normalized = name.lower().strip()
if normalized in {"all", "default"}:
for catalog_name in _SENSITIVE_PATTERN_CATALOG:
if catalog_name not in configured:
configured.append(catalog_name)
if catalog_name not in active:
active.append(catalog_name)
continue
configured.append(normalized)
if normalized in _SENSITIVE_PATTERN_CATALOG:
if normalized not in active:
active.append(normalized)
elif normalized not in unknown:
unknown.append(normalized)
return configured, active, unknown
def _active_sensitive_pattern_names(config) -> list[str]:
if not bool(getattr(config, "sensitive_patterns_enabled", False)):
return []
_configured, active, _unknown = _configured_sensitive_pattern_names(config)
return active
def _sensitive_placeholder(pattern_name: str, secret: str) -> str:
parts = [
f"{_SENSITIVE_PLACEHOLDER_PREFIX} "
f"name={_safe_placeholder_metadata(pattern_name)}; "
f"chars={len(secret)}; bytes={len(secret.encode('utf-8', errors='surrogatepass'))}"
]
if pattern_name != "password_assignment":
digest = hashlib.sha256(secret.encode("utf-8", errors="surrogatepass")).hexdigest()[:16]
parts.append(f"sha256={digest}")
return "; ".join(parts) + "]"
def _redact_match(pattern_name: str, match: re.Match[str]) -> str:
group_names = match.re.groupindex
secret_group = None
for candidate in ("secret", "secret_quoted", "secret_unquoted"):
if candidate in group_names and match.groupdict().get(candidate) is not None:
secret_group = candidate
break
if secret_group is None:
return _sensitive_placeholder(pattern_name, match.group(0))
secret = match.group(secret_group)
relative_start = match.start(secret_group) - match.start(0)
relative_end = match.end(secret_group) - match.start(0)
full = match.group(0)
return full[:relative_start] + _sensitive_placeholder(pattern_name, secret) + full[relative_end:]
def _sensitive_pattern_for_key(key: Any, active_names: list[str]) -> str | None:
if not isinstance(key, str):
return None
normalized = re.sub(r"[^a-z0-9]+", "_", key.lower()).strip("_")
compact = normalized.replace("_", "")
if "api_key" in active_names and (
compact in {"apikey", "apitoken", "accesstoken", "secretkey", "clientsecret"}
or ("api" in normalized and "key" in normalized)
or ("access" in normalized and "token" in normalized)
or ("secret" in normalized and "key" in normalized)
):
return "api_key"
if "bearer_token" in active_names and compact in {"authorization", "authtoken", "bearertoken", "token"}:
return "bearer_token"
if "password_assignment" in active_names and compact in {"password", "passwd", "pwd", "passphrase"}:
return "password_assignment"
return None
def redact_sensitive_text(text: str, config) -> str:
"""Replace configured sensitive spans with deterministic placeholders."""
if not isinstance(text, str) or not text:
return text
active_names = _active_sensitive_pattern_names(config)
if not active_names:
return text
protected = text
for name in active_names:
pattern = _SENSITIVE_PATTERN_CATALOG[name]
protected = pattern.sub(lambda match, pattern_name=name: _redact_match(pattern_name, match), protected)
return protected
def _redact_entire_sensitive_string(text: str, pattern_name: str) -> str:
if not text or _SENSITIVE_PLACEHOLDER_PREFIX in text:
return text
return _sensitive_placeholder(pattern_name, text)
def redact_sensitive_value(value: Any, config, *, parse_json_strings: bool = False) -> Any:
"""Recursively redact configured sensitive values without externalizing data."""
active_names = _active_sensitive_pattern_names(config)
if not active_names:
return value
if isinstance(value, dict):
protected: dict[Any, Any] = {}
for key, val in value.items():
protected_key = redact_sensitive_text(key, config) if isinstance(key, str) else key
key_pattern = _sensitive_pattern_for_key(key, active_names)
if key_pattern and isinstance(val, str):
text_redacted = redact_sensitive_text(val, config)
if text_redacted == val:
text_redacted = _redact_entire_sensitive_string(val, key_pattern)
protected[protected_key] = text_redacted
else:
protected[protected_key] = redact_sensitive_value(
val,
config,
parse_json_strings=parse_json_strings,
)
return protected
if isinstance(value, list):
return [redact_sensitive_value(item, config, parse_json_strings=parse_json_strings) for item in value]
if not isinstance(value, str):
return value
if parse_json_strings:
parsed = _maybe_parse_json_string(value)
if parsed is not None and not _json_has_duplicate_object_keys(value):
protected = redact_sensitive_value(parsed, config, parse_json_strings=True)
if protected != parsed:
return json.dumps(protected, ensure_ascii=False, separators=(",", ":"))
return redact_sensitive_text(value, config)
def _safe_placeholder_metadata(value: Any) -> str:
text = str(value or "?")
safe = re.sub(r"[^A-Za-z0-9_.:/-]+", "-", text).strip("-")
return (safe or "?")[:120]
def _normalized_repetition_segments(text: str) -> list[str]:
segments = []
for segment in _REPETITION_SEGMENT_SPLIT_RE.split(text):
normalized = re.sub(r"\s+", " ", segment.strip().lower())
if len(normalized) >= 32:
segments.append(normalized)
return segments
def assistant_output_quarantine_reason(text: str) -> str | None:
"""Return a quarantine reason for obviously broken assistant output.
The gate is intentionally conservative: content must be very large and show
both low token novelty and repeated sentence/line segments. Long diverse
reports and code with varied identifiers should stay inline.
"""
if not isinstance(text, str) or len(text) < _QUARANTINED_ASSISTANT_MIN_CHARS:
return None
normalized = re.sub(r"\s+", " ", text.strip().lower())
tokens = _WORD_TOKEN_RE.findall(normalized)
if len(tokens) < _QUARANTINED_ASSISTANT_MIN_TOKENS:
if len(normalized) >= _QUARANTINED_ASSISTANT_MIN_CHARS and len(set(normalized)) <= 12:
return _QUARANTINED_ASSISTANT_REASON
return None
token_counts = Counter(tokens)
unique_token_ratio = len(token_counts) / max(1, len(tokens))
top_token_ratio = token_counts.most_common(1)[0][1] / max(1, len(tokens))
segments = _normalized_repetition_segments(text)
top_segment_ratio = 0.0
duplicate_segment_ratio = 0.0
if len(segments) >= 20:
segment_counts = Counter(segments)
top_segment_ratio = segment_counts.most_common(1)[0][1] / len(segments)
duplicate_segment_ratio = 1.0 - (len(segment_counts) / len(segments))
if unique_token_ratio <= 0.03 and (
top_segment_ratio >= 0.10
or duplicate_segment_ratio >= 0.50
or top_token_ratio >= 0.08
):
return _QUARANTINED_ASSISTANT_REASON
# Covers degenerate long loops with little punctuation/newline structure.
if unique_token_ratio <= 0.015 and len(set(normalized)) <= 64:
return _QUARANTINED_ASSISTANT_REASON
return None
def _quarantined_assistant_placeholder(summary: Dict[str, Any], *, reason: str) -> str:
return (
"[Externalized LCM ingest payload: assistant output quarantined; "
f"kind={_safe_placeholder_metadata(summary.get('kind') or _QUARANTINED_ASSISTANT_KIND)}; "
f"reason={_safe_placeholder_metadata(reason)}; "
f"field={_safe_placeholder_metadata(summary.get('field_path') or 'content')}; "
f"chars={summary.get('content_chars', 0)}; bytes={summary.get('content_bytes', 0)}; "
f"ref={summary.get('ref', '')}]"
)
def _volatile_quarantined_assistant_placeholder(content: str, *, reason: str) -> str:
digest = hashlib.sha256(content.encode("utf-8")).hexdigest()[:16]
return (
"[LCM active replay placeholder: assistant output quarantined; "
f"kind={_QUARANTINED_ASSISTANT_KIND}; "
f"reason={_safe_placeholder_metadata(reason)}; "
"scope=ignored_message_pattern; field=content; "
f"chars={len(content)}; bytes={len(content.encode('utf-8'))}; "
f"sha256={digest}]"
)
def _externalize_quarantined_assistant_output(
content: str,
*,
role: str,
session_id: str,
config,
hermes_home: str,
reason: str,
) -> str | None:
existing = find_externalized_payload_for_message(
content,
session_id=session_id,
kind=_QUARANTINED_ASSISTANT_KIND,
role=role,
config=config,
hermes_home=hermes_home,
)
if existing is not None:
return _quarantined_assistant_placeholder(existing, reason=reason)
result = externalize_ingest_payload(
content,
role=role,
session_id=session_id,
field_path="content",
config=config,
hermes_home=hermes_home,
kind=_QUARANTINED_ASSISTANT_KIND,
)
if result is None:
logger.warning(
"LCM ingest protection could not quarantine repetitive assistant output; preserving inline content for lossless recovery"
)
return None
payload = result.get("payload") or {}
path = result.get("path")
summary = {
"ref": getattr(path, "name", ""),
"kind": payload.get("kind", _QUARANTINED_ASSISTANT_KIND),
"role": payload.get("role", role),
"field_path": payload.get("field_path", "content"),
"content_chars": payload.get("content_chars", len(content)),
"content_bytes": payload.get("content_bytes", len(content.encode("utf-8"))),
}
return _quarantined_assistant_placeholder(summary, reason=reason)
def _existing_quarantined_assistant_placeholder(
content: str,
*,
role: str,
session_id: str,
config,
hermes_home: str,
reason: str,
) -> str | None:
existing = find_externalized_payload_for_message(
content,
session_id=session_id,
kind=_QUARANTINED_ASSISTANT_KIND,
role=role,
config=config,
hermes_home=hermes_home,
)
if existing is None:
return None
return _quarantined_assistant_placeholder(existing, reason=reason)
def restore_ingest_payload_placeholders(
text: str,
*,
config,
hermes_home: str = "",
session_id: str = "",
) -> str:
"""Restore ingest placeholders in a stored identity string for matching only.
Missing or mismatched payload files leave the placeholder untouched so callers
never fabricate content or hide a recovery problem.
"""
if not isinstance(text, str) or _EXTERNALIZED_PLACEHOLDER_PREFIX not in text:
return text
def replace(match: re.Match[str]) -> str:
ref = match.group(1).strip()
payload = load_externalized_payload(ref, config=config, hermes_home=hermes_home)
if payload is None or payload.get("kind") != "ingest_payload":
return match.group(0)
payload_session_id = payload.get("session_id") or ""
if session_id and payload_session_id and payload_session_id != session_id:
return match.group(0)
content = payload.get("content")
return content if isinstance(content, str) else match.group(0)
return _INGEST_PLACEHOLDER_RE.sub(replace, text)
def looks_like_long_base64(text: str, *, min_chars: int = _GENERIC_BASE64_MIN_CHARS) -> bool:
"""Conservative long-base64 heuristic.
Avoids short hashes/IDs/JWT-ish snippets by requiring a very long run and a
high base64 alphabet ratio. PEM blocks and ordinary logs contain delimiters
or whitespace/headers that keep them from matching as one clean run.
"""
if not isinstance(text, str) or len(text) < min_chars:
return False
compact = "".join(text.split())
if len(compact) < min_chars:
return False
if len(compact) % 4 == 1:
return False
if not _BASE64_ALPHABET_RE.match(text):
return False
base64_chars = sum(1 for ch in text if ch.isalnum() or ch in "+/=_-")
ratio = base64_chars / max(1, len(text))
if ratio < 0.98:
return False
# Require at least a bit of mixed alphabet so a long log line of one
# repeated character is not treated as a binary payload.
return len(set(compact.rstrip("="))) >= 8
def _placeholder_for_payload(
payload: str,
*,
role: str,
session_id: str,
field_path: str,
config,
hermes_home: str,
) -> str | None:
result = externalize_ingest_payload(
payload,
role=role,
session_id=session_id,
field_path=field_path,
config=config,
hermes_home=hermes_home,
)
if result is None:
logger.warning(
"LCM ingest protection could not externalize payload at %s; preserving inline content for lossless recovery",
field_path,
)
return None
return result["placeholder"]
def _protect_payload_substrings(
text: str,
*,
role: str,
session_id: str,
field_path: str,
config,
hermes_home: str,
) -> str:
if not text or is_externalized_ingest_placeholder(text):
return text
def replace_data_uri(match: re.Match[str]) -> str:
payload = match.group(0)
return _placeholder_for_payload(
payload,
role=role,
session_id=session_id,
field_path=field_path,
config=config,
hermes_home=hermes_home,
) or payload
protected = _DATA_URI_BASE64_RE.sub(replace_data_uri, text)
def replace_base64_run(match: re.Match[str]) -> str:
payload = match.group(1)
if not looks_like_long_base64(payload):
return payload
return _placeholder_for_payload(
payload,
role=role,
session_id=session_id,
field_path=field_path,
config=config,
hermes_home=hermes_home,
) or payload
return _BASE64_RUN_RE.sub(replace_base64_run, protected)
def _maybe_parse_json_string(text: str) -> Any | None:
stripped = text.strip()
if not stripped or stripped[0] not in "[{":
return None
try:
parsed = json.loads(text)
except Exception:
return None
if isinstance(parsed, (dict, list)):
return parsed
return None
def _json_has_duplicate_object_keys(text: str) -> bool:
stripped = text.strip() if isinstance(text, str) else ""
if not stripped or stripped[0] not in "[{":
return False
duplicate = False
def detect_pairs(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
nonlocal duplicate
seen: set[str] = set()
result: dict[str, Any] = {}
for key, value in pairs:
if key in seen:
duplicate = True
seen.add(key)
result[key] = value
return result
try:
json.loads(text, object_pairs_hook=detect_pairs)
except Exception:
return False
return duplicate
def _dict_field_path(parent: str, key: Any) -> str:
component = str(key)
return f"{parent}.{component}" if parent else component
def _payload_key_field_path(parent: str) -> str:
return f"{parent}.<key>" if parent else "<key>"
def _protect_value(
value: Any,
*,
role: str,
session_id: str,
field_path: str,
config,
hermes_home: str,
parse_json_strings: bool = False,
) -> Any:
value = redact_sensitive_value(
value,
config,
parse_json_strings=parse_json_strings,
)
if isinstance(value, dict):
protected: dict[Any, Any] = {}
for key, val in value.items():
protected_key = (
_protect_payload_substrings(
key,
role=role,
session_id=session_id,
field_path=_payload_key_field_path(field_path),
config=config,
hermes_home=hermes_home,
)
if isinstance(key, str)
else key
)
child_path_key = "<key>" if protected_key != key else protected_key
protected[protected_key] = _protect_value(
val,
role=role,
session_id=session_id,
field_path=_dict_field_path(field_path, child_path_key),
config=config,
hermes_home=hermes_home,
parse_json_strings=parse_json_strings,
)
return protected
if isinstance(value, list):
return [
_protect_value(
item,
role=role,
session_id=session_id,
field_path=f"{field_path}[{idx}]",
config=config,
hermes_home=hermes_home,
parse_json_strings=parse_json_strings,
)
for idx, item in enumerate(value)
]
if not isinstance(value, str):
return value
if parse_json_strings:
if _json_has_duplicate_object_keys(value):
return _protect_payload_substrings(
value,
role=role,
session_id=session_id,
field_path=field_path,
config=config,
hermes_home=hermes_home,
)
parsed = _maybe_parse_json_string(value)
if parsed is not None:
canonical = json.dumps(parsed, ensure_ascii=False, separators=(",", ":"))
if canonical != value:
raw_protected = _protect_payload_substrings(
value,
role=role,
session_id=session_id,
field_path=field_path,
config=config,
hermes_home=hermes_home,
)
if raw_protected != value:
return raw_protected
protected = _protect_value(
parsed,
role=role,
session_id=session_id,
field_path=field_path,
config=config,
hermes_home=hermes_home,
parse_json_strings=True,
)
if protected != parsed:
return json.dumps(protected, ensure_ascii=False, separators=(",", ":"))
return value
return _protect_payload_substrings(
value,
role=role,
session_id=session_id,
field_path=field_path,
config=config,
hermes_home=hermes_home,
)
def protect_inline_payloads_in_text(
text: str,
*,
role: str,
session_id: str,
field_path: str,
config,
hermes_home: str,
) -> str:
"""Externalize inline media/base64 payloads inside a text scaffold.
This is used for non-SQLite active-context scaffolds that still must not
duplicate media-ish payloads into summaries or preserved objective text.
"""
if not isinstance(text, str):
return text
text = redact_sensitive_text(text, config)
return _protect_payload_substrings(
text,
role=role,
session_id=session_id,
field_path=field_path,
config=config,
hermes_home=hermes_home,
)
def _protect_tool_calls(tool_calls: Any, *, role: str, session_id: str, config, hermes_home: str) -> Any:
return _protect_value(
tool_calls,
role=role,
session_id=session_id,
field_path="tool_calls",
config=config,
hermes_home=hermes_home,
parse_json_strings=True,
)
def protect_message_for_ingest(
message: Dict[str, Any],
config,
hermes_home: str = "",
session_id: str = "",
) -> Dict[str, Any]:
"""Return a copy of ``message`` safe to persist in SQLite.
Payloads are externalized losslessly when they are inline media/base64-like
strings before they hit ``messages.content`` or ``messages.tool_calls``.
When the opt-in generic large-output externalization setting is enabled,
whole-message content still follows the existing threshold-based behavior.
"""
msg = dict(message or {})
role = str(msg.get("role") or "unknown")
original_content = redact_sensitive_value(
msg.get("content"),
config,
parse_json_strings=False,
)
normalized_content = normalize_content_value(original_content)
# Preserve the pre-existing opt-in large-output behavior on message content.
# The always-on storage-boundary sanitizer below is a narrower safety net for
# inline media/base64 substrings, including cases below the generic threshold
# or when generic externalization is disabled.
if normalized_content:
if is_externalized_ingest_placeholder(normalized_content) or is_externalized_placeholder(normalized_content):
msg["content"] = original_content
else:
reason = (
assistant_output_quarantine_reason(normalized_content)
if role == "assistant"
else None
)
externalized = None
if reason:
placeholder = _externalize_quarantined_assistant_output(
normalized_content,
role=role,
session_id=session_id,
config=config,
hermes_home=hermes_home,
reason=reason,
)
if placeholder:
externalized = {"placeholder": placeholder}
if externalized is None:
kind = _externalization_kind_for_message(msg)
externalized = maybe_externalize_payload(
normalized_content,
kind=kind,
tool_call_id=str(msg.get("tool_call_id") or ""),
session_id=session_id,
role=role,
config=config,
hermes_home=hermes_home,
)
if externalized:
msg["content"] = externalized["placeholder"]
else:
msg["content"] = _protect_value(
original_content,
role=role,
session_id=session_id,
field_path="content",
config=config,
hermes_home=hermes_home,
parse_json_strings=False,
)
else:
msg["content"] = original_content
if msg.get("tool_calls"):
msg["tool_calls"] = _protect_tool_calls(
msg.get("tool_calls"),
role=role,
session_id=session_id,
config=config,
hermes_home=hermes_home,
)
return msg
def quarantine_suspicious_assistant_message(
message: Dict[str, Any],
config,
hermes_home: str = "",
session_id: str = "",
*,
externalize: bool = True,
prefer_existing_externalized: bool = False,
) -> Dict[str, Any]:
"""Return ``message`` with obviously broken assistant output quarantined.
Unlike full ingest protection, this only touches suspicious assistant text.
It is safe for active-context replay because it does not externalize user
media, tool results, or ordinary long content.
"""
msg = dict(message or {})
role = str(msg.get("role") or "unknown")
if role != "assistant":
return msg
normalized_content = normalize_content_value(msg.get("content"))
reason = assistant_output_quarantine_reason(normalized_content)
if not reason:
return msg
if externalize:
placeholder = _externalize_quarantined_assistant_output(
normalized_content,
role=role,
session_id=session_id,
config=config,
hermes_home=hermes_home,
reason=reason,
)
else:
placeholder = None
if prefer_existing_externalized:
placeholder = _existing_quarantined_assistant_placeholder(
normalized_content,
role=role,
session_id=session_id,
config=config,
hermes_home=hermes_home,
reason=reason,
)
if placeholder is None:
placeholder = _volatile_quarantined_assistant_placeholder(
normalized_content,
reason=reason,
)
if not placeholder:
return msg
msg["content"] = placeholder
return msg
def quarantine_suspicious_assistant_messages(
messages: List[Dict[str, Any]],
config,
hermes_home: str = "",
session_id: str = "",
externalize: List[bool] | None = None,
prefer_existing_externalized: List[bool] | None = None,
) -> List[Dict[str, Any]]:
return [
quarantine_suspicious_assistant_message(
message,
config=config,
hermes_home=hermes_home,
session_id=session_id,
externalize=True if externalize is None else externalize[idx],
prefer_existing_externalized=False
if prefer_existing_externalized is None
else prefer_existing_externalized[idx],
)
for idx, message in enumerate(messages)
]
def protect_messages_for_ingest(
messages: List[Dict[str, Any]],
config,
hermes_home: str = "",
session_id: str = "",
) -> List[Dict[str, Any]]:
return [
protect_message_for_ingest(
message,
config=config,
hermes_home=hermes_home,
session_id=session_id,
)
for message in messages
]
def scan_sqlite_payload_risks(conn, *, limit: int = 5) -> dict[str, Any]:
"""Return bounded diagnostics for suspicious inline payload storage.
Diagnostics intentionally omit previews/raw payload text. Rows include only
metadata needed for triage and a recoverability ref when a compact
externalized placeholder is present.
"""
def make_row(row, *, field: str, length_key: str, category: str) -> dict[str, Any]:
store_id, session_id, source, role, length, value = row
value = value or ""
result = {
"store_id": int(store_id),
"session_id": session_id,
"source": source,
"role": role,
"field": field,
"length": int(length or 0),
length_key: int(length or 0),
"suspicious_category": category,
}
refs = extract_ingest_externalized_refs(value) if isinstance(value, str) else []
ref = refs[0] if refs else (extract_externalized_ref(value) if isinstance(value, str) else None)
if ref:
result["externalized_ref"] = ref
return result
largest_content = conn.execute(
"""
SELECT store_id, session_id, source, role, COALESCE(length(content), 0) AS content_len, content
FROM messages
ORDER BY content_len DESC
LIMIT ?
""",
(limit,),
).fetchall()
largest_tool_calls = conn.execute(
"""
SELECT store_id, session_id, source, role, COALESCE(length(tool_calls), 0) AS tool_calls_len, tool_calls
FROM messages
ORDER BY tool_calls_len DESC
LIMIT ?
""",
(limit,),
).fetchall()
candidate_cap = max(limit * 20, limit)
# Pre-filter broadly in SQL, then apply the same conservative Python regex
# used by ingest externalization. This avoids false positives from code or
# doctor text that quotes scaffolds such as "data:%;base64,%" or
# `DATA_URI = "data:image/png;base64," + DATA_PAYLOAD`.
data_uri_content_candidates = conn.execute(
"""
SELECT store_id, session_id, source, role, COALESCE(length(content), 0) AS content_len, content
FROM messages
WHERE lower(content) GLOB '*data:*;base64,*'
ORDER BY content_len DESC
LIMIT ?
""",