The profile form has two fields: name and email. Add one more field to the request, is_admin=1, and the user is an admin.
No password cracking and no SQL injection. This is mass assignment, and two lines of very common Laravel code make it possible. Below I run the attack against a fresh Laravel 13.43 app (PHP 8.5, SQLite, on my own machine), then shut it down four different ways. Every output shown is real.
Video version:
The vulnerable code
// app/Models/Member.php class Member extends Model { protected $guarded = []; // "I'm tired of maintaining $fillable" } // routes/web.php Route::put('/profile', function (Request $request) { $user = Member::find(1); $user->update($request->all()); return $user->only('id', 'name', 'email', 'is_admin'); }); The members table has name, email and is_admin (default false).
$guarded = [] turns off Eloquent's mass-assignment protection. $request->all() passes every field the client sent, not just the ones your form shows.
The attack
A normal request:
$ curl -X PUT localhost:8000/profile -d name=Ana -d email=ana@example.com {"id":1,"name":"Ana","email":"ana@example.com","is_admin":0} The same request with one extra field:
$ curl -X PUT localhost:8000/profile -d name=Ana -d email=ana@example.com -d is_admin=1 {"id":1,"name":"Ana","email":"ana@example.com","is_admin":"1"} That's the whole attack. Anyone can add a field from the browser's dev tools, and CSRF protection doesn't help, because the request comes from the logged-in user's own session. (I turned CSRF off for this route only so the demo could use plain curl.)
This class of bug is old and famous. In 2012, a developer exploited mass assignment in Ruby on Rails to add his own key to the Rails organization on GitHub and push a commit. That incident is a big part of why frameworks like Laravel protect you by default. $guarded = [] throws that protection away.
Fix 1: allow-list the fields with #[Fillable]
Laravel 13 lets you declare fillable fields with a PHP attribute. The fresh User model in a new Laravel 13 app already uses it:
use Illuminate\Database\Eloquent\Attributes\Fillable; #[Fillable(['name', 'email'])] class Member extends Model { } The classic protected $fillable = ['name', 'email']; still works too. Same attack, same curl:
{"id":1,"name":"Ana","email":"ana@example.com","is_admin":0} is_admin is silently dropped.
Fix 2: never pass $request->all()
$data = $request->validate([ 'name' => 'required|string|max:100', 'email' => 'required|email', ]); $user->update($data); validate() returns only the fields that have rules. A Form Request's validated() method does the same. I tested this fix on its own, with $guarded = [] still in the model, and the attack failed: is_admin stayed 0.
Use both Fix 1 and Fix 2. Then a single mistake (a new route that forgets validation, or a model someone unguards) doesn't open the door.
The trap: block-lists with #[Guarded]
A tempting middle ground is to guard only the dangerous columns:
#[Guarded(['is_admin'])] class Member extends Model {} It works today. Six months later someone adds a role column, and nobody remembers to update the guarded list. I added a role column and sent role=superadmin:
{"id":1,"name":"Ana","email":"ana@x.com","is_admin":0,"role":"superadmin", …} It went straight in. An allow-list fails safe: new columns stay closed until you deliberately open them.
Fix 3: make Laravel throw instead of silently ignoring
Silently dropping unknown fields is safe, but it can also hide bugs in your own code, like a field you meant to save. Turn on strict behavior outside production:
// app/Providers/AppServiceProvider.php public function boot(): void { Model::preventSilentlyDiscardingAttributes(! app()->isProduction()); } Now, in local and test environments:
Member::find(1)->update(['name' => 'X', 'is_admin' => 1]); // Illuminate\Database\Eloquent\MassAssignmentException: // Add fillable property [is_admin] to allow mass assignment on [App\Models\Member]. Your test suite catches the mistake before production does. (Model::shouldBeStrict() turns this on together with lazy-loading prevention and missing-attribute checks.)
Fix 4: set sensitive fields explicitly, behind authorization
When an admin really does need to promote someone, don't route it through mass assignment at all:
Gate::authorize('promote', $member); $member->is_admin = true; $member->save(); Use Gate::authorize() here. Since Laravel 11, the base controller no longer includes the AuthorizesRequests trait, so $this->authorize() isn't there by default. If the gate says no, you get AuthorizationException: This action is unauthorized.
forceFill(['is_admin' => true]) also works, but it skips the fillable list entirely. Only pass values you control, never request data.
Recap
-
$guarded = []+$request->all()= anyone can make themselves admin. - Allow-list with
#[Fillable([...])](or$fillable). - Save
$request->validated(), never$request->all(). - Avoid block-lists (
#[Guarded]/$guarded): new columns become writable. -
Model::preventSilentlyDiscardingAttributes()in dev and tests. - Set sensitive fields explicitly, behind a gate. Never
forceFill()user input.
Now search your project for $guarded = []. Did you find one? 👀
I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.
