laravelstudentsappwithinertia: Locked Page, Open API: A Two-Star Laravel App's Quiet Security Lesson

Inside jobic10/laravelstudentsappwithinertia, where the front door is guarded, the back door is missing, and the course name never became a foreign key.

7 min read • View on GitHub • More from jobic10

Editorial illustration of a modest single-story house. Its front door is fitted with an oversized padlock and a heavy chain drawn in dense crosshatching, while around the side a plain back door stands open with no lock, and small figures walk through it in single file carrying clipboards and folders. The image represents a web app that guards its visible page while leaving the data endpoint that feeds it completely open.
The front door is chained. The back door is not even closed.
Key Takeaways

The Front Door Is Locked. The Back Door Isn't.

Two route files, two different ideas about who gets in. The web routes guard the page you look at. The API routes guard almost nothing.

// routes/web.php
Route::middleware(['auth:sanctum', 'verified'])->get('/students', function () {
    return Inertia::render('Students');
})->name('students');
// routes/api.php
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

Route::apiResource('students', 'App\Http\Controllers\StudentController');
Route::apiResource('courses', 'App\Http\Controllers\CourseController');

The first route is guarded twice over: a Sanctum guard and a verified email check. The second group is guarded by nothing. apiResource expands to index, store, show, update, and destroy for both students and courses, and every one of those endpoints answers an unauthenticated request.

So the page you have to log in to see, and the data you do not. Any client can list, create, modify, or delete every student record without ever logging in. The page is gated, but its data is not. The protection is cosmetic.

How Inertia Splits One Page Into Two Requests

Inertia's contract is simple. The server returns a page component name plus props, the client hydrates it, and the data arrives with the HTML. One request, one guard, one place to get it wrong. The whole point is that you never build a second data path.

This repo renders the Students shell with no props at all. So the page has to fetch its own data, and it calls /api/students from the client. That is a second request, down a second path, with its own middleware and its own guard. Two data paths for one screen, and only one of them got guarded.

One screen, two requests, two guards. Only one of them was written.

The tell is in HandleInertiaRequests::share(). It merges the parent props with an empty array. That method is where global props like the authenticated user are supposed to live. Leaving it blank says props were never the plan.

The Course Name That Should Have Been a Foreign Key

The students table has a course column, and it is a string. The form sets each option's value to the course's name, so every student row stores a copy of a name that also lives in the courses table.

// create_students_table migration
$table->string('course');
$table->date('date_of_enrollment');
<!-- Students.vue -->
<option :value="course.course_name" v-for='course in courses' :key='course.id'>
Close-up editorial illustration of a single index card pinned to a board, headed STUDENT, with a field labeled COURSE filled in by careful handwriting. Behind it a shallow stack of dozens of identical cards shows the same course name copied into the same field by hand. At the edge of frame a separate master card labeled COURSES shows a newer name, with a snapped thread dangling between the two cards and its loose end frayed. It visualizes a denormalized string column and the silent break that follows a rename.
Every student row copies the course name. Renaming the course snaps the thread.

Rename a course and every student still points at the old name. Delete a course and the students are orphaned with no error. Neither model declares belongsTo or hasMany, because there is no relationship to declare.

Actioncourse stringForeign key
Rename a courseStudents keep the old nameStudents follow the new name
Delete a courseStudents silently orphanedRestrict or cascade, by choice
Filter students by courseString matchIndexed join

It is the same instinct as the open API. Reach for the simple thing, skip the constraint. A tutorial can afford it. Anything holding real records cannot.

A Time Capsule From January 2021

The repo was created on 2021-01-17, the same month Laravel Jetstream launched. That timing explains almost everything odd about it.

Tailwind arrives from Jetstream, and Bootstrap modal attributes (data-toggle="modal", data-target="#addStudent") sit on the same page. About thirty Jetstream components were copied into resources/js/Jetstream/. auth:sanctum guards a web route where plain auth would do. And response('Student Created', 201) returns plain text from an API that claims to speak JSON.

The unfinished edges belong to the same picture. find() calls are unchecked, so a missing ID throws a 500 instead of a 404. Features::api() is commented out in the config while API token permissions are still configured. The README is the untouched Laravel default. None of this is a failure of skill. It is a scaffold built fast and never revisited.

Props or API? A Decision Table

The repo's real error was not choosing an API. It was doing both without deciding. Here is how to decide.

DimensionData via Inertia propsData via separate JSON API
Auth surfaceOne guarded routeA second route that must be guarded separately
Initial page loadData arrives with the HTML, no extra round tripShell loads, then a fetch fires
Client-side updatesFull page visits or Inertia partial reloadsNative fetch, no Inertia involvement
CouplingPage and data move togetherPage and data can drift apart
Failure modeMissing prop, visible immediatelyUnguarded endpoint, invisible until someone finds it

The last row is the point. When data flows through props, a missing prop is visible the moment you load the page. When it flows through a separate endpoint, an unguarded one is invisible until someone goes looking.

What to Steal, What to Avoid