input_sanitize.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Sanitize user prompt text leaked from terminal / paste control sequences."""
  2. from __future__ import annotations
  3. import re
  4. # Degraded visible bracketed-paste forms, matched only at boundaries so embedded literals stay intact.
  5. _BOUNDARY_SUBS = (
  6. (re.compile(r"(^|[\s\n>:\]\)])\[200~"), r"\1"),
  7. (re.compile(r"\[201~(?=$|[\s\n<\[\(\):;.,!?])"), ""),
  8. (re.compile(r"(^|[\s\n>:\]\)])00~"), r"\1"),
  9. (re.compile(r"01~(?=$|[\s\n<\[\(\):;.,!?])"), ""),
  10. )
  11. # Corruption signature from desktop bracketed-paste leaks (#62557).
  12. _DESKTOP_PASTE_ARTIFACT = "~[[e"
  13. def strip_leaked_bracketed_paste_wrappers(text: str) -> str:
  14. """Strip leaked bracketed-paste wrapper markers: canonical wrappers unconditionally, degraded visible forms
  15. (``[200~`` / ``[201~`` and ``00~`` / ``01~``) only at boundaries so ``literal[200~tag`` stays intact."""
  16. if not text:
  17. return text
  18. for wrapper in ("\x1b[200~", "\x1b[201~", "^[[200~", "^[[201~"):
  19. text = text.replace(wrapper, "")
  20. for pattern, repl in _BOUNDARY_SUBS:
  21. text = pattern.sub(repl, text)
  22. return text
  23. def collapse_repeated_input_artifacts(text: str, min_repeats: int = 4) -> str:
  24. """Drop a trailing run of the desktop ~[[e corruption signature (#62557)."""
  25. if not text:
  26. return text
  27. marker = _DESKTOP_PASTE_ARTIFACT
  28. index = len(text)
  29. repeat_count = 0
  30. while index >= len(marker) and text[index - len(marker) : index] == marker:
  31. repeat_count += 1
  32. index -= len(marker)
  33. if repeat_count < min_repeats:
  34. return text
  35. if index >= 2 and text[index - 2 : index] == "[e":
  36. index -= 2
  37. elif index >= 1 and text[index - 1] == "[":
  38. index -= 1
  39. return text[:index]
  40. def sanitize_user_prompt_text(text: str) -> str:
  41. """Normalize user-authored prompt text before persistence or model input."""
  42. if not isinstance(text, str) or not text:
  43. return text
  44. return collapse_repeated_input_artifacts(strip_leaked_bracketed_paste_wrappers(text))