cronjob_prompt_scan.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """Cron prompt threat scanning: the small user-authored prompt gets the strict pattern set;
  2. the assembled prompt (with skill bodies) gets only prose-proof directives."""
  3. import logging
  4. import re
  5. # Single source of truth shared with the install-time scanner (skills_guard): a narrower
  6. # cron-local copy once let obfuscated directives slip past this runtime tripwire.
  7. from tools.threat_patterns import INVISIBLE_CHARS as _CRON_INVISIBLE_CHARS
  8. # Logger parity with the origin module (these functions used to log there).
  9. logger = logging.getLogger("tools.cronjob_tools")
  10. # Strict patterns — user prompt only. A directive-shaped cron prompt has no business
  11. # containing `cat ~/.hermes/.env` or `rm -rf /`; there it is a smoking gun, not prose.
  12. # Two threat surfaces, two scanners: 1. `_scan_cron_prompt()` runs against this at create/update time and as
  13. # a runtime defense-in-depth. 2. Assembled prompt that includes loaded skill content (large markdown bodies,
  14. # often security docs, postmortems, runbooks discussing attack patterns in PROSE). Reusing the strict
  15. # patterns here false-positives every time a skill *describes* a command — see #3968 follow-up: the
  16. # `hermes-agent-dev` skill contains a security postmortem mentioning `cat ~/.hermes/.env`, which tripped
  17. # `read_secrets` and silently killed all PR-scout jobs. Skill bodies are user-curated and scanned at install
  18. # time by `skills_guard.py`. The runtime cron scan only needs to catch the patterns whose phrasing does NOT
  19. # survive normal English prose: classic prompt-injection directives ("ignore previous instructions",
  20. # "disregard your rules"), deception directives, and invisible unicode. `_scan_cron_skill_assembled()` runs
  21. # against the assembled prompt with this tighter pattern set. Both scanners share the invisible-unicode
  22. # check and the GitHub Authorization header exemption.
  23. _CRON_THREAT_PATTERNS = [
  24. (r'ignore\s+(?:\w+\s+)*(?:previous|all|above|prior)\s+(?:\w+\s+)*instructions', "prompt_injection"),
  25. (r'do\s+not\s+tell\s+the\s+user', "deception_hide"),
  26. (r'system\s+prompt\s+override', "sys_prompt_override"),
  27. (r'disregard\s+(your|all|any)\s+(instructions|rules|guidelines)', "disregard_rules"),
  28. (r'cat\s+[^\n]*(\.env|credentials|\.netrc|\.pgpass|id_rsa|id_ed25519|id_ecdsa)', "read_secrets"),
  29. (r'authorized_keys', "ssh_backdoor"), (r'/etc/sudoers|visudo', "sudoers_mod"),
  30. (r'rm\s+-rf\s+/', "destructive_root_rm"),
  31. ]
  32. # Looser set for the assembled prompt: command-shape patterns are dropped because skill
  33. # markdown (postmortems, runbooks) legitimately *describes* those commands and skill bodies
  34. # are vetted at install time — only unambiguous injection directives remain.
  35. _CRON_SKILL_ASSEMBLED_PATTERNS = _CRON_THREAT_PATTERNS[:4]
  36. _CRON_SECRET_VAR_RE = r'\$\{?\w*(?:KEY|TOKEN|SECRET|PASSWORD|CREDENTIAL|API)\w*\}?'
  37. # Obvious leak paths only: secret in the destination URL, in a POST/form body, or in an
  38. # Authorization header to an arbitrary host.
  39. _CRON_EXFIL_COMMAND_PATTERNS = [
  40. (rf'curl\s+[^\n]*https?://[^\s"\'`]*{_CRON_SECRET_VAR_RE}', "exfil_curl_url"),
  41. (rf'wget\s+[^\n]*https?://[^\s"\'`]*{_CRON_SECRET_VAR_RE}', "exfil_wget_url"),
  42. (rf'curl\s+[^\n]*(?:--data(?:-raw|-binary|-urlencode)?|-d|--form|-F)\s+[^\n]*{_CRON_SECRET_VAR_RE}', "exfil_curl_data"),
  43. (rf'wget\s+[^\n]*--post-(?:data|file)=[^\n]*{_CRON_SECRET_VAR_RE}', "exfil_wget_post"),
  44. (rf'curl\s+[^\n]*(?:-H|--header)\s+["\']Authorization:\s*(?:Bearer|token)\s+{_CRON_SECRET_VAR_RE}["\']', "exfil_curl_auth_header"),
  45. ]
  46. # U+200D (ZWJ) is a required part of many emoji sequences (👨‍👩‍👧, 🏳️‍🌈): block it
  47. # between plain text, allow it inside an emoji grapheme cluster.
  48. _EMOJI_NEIGHBOUR_CP_RANGES = ((0x1F000, 0x1FFFF), (0x2600, 0x27BF), (0x2300, 0x23FF), (0x1F1E6, 0x1F1FF), (0x20E3, 0x20E3))
  49. _VARIATION_SELECTOR_CP = 0xFE0F
  50. def _zwj_has_emoji_neighbour(text: str, idx: int) -> bool:
  51. """True when the ZWJ at text[idx] sits between emoji codepoints (skipping VS16)."""
  52. left = idx - 1
  53. while left >= 0 and ord(text[left]) == _VARIATION_SELECTOR_CP:
  54. left -= 1
  55. right = idx + 1
  56. while right < len(text) and ord(text[right]) == _VARIATION_SELECTOR_CP:
  57. right += 1
  58. if left < 0 or right >= len(text):
  59. return False
  60. return all(
  61. any(lo <= ord(text[pos]) <= hi for lo, hi in _EMOJI_NEIGHBOUR_CP_RANGES) for pos in (left, right)
  62. )
  63. def _strip_cron_safe_constructs(prompt: str) -> str:
  64. """Scrub the bundled GitHub skill's `Authorization: token $GITHUB_TOKEN` + api.github.com
  65. curl so it doesn't trip the auth-header exfil rule.
  66. re.sub scrubs EVERY occurrence. The trailing ``[^\\s;&|$`]*`` consumes only the URL path —
  67. never separators or subshell openers — so a payload smuggled onto the same line still gets
  68. scanned. Host must be exactly api.github.com followed by ``/``, whitespace, quote, or end:
  69. lookalike authorities (api.github.com.evil.com, api.github.com@evil.com) fall through.
  70. """
  71. return re.sub(
  72. rf'curl\s+[^\n;&|$`]*(?:-H|--header)\s+["\']Authorization:\s*token\s+{_CRON_SECRET_VAR_RE}["\']'
  73. r'\s+["\']?https://api\.github\.com(?::\d+)?(?:/|\s|$|["\'])[^\s;&|$`]*',
  74. 'curl https://api.github.com/user',
  75. prompt,
  76. flags=re.IGNORECASE,
  77. )
  78. def _strip_invisible_unicode(prompt: str) -> tuple[str, list[str]]:
  79. """Strip invisible-unicode chars, keeping ZWJ inside legitimate emoji.
  80. Returns ``(cleaned, sorted U+XXXX labels removed)``. The skills-attached path sanitizes
  81. (a stray zero-width space in vetted skill content must not permanently kill the job).
  82. """
  83. if not prompt:
  84. return prompt, []
  85. removed: set[str] = set()
  86. cleaned: list[str] = []
  87. for idx, ch in enumerate(prompt):
  88. if ch in _CRON_INVISIBLE_CHARS and not (ch == '\u200d' and _zwj_has_emoji_neighbour(prompt, idx)):
  89. removed.add(f"U+{ord(ch):04X}")
  90. continue
  91. cleaned.append(ch)
  92. return ''.join(cleaned), sorted(removed)
  93. def _first_pattern_error(text: str, *pattern_sets) -> str:
  94. for patterns in pattern_sets:
  95. for pattern, pid in patterns:
  96. if re.search(pattern, text, re.IGNORECASE):
  97. return (
  98. f"Blocked: prompt matches threat pattern '{pid}'. Cron prompts must not "
  99. "contain injection or exfiltration payloads."
  100. )
  101. return ""
  102. def _scan_cron_prompt(prompt: str) -> str:
  103. """Strict scan of the USER-SUPPLIED prompt (create/update + runtime defense-in-depth).
  104. Returns an error string when blocked, else "". Invisible unicode is reported first, in
  105. ``_CRON_INVISIBLE_CHARS`` order (emoji ZWJ allowed)."""
  106. prompt_to_scan = _strip_cron_safe_constructs(prompt)
  107. removed = set(_strip_invisible_unicode(prompt_to_scan)[1])
  108. for char in _CRON_INVISIBLE_CHARS:
  109. if f"U+{ord(char):04X}" in removed:
  110. return f"Blocked: prompt contains invisible unicode U+{ord(char):04X} (possible injection)."
  111. return _first_pattern_error(prompt_to_scan, _CRON_THREAT_PATTERNS, _CRON_EXFIL_COMMAND_PATTERNS)
  112. def _scan_cron_skill_assembled(assembled: str) -> tuple[str, str]:
  113. """Loose scan of the ASSEMBLED prompt (skill content included). Invisible unicode is
  114. SANITIZED (stripped + logged), not blocked — the hard block stays on raw user prompts,
  115. the actual injection surface. Returns ``(cleaned_prompt, error)``, error "" when passed."""
  116. cleaned, removed = _strip_invisible_unicode(assembled)
  117. if removed:
  118. logger.warning(
  119. "Cron skill-assembled prompt: stripped %d invisible-unicode "
  120. "char(s) (%s) from vetted skill content",
  121. len(removed), ", ".join(removed),
  122. )
  123. return cleaned, _first_pattern_error(_strip_cron_safe_constructs(cleaned), _CRON_SKILL_ASSEMBLED_PATTERNS)