[P1 Security] Rate Limiting für API-Routes und Livewire-Actions (vibe-kanban e1f85c61)

## Security Audit: Fehlendes Rate Limiting

### Problem
Die Anwendung hat **kein Rate Limiting** auf API-Routes oder Livewire-Actions. Das ermöglicht:
- Brute-Force-Angriffe auf Authentication-Endpoints
- Denial-of-Service durch massenhaftes Aufrufen von API-Endpoints
- Vote-Manipulation durch schnelle, wiederholte Requests
- Ressourcen-Erschöpfung durch unkontrollierte Datenbankabfragen

### Betroffene Endpoints

**API-Routes (`routes/api.php`):**
```php
Route::get('/nostr/profile/{key}', GetProfile::class);  // kein Rate Limit
Route::get('/members/{year}', GetPaidMembers::class);    // kein Rate Limit
```

**Kritische Livewire-Actions (kein Throttling):**
- Voting auf ProjectProposals (`association/project-support/show`)
- Login via Nostr (`handleNostrLogin`)
- ProjectProposal-Erstellung
- Election Voting

### Lösung

**1. API Rate Limiting in `bootstrap/app.php`:**
Nutze Laravel 12's Middleware-Konfiguration um Rate Limiting zu aktivieren:

```php
->withMiddleware(function (Middleware $middleware) {
    $middleware->api(prepend: [
        \Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
    ]);
})
```

Und definiere den `api` Rate Limiter in `app/Providers/AppServiceProvider.php`:

```php
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;

public function boot(): void
{
    RateLimiter::for('api', function (Request $request) {
        return Limit::perMinute(60)->by($request->ip());
    });
}
```

**2. Livewire Action Throttling:**
Nutze Livewire's `#[Throttle]` Attribut auf sensiblen Actions. Suche in der Livewire-Dokumentation nach der korrekten v4-Syntax mit `search-docs`:
- `queries: ['throttle', 'rate limiting']`
- `packages: ['livewire/livewire']`

Wende Throttling an auf:
- Vote-Submit-Methoden in den ProjectSupport-Components
- Login-Handler (`handleNostrLogin` in `WithNostrAuth` Trait)
- ProjectProposal Create/Update Actions

**3. Zusätzlicher Custom Rate Limiter für Voting:**

```php
RateLimiter::for('voting', function (Request $request) {
    return Limit::perMinute(10)->by($request->ip());
});
```

### Betroffene Dateien
- `bootstrap/app.php` – Middleware-Konfiguration (Rate Limit Middleware hinzufügen)
- `app/Providers/AppServiceProvider.php` – RateLimiter Definitionen
- `routes/api.php` – Rate Limit Middleware anwenden
- `app/Livewire/Traits/WithNostrAuth.php` – Throttle auf `handleNostrLogin`
- Livewire-Components in `app/Livewire/Association/ProjectSupport/` – Throttle auf Vote/Create Actions

### Vorgehen
1. `search-docs` nutzen für: `['rate limiting', 'throttle']` (Laravel) und `['throttle']` (Livewire)
2. Rate Limiter in AppServiceProvider definieren
3. API-Middleware in `bootstrap/app.php` konfigurieren
4. Livewire-Actions mit Throttle versehen
5. Pest-Tests schreiben, die verifizieren dass Rate Limiting greift (429 Response bei Überschreitung)
6. `vendor/bin/pint --dirty` und `php artisan test --compact`

### Akzeptanzkriterien
- API-Routes geben HTTP 429 nach 60 Requests/Minute zurück
- Voting-Actions sind auf max. 10/Minute limitiert
- Login-Attempts sind throttled
- Tests verifizieren Rate Limiting Verhalten
This commit is contained in:
vk
2026-02-11 20:53:29 +01:00
parent af3090e694
commit 9faae15212
8 changed files with 221 additions and 2 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Livewire\Traits; namespace App\Livewire\Traits;
use App\Support\NostrAuth; use App\Support\NostrAuth;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Attributes\On; use Livewire\Attributes\On;
trait WithNostrAuth trait WithNostrAuth
@@ -18,6 +19,16 @@ trait WithNostrAuth
#[On('nostrLoggedIn')] #[On('nostrLoggedIn')]
public function handleNostrLogin(string $pubkey): void public function handleNostrLogin(string $pubkey): void
{ {
$executed = RateLimiter::attempt(
'nostr-login:'.request()->ip(),
10,
function () {},
);
if (! $executed) {
abort(429, 'Too many login attempts.');
}
NostrAuth::login($pubkey); NostrAuth::login($pubkey);
$this->currentPubkey = $pubkey; $this->currentPubkey = $pubkey;

View File

@@ -2,6 +2,9 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
@@ -19,6 +22,16 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function boot(): void public function boot(): void
{ {
// RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->ip());
});
RateLimiter::for('voting', function (Request $request) {
return Limit::perMinute(10)->by($request->ip());
});
RateLimiter::for('nostr-login', function (Request $request) {
return Limit::perMinute(10)->by($request->ip());
});
} }
} }

View File

@@ -15,7 +15,9 @@ return Application::configure(basePath: dirname(__DIR__))
health: '/up', health: '/up',
) )
->withMiddleware(function (Middleware $middleware) { ->withMiddleware(function (Middleware $middleware) {
// $middleware->api(prepend: [
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
]);
}) })
->withExceptions(function (Exceptions $exceptions) { ->withExceptions(function (Exceptions $exceptions) {
Integration::handles($exceptions); Integration::handles($exceptions);

View File

@@ -4,6 +4,7 @@ use App\Models\Election;
use App\Models\EinundzwanzigPleb; use App\Models\EinundzwanzigPleb;
use App\Models\Profile; use App\Models\Profile;
use App\Support\NostrAuth; use App\Support\NostrAuth;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Attributes\Computed; use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked; use Livewire\Attributes\Locked;
use Livewire\Component; use Livewire\Component;
@@ -204,6 +205,16 @@ new class extends Component {
public function handleNostrLoggedIn(string $pubkey): void public function handleNostrLoggedIn(string $pubkey): void
{ {
$executed = RateLimiter::attempt(
'nostr-login:'.request()->ip(),
10,
function () {},
);
if (! $executed) {
abort(429, 'Too many login attempts.');
}
$this->currentPubkey = $pubkey; $this->currentPubkey = $pubkey;
$this->currentPleb = EinundzwanzigPleb::query() $this->currentPleb = EinundzwanzigPleb::query()
->where('pubkey', $pubkey)->first(); ->where('pubkey', $pubkey)->first();
@@ -279,6 +290,16 @@ new class extends Component {
public function vote($pubkey, $type, $board = false): void public function vote($pubkey, $type, $board = false): void
{ {
$executed = RateLimiter::attempt(
'voting:'.request()->ip(),
10,
function () {},
);
if (! $executed) {
abort(429, 'Too many voting attempts.');
}
if ($this->election->end_time?->isPast()) { if ($this->election->end_time?->isPast()) {
$this->isNotClosed = false; $this->isNotClosed = false;
@@ -303,6 +324,16 @@ new class extends Component {
public function signEvent($event): void public function signEvent($event): void
{ {
$executed = RateLimiter::attempt(
'voting:'.request()->ip(),
10,
function () {},
);
if (! $executed) {
abort(429, 'Too many voting attempts.');
}
$note = new NostrEvent; $note = new NostrEvent;
$note->setId($event['id']); $note->setId($event['id']);
$note->setSignature($event['sig']); $note->setSignature($event['sig']);

View File

@@ -2,6 +2,7 @@
use App\Models\ProjectProposal; use App\Models\ProjectProposal;
use App\Support\NostrAuth; use App\Support\NostrAuth;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Attributes\Layout; use Livewire\Attributes\Layout;
use Livewire\Attributes\Locked; use Livewire\Attributes\Locked;
use Livewire\Attributes\Title; use Livewire\Attributes\Title;
@@ -58,6 +59,16 @@ class extends Component
public function save(): void public function save(): void
{ {
$executed = RateLimiter::attempt(
'project-proposal-create:'.request()->ip(),
5,
function () {},
);
if (! $executed) {
abort(429, 'Too many requests.');
}
$this->validate([ $this->validate([
'form.name' => 'required|string|max:255', 'form.name' => 'required|string|max:255',
'form.description' => 'required|string', 'form.description' => 'required|string',

View File

@@ -2,6 +2,7 @@
use App\Models\ProjectProposal; use App\Models\ProjectProposal;
use App\Support\NostrAuth; use App\Support\NostrAuth;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Attributes\Layout; use Livewire\Attributes\Layout;
use Livewire\Attributes\Locked; use Livewire\Attributes\Locked;
use Livewire\Attributes\Title; use Livewire\Attributes\Title;
@@ -84,6 +85,16 @@ class extends Component
public function update(): void public function update(): void
{ {
$executed = RateLimiter::attempt(
'project-proposal-update:'.request()->ip(),
5,
function () {},
);
if (! $executed) {
abort(429, 'Too many requests.');
}
$this->validate([ $this->validate([
'form.name' => 'required|string|max:255', 'form.name' => 'required|string|max:255',
'form.description' => 'required|string', 'form.description' => 'required|string',

View File

@@ -4,6 +4,7 @@ use App\Livewire\Traits\WithNostrAuth;
use App\Models\ProjectProposal; use App\Models\ProjectProposal;
use App\Models\Vote; use App\Models\Vote;
use App\Support\NostrAuth; use App\Support\NostrAuth;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Attributes\Locked; use Livewire\Attributes\Locked;
use Livewire\Component; use Livewire\Component;
@@ -64,6 +65,16 @@ new class extends Component {
return; return;
} }
$executed = RateLimiter::attempt(
'voting:'.request()->ip(),
10,
function () {},
);
if (! $executed) {
abort(429, 'Too many voting attempts.');
}
Vote::query()->updateOrCreate([ Vote::query()->updateOrCreate([
'project_proposal_id' => $this->projectProposal->id, 'project_proposal_id' => $this->projectProposal->id,
'einundzwanzig_pleb_id' => $this->currentPleb->id, 'einundzwanzig_pleb_id' => $this->currentPleb->id,
@@ -79,6 +90,16 @@ new class extends Component {
return; return;
} }
$executed = RateLimiter::attempt(
'voting:'.request()->ip(),
10,
function () {},
);
if (! $executed) {
abort(429, 'Too many voting attempts.');
}
Vote::query()->updateOrCreate([ Vote::query()->updateOrCreate([
'project_proposal_id' => $this->projectProposal->id, 'project_proposal_id' => $this->projectProposal->id,
'einundzwanzig_pleb_id' => $this->currentPleb->id, 'einundzwanzig_pleb_id' => $this->currentPleb->id,

View File

@@ -0,0 +1,119 @@
<?php
use App\Models\EinundzwanzigPleb;
use App\Models\ProjectProposal;
use App\Support\NostrAuth;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Livewire;
beforeEach(function () {
RateLimiter::clear('api');
RateLimiter::clear('voting');
RateLimiter::clear('nostr-login');
});
test('api routes return 429 after exceeding rate limit', function () {
for ($i = 0; $i < 60; $i++) {
$this->getJson('/api/members/2024')->assertSuccessful();
}
$this->getJson('/api/members/2024')->assertStatus(429);
});
test('api routes include rate limit headers', function () {
$response = $this->getJson('/api/members/2024');
$response->assertSuccessful();
$response->assertHeader('X-RateLimit-Limit', 60);
$response->assertHeader('X-RateLimit-Remaining');
});
test('nostr profile api route is rate limited', function () {
for ($i = 0; $i < 60; $i++) {
$this->getJson('/api/nostr/profile/testkey'.$i);
}
$this->getJson('/api/nostr/profile/testkey')->assertStatus(429);
});
test('voting actions are rate limited after 10 attempts', function () {
$pleb = EinundzwanzigPleb::factory()->create();
$project = ProjectProposal::factory()->create();
NostrAuth::login($pleb->pubkey);
for ($i = 0; $i < 10; $i++) {
RateLimiter::attempt('voting:127.0.0.1', 10, function () {});
}
Livewire::test('association.project-support.show', ['projectProposal' => $project->slug])
->call('handleApprove')
->assertStatus(429);
});
test('nostr login is rate limited after 10 attempts', function () {
$pleb = EinundzwanzigPleb::factory()->create();
for ($i = 0; $i < 10; $i++) {
RateLimiter::attempt('nostr-login:127.0.0.1', 10, function () {});
}
Livewire::test('association.project-support.index')
->call('handleNostrLogin', $pleb->pubkey)
->assertStatus(429);
});
test('project proposal creation is rate limited after 5 attempts', function () {
$pleb = EinundzwanzigPleb::factory()->active()->withPaidCurrentYear()->create();
NostrAuth::login($pleb->pubkey);
for ($i = 0; $i < 5; $i++) {
RateLimiter::attempt('project-proposal-create:127.0.0.1', 5, function () {});
}
Livewire::test('association.project-support.form.create')
->set('form.name', 'Test Project')
->set('form.description', 'Test Description')
->set('form.support_in_sats', 21000)
->set('form.website', 'https://example.com')
->call('save')
->assertStatus(429);
});
test('project proposal update is rate limited after 5 attempts', function () {
$pleb = EinundzwanzigPleb::factory()->create();
$project = ProjectProposal::factory()->create([
'einundzwanzig_pleb_id' => $pleb->id,
]);
NostrAuth::login($pleb->pubkey);
for ($i = 0; $i < 5; $i++) {
RateLimiter::attempt('project-proposal-update:127.0.0.1', 5, function () {});
}
Livewire::test('association.project-support.form.edit', ['projectProposal' => $project->slug])
->set('form.name', 'Updated Name')
->call('update')
->assertStatus(429);
});
test('voting works within rate limit', function () {
$pleb = EinundzwanzigPleb::factory()->create();
$project = ProjectProposal::factory()->create();
NostrAuth::login($pleb->pubkey);
Livewire::test('association.project-support.show', ['projectProposal' => $project->slug])
->call('handleApprove')
->assertHasNoErrors();
$vote = \App\Models\Vote::query()
->where('project_proposal_id', $project->id)
->where('einundzwanzig_pleb_id', $pleb->id)
->first();
expect($vote)->not->toBeNull()
->and($vote->value)->toBeTrue();
});