Bypassing Laravel's 20 MiB TrimStrings cap with 450,000 tiny arrays

Right after Laravel capped cumulative key bytes at 20 MiB to fix the 790 KB CPU DoS, a flat JSON list of tiny arrays stayed under the cap and caused RAM exhaustion instead.

Right after Taylor patched the 790 KB TrimStrings CPU DoS in Laravel 13.38.0 by capping cumulative key bytes at 20 MiB, I tested whether a different JSON shape could slip underneath that limit.

It could, and the failure mode was completely different. Where the first bug burned CPU by rebuilding one massive key over and over, this one left CPU mostly normal and caused RAM exhaustion instead.

A payload with no long keys

Instead of a single 395 KB key, the body is just a JSON array of tiny one-element arrays:

[[0],[0],[0],[0],[0],...]

There are no long keys and no deep nesting. With 450,000 inner arrays, the request body is about 1.8 MB on the wire. Because the generated dotted paths are short (0.0, 1.0, 2.0, and so on), the entire payload only adds up to roughly 6.08 MiB of cumulative key bytes per middleware pass, well below the new 20 MiB cap.

Both TrimStrings and ConvertEmptyStringsToNull still walked every single element before Laravel even looked for a route.

Why memory spiked

In local tests, a 300,000-element payload was only ~1.2 MB on the wire, yet the PHP process had allocated around 140 MiB of RAM by the time TrimStrings finished. Sending 30 parallel requests to a fresh Laravel Cloud instance knocked it offline in a few seconds.

Two things inside TransformsRequest and TrimStrings amplified the memory and work:

// src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php (before 31dbc2b)
protected function cleanArray(array $data, $keyPrefix = '')
{
    $prefixLength = strlen($keyPrefix);

    foreach ($data as $key => $value) {
        $this->keyBytes += $prefixLength + strlen((string) $key);

        if ($this->keyBytes > $this->maxKeyBytes) {
            throw new PostTooLargeException('The cumulative size of the input keys is too large.');
        }

        $data[$key] = $this->cleanValue($keyPrefix.$key, $value);
    }

    return $data;
}

First, unconditionally writing $data[$key] = ... on every iteration broke PHP's copy-on-write optimization, forcing PHP to duplicate hundreds of thousands of unchanged inner arrays in memory even though nothing was modified.

Second, TrimStrings::transform() checked exclusion patterns before checking whether the value was even a string:

// src/Illuminate/Foundation/Http/Middleware/TrimStrings.php (before 31dbc2b)
protected function transform($key, $value)
{
    $except = array_merge($this->except, static::$neverTrim);

    if ($this->shouldSkip($key, $except) || ! is_string($value)) {
        return $value;
    }

    return Str::trim($value);
}

So for every integer 0, Laravel merged the exclusion arrays and ran Str::is() on the path before realizing there was nothing to trim.

The mitigation

Taylor shipped a mitigation the same day in commit 31dbc2b (noting that completely blocking this class of payload at the framework layer would be a larger undertaking).

In TransformsRequest::cleanArray(), Laravel now checks if the value actually changed before writing it back, preserving copy-on-write for unchanged nested arrays:

$cleaned = $this->cleanValue($keyPrefix.$key, $value);

// Avoid copying arrays when none of their values have changed...
if ($cleaned !== $value) {
    $data[$key] = $cleaned;
}

And in TrimStrings::transform(), non-string values return immediately before touching $except:

protected function transform($key, $value)
{
    if (! is_string($value)) {
        return $value;
    }

    $except = array_merge($this->except, static::$neverTrim);

    if ($this->shouldSkip($key, $except)) {
        return $value;
    }

    return Str::trim($value);
}

The mitigation shipped in Laravel 13.39.0 on October 27, 2026 with no public CVE assigned. If you updated to 13.38.0 for the first TrimStrings fix, bump to 13.39.0+ to pick this one up as well.

Hristijan Manasijev · Member of Technical Staff at Native Teams & Laravel framework contributor.

Need a Laravel security or performance audit, or looking to build AI automations? Get in touch →