Search by

mucan54 / laravel-transforms-request-guard

mucan54

Guards Laravel's global TrimStrings / ConvertEmptyStringsToNull middleware against the large-input-key denial of service, where a sub-1MB request can pin a PHP-FPM worker for ~55s. Backport-style protection for Laravel 11.x/12.x; 13.38.0+ ships the fix natively.

Package info

github.com/mucan54/laravel-transforms-request-guard

pkg:composer/mucan54/laravel-transforms-request-guard

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-11-23 03:58 UTC

This package is auto-updated.

Last update: 2026-11-23 04:14:33 UTC


README

Tests License

Protects Laravel applications against a denial-of-service in the global TrimStrings / ConvertEmptyStringsToNull middleware — where a request comfortably under 1 MB can pin a PHP-FPM worker for tens of seconds, with no authentication and no knowledge of your routes.

Instead of adding another middleware, this package swaps the vulnerable framework classes for hardened subclasses through the service container, so the fix lives exactly where the bug is and adds no extra pass over the input.

Already on Laravel 13.38.0 or newer? The framework ships this fix natively and you do not need this package. It exists for apps still on Laravel 11.x / 12.x (or earlier 13.x), and as defense-in-depth with a tighter, configurable budget.

The problem

TrimStrings and ConvertEmptyStringsToNull both extend Illuminate\Foundation\Http\Middleware\TransformsRequest, which runs before routing on every request. It walks the entire input recursively and, for each leaf element, rebuilds a dotted key path (parentKey.childKey...) and runs Str::is() against it.

A payload shaped like one very long key mapped to a large array defeats this:

{ "<a ~395 KB multibyte key>": [0, 0, 0, /* ...200,000 zeros... */] }

Each of the 200,000 elements forces a fresh ~395 KB key-string build plus a unicode regex match over it. The work is O(keyLength × elementCount), the request body stays under the ~1 MB default limits (so Cloudflare, nginx client_max_body_size and PHP post_max_size all pass it), and it needs no auth.

Measured against the real framework middleware (Laravel 13.37.0), a single ~790 KB request:

key size array size time (1 request) peak RSS
100 KB 20,000 1.48 s 2 MB
395 KB 50,000 13.7 s 6 MB
395 KB 200,000 55.2 s 10 MB

The weapon is CPU time, not memory — resident memory stays flat while one worker is held well past a typical max_execution_time. A handful of concurrent requests exhausts the FPM pool.

How this package fixes it

Laravel resolves global middleware through the service container, so the kernel's TrimStrings::class / ConvertEmptyStringsToNull::class entries can be transparently rebound. On boot this package binds them to FixedTrimStrings and FixedConvertEmptyStringsToNull, which override only clean() and cleanArray() via the CapsInputKeyBytes trait.

The override reproduces the exact key-byte accounting of the official 13.38.0 fix: the cumulative size of all input keys (counted with their dot-path prefixes) is capped, and the request is rejected with Illuminate\Http\Exceptions\PostTooLargeException (HTTP 413) the moment the budget is exceeded — inside the single traversal the middleware already performs, before any expensive per-element work runs. Trimming and empty-to-null behaviour are inherited unchanged.

Against the same 395 KB × 200,000 payload the hardened class rejects in ~20 ms instead of 55 s, and ordinary input is still trimmed exactly as before.

No vendor files are patched, so the fix survives composer update.

Installation

composer require mucan54/laravel-transforms-request-guard

The service provider is auto-discovered and performs the swap automatically. There is nothing else to do.

Configuration (optional)

Publish the config file:

php artisan vendor:publish --tag=transforms-request-guard-config
// config/transforms-request-guard.php
return [
    // Swap the framework middleware for the hardened subclasses.
    'enabled' => (bool) env('TRANSFORMS_REQUEST_GUARD_ENABLED', true),

    // Reject once cumulative input-key bytes exceed this. Default: 20 MiB
    // (matches laravel/framework 13.38.0). Set to 0 to keep the swap but
    // disable the cap.
    'max_key_bytes' => (int) env('TRANSFORMS_REQUEST_GUARD_MAX_KEY_BYTES', 20 * 1024 * 1024),
];

Most apps never legitimately send multi-megabyte input keys, so a much tighter budget is a reasonable hardening step:

TRANSFORMS_REQUEST_GUARD_MAX_KEY_BYTES=262144   # 256 KB

Binding it yourself

If you set enabled to false and want to wire the swap manually (e.g. in your own service provider):

use Illuminate\Foundation\Http\Middleware\TrimStrings;
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
use Mucan54\TransformsRequestGuard\Http\Middleware\FixedTrimStrings;
use Mucan54\TransformsRequestGuard\Http\Middleware\FixedConvertEmptyStringsToNull;

$this->app->bind(TrimStrings::class, FixedTrimStrings::class);
$this->app->bind(ConvertEmptyStringsToNull::class, FixedConvertEmptyStringsToNull::class);

Notes

  • Normal input is untouched; only pathological requests are rejected.
  • TrimStrings::except() / skipWhen() keep working — the hardened classes inherit them.
  • The counter resets per request, so it is safe under Octane.

Testing

# Dependency-free smoke test (no Composer install required)
php tests/standalone.php

# Full suite (requires dev dependencies)
composer install
vendor/bin/phpunit

Credits

License

MIT. See LICENSE.