DEV Community

Cover image for Your guardrail regex didn't protect your agent. It froze your entire Python event loop.
Dhrubojyoti Gangopadhyay
Dhrubojyoti Gangopadhyay

Posted on

Your guardrail regex didn't protect your agent. It froze your entire Python event loop.

In async Python, a synchronous regex running before your size limit is an unauthenticated CPU denial-of-service vector.

Your guardrail regex didn't protect your agent. It froze your entire Python event loop.

What changed

Pydantic AI v2.70.0 (released 17 November 2026) shipped with two high-severity security advisories in pydantic-ai-harness (GHSA-5m5q-5qj4-gx9h and GHSA-m4cr-m4qc-9hh3) that expose a dangerous architectural blind spot in how most engineering teams wire async agent guardrails and tool output sanitizers.

From the advisories, verbatim:

  1. Default JWT secret redaction (GHSA-5m5q-5qj4-gx9h): > "The jwt pattern in Pydantic AI Harness's secret redaction backtracks in time quadratic in the length of the text it scanned, so a few hundred kilobytes of crafted text could occupy the event loop for tens of seconds to minutes." > "This affects applications that use the guardrail detectors redact_secrets or secret_data() with the jwt pattern, which is included by default, on text an attacker can influence: user prompts checked by InputGuardrail, tool results checked by ToolGuardrail, or model output checked by OutputGuardrail. Synchronous guards run on the event loop."
  2. ANSI stripping before output truncation (GHSA-m4cr-m4qc-9hh3): > "With strip_ansi=True, Pydantic AI Harness's ToolOutputLimits capability removed ANSI escape sequences with a pattern that took time quadratic in the length of a line, so a tool result of a few hundred kilobytes with many unterminated escape sequences could occupy the event loop for tens of seconds to minutes." > "Stripping runs on the event loop before the capability reduces the output, so its own size limits do not bound it."

Both vulnerabilities share the same root weakness: CWE-1333 (Inefficient Regular Expression Complexity) executing synchronously on the Python asyncio event loop before or independent of payload size limits.

The mechanics of an event-loop freeze

Almost every production Python agent service uses asyncio (via FastAPI, Starlette, or an async worker pool) so a single worker process can multiplex dozens of concurrent agent runs waiting on network I/O (LLM streams, vector searches, MCP calls).

To keep secrets out of logs and terminal garbage out of context windows, teams add guardrails and output limiters:

# Illustrative configuration of tool limits and secret redaction prior to 0.70.0
# You assume ToolOutputLimits caps output size, and redact_secrets just scrubs tokens.
harness_capabilities = [
    ToolOutputLimits(max_bytes=10_000, strip_ansi=True),
    ToolGuardrail(detector=redact_secrets()),  # includes 'jwt' pattern by default
]
Enter fullscreen mode Exit fullscreen mode

Now trace what happens when an agent runs a tool whose output an external source can influence — fetching a web page, reading a repository file, running a shell command, or querying a third-party MCP server:

  1. The untrusted payload arrives: A tool returns a 300 KB single-line payload containing repeated -eyJ segments or thousands of unterminated \x1b[ ANSI escape prefixes (illustrative payload):
   [ILLUSTRATIVE CRAFTED TOOL OUTPUT: 300 KB line]
   ... -eyJhbGciOi-eyJhbGciOi-eyJhbGciOi ... (or repeated \x1b[31;1;4m without terminator)
Enter fullscreen mode Exit fullscreen mode
  1. Sanitization runs before truncation: You configured ToolOutputLimits specifically so oversized tool responses get cut down before eating your context window. Backwards ordering defeats it: strip_ansi=True ran on the raw, unbounded string before the capability reduced the output.
  2. Default regexes scan the full string on the main thread: Even if strip_ansi was left at its default False, redact_secrets and secret_data() include the jwt regex pattern by default across InputGuardrail, ToolGuardrail, and OutputGuardrail.
  3. Quadratic backtracking locks the CPU: Python's standard re engine is a backtracking NFA. On crafted non-matching strings of length $N$, quadratic complexity $O(N^2)$ turns a 300 KB string scan into billions of backtracking steps.
  4. The entire worker dies for 30–90 seconds: Because synchronous guardrails run directly on the asyncio event loop, the loop cannot yield at an await point while inside re.sub() or re.search().

Why this looks like a mysterious infrastructure outage

When an asyncio event loop blocks in C-level regex evaluation for 60 seconds:

  • Every concurrent user on that worker freezes: Other agent runs waiting for a socket read or streaming token chunk stop mid-sentence.
  • Health checks time out: Kubernetes /healthz probes cannot be answered by the blocked event loop, triggering pod restarts and cascading 502s.
  • WebSockets drop: Heartbeat pings expire while the thread is stuck inside the guardrail.
  • The wrong component gets blamed: Engineers check LLM provider status pages and database connection pools, never suspecting that a "lightweight regex guardrail" burned 100% of one CPU core for a minute.

How pydantic-ai-harness 0.70.0 fixed it

In pydantic-ai-harness 0.70.0 (shipped with Pydantic AI v2.70.0):

  • The ANSI stripping pattern in ToolOutputLimits was fixed to avoid quadratic backtracking on unterminated escape sequences.
  • The jwt secret redaction pattern was tightened so it no longer backtracks quadratically (specifically, as the release notes state: "The fixed pattern no longer matches a JWT whose header segment contains -eyJ.").
  • If you cannot upgrade immediately, the advisories document two immediate mitigations:
    • Leave strip_ansi at its default (False).
    • Exclude the jwt pattern by explicitly whitelisting only the patterns you need: secret_data(only=[...]).

The discipline for async agent guardrails

  1. Enforce hard byte-length ceilings before any regex or parser runs. A size limit that runs after a regex, HTML parser, or JSON sanitizer is not a size limit. Slice or reject by raw byte/character count at the I/O boundary first:
   # Illustrative defensive ordering: bound size BEFORE running any regex
   MAX_SCAN_CHARS = 32_768
   bounded_output = raw_tool_output[:MAX_SCAN_CHARS]
   sanitized_output = linear_time_scrubber(bounded_output)
Enter fullscreen mode Exit fullscreen mode
  1. Treat tool outputs as untrusted attacker-controlled input. Prompt injection isn't the only threat from a fetched webpage, file read, or MCP response. Resource exhaustion (ReDoS, decompression bombs, deeply nested JSON) enters through the exact same tool-result channel.
  2. Ban backtracking regexes on the asyncio event loop. Audit every custom regex in your InputGuardrail, ToolGuardrail, and OutputGuardrail pipeline for CWE-1333 (nested quantifiers, overlapping alternations, unanchored greedy lookaheads), or replace Python's backtracking re module on untrusted inputs with a linear-time automaton engine (such as google-re2 or a Rust-backed scanner).

Two hard rules

A guardrail that runs before your size limit is an unauthenticated CPU denial-of-service vector.

In async Python, one quadratic regex on untrusted tool output freezes every user on the worker.


Honest limitation: I am reading Pydantic AI's v2.70.0 release notes and GitHub Security Advisories GHSA-5m5q-5qj4-gx9h and GHSA-m4cr-m4qc-9hh3. The configuration snippets and crafted payload examples above are illustrative; I have not run ReDoS payloads against a live deployment.

Checkable references:

What this series is: one primary source a day — a spec, a changelog, a status page — and what actually changed, with the quote. Follow if you prefer the change over someone's summary.

In your agent stack today, do your regex scrubbers run before or after you enforce hard byte-length caps on tool outputs?

Top comments (1)

Collapse
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

The ordering point is the one that generalizes furthest — a size limit that runs after the regex isn't a size limit at all, it's a hope. The JWT pattern being included by default in redact_secrets makes this worse: teams get the quadratic scan without ever opting in. Do you run guardrails off the event loop now (thread pool or a linear-time engine like RE2), or does the patched pattern make that unnecessary in your view?