Files
SealShare/tests/Feature/SecurityAuditTest.php
T
Andreas Reinhold / reiniandClaude Opus 5.5 6578335568 Fix the security audit's findings
ShareDownload::$authenticated was a public property without #[Locked],
so a visitor with a share's link but not its password could set it from
the browser and see the file list: names, folder paths, sizes and
whether the share holds a text. The contents stayed protected; the flag
is now locked, and a test tries to set it.

Sessions hold the keys of the password shares a recipient unlocked, and
were stored unencrypted in the sessions table wherever SESSION_ENCRYPT
was not set, which includes every Docker install. They are now
encrypted unless SESSION_ENCRYPT=false. Share keys never live in the
session, so every share stays readable; people sign in again once.

The container generated an APP_KEY when none was set, printed it to the
logs and generated another on the next start, which left the shares
without a password unreadable. It now refuses to start without one and
says how to create it; docker-compose.example.yml requires it for both
services. The command it names, and the README's, now pass
--entrypoint php: the image's entrypoint ignores its arguments, so
"docker run <image> php artisan key:generate --show" never ran it.

Every proxy is trusted, so the password-reset mail built its link from
a host a forged X-Forwarded-Host could choose. It is now built from
APP_URL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 23:23:00 +02:00

244 lines
7.9 KiB
PHP

<?php
use App\Livewire\Admin\AdminSettings;
use App\Livewire\FileUploader;
use App\Livewire\ShareDownload;
use App\Models\Share;
use App\Models\ShareFile;
use App\Models\User;
use App\Services\ShareService;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Storage;
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
use Livewire\Livewire;
// --- Session stores derived key, not plaintext password ---
test('session stores derived encryption key instead of plaintext password', function () {
Storage::fake('shares');
$service = app(ShareService::class);
$file = UploadedFile::fake()->create('file.txt', 100);
$share = $service->createShare([
['file' => $file, 'relativePath' => null],
], [
'password' => 'test-password-secure',
]);
Livewire::test(ShareDownload::class, ['share' => $share])
->set('password', 'test-password-secure')
->call('verifyPassword');
expect(session('share_key_'.$share->token))->not->toBeNull();
expect(session('share_key_'.$share->token))->not->toBe('test-password-secure');
expect(strlen(session('share_key_'.$share->token)))->toBe(64);
});
// --- The password gate cannot be opened from the browser ---
test('a visitor without the password cannot unlock a share\'s file list from the browser', function () {
Storage::fake('shares');
$share = app(ShareService::class)->createShare([
['file' => UploadedFile::fake()->createWithContent('salary-review.pdf', 'secret'), 'relativePath' => null],
], [
'password' => 'test-password-secure',
]);
Livewire::test(ShareDownload::class, ['share' => $share])
->assertDontSee('salary-review.pdf')
->set('authenticated', true);
})->throws(CannotUpdateLockedPropertyException::class);
// --- Rate limiting on password verification ---
test('rate limiting blocks after 5 failed password attempts', function () {
Storage::fake('shares');
$service = app(ShareService::class);
$file = UploadedFile::fake()->create('file.txt', 100);
$share = $service->createShare([
['file' => $file, 'relativePath' => null],
], [
'password' => 'correct-password',
]);
$component = Livewire::test(ShareDownload::class, ['share' => $share]);
for ($i = 0; $i < 5; $i++) {
$component->set('password', 'wrong-password')
->call('verifyPassword')
->assertHasErrors(['password']);
}
$component->set('password', 'correct-password')
->call('verifyPassword')
->assertHasErrors(['password'])
->assertSet('authenticated', false);
});
test('rate limiter clears after successful password verification', function () {
Storage::fake('shares');
$service = app(ShareService::class);
$file = UploadedFile::fake()->create('file.txt', 100);
$share = $service->createShare([
['file' => $file, 'relativePath' => null],
], [
'password' => 'correct-password',
]);
$component = Livewire::test(ShareDownload::class, ['share' => $share]);
$component->set('password', 'wrong-password')
->call('verifyPassword')
->assertHasErrors(['password']);
$component->set('password', 'correct-password')
->call('verifyPassword')
->assertSet('authenticated', true);
$rateLimitKey = 'share-password:'.$share->token.'|127.0.0.1';
expect(RateLimiter::remaining($rateLimitKey, 5))->toBe(5);
});
// --- Share password minimum length ---
test('share password must be at least 8 characters', function () {
Storage::fake('shares');
Livewire::test(FileUploader::class)
->set('usePassword', true)
->set('password', 'short')
->call('createShare')
->assertHasErrors(['password' => 'min']);
});
test('share password of 8 characters is accepted', function () {
Storage::fake('shares');
Livewire::test(FileUploader::class)
->set('usePassword', true)
->set('password', 'longenough')
->call('createShare')
->assertHasNoErrors(['password']);
});
// --- is_admin not mass assignable ---
test('is_admin is not mass assignable on User model', function () {
$user = User::query()->create([
'name' => 'Test User',
'email' => 'mass-assign-test@example.com',
'password' => bcrypt('password'),
'is_admin' => true,
]);
expect($user->is_admin)->toBeFalsy();
});
// --- Setup wizard guard prevents duplicate admins ---
test('setup wizard createAdmin is blocked when admin already exists', function () {
$adminCountBefore = User::query()->where('is_admin', true)->count();
$this->post(route('setup'), [
'name' => 'Second Admin',
'email' => 'second-admin@example.com',
'password' => 'password123',
'password_confirmation' => 'password123',
]);
expect(User::query()->where('email', 'second-admin@example.com')->exists())->toBeFalse();
expect(User::query()->where('is_admin', true)->count())->toBe($adminCountBefore);
});
// --- Content-Disposition sanitization ---
test('content disposition handles special characters in filename', function () {
Storage::fake('shares');
$share = app(ShareService::class)->createShare([
['file' => UploadedFile::fake()->createWithContent('normal.txt', 'test content'), 'relativePath' => null],
]);
$shareFile = $share->files->first();
$shareFile->update(['original_name' => 'file"with"quotes.txt']);
$response = $this->get(route('share.download.file', [$share, $shareFile]));
expect($response->headers->get('Content-Disposition'))
->toContain('attachment')
->not->toContain('file"with"quotes.txt');
});
// --- SVG upload rejected ---
test('svg upload is rejected for site logo', function () {
$admin = User::query()->where('is_admin', true)->first();
Livewire::actingAs($admin)
->test(AdminSettings::class)
->set('maxFileSize', 100)
->set('siteLogo', UploadedFile::fake()->create('logo.svg', 100, 'image/svg+xml'))
->call('saveSettings')
->assertHasErrors(['siteLogo']);
});
// --- Relative path validation (Zip Slip prevention) ---
test('relative paths from a dropped folder that could reach outside the share are dropped', function (string $relativePath) {
Storage::fake('shares');
Livewire::test(FileUploader::class)
->call('registerFiles', [['name' => 'file.txt', 'size' => 100, 'path' => $relativePath]]);
expect(ShareFile::query()->sole()->relative_path)->toBeNull();
})->with([
'directory traversal' => '../../etc/passwd',
'absolute path' => '/etc/passwd',
'windows traversal' => '..\\..\\windows\\system.ini',
]);
test('valid relative paths are preserved', function () {
Storage::fake('shares');
Livewire::test(FileUploader::class)
->call('registerFiles', [['name' => 'file.txt', 'size' => 100, 'path' => 'folder/subfolder/file.txt']]);
expect(ShareFile::query()->sole()->relative_path)->toBe('folder/subfolder/file.txt');
});
// --- Security headers ---
test('security headers are present on responses', function () {
$response = $this->get(route('upload'));
$response->assertHeader('X-Content-Type-Options', 'nosniff');
$response->assertHeader('X-Frame-Options', 'DENY');
$response->assertHeader('Referrer-Policy', 'strict-origin-when-cross-origin');
$response->assertHeader('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');
});
// --- Token collision retry ---
test('share service generates unique tokens', function () {
Storage::fake('shares');
$service = app(ShareService::class);
$shares = [];
for ($i = 0; $i < 5; $i++) {
$file = UploadedFile::fake()->create("file{$i}.txt", 100);
$shares[] = $service->createShare([
['file' => $file, 'relativePath' => null],
]);
}
$tokens = array_map(fn ($s) => $s->token, $shares);
expect(array_unique($tokens))->toHaveCount(5);
});