Close anonymous write access to the public API and repair the broken test suite
Deploy Backend / deploy (push) Has been cancelled
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:
1 parent
35686d82c3
commit
d2f6bd4d7d
29 files changed
+1002
-188
No files matched your search
@@ -2,12 +2,13 @@
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
|
||||
* @extends Factory<User>
|
||||
*/
|
||||
class UserFactory extends Factory
|
||||
{
|
||||
@@ -37,8 +38,18 @@ class UserFactory extends Factory
|
||||
*/
|
||||
public function unverified(): static
|
||||
{
|
||||
return $this->state(fn(array $attributes) => [
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'email_verified_at' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* An account allowed to modify site content.
|
||||
*/
|
||||
public function admin(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'is_admin' => true,
|
||||
]);
|
||||
}
|
||||
}
|
||||
+99
-53
@@ -2,72 +2,118 @@
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Collapse the legacy "<table>_translations" rows into locale => value JSON
|
||||
* on the parent table.
|
||||
*
|
||||
* Written in PHP rather than MySQL's JSON_OBJECTAGG so the migration runs
|
||||
* on MySQL, MariaDB and SQLite alike. The previous implementation used
|
||||
* `UPDATE <table> alias SET alias.col = (SELECT JSON_OBJECTAGG(...))`,
|
||||
* which is MySQL-only and aborted the entire test suite during migration.
|
||||
*/
|
||||
private function collapseTranslations(string $table, string $foreignKey, array $columns): void
|
||||
{
|
||||
$translationsTable = $table.'_translations';
|
||||
|
||||
if (! Schema::hasTable($table) || ! Schema::hasTable($translationsTable)) {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::table($table)->orderBy('id')->chunkById(200, function ($parents) use ($table, $translationsTable, $foreignKey, $columns) {
|
||||
foreach ($parents as $parent) {
|
||||
$rows = DB::table($translationsTable)->where($foreignKey, $parent->id)->get();
|
||||
|
||||
$update = [];
|
||||
|
||||
foreach ($columns as $column) {
|
||||
$map = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
if (isset($row->{$column}) && $row->{$column} !== null) {
|
||||
$map[$row->locale] = $row->{$column};
|
||||
}
|
||||
}
|
||||
|
||||
$update[$column] = $map === []
|
||||
? null
|
||||
: json_encode($map, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
if ($update !== []) {
|
||||
DB::table($table)->where('id', $parent->id)->update($update);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function up(): void
|
||||
{
|
||||
// Add JSON columns for spatie/laravel-translatable on website_about_me
|
||||
Schema::table('website_about_me', function (Blueprint $table) {
|
||||
if (!Schema::hasColumn('website_about_me', 'title')) {
|
||||
$table->json('title')->nullable()->after('image');
|
||||
}
|
||||
if (!Schema::hasColumn('website_about_me', 'description')) {
|
||||
$table->json('description')->nullable()->after('title');
|
||||
}
|
||||
});
|
||||
// `Schema::hasColumn()` is checked up front: calling it inside the
|
||||
// `Schema::table()` closure issues a query mid-DDL, which implicitly
|
||||
// commits on MySQL.
|
||||
$addAboutMeTitle = ! Schema::hasColumn('website_about_me', 'title');
|
||||
$addAboutMeDescription = ! Schema::hasColumn('website_about_me', 'description');
|
||||
|
||||
// Migrate data from translations table to JSON columns
|
||||
DB::statement('
|
||||
UPDATE website_about_me a
|
||||
SET a.title = (
|
||||
SELECT JSON_OBJECTAGG(locale, title)
|
||||
FROM website_about_me_translations t
|
||||
WHERE t.about_me_id = a.id
|
||||
),
|
||||
a.description = (
|
||||
SELECT JSON_OBJECTAGG(locale, description)
|
||||
FROM website_about_me_translations t
|
||||
WHERE t.about_me_id = a.id
|
||||
)
|
||||
');
|
||||
if ($addAboutMeTitle || $addAboutMeDescription) {
|
||||
Schema::table('website_about_me', function (Blueprint $table) use ($addAboutMeTitle, $addAboutMeDescription) {
|
||||
if ($addAboutMeTitle) {
|
||||
$table->json('title')->nullable()->after('image');
|
||||
}
|
||||
|
||||
// Add JSON columns for spatie/laravel-translatable on website_skills
|
||||
Schema::table('website_skills', function (Blueprint $table) {
|
||||
if (!Schema::hasColumn('website_skills', 'title')) {
|
||||
$table->json('title')->nullable()->after('image');
|
||||
}
|
||||
if (!Schema::hasColumn('website_skills', 'description')) {
|
||||
$table->json('description')->nullable()->after('title');
|
||||
}
|
||||
});
|
||||
if ($addAboutMeDescription) {
|
||||
$table->json('description')->nullable()->after('title');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Migrate data from skill translations table to JSON columns
|
||||
DB::statement('
|
||||
UPDATE website_skills s
|
||||
SET s.title = (
|
||||
SELECT JSON_OBJECTAGG(locale, title)
|
||||
FROM website_skill_translations t
|
||||
WHERE t.skill_id = s.id
|
||||
),
|
||||
s.description = (
|
||||
SELECT JSON_OBJECTAGG(locale, description)
|
||||
FROM website_skill_translations t
|
||||
WHERE t.skill_id = s.id
|
||||
)
|
||||
');
|
||||
$this->collapseTranslations('website_about_me', 'about_me_id', ['title', 'description']);
|
||||
|
||||
$addSkillTitle = ! Schema::hasColumn('website_skills', 'title');
|
||||
$addSkillDescription = ! Schema::hasColumn('website_skills', 'description');
|
||||
|
||||
if ($addSkillTitle || $addSkillDescription) {
|
||||
Schema::table('website_skills', function (Blueprint $table) use ($addSkillTitle, $addSkillDescription) {
|
||||
if ($addSkillTitle) {
|
||||
$table->json('title')->nullable()->after('image');
|
||||
}
|
||||
|
||||
if ($addSkillDescription) {
|
||||
$table->json('description')->nullable()->after('title');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$this->collapseTranslations('website_skills', 'skill_id', ['title', 'description']);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('website_about_me', function (Blueprint $table) {
|
||||
$table->dropColumn(['title', 'description']);
|
||||
});
|
||||
// Guarded to mirror up(): the columns may predate this migration.
|
||||
$aboutMe = array_values(array_filter(
|
||||
['title', 'description'],
|
||||
fn (string $column) => Schema::hasColumn('website_about_me', $column)
|
||||
));
|
||||
|
||||
Schema::table('website_skills', function (Blueprint $table) {
|
||||
$table->dropColumn(['title', 'description']);
|
||||
});
|
||||
if ($aboutMe !== []) {
|
||||
Schema::table('website_about_me', function (Blueprint $table) use ($aboutMe) {
|
||||
$table->dropColumn($aboutMe);
|
||||
});
|
||||
}
|
||||
|
||||
$skills = array_values(array_filter(
|
||||
['title', 'description'],
|
||||
fn (string $column) => Schema::hasColumn('website_skills', $column)
|
||||
));
|
||||
|
||||
if ($skills !== []) {
|
||||
Schema::table('website_skills', function (Blueprint $table) use ($skills) {
|
||||
$table->dropColumn($skills);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* StorePostRequest declares `excerpt` as nullable, but the column was
|
||||
* created NOT NULL, so every request that legitimately omitted an excerpt
|
||||
* died with a SQL integrity error (HTTP 500). Align the schema with the
|
||||
* already-published validation contract.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('posts', function (Blueprint $table) {
|
||||
$table->text('excerpt')->nullable()->change();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
// Backfill first: existing NULLs would otherwise fail the NOT NULL
|
||||
// constraint when rolling back.
|
||||
DB::table('posts')
|
||||
->whereNull('excerpt')
|
||||
->update(['excerpt' => '']);
|
||||
|
||||
Schema::table('posts', function (Blueprint $table) {
|
||||
$table->text('excerpt')->nullable(false)->change();
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Every write endpoint on this API was previously reachable without any
|
||||
* authentication, which let anonymous visitors create/edit/delete content.
|
||||
* Authorisation is enforced by the `admin` middleware, which requires this
|
||||
* flag in addition to a valid Sanctum session.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
if (! Schema::hasColumn('users', 'is_admin')) {
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->boolean('is_admin')->default(false)->after('password');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
if (Schema::hasColumn('users', 'is_admin')) {
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->dropColumn('is_admin');
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user