| 1 | """Regression guard: the Hermes install-time scanner must find zero CRITICAL. |
| 2 | |
| 3 | Hermes (NousResearch/hermes-agent, tools/skills_guard.py) blocks community-tier |
| 4 | installs on a `dangerous` verdict, which any single CRITICAL finding produces, |
| 5 | and `--force` cannot override it. With zero CRITICAL the verdict is `caution`, |
| 6 | which `hermes skills install ... --force` installs. Issues #513 and #1502 were |
| 7 | both CRITICAL findings sneaking back in; this test fails CI instead of silently |
| 8 | re-blocking every Hermes user. |
| 9 | |
| 10 | Self-contained replica (no Hermes dependency): every CRITICAL rule from |
| 11 | skills_guard.py's THREAT_PATTERNS, copied verbatim from scanner version |
| 12 | `skills-guard-v16` (hermes-agent 27e38935). The real scanner compiles them with |
| 13 | re.IGNORECASE and checks each line. It also demotes some hits by context; this |
| 14 | replica does not, so it is stricter than Hermes, never looser. |
| 15 | |
| 16 | Scanner v16 no longer honors a skill's own `.skillignore` (publisher-authored |
| 17 | ignore files are untrusted), so the whole skill directory is scanned, vendored |
| 18 | code included. HIGH/MEDIUM findings are intentionally not checked: they only |
| 19 | produce `caution`, which is installable. |
| 20 | |
| 21 | To resync after Hermes changes its rules, regenerate CRITICAL_RULES from |
| 22 | `[p for p in THREAT_PATTERNS if p[2] == "critical"]` and update the version |
| 23 | above. |
| 24 | """ |
| 25 | from __future__ import annotations |
| 26 | |
| 27 | import re |
| 28 | from pathlib import Path |
| 29 | |
| 30 | # scan root == the skill directory (where SKILL.md lives), matching how Hermes |
| 31 | # resolves owner/repo/skills/<name> and raw SKILL.md URLs. |
| 32 | SKILL_ROOT = Path(__file__).resolve().parents[2] / "skills" / "last30days" |
| 33 | |
| 34 | # Every CRITICAL rule in skills-guard-v16 THREAT_PATTERNS, verbatim. |
| 35 | CRITICAL_RULES = [ |
| 36 | ('curl\\s+(?![^\\n]*https?://(?:localhost|127\\.0\\.0\\.1|\\[::1\\]))[^\\n]*\\$\\{?\\w*(?:KEY|TOKEN|SECRET|PASSWORD|CREDENTIAL)S?\\b', 'env_exfil_curl'), |
| 37 | ('wget\\s+(?![^\\n]*https?://(?:localhost|127\\.0\\.0\\.1|\\[::1\\]))[^\\n]*\\$\\{?\\w*(?:KEY|TOKEN|SECRET|PASSWORD|CREDENTIAL)S?\\b', 'env_exfil_wget'), |
| 38 | ('fetch\\s*\\((?![^\\n]*https?://(?:localhost|127\\.0\\.0\\.1|\\[::1\\]))[^\\n]*\\$\\{?\\w*(?:KEY|TOKEN|SECRET|PASSWORD)S?\\b', 'env_exfil_fetch'), |
| 39 | ('httpx?\\.(get|post|put|patch)\\s*\\((?![^\\n]*https?://(?:localhost|127\\.0\\.0\\.1|\\[::1\\]))[^\\n]*(KEY|TOKEN|SECRET|PASSWORD)', 'env_exfil_httpx'), |
| 40 | ('requests\\.(get|post|put|patch)\\s*\\((?![^\\n]*https?://(?:localhost|127\\.0\\.0\\.1|\\[::1\\]))[^\\n]*(KEY|TOKEN|SECRET|PASSWORD)', 'env_exfil_requests'), |
| 41 | ('\\$HOME/\\.hermes/\\.env|\\~/\\.hermes/\\.env', 'hermes_env_access'), |
| 42 | ('cat\\s+(?!>)[^\\n]*(\\.env|credentials|\\.netrc|\\.pgpass|\\.npmrc|\\.pypirc)', 'read_secrets_file'), |
| 43 | ('\\b(?:readFile(?:Sync)?|readTextFile)\\s*\\(\\s*["\\\'][^"\\\'\\n]*(?:\\.ssh[/\\\\]id_(?:rsa|ed25519|ecdsa|dsa)(?!\\.pub)|\\.env\\b|credentials\\b|\\.netrc\\b|\\.pgpass\\b|\\.npmrc\\b|\\.pypirc\\b)[^"\\\'\\n]*["\\\']', 'js_read_secrets_file'), |
| 44 | ('\\bopen\\s*\\(\\s*(?:os\\.path\\.expanduser\\s*\\(\\s*)?["\\\'][^"\\\'\\n]*(?:\\.ssh[/\\\\]id_(?:rsa|ed25519|ecdsa|dsa)(?!\\.pub)|\\.env\\b|credentials\\b|\\.netrc\\b|\\.pgpass\\b|\\.npmrc\\b|\\.pypirc\\b)[^"\\\'\\n]*["\\\']\\s*\\)?(?!\\s*\\)?\\s*,\\s*["\\\'][^"\\\']*[wax][^"\\\']*["\\\'])(?![^\\n]*\\bmode\\s*=\\s*["\\\'][^"\\\']*[wax])|\\bPath\\s*\\(\\s*(?:os\\.path\\.expanduser\\s*\\(\\s*)?["\\\'][^"\\\'\\n]*(?:\\.ssh[/\\\\]id_(?:rsa|ed25519|ecdsa|dsa)(?!\\.pub)|\\.env\\b|credentials\\b|\\.netrc\\b|\\.pgpass\\b|\\.npmrc\\b|\\.pypirc\\b)[^"\\\'\\n]*["\\\']\\s*\\)?\\s*\\)\\.(?:read_text|read_bytes|readlines|readline)\\s*\\(', 'py_read_secrets_file'), |
| 45 | ('(?-i:ENV)\\[.*(?:KEY|TOKEN|SECRET|PASSWORD)', 'ruby_env_secret'), |
| 46 | ('(?<![-/])\\b(?:dig|nslookup)\\s+(?:[-+@]\\S*(?:\\s+[^\\s$"\\\'-][^\\s$]*)?\\s+)*["\\\']?[^\\s"\\\'$()]*\\$(?:\\{[^}\\s]*\\}|\\([^\\n]*?\\)+|\\w+)[\\w-]*["\\\']?\\.[a-z]|(?<![-/])\\bhost\\s+(?:[-+@]\\S*(?:\\s+[^\\s$"\\\'-][^\\s$]*)?\\s+)*["\\\']?[^\\s"\\\'$()]*\\$(?:\\{[^}\\s]*\\}|\\([^\\n]*?\\)+|\\w+)[\\w-]*\\.[a-z]|(?:^|[;&|]|\\$\\(|\\b(?:do|then|else)\\b)\\s*host\\s+(?:[-+@]\\S*(?:\\s+[^\\s$"\\\'-][^\\s$]*)?\\s+)*["\\\']?\\$', 'dns_exfil'), |
| 47 | ('>\\s*/tmp/[^\\s]*\\s*&&\\s*(curl|wget|nc|python)', 'tmp_staging'), |
| 48 | ('ignore\\s+(?:\\w+\\s+)*(previous|all|above|prior)\\s+instructions', 'prompt_injection_ignore'), |
| 49 | ('system\\s+(?:\\w+\\s+)*prompt\\s+(?:\\w+\\s+)*override', 'sys_prompt_override'), |
| 50 | ('disregard\\s+(?:\\w+\\s+)*(your|all|any)\\s+(?:\\w+\\s+)*(instructions|rules|guidelines)', 'disregard_rules'), |
| 51 | ("act\\s+as\\s+(if|though)\\s+(?:\\w+\\s+)*you\\s+(?:\\w+\\s+)*(have\\s+no|don\\'t\\s+have)\\s+(?:\\w+\\s+)*(restrictions|limits|rules)", 'bypass_restrictions'), |
| 52 | ('translate\\s+.*\\s+into\\s+.*\\s+and\\s+(execute|run|eval)', 'translate_execute'), |
| 53 | ('rm\\s+-rf\\s+((?:[^\\s;&|<>#][^\\s;&|<>]*\\s+)+?)?/(?(1)(?!/(?:\\s|$)))(?:(?!(?:tmp|var/tmp|dev/shm|run|var/lib/apt/lists|var/cache/(?:apt|apk|yum|dnf))(?:\\b|/))|(?:tmp|var/tmp|dev/shm|run|var/lib/apt/lists|var/cache/(?:apt|apk|yum|dnf))/(?:[^/\\s]*/)*\\.\\.(?=/|[\\s;&|]|$))', 'destructive_root_rm'), |
| 54 | ('(?<![A-Za-z0-9])rm\\s+(-[^\\s]*)?r.*(?:\\$HOME|~[/\\s*]|~$)|(?<![A-Za-z0-9])rmdir\\s+.*(?:\\$HOME|~[/\\s*]|~$)', 'destructive_home_rm'), |
| 55 | ('>\\s*/etc/', 'system_overwrite'), |
| 56 | ('\\bmkfs\\b', 'format_filesystem'), |
| 57 | ('\\bdd\\s+.*if=.*of=/dev/', 'disk_overwrite'), |
| 58 | ('truncate\\s+-s\\s*0\\s+/', 'truncate_system'), |
| 59 | ('authorized_keys', 'ssh_backdoor'), |
| 60 | ('/etc/sudoers|visudo', 'sudoers_mod'), |
| 61 | ('\\bnc\\s+-[lp]|ncat\\s+-[lp]|\\bsocat\\b[^\\n]*\\b(?:tcp|udp|openssl|ssl|exec|system|pty|unix)[\\w-]*:', 'reverse_shell'), |
| 62 | ('/bin/(?:bash|sh|zsh|ksh|dash)\\b\\s+-i\\s+.*>/dev/tcp/', 'bash_reverse_shell'), |
| 63 | ('python[23]?\\s+-c\\s+["\\\']import\\s+socket', 'python_socket_oneliner'), |
| 64 | ('echo\\s+[^\\n]*\\|\\s*(?:(?:bash|sh|zsh|ksh|dash)\\b|python|perl|ruby|node)', 'echo_pipe_exec'), |
| 65 | ('/etc/passwd|/etc/shadow', 'system_passwd_access'), |
| 66 | ('xmrig|stratum\\+tcp|monero|coinhive|cryptonight', 'crypto_mining'), |
| 67 | ('curl\\s+[^|\\s][^\\n]*\\|\\s*(?:sudo\\s+(?:-\\S+\\s+)*)?(?:bash|sh|zsh|ksh|dash)\\b', 'curl_pipe_shell'), |
| 68 | ('wget\\s+[^\\n]*-O\\s*-\\s*\\|\\s*(?:sudo\\s+(?:-\\S+\\s+)*)?(?:bash|sh|zsh|ksh|dash)\\b', 'wget_pipe_shell'), |
| 69 | ('curl\\s+[^|\\s][^\\n]*\\|\\s*(?:sudo\\s+(?:-\\S+\\s+)*)?python', 'curl_pipe_python'), |
| 70 | ('(?<![a-z])set(?:e|re|res)?[ug]id\\s*\\(|\\bcap_set[ug]id\\b|\\bchmod\\s+(?:-\\w+\\s+)*(?:[ugoa]*[+=][rwxXt]*s|0?[2-7][0-7]{3}\\b)', 'setuid_setgid'), |
| 71 | ('NOPASSWD', 'nopasswd_sudo'), |
| 72 | ('^\\s*(?:[-*+]\\s+|\\d+[.)]\\s+)?(?:\\bwrit(?:e|es|ing)\\b|\\bwritten\\b|\\bedit(?:s|ed|ing)?\\b|\\bmodif(?:y|ies|ied|ying|ication)s?\\b|\\bupdat(?:e|es|ed|ing)\\b|\\bappend(?:s|ed|ing)?\\b|\\bprepend(?:s|ed|ing)?\\b|\\binject(?:s|ed|ing)?\\b|\\boverwrit(?:e|es|ing)\\b|\\boverwritten\\b|\\breplac(?:e|es|ed|ing)\\b|\\balter(?:s|ed|ing)?\\b|\\badd(?:s|ed|ing)\\b)[^\\n,]{0,80}?(?:AGENTS\\.md|CLAUDE\\.md|\\.cursorrules|\\.clinerules)\\b|(?:\\byou\\s+(?:must|should|need\\s+to)\\s+|\\bplease\\s+|\\bmake\\s+sure\\s+(?:to\\s+|you\\s+)|\\bbe\\s+sure\\s+to\\s+)(?:\\bwrit(?:e|es|ing)\\b|\\bwritten\\b|\\bedit(?:s|ed|ing)?\\b|\\bmodif(?:y|ies|ied|ying|ication)s?\\b|\\bupdat(?:e|es|ed|ing)\\b|\\bappend(?:s|ed|ing)?\\b|\\bprepend(?:s|ed|ing)?\\b|\\binject(?:s|ed|ing)?\\b|\\boverwrit(?:e|es|ing)\\b|\\boverwritten\\b|\\breplac(?:e|es|ed|ing)\\b|\\balter(?:s|ed|ing)?\\b|\\badd(?:s|ed|ing)\\b)[^\\n,]{0,80}?(?:AGENTS\\.md|CLAUDE\\.md|\\.cursorrules|\\.clinerules)\\b', 'agent_config_mod'), |
| 73 | ('(?:>>|[\\w"\\\'`)\\]]\\s*>)\\s*[~\\w./-]*(?:AGENTS\\.md|CLAUDE\\.md|\\.cursorrules|\\.clinerules)(?!\\.?\\w)|\\bsed\\b[^\\n]*\\s(?:-[A-Za-z]*i[A-Za-z]*|--in-place)\\b[^\\n]*(?:AGENTS\\.md|CLAUDE\\.md|\\.cursorrules|\\.clinerules)(?!\\.?\\w)|\\btee\\s+(?:-a\\s+)?[~\\w./"\\\'-]*(?:AGENTS\\.md|CLAUDE\\.md|\\.cursorrules|\\.clinerules)(?!\\.?\\w)|\\b(?:cp|mv)\\s+[^\\s|;&]+\\s+[^\\n|;&]{0,40}?(?:AGENTS\\.md|CLAUDE\\.md|\\.cursorrules|\\.clinerules)(?!\\.?\\w)', 'agent_config_mod_shell'), |
| 74 | ('(?:>>|[\\w"\\\'`)\\]]\\s*>)\\s*[~\\w./-]*\\.hermes/(?:config\\.yaml|SOUL\\.md)(?!\\.?\\w)|\\bsed\\b[^\\n]*\\s(?:-[A-Za-z]*i[A-Za-z]*|--in-place)\\b[^\\n]*\\.hermes/(?:config\\.yaml|SOUL\\.md)(?!\\.?\\w)|\\btee\\s+(?:-a\\s+)?[~\\w./"\\\'-]*\\.hermes/(?:config\\.yaml|SOUL\\.md)(?!\\.?\\w)|\\b(?:cp|mv)\\s+[^\\s|;&]+\\s+[^\\n|;&]{0,40}?\\.hermes/(?:config\\.yaml|SOUL\\.md)(?!\\.?\\w)', 'hermes_config_mod_shell'), |
| 75 | ('(?:>>|[\\w"\\\'`)\\]]\\s*>)\\s*[~\\w./-]*\\.(?:claude/settings|codex/config)[\\w.]*(?!\\.?\\w)|\\bsed\\b[^\\n]*\\s(?:-[A-Za-z]*i[A-Za-z]*|--in-place)\\b[^\\n]*\\.(?:claude/settings|codex/config)[\\w.]*(?!\\.?\\w)|\\btee\\s+(?:-a\\s+)?[~\\w./"\\\'-]*\\.(?:claude/settings|codex/config)[\\w.]*(?!\\.?\\w)|\\b(?:cp|mv)\\s+[^\\s|;&]+\\s+[^\\n|;&]{0,40}?\\.(?:claude/settings|codex/config)[\\w.]*(?!\\.?\\w)', 'other_agent_config_mod_shell'), |
| 76 | ('(?:api[_-]?key|token|secret|password)\\s*[=:]\\s*["\\\'](?!(?-i:[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+)["\\\'])(?!(?-i:TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA|TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb|ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL|MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr|11111111111111111111111111111111|metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s)["\\\'])[A-Za-z0-9+/=_-]{20,}', 'hardcoded_secret'), |
| 77 | ('(?-i:-----BEGIN\\s+(RSA\\s+)?PRIVATE\\s+KEY-----)', 'embedded_private_key'), |
| 78 | ('(?-i:ghp_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{80,})', 'github_token_leaked'), |
| 79 | ('(?-i:sk-[A-Za-z0-9]{20,})', 'openai_key_leaked'), |
| 80 | ('(?-i:sk-ant-[A-Za-z0-9_-]{90,})', 'anthropic_key_leaked'), |
| 81 | ('(?-i:AKIA[0-9A-Z]{16})', 'aws_access_key_leaked'), |
| 82 | ('(?-i:glpat-[A-Za-z0-9_\\-]{20,})', 'gitlab_token_leaked'), |
| 83 | ('\\bDAN\\s+mode\\b|Do\\s+Anything\\s+Now', 'jailbreak_dan'), |
| 84 | ('\\bdeveloper\\s+mode\\b.*\\benabled?\\b', 'jailbreak_dev_mode'), |
| 85 | ('(respond|answer|reply)\\s+without\\s+(?:\\w+\\s+)*(restrictions|limitations|filters|safety)', 'remove_filters'), |
| 86 | ] |
| 87 | |
| 88 | # Binary/asset extensions the scanner does not run text rules on. |
| 89 | _SKIP_SUFFIXES = {".jpg", ".jpeg", ".png", ".gif", ".mp3", ".json"} |
| 90 | |
| 91 | |
| 92 | def _scanned_files(): |
| 93 | for p in sorted(SKILL_ROOT.rglob("*")): |
| 94 | if not p.is_file() or "__pycache__" in p.parts: |
| 95 | continue |
| 96 | if p.suffix.lower() in _SKIP_SUFFIXES or p.suffix.lower() == ".pyc": |
| 97 | continue |
| 98 | yield p.relative_to(SKILL_ROOT).as_posix(), p |
| 99 | |
| 100 | |
| 101 | def _critical_hits(rules=CRITICAL_RULES): |
| 102 | compiled = [(re.compile(rx, re.IGNORECASE), name) for rx, name in rules] |
| 103 | hits = [] |
| 104 | for rel, path in _scanned_files(): |
| 105 | try: |
| 106 | text = path.read_text(encoding="utf-8", errors="replace") |
| 107 | except OSError: |
| 108 | continue |
| 109 | for i, line in enumerate(text.splitlines(), 1): |
| 110 | for rx, name in compiled: |
| 111 | if rx.search(line): |
| 112 | hits.append(f"{name} {rel}:{i} {line.strip()[:90]}") |
| 113 | return hits |
| 114 | |
| 115 | |
| 116 | def test_zero_critical_scanner_findings(): |
| 117 | hits = _critical_hits() |
| 118 | assert not hits, ( |
| 119 | "Hermes scanner CRITICAL patterns reappeared in the scanned subtree " |
| 120 | "(this re-blocks every community install). Findings:\n " + "\n ".join(hits) |
| 121 | ) |
| 122 | |
| 123 | |
| 124 | def test_replica_catches_download_and_execute_install_hint(): |
| 125 | """The rule that caused #1502 must stay in the replica.""" |
| 126 | rx = dict((name, rx) for rx, name in CRITICAL_RULES)["curl_pipe_shell"] |
| 127 | assert re.search(rx, "install with curl -fsSL https://example.com/install.sh | bash", re.IGNORECASE) |
| 128 | assert not re.search(rx, "npm install -g @xai-official/grok", re.IGNORECASE) |
| 129 |