One command froze the whole gateway for 49 seconds: the regex trap inside the dangerous-command check

You know the moment: you send Hermes something perfectly ordinary from Telegram, and nothing comes back. You assume the model is slow, or the network is bad. You switch to another chat and that one is frozen too. A minute or two later everything catches up at once, and the logs have nothing in them — no error, no timeout, no warning.
The model is not the problem. Neither is the network. The problem can be the question “is this command dangerous?” — because that question is answered by regular expressions, and one of them used to run for tens of seconds on the wrong input while every thread in the process waited for it.
Before approval, someone has to decide what counts as dangerous
When the model wants to run a terminal command, detect_dangerous_command scans it first. Match a dangerous rule — rm -rf /, mkfs, writing an image to a raw device, shutting the machine down, decoding base64 and piping it into a shell — and you get an approval prompt. Otherwise the command just runs.
For those rules to match a command in command position rather than any prose inside a command line, the detector anchors them on _CMDPOS: start of string, a newline, $(, or a backtick, optionally followed by sudo, env VAR=VAL, or the wrappers exec/nohup/setsid/time. Every dangerous rule hangs off it, which is why echo reboot is not a reboot and grep 'shutdown' app.log reads a log without asking you anything.
The trouble was inside that prefix.
Three shapes, three ways to go super-linear
PR #155269, merged on November 15, 2026, fixed three separate performance shapes. They share one property: the input is completely ordinary — it is just a command — but the cost explodes with its length.
Blank lines. _CMDPOS used \s* to consume the whitespace before the command word. That quantifier is not possessive, so after a failed attempt the engine can come back and re-split the same whitespace run another way. One blank line is invisible; a few hundred turn cubic. The PR’s own measurements: 400 blank lines take roughly 1.4 seconds, 1000 take roughly 49. A heredoc with blank lines in it is a perfectly normal script.
.env file paths. One rule checks whether a command touches the project’s sensitive files, matching .env and its variants. The original \.env(?:\.[^…]+)* is ambiguous: on input like .env.x.x.x.x… the engine tries different ways of distributing the dotted fragments across groups, and the combinations grow exponentially.
$( nesting. The function that finds command starts, _iter_shell_command_starts, used to rescan each blank-line run once per newline, and deep $( … ) nesting made that cubic too.
The fix: possessive quantifiers, one ambiguity removed, one ceiling added
Three bugs, three textbook remedies:
\s*became\s*+, and character classes like[^\s]got++. A possessive quantifier never gives characters back, so the engine cannot re-split the same run and the cost drops back to linear.- Only the last newline of a whitespace run starts a match. Earlier newlines in the same run would consume the same whitespace and reach the same command word; trying each one was just rescanning.
\.env(?:\.[^…]+)*became\.env(?:\.[^…]+)?. The PR notes the two describe an identical language with identical end offsets — an equivalence-preserving rewrite that removes the ambiguity.$(nesting got a ceiling of 32 (_MAX_SUBSTITUTION_DEPTH). Past it the scan stops descending and treats the command as dangerous, so it goes to approval. The failure direction is conservative: one extra question beats a crafted nesting that pins the process.
The change also keeps two upstream contributions (#131097 and #152839) and closes tracking issue #7485.
Why this was worse than “a bit slow”
Python runs regex matching in C, but it holds the global interpreter lock while doing it. This is not “one request is slow” — it is “every thread in the process queues behind this match”. For a gateway serving several chats and running scheduled jobs, a single terminal command can stop all of them for tens of seconds.
What the user sees is nothing at all: no error, no timeout, no log line. That is what makes this class of bug so hard to spot — unlike a crash, it leaves no trace.
What is still slow
The PR is honest about the shapes it did not fix, which exist on main today:
- the credential-file-to-curl rule is quadratic on a single very long word (about 16 seconds at 100,000 characters);
- the Python command-word walk is quadratic on long runs of launcher words (about 29 seconds for 3,200 repetitions of
sudo); - a construct with a backtick inside each
$(that never actually nests stays quadratic (about 10 seconds at 320 groups — faster than the 2.2 seconds at 80 groups before the fix, but still unbounded).
It is also worth being precise about what this gate is: an in-process heuristic, not a security boundary, as the project’s security notes say. It exists so dangerous operations ask first; it is not a guarantee that they cannot run. Real boundaries live in sandboxes, permissions and the operating system. That framing is why “too deep to analyse means ask the user” is an acceptable degradation.
What it means for you
Nothing to configure — the fix adds no setting and changes no rule’s meaning. On the release timeline, v0.23.1 shipped on November 10, 2026 and this PR merged on November 15, so it is on main only, not in a release yet.
Update (2026-11-17): these changes shipped in v0.23.2 (November 16) —
hermes updatebrings them in. The “not in v0.23.1” wording above is a snapshot from publication time.
If you suspect you have hit this, a local check is easy: feed the detector a command carrying a few hundred blank lines (a heredoc is enough) and watch how long the approval decision takes. If you can feel the pause, you are on the old code. A more practical habit is to dry-run your approval rules so you can see which commands will ask before you run them.
Two neighbours on the same path are worth knowing: invisible characters in approval prompts explain why a “yes” sometimes does not match, and if your goal is fewer interruptions rather than faster checks, unattended approvals is the right lever.
The one-line version
The cost of a safety check should not be paid by every session at once. No dangerous rule changed meaning here: possessive quantifiers, one ambiguity removed, and an explicit, conservative recursion ceiling turned a potential 49-second global stall back into linear time.