Posts, projects and about-me all carry an image column that could only ever be filled by typing a URL by hand, so every featured image in the database is still a placeholder. Uploads are the missing half of content management. Filenames are generated rather than reused from the client, which is what keeps a double extension or a path traversal out of a web-served directory. The upload surface is restricted to raster formats (no SVG, which can carry script) with a size and dimension ceiling, and throttled at 12/min against the general admin API's 30/min because an upload costs far more than a JSON write. Routes move to per-feature files registered from bootstrap/app.php, since api.php had become a chokepoint for parallel work on this repo.
176 lines
5.9 KiB
PHP
176 lines
5.9 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Laravel\Sanctum\Sanctum;
|
|
use Tests\TestCase;
|
|
|
|
class MediaUploadTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Storage::fake('public');
|
|
}
|
|
|
|
public function test_anonymous_requests_cannot_upload(): void
|
|
{
|
|
$this->post('/api/admin/media', ['file' => UploadedFile::fake()->image('cover.jpg')])
|
|
->assertUnauthorized();
|
|
}
|
|
|
|
public function test_non_admins_cannot_upload(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create());
|
|
|
|
$this->post('/api/admin/media', ['file' => UploadedFile::fake()->image('cover.jpg')])
|
|
->assertForbidden();
|
|
}
|
|
|
|
public function test_admins_can_upload_an_image(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$this->post('/api/admin/media', ['file' => UploadedFile::fake()->image('cover.jpg', 400, 300)])
|
|
->assertCreated()
|
|
->assertJsonStructure(['data' => ['url', 'path', 'name', 'size', 'mime']]);
|
|
|
|
Storage::disk('public')->assertExists($this->storedPath());
|
|
}
|
|
|
|
public function test_upload_requires_a_file(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$this->postJson('/api/admin/media')->assertStatus(422)->assertJsonValidationErrors('file');
|
|
}
|
|
|
|
public function test_upload_rejects_a_non_image_disguised_as_an_image(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$this->post('/api/admin/media', [
|
|
'file' => UploadedFile::fake()->createWithContent('payload.php', '<?php echo 1;'),
|
|
])->assertStatus(422)->assertJsonValidationErrors('file');
|
|
}
|
|
|
|
/**
|
|
* SVG is refused on purpose: it can carry script, and the site serves the
|
|
* upload directory from the same origin as the admin panel.
|
|
*/
|
|
public function test_upload_rejects_svg(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$this->post('/api/admin/media', [
|
|
'file' => UploadedFile::fake()->createWithContent(
|
|
'payload.svg',
|
|
'<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>',
|
|
),
|
|
])->assertStatus(422)->assertJsonValidationErrors('file');
|
|
}
|
|
|
|
public function test_upload_rejects_a_file_over_the_size_limit(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$this->post('/api/admin/media', [
|
|
'file' => UploadedFile::fake()->create('big.jpg', 5120, 'image/jpeg'),
|
|
])->assertStatus(422)->assertJsonValidationErrors('file');
|
|
}
|
|
|
|
/**
|
|
* The client-supplied filename must never reach the filesystem verbatim,
|
|
* otherwise a double extension or a traversal could be smuggled in.
|
|
*/
|
|
public function test_upload_ignores_the_client_filename(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$this->post('/api/admin/media', [
|
|
'file' => UploadedFile::fake()->image('../../evil.php.jpg', 200, 200),
|
|
])->assertCreated();
|
|
|
|
$stored = $this->storedPath();
|
|
|
|
$this->assertStringStartsWith('media/', $stored);
|
|
$this->assertStringNotContainsString('..', $stored);
|
|
$this->assertStringNotContainsString('evil.php', $stored);
|
|
}
|
|
|
|
public function test_admins_can_delete_an_uploaded_image(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$path = UploadedFile::fake()->image('cover.jpg')->store('media', 'public');
|
|
|
|
$this->deleteJson("/api/admin/media/{$path}")->assertOk();
|
|
|
|
Storage::disk('public')->assertMissing($path);
|
|
}
|
|
|
|
public function test_delete_rejects_a_path_traversal(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
Storage::disk('public')->put('.env', 'SECRET=1');
|
|
|
|
// The route pattern itself already blocks `..`, so this asserts the
|
|
// defence survives even if that pattern is ever loosened.
|
|
$this->deleteJson('/api/admin/media/media/../../.env')->assertStatus(404);
|
|
|
|
Storage::disk('public')->assertExists('.env');
|
|
}
|
|
|
|
public function test_delete_rejects_a_path_outside_the_media_directory(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
Storage::disk('public')->put('secret.txt', 'nope');
|
|
|
|
$this->deleteJson('/api/admin/media/secret.txt')->assertStatus(404);
|
|
|
|
Storage::disk('public')->assertExists('secret.txt');
|
|
}
|
|
|
|
public function test_deleting_a_missing_file_is_a_404(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$this->deleteJson('/api/admin/media/media/2026/01/missing.jpg')->assertNotFound();
|
|
}
|
|
|
|
public function test_uploads_are_rate_limited_harder_than_other_admin_writes(): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
// 12 allowed, the 13th is throttled. Keeps the disk-exhaustion cost
|
|
// of the upload endpoint far below the general admin API budget.
|
|
foreach (range(1, 12) as $ignored) {
|
|
$this->post('/api/admin/media', [
|
|
'file' => UploadedFile::fake()->image("cover{$ignored}.jpg", 10, 10),
|
|
])->assertCreated();
|
|
}
|
|
|
|
$this->post('/api/admin/media', ['file' => UploadedFile::fake()->image('overflow.jpg', 10, 10)])
|
|
->assertStatus(429);
|
|
}
|
|
|
|
/**
|
|
* Read the single path written by the fake disk during a request.
|
|
*/
|
|
private function storedPath(): string
|
|
{
|
|
$files = Storage::disk('public')->allFiles('media');
|
|
|
|
$this->assertCount(1, $files, 'expected exactly one stored upload');
|
|
|
|
return $files[0];
|
|
}
|
|
}
|