Skip to main content
RU Digital Services
Laravel & Backend 3 min read •

Hardening Laravel 11 Against Brute-Force and PDF Denial-of-Service Attacks

Most developers rate-limit /login and stop there. Here are the overlooked CPU and disk exhaustion vectors we audit on every Laravel deployment—including DomPDF and file uploads.

AA
Aaqib Khalique
Chief Technology Officer (CTO) • RU Digital Services

When teams run a security checklist on a Laravel app, they usually check CSRF tokens, SQL injection, and password hashing. Those are table stakes—Laravel handles most of them out of the box. The attacks that actually take down small and mid-sized PHP applications in production are much simpler: resource exhaustion.

The Unthrottled PDF Generation Trap

Generating a formatted PDF with DomPDF takes roughly 300ms to 600ms of CPU time and 30MB to 50MB of RAM per request. If you expose a public download route like GET /academy/syllabus-pdf without rate limiting or caching, a single script sending 30 requests per second will saturate every PHP-FPM or LiteSpeed worker on your server in under five seconds.

The fix takes two layers: throttle the route and cache the rendered PDF binary keyed by the underlying data's last update timestamp.

// routes/web.php
Route::get('/academy/syllabus-pdf', [StaticPageController::class, 'syllabusPdf'])
    ->name('academy.syllabus.pdf')
    ->middleware('throttle:10,1');
// Inside StaticPageController::syllabusPdf()
$cacheKey = 'academy_syllabus_pdf_' . md5(
    (string) $modules->max('updated_at') . '|' . ($activeCohort?->id ?? 0)
);

$pdfBinary = Cache::remember($cacheKey, 3600, function () use ($modules, $activeCohort): string {
    return Pdf::loadView('academy.syllabus-pdf', [
        'modules' => $modules,
        'activeCohort' => $activeCohort,
    ])->setPaper('a4', 'portrait')->output();
});

return response($pdfBinary, 200, [
    'Content-Type' => 'application/pdf',
    'Content-Disposition' => 'attachment; filename="syllabus.pdf"',
]);

Now DomPDF runs at most once per hour (or whenever an admin edits the curriculum), and serving the cached bytes takes less than 5 milliseconds.

Rate-Limiting Authenticated File Uploads

Developers often leave authenticated routes unthrottled because "the user is logged in." If a single student account is compromised—or someone writes a loop against your receipt or avatar upload endpoint—unthrottled 5MB uploads combined with server-side WebP conversion will rapidly fill disk storage and spike CPU.

Put explicit throttle:10,1 middleware on every upload route, validate MIME types strictly (never trust file extensions alone), generate random UUID filenames, and store uploaded files outside public/ so they can never be executed as PHP scripts.

Masking PII on Public Verification URLs

QR-verified receipts and certificates must be publicly viewable by employers or banks, but public access should never mean full PII exposure. If a guest opens a receipt URL without logging in, mask their email (za***@example.com) and phone number (+9230****567), and reveal full contact fields only when the logged-in user is the owning student or an administrator.

Essential Security Headers Middleware

Attach X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, Referrer-Policy: strict-origin-when-cross-origin, Strict-Transport-Security, and a clean Content-Security-Policy in a global middleware. Verify your CSP in Chrome DevTools after every deployment so legitimate scripts like Turnstile or Cloudflare analytics aren't accidentally blocked.

Written by Aaqib Khalique

Need help architecting a Laravel backend, Kotlin Android app, or joining our weekend engineering cohort?

Related Engineering Guides

All articles →
Send Inquiry WhatsApp Chat
Cookie & Privacy Notice

We use essential cookies to maintain security and authentication, as well as optional analytical cookies to deliver relevant content and analyze traffic. Read our Privacy Policy.