Files
petriandClaude Opus 5.5 1acdaa5b19 Harden the app: block project files, add rate limits and security headers
- Root .htaccess refuses everything outside public/, because local Apache
  serves the whole project folder (.env, logs with set-password links, .git).
- Login allows 20 failed attempts a minute per IP across accounts, besides
  5 per email and IP; the forgot-password page 5 requests a minute per IP.
- A global middleware sends X-Frame-Options, frame-ancestors, nosniff,
  Referrer-Policy, Permissions-Policy and HSTS over HTTPS, and drops
  X-Powered-By.
- The weekly time sheet locks the shown week and selected day, so the
  browser cannot set them.
- Users who may not see personal identity codes can save a new code at
  most 10 times an hour, so the duplicate check cannot be used to test codes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 06:25:26 +02:00

204 lines
7.7 KiB
PHP

<?php
use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Str;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
uses(RefreshDatabase::class);
it('emails a Finnish set-password link to a known address', function () {
Notification::fake();
$user = User::factory()->create(['email' => '[email protected]']);
$this->get(route('password.request'))->assertOk();
Livewire::test('users::forgot-password')
->set('email', '[email protected]')
->call('sendLink')
->assertHasNoErrors()
->assertSee('Jos osoitteella on käyttäjätunnus');
Notification::assertSentTo($user, ResetPassword::class, function (ResetPassword $notification) use ($user) {
$mail = $notification->toMail($user);
return $mail->subject === 'Aseta salasana'
&& str_contains($mail->actionUrl, '/reset-password/'.$notification->token);
});
});
it('renders the whole set-password email in Finnish', function () {
$user = User::factory()->create();
$html = (string) (new ResetPassword('token'))->toMail($user)->render();
expect($html)->toContain('Aseta salasana')
->toContain('kopioi alla oleva osoite')
->toContain('Kaikki oikeudet pidätetään.')
->not->toContain('All rights reserved');
});
it('shows the same message for an unknown address', function () {
Notification::fake();
Livewire::test('users::forgot-password')
->set('email', '[email protected]')
->call('sendLink')
->assertHasNoErrors()
->assertSee('Jos osoitteella on käyttäjätunnus');
Notification::assertNothingSent();
});
it('limits how many links one address can ask for', function () {
Notification::fake();
$forgot = Livewire::test('users::forgot-password');
foreach (range(1, 5) as $attempt) {
$forgot->set('email', "tuntematon{$attempt}@example.test")->call('sendLink')->assertHasNoErrors();
}
$forgot->set('email', '[email protected]')
->call('sendLink')
->assertHasErrors(['email'])
->assertSee('Liian monta linkkipyyntöä.');
});
it('sends no link to a deactivated user or an intern past the last day, with the same message', function (string $state) {
Notification::fake();
$user = match ($state) {
'inactive' => User::factory()->inactive()->create(),
'intern ended' => User::factory()->intern(now()->subDays(2)->toDateString())->create(),
};
Livewire::test('users::forgot-password')
->set('email', $user->email)
->call('sendLink')
->assertHasNoErrors()
->assertSee('Jos osoitteella on käyttäjätunnus');
Notification::assertNothingSent();
})->with(['inactive', 'intern ended']);
it('sets a new password with a valid link', function () {
$user = User::factory()->create(['email' => '[email protected]']);
$token = Password::createToken($user);
$this->get(route('password.reset', ['token' => $token, 'email' => $user->email]))
->assertOk()
->assertSee('Aseta salasana');
Livewire::test('users::reset-password', ['token' => $token])
->set('email', '[email protected]')
->set('password', 'Uusi-salasana-123')
->set('password_confirmation', 'Uusi-salasana-123')
->call('resetPassword')
->assertHasNoErrors()
->assertRedirect(route('login'));
expect(Hash::check('Uusi-salasana-123', $user->fresh()->password))->toBeTrue();
});
it('rejects a password that breaks a rule', function (string $password, string $message) {
$user = User::factory()->create(['email' => '[email protected]']);
Livewire::test('users::reset-password', ['token' => Password::createToken($user)])
->set('email', '[email protected]')
->set('password', $password)
->set('password_confirmation', $password)
->call('resetPassword')
->assertHasErrors(['password'])
->assertSee($message);
expect(Hash::check('password', $user->fresh()->password))->toBeTrue();
})->with([
'no upper case' => ['uusi-salasana-123', 'sekä isoja että pieniä kirjaimia'],
'no number' => ['Uusi-salasana-abc', 'vähintään yksi numero'],
'no symbol' => ['Uusisalasana123', 'vähintään yksi erikoismerkki'],
]);
it('rejects a short password and an invalid link', function () {
$user = User::factory()->create(['email' => '[email protected]']);
Livewire::test('users::reset-password', ['token' => 'väärä'])
->set('email', '[email protected]')
->set('password', 'lyhyt')
->set('password_confirmation', 'lyhyt')
->call('resetPassword')
->assertHasErrors(['password'])
->set('password', 'Uusi-salasana-123')
->set('password_confirmation', 'Uusi-salasana-123')
->call('resetPassword')
->assertHasErrors(['email'])
->assertSee('Linkki on vanhentunut tai virheellinen.');
expect(Hash::check('password', $user->fresh()->password))->toBeTrue();
});
/**
* Sets the user's password through the reset page with the given token.
*/
function setPasswordWithToken(string $token, string $email = '[email protected]'): Testable
{
return Livewire::test('users::reset-password', ['token' => $token])
->set('email', $email)
->set('password', 'Uusi-salasana-123')
->set('password_confirmation', 'Uusi-salasana-123')
->call('resetPassword');
}
it('accepts a link made by an admin for 72 hours, but a forgot-password link only for 60 minutes', function (string $broker, int $minutes, bool $valid) {
$user = User::factory()->create(['email' => '[email protected]']);
$token = Password::broker($broker)->createToken($user);
$this->travel($minutes)->minutes();
$reset = setPasswordWithToken($token);
$valid ? $reset->assertHasNoErrors() : $reset->assertHasErrors(['email']);
expect(Hash::check('Uusi-salasana-123', $user->fresh()->password))->toBe($valid);
})->with([
'admin link after 2 hours' => ['admin_links', 120, true],
'admin link after 73 hours' => ['admin_links', 73 * 60, false],
'forgot-password link after 59 minutes' => ['users', 59, true],
'forgot-password link after 61 minutes' => ['users', 61, false],
]);
it('ends earlier links when an admin makes a new one, and every link when one is used', function () {
Notification::fake();
$admin = User::factory()->admin()->create();
$user = User::factory()->create(['email' => '[email protected]']);
$forgotToken = Password::createToken($user);
$page = Livewire::actingAs($admin)->test('users::form', ['user' => $user->id])->call('sendPasswordLink');
$adminToken = Str::between($page->get('setupLink'), '/reset-password/', '?');
expect(Password::tokenExists($user, $forgotToken))->toBeFalse();
auth()->logout();
setPasswordWithToken($adminToken)->assertHasNoErrors();
expect(Password::broker('admin_links')->tokenExists($user, $adminToken))->toBeFalse();
$newForgotToken = Password::createToken($user);
$newAdminToken = Password::broker('admin_links')->createToken($user);
setPasswordWithToken($newForgotToken)->assertHasNoErrors();
expect(Password::broker('admin_links')->tokenExists($user, $newAdminToken))->toBeFalse();
});
it('ends the other sessions of a user whose password changes', function () {
$user = User::factory()->create(['email' => '[email protected]']);
$this->actingAs($user)->get(route('home'))->assertOk();
$user->forceFill(['password' => 'Uusi-salasana-123'])->save();
$this->get(route('home'))->assertRedirect(route('login'));
$this->assertGuest();
});