Close anonymous write access to the public API and repair the broken test suite
Deploy Backend / deploy (push) Has been cancelled

Every mutating endpoint was reachable without authentication, so any visitor
could create, edit or delete posts, categories, tags, projects and about-me
records on the live site.

Security
- Split routes into an anonymous read-only group and a write group gated by
  `auth:sanctum` + a new `admin` middleware.
- Add `is_admin` to users; Sanctum token support on the User model.
- Add AuthController (login / me / logout). The Sanctum token is delivered in
  an httpOnly cookie and never returned in the JSON body, because the admin
  panel reaches the API through a Next.js server-side proxy that breaks
  Sanctum's first-party domain detection.
- AuthenticateAdminPanelSession promotes that cookie to a bearer header so
  Sanctum's normal validation, expiry and revocation still apply.
- Remove the unauthenticated `/user/{id}` route, which exposed user emails.
- Non-admin accounts are refused at login and the failure shape is identical
  to a bad password, so the endpoint cannot enumerate accounts.
- Fix `Route [login] not defined`: a non-JSON unauthenticated request resolved
  a route that does not exist in this API-only app and returned 500.
- Reject invalid list query parameters with 422 instead of 500, and cap
  per_page at 50 (`?per_page=100000` previously dumped whole tables).

Correctness
- Make the translation migration portable. It used MySQL-only JSON_OBJECTAGG,
  which aborted all 29 tests during migration; the data collapse now runs in
  PHP so it works on MySQL, MariaDB and SQLite.
- Add HasJsonTranslations trait providing the missing setTranslation(), which
  made project create/update return 500, and replace five copy-pasted
  getTranslation()/hasTranslation() bodies.
- Make posts.excerpt nullable to match StorePostRequest; omitting an excerpt
  was a guaranteed SQL integrity error.
- Route the AboutMeController CRUD methods that existed but were never
  registered.
- CategoryTest omitted a required description; fixed the test, the existing
  schema and validation already agreed.

Tests: 83 passing / 126 assertions (was 29 failing / 0 assertions)
This commit is contained in:
root authored and DevOps committed 2026-10-01 18:47:34 +00:00
1 parent 35686d82c3
commit d2f6bd4d7d
29 files changed
+1002 -188

No files matched your search

+76 -11
View File
@@ -1,6 +1,6 @@
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\Blog\CategoryController;
use App\Http\Controllers\Api\Blog\HomeController;
use App\Http\Controllers\Api\Blog\PostController;
@@ -8,26 +8,91 @@ use App\Http\Controllers\Api\Blog\TagController;
use App\Http\Controllers\Api\Website\AboutMeController;
use App\Http\Controllers\Api\Website\ProjectController;
use App\Http\Controllers\Api\Website\ServiceController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Authentication
|--------------------------------------------------------------------------
| Used by the admin panel. Login is throttled hard because it is the one
| endpoint that accepts attacker-controlled credentials.
*/
Route::post('auth/login', [AuthController::class, 'login'])
->middleware('throttle:5,1')
->name('auth.login');
Route::middleware(['auth:sanctum', 'throttle:60,1'])->group(function () {
Route::get('auth/me', [AuthController::class, 'me'])->name('auth.me');
Route::post('auth/logout', [AuthController::class, 'logout'])->name('auth.logout');
});
/*
|--------------------------------------------------------------------------
| Public read API
|--------------------------------------------------------------------------
| Anonymous, cacheable, GET-only. Nothing here can change state.
*/
Route::middleware('throttle:60,1')->group(function () {
Route::prefix('website')->group(function () {
Route::get('about-me', [AboutMeController::class, 'index']);
Route::get('about-me/{id}', [AboutMeController::class, 'show']);
Route::get('about-me/{id}/translation/{locale}', [AboutMeController::class, 'getTranslation']);
Route::get('services/{locale}', [ServiceController::class, 'index']);
Route::get('services/{id}/translation/{locale}', [ServiceController::class, 'showTranslation']);
Route::group(['prefix' => 'projects'], function () {
Route::get('/translation/{locale}', [ProjectController::class, 'index']);
Route::get('/{id}/translation/{locale}', [ProjectController::class, 'show']);
Route::post('/', [ProjectController::class, 'store']);
Route::put('/{id}', [ProjectController::class, 'update']);
Route::delete('/{id}', [ProjectController::class, 'destroy']);
});
Route::get('projects/translation/{locale}', [ProjectController::class, 'index']);
Route::get('projects/{id}/translation/{locale}', [ProjectController::class, 'show']);
});
Route::prefix('blog')->group(function () {
Route::apiResource('posts', PostController::class);
Route::apiResource('categories', CategoryController::class);
Route::apiResource('tags', TagController::class);
Route::get('homepage', [HomeController::class, 'index']);
Route::get('posts', [PostController::class, 'index']);
Route::get('posts/{slug}', [PostController::class, 'show']);
Route::get('categories', [CategoryController::class, 'index']);
Route::get('categories/{id}', [CategoryController::class, 'show']);
Route::get('categories/{slug}/posts', [CategoryController::class, 'getCategoryWithPosts']);
Route::get('tags', [TagController::class, 'index']);
Route::get('tags/{id}', [TagController::class, 'show']);
});
});
/*
|--------------------------------------------------------------------------
| Admin write API
|--------------------------------------------------------------------------
| Every mutating endpoint requires an authenticated admin. These routes were
| previously anonymous, which allowed anyone to create, edit or delete every
| post, category, tag, project and about-me record on the live site.
*/
Route::middleware(['auth:sanctum', 'admin', 'throttle:30,1'])->group(function () {
Route::prefix('website')->group(function () {
Route::post('about-me', [AboutMeController::class, 'store']);
Route::put('about-me/{id}', [AboutMeController::class, 'update']);
Route::delete('about-me/{id}', [AboutMeController::class, 'destroy']);
Route::post('projects', [ProjectController::class, 'store']);
Route::put('projects/{id}', [ProjectController::class, 'update']);
Route::delete('projects/{id}', [ProjectController::class, 'destroy']);
});
Route::prefix('blog')->group(function () {
Route::post('posts', [PostController::class, 'store']);
Route::put('posts/{id}', [PostController::class, 'update']);
Route::delete('posts/{id}', [PostController::class, 'destroy']);
Route::post('categories', [CategoryController::class, 'store']);
Route::put('categories/{id}', [CategoryController::class, 'update']);
Route::delete('categories/{id}', [CategoryController::class, 'destroy']);
Route::post('tags', [TagController::class, 'store']);
Route::put('tags/{id}', [TagController::class, 'update']);
Route::delete('tags/{id}', [TagController::class, 'destroy']);
});
});