Close anonymous write access to the public API and repair the broken test suite
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
8b834189a4
29 files changed
+1002
-188
No files matched your search
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Blog\Category;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* The list endpoints previously forwarded raw request input into
|
||||
* `orderBy()` and `paginate()`, so a bad query string produced an HTTP 500
|
||||
* and `per_page` was unbounded.
|
||||
*/
|
||||
class ListQueryValidationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @return array<string, array{0: string}>
|
||||
*/
|
||||
public static function badQueryProvider(): array
|
||||
{
|
||||
return [
|
||||
'categories bad direction' => ['/api/blog/categories?sort_direction=foo'],
|
||||
'categories unknown column' => ['/api/blog/categories?sort_by=no_such_column'],
|
||||
'categories oversized page' => ['/api/blog/categories?per_page=100000'],
|
||||
'categories zero page size' => ['/api/blog/categories?per_page=0'],
|
||||
'posts unknown sort column' => ['/api/blog/posts?sort_field=password'],
|
||||
'posts oversized page' => ['/api/blog/posts?per_page=9999'],
|
||||
'tags bad direction' => ['/api/blog/tags?sort_direction=sideways'],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('badQueryProvider')]
|
||||
public function test_invalid_list_parameters_are_rejected(string $url): void
|
||||
{
|
||||
$this->getJson($url)->assertStatus(422);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array{0: string}>
|
||||
*/
|
||||
public static function goodQueryProvider(): array
|
||||
{
|
||||
return [
|
||||
'categories default' => ['/api/blog/categories'],
|
||||
'categories max page size' => ['/api/blog/categories?per_page=50'],
|
||||
'categories valid sort' => ['/api/blog/categories?sort_by=name&sort_direction=asc'],
|
||||
'categories filter' => ['/api/blog/categories?filter=web'],
|
||||
'posts default' => ['/api/blog/posts'],
|
||||
'posts valid sort' => ['/api/blog/posts?sort_field=views&sort_direction=asc'],
|
||||
'tags default' => ['/api/blog/tags'],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('goodQueryProvider')]
|
||||
public function test_valid_list_parameters_are_accepted(string $url): void
|
||||
{
|
||||
$this->getJson($url)->assertOk();
|
||||
}
|
||||
|
||||
public function test_page_size_is_capped(): void
|
||||
{
|
||||
Category::factory()->count(60)->create();
|
||||
|
||||
$response = $this->getJson('/api/blog/categories?per_page=50');
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('meta.per_page', 50)
|
||||
->assertJsonCount(50, 'data');
|
||||
}
|
||||
|
||||
public function test_defaults_are_unchanged_for_existing_consumers(): void
|
||||
{
|
||||
$response = $this->getJson('/api/blog/categories');
|
||||
|
||||
$response->assertOk()->assertJsonPath('meta.per_page', 10);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user