# Two ReDoS Bugs Were Hiding in Pydantic AI's Own Safety Features

> pydantic-ai-harness shipped two high-severity ReDoS bugs in its own output sanitizer and secret redactor. A runnable demo shows the backtracking and the fix.

- Canonical URL: https://webofmike.com/pydantic-ai-harness-redos/
- Author: Mike Moore (https://webofmike.com/about/)
- Published: 2026-11-27
- Last modified: 2026-11-27
- Tags: Security, AI Agents, Code
- Cite as: Mike Moore, "Two ReDoS Bugs Were Hiding in Pydantic AI's Own Safety Features", Web of Mike (webofmike.com), 2026-11-27. https://webofmike.com/pydantic-ai-harness-redos/


Two high-severity advisories landed against [`pydantic-ai-harness`](https://github.com/pydantic/pydantic-ai) on November 17, 2026, both regular-expression denial of service (ReDoS) bugs, both inside code whose entire job was to make agent tool output *safer*. [`GHSA-m4cr-m4qc-9hh3`](https://github.com/pydantic/pydantic-ai/security/advisories/GHSA-m4cr-m4qc-9hh3) is in `ToolOutputLimits`' ANSI-escape stripper. [`GHSA-5m5q-5qj4-gx9h`](https://github.com/pydantic/pydantic-ai/security/advisories/GHSA-5m5q-5qj4-gx9h) is in `redact_secrets`' default JWT pattern. Both were fixed in `pydantic-ai-harness` 0.70.0, shipped as part of `pydantic-ai` v2.70.0. I checked the next release, v2.71.0 (November 18), and it carries no further fix or regression on either one, so the advisory is current, not stale.

I built a [small, runnable reproduction](https://github.com/themsquared/redos-guardrail-demo) of the vulnerability class both advisories describe, to see the backtracking blow up and time exactly where the fix stops it.

## What the advisories actually say

From the release notes for `pydantic-ai` [v2.70.0](https://github.com/pydantic/pydantic-ai/releases/tag/v2.70.0):

> 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.

> The `jwt` pattern in Pydantic AI Harness's secret redaction backtracked 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. You are affected if you use `redact_secrets` or `secret_data()` with the `jwt` pattern, which is included by default, on text an attacker can influence.

Neither the advisories nor the release notes publish the literal vulnerable regex, so I'm not reproducing pydantic-ai's actual source below. What I reproduced is the general shape of bug both descriptions point at: a regex with nested or overlapping quantifiers, applied to the same two jobs, that backtracks catastrophically on crafted input.

## Why a "safety" regex is a good place to hide this bug

A regex backtracks when it tries one way to match, fails, and tries another. Most patterns have exactly one reasonable way to match any given input, so backtracking is cheap. The bug shows up when a pattern lets the engine split the same stretch of input multiple equivalent ways: a quantifier nested inside another quantifier, like `(?:x+)+`, or two adjacent groups over the same character class. Each extra character in the input can multiply the number of ways to split it, so the number of attempts the engine tries grows exponentially, not linearly, with input length.

Writing that kind of pattern is easy to do by accident in exactly the code these two advisories hit: "scan this text for something that looks like an ANSI escape" or "scan this text for something that looks like a JWT." Both are fuzzy-match problems, and fuzzy matching tempts you toward "match as much of this shape as you can, then a bit more," which is the nested-quantifier shape. Nobody writing an output sanitizer or a secret redactor sets out to add a DoS bug. They set out to be a little lenient about what counts as "escape-looking" or "token-looking," and leniency is where the ambiguity creeps in.

## What I built to see it

Two scenarios, matching the two advisories, each with a vulnerable pattern and a fixed one:

**Scenario 1, ANSI stripping.** Vulnerable: `(?:\x1b\[(?:[0-9;]*)*)+m`, a quantifier nested inside another quantifier over the escape sequence's body. Fixed: `\x1b\[[0-9;]*m`, one pass, no nesting.

**Scenario 2, token redaction.** Vulnerable: `eyJ(?:[A-Za-z0-9_-]+)+\.SIGNATURE`, the same nested-quantifier shape over a JWT-like token body. Fixed: `eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+`, matching the three dot-separated segments directly.

Each `re.sub()` call runs in its own subprocess, with a 3-second timeout enforced by killing the subprocess outright. That's deliberate: a Python thread can't be cancelled once it's inside the C regex engine, but a process can, and a demo that quietly hangs for several minutes isn't a demo anyone runs twice.

```bash
git clone https://github.com/themsquared/redos-guardrail-demo.git
cd redos-guardrail-demo
python3 demo.py
```

Output, lightly trimmed:

```
Scenario 1: ANSI-escape stripping (ToolOutputLimits.strip_ansi shape)
  input size |     vulnerable |      fixed
          10 |        116.0ms |    28.68ms
          20 |     TIMEOUT>3s |    33.99ms
  -> vulnerable pattern did not finish within 3s on a 20-byte input.

Scenario 2: token redaction (redact_secrets jwt pattern shape)
  input size |     vulnerable |      fixed
          15 |         28.8ms |    25.73ms
          20 |         43.1ms |    42.19ms
          25 |        523.0ms |    30.15ms
          30 |     TIMEOUT>3s |    39.48ms
```

The fixed column stays flat, 25 to 40 milliseconds, regardless of input size, because it consumes the string once, linearly. The vulnerable column is fine at 10 to 20 characters and gone by 30: an unbounded number of equivalent ways to split that short a string between two nested quantifiers already exceeds a 3-second budget. The real advisories describe this at a few hundred kilobytes taking tens of seconds to minutes, which tracks: an attacker doesn't need a large payload, just one shaped wrong.

## The lesson isn't about one project

Pydantic AI found both bugs, fixed them in a day, and published exactly what was wrong and what it affects. That's the process working as intended for a popular, widely used agent framework. The part that outlasts this specific release is where the bug lived: not in business logic, but in the code whose entire purpose was to make tool output safer to look at.

Any agent runtime, gateway, or guardrail that runs a regex against text an attacker can influence, tool output, a header, a file an agent read, anything that round-trips through a model, is carrying the same risk until someone has actually looked for nested quantifiers in that regex and tested it against a worst-case input. [What a cloud-native agent harness actually does](/what-is-a-cloud-native-agent-harness/) includes exactly this kind of boundary code: output limits, redaction, anything that sits between a tool's raw response and whatever reads it next. [MCP tool-call guardrails](/tool-call-guardrails/) are the request/response-filtering version of the same idea at the protocol layer. Both are worth auditing for this specific failure mode, not because either one is known to have it, but because the two advisories here prove the failure mode shows up in exactly this kind of code, in a project with real engineering behind it.

## Try it yourself

```bash
git clone https://github.com/themsquared/redos-guardrail-demo.git
cd redos-guardrail-demo
python3 demo.py
```

No dependencies beyond the Python 3 standard library, and it runs in about 10 seconds. The repo is public at [themsquared/redos-guardrail-demo](https://github.com/themsquared/redos-guardrail-demo). It's an illustrative reproduction of the vulnerability class, not pydantic-ai's literal source, since neither advisory publishes the actual pattern. What it leaves you with is a quick way to test whether a pattern you wrote has the same shape, before someone else finds out for you.

