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]>
This commit is contained in:
2026-11-24 06:25:26 +02:00
co-authored by Claude Opus 5.5
parent fcad0ea939
commit 1acdaa5b19
15 changed files with 229 additions and 8 deletions
@@ -144,3 +144,23 @@ it('drops the code when a consumer becomes a company', function () {
expect($customer->fresh()->personal_identity_code)->toBeNull();
});
it('limits how many codes an employee can try, but not management', function (string $role, bool $limited) {
$this->actingAs(User::factory()->create(['role' => $role]));
$form = Livewire::test('customers::form')
->set('form.name', 'Maija Meikäläinen')
->set('form.type', CustomerType::Consumer->value);
foreach (range(1, 10) as $attempt) {
$form->set('form.personal_identity_code', 'väärä')->call('save')->assertHasErrors(['form.personal_identity_code']);
}
$form->set('form.personal_identity_code', '131052-308T')->call('save');
$limited
? $form->assertSee('liian monta kertaa tunnin aikana')
: $form->assertHasNoErrors();
})->with([
'employee' => ['employee', true],
'management' => ['management', false],
]);
@@ -0,0 +1,16 @@
<?php
it('sends browser security headers', function () {
$this->get(route('login'))
->assertOk()
->assertHeader('X-Frame-Options', 'DENY')
->assertHeader('Content-Security-Policy', "frame-ancestors 'none'; base-uri 'self'; object-src 'none'")
->assertHeader('X-Content-Type-Options', 'nosniff')
->assertHeader('Referrer-Policy', 'strict-origin-when-cross-origin')
->assertHeaderMissing('Strict-Transport-Security');
});
it('asks browsers to keep to HTTPS when the request came over HTTPS', function () {
$this->get(str_replace('http://', 'https://', route('login')))
->assertHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
});
@@ -6,6 +6,7 @@ use App\Modules\Jobs\Domain\Exceptions\TaskHasTimeEntries;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
use Livewire\Livewire;
uses(RefreshDatabase::class);
@@ -33,6 +34,17 @@ it('saves and clears a cell of the weekly view', function () {
expect(TimeEntryModel::query()->count())->toBe(0);
});
it('takes the week only from the URL and the week buttons', function () {
$this->get(route('time.week', ['viikko' => "abc'); alert(1); ('"]))->assertOk()->assertSee('Mainoskuvat');
$week = Livewire::test('time::week');
expect(fn () => $week->set('week', '2026-05-20'))->toThrow(CannotUpdateLockedPropertyException::class)
->and(fn () => $week->set('selectedDay', "x'); alert(1); ('"))->toThrow(CannotUpdateLockedPropertyException::class);
$week->call('selectDay', "x'); alert(1); ('")->assertSet('selectedDay', now()->toDateString());
});
it('adds hours from the task page to the day entry and locks done jobs', function () {
Livewire::test('time::task', ['task' => $this->task->id])
->set('workedOn', $this->monday)
+17
View File
@@ -90,6 +90,23 @@ it('blocks login after five failed attempts', function () {
$this->assertGuest();
});
it('blocks one address that tries a password on many accounts', function () {
RateLimiter::clear('login-ip|127.0.0.1');
User::factory()->create(['email' => '[email protected]']);
$login = Livewire::test('users::login');
foreach (range(1, 20) as $attempt) {
$login->set('email', "tuntematon{$attempt}@example.test")->set('password', 'Kesä2027!')->call('login');
}
$login->set('email', '[email protected]')
->set('password', 'password')
->call('login')
->assertSee('Liian monta kirjautumisyritystä.');
$this->assertGuest();
});
it('ends the session of a user who is deactivated while signed in', function () {
$user = User::factory()->create();
@@ -55,6 +55,20 @@ it('shows the same message for an unknown address', function () {
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) {