mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-nostr.git
synced 2026-02-15 03:23:17 +00:00
## Security Audit: Fehlende zentralisierte Autorisierung
### Problem
Die Anwendung hat **keine Laravel Policy-Klassen**. Autorisierungslogik ist verstreut in:
- **Blade Templates:** Inline `@if`-Checks gegen `config('einundzwanzig.config.current_board')`
- **Livewire Trait:** `app/Livewire/Traits/WithNostrAuth.php` setzt `$isAllowed` und `$canEdit` Booleans
- **Component-Mount-Methoden:** z.B. in `project-support/form/create.blade.php` (Zeile 41-47)
Die Board-Member-Autorisierung funktioniert über einen Vergleich mit hartkodierten npubs in `config/einundzwanzig/config.php`:
```php
'current_board' => [
'npub1pt0kw36...', 'npub1gvqkjc...', // etc.
]
```
**Probleme:**
- Keine zentrale Stelle für Berechtigungsprüfungen
- Autorisierung kann leicht vergessen werden wenn neue Endpoints hinzukommen
- Board-Member-Check wird an vielen Stellen dupliziert
- Livewire-Actions können ggf. direkt aufgerufen werden ohne UI-seitige Prüfung
### Lösung
**1. Laravel Policies erstellen:**
```bash
php artisan make:policy ProjectProposalPolicy --model=ProjectProposal --no-interaction
php artisan make:policy VotePolicy --model=Vote --no-interaction
php artisan make:policy ElectionPolicy --model=Election --no-interaction
```
**2. Policy-Methoden implementieren:**
Für `ProjectProposalPolicy`:
- `viewAny()` – Jeder darf die Liste sehen
- `view()` – Jeder darf ein Proposal sehen
- `create()` – Nur authentifizierte User mit `association_status > 1` und bezahlter Mitgliedschaft im aktuellen Jahr
- `update()` – Nur der Ersteller ODER Board-Members
- `delete()` – Nur Board-Members
- `accept()` – Nur Board-Members (custom method für `accepted`-Flag)
Für `VotePolicy`:
- `create()` – Authentifizierte User, die noch nicht für dieses Proposal abgestimmt haben
- `update()` / `delete()` – Nur der eigene Vote
Für `ElectionPolicy`:
- `viewAny()` / `view()` – Jeder
- `create()` / `update()` / `delete()` – Nur Board-Members
- `vote()` – Authentifizierte Mitglieder mit gültigem Status
**3. Auth-Checks in der Nostr-Auth-Architektur:**
Die App nutzt eine Custom NostrAuth (`app/Support/NostrAuth.php`, `app/Auth/NostrUser.php`). Die Policies müssen mit `NostrUser` funktionieren. Prüfe ob `NostrUser` das `Authenticatable` Interface korrekt implementiert, damit `$this->authorize()` und `Gate::allows()` in Livewire-Components funktionieren.
**4. Policies in Livewire-Components nutzen:**
Ersetze die inline-Checks in den Components durch Policy-Aufrufe:
```php
// Vorher (verstreut in Blade/Component):
if (in_array($this->currentPleb->npub, config('einundzwanzig.config.current_board'), true)) { ... }
// Nachher (zentralisiert):
$this->authorize('update', $projectProposal);
```
### Betroffene Dateien
- **Neue Dateien:** `app/Policies/ProjectProposalPolicy.php`, `app/Policies/VotePolicy.php`, `app/Policies/ElectionPolicy.php`
- **Anpassen:** `app/Livewire/Traits/WithNostrAuth.php` – Board-Check in Policy auslagern
- **Anpassen:** Livewire-Components in `app/Livewire/Association/ProjectSupport/` – `$this->authorize()` nutzen
- **Anpassen:** Livewire-Components in `app/Livewire/Association/Election/` – `$this->authorize()` nutzen
- **Prüfen:** `app/Auth/NostrUser.php` – Kompatibilität mit Policy-System
- **Prüfen:** `config/einundzwanzig/config.php` – Board-Member-Liste wird weiterhin als Datenquelle genutzt
### Vorgehen
1. `search-docs` nutzen: `queries: ['policies', 'authorization', 'gates']` und `packages: ['laravel/framework']`
2. Prüfe wie `NostrUser` mit Laravel's Authorization-System zusammenspielt
3. Policies mit `php artisan make:policy` erstellen
4. Policy-Methoden implementieren (Board-Check-Logik zentralisieren)
5. Livewire-Components auf Policy-Aufrufe umstellen
6. Blade-Templates: `@can` / `@cannot` Directives nutzen statt inline `@if`
7. Pest Feature-Tests für jede Policy-Methode schreiben
8. `vendor/bin/pint --dirty` und `php artisan test --compact`
### Akzeptanzkriterien
- 3 Policy-Klassen existieren und sind registriert
- Board-Member-Check ist an EINER Stelle definiert (in Policy oder Helper)
- Livewire-Components nutzen `$this->authorize()` statt inline-Checks
- Blade-Templates nutzen `@can` / `@cannot` Directives
- Pest-Tests decken alle Policy-Methoden ab (allow & deny)
- Bestehende Funktionalität bleibt erhalten
120 lines
5.8 KiB
PHP
120 lines
5.8 KiB
PHP
@props(['project', 'currentPleb', 'section' => 'all'])
|
|
|
|
@php
|
|
$boardVotes = $project->votes->filter(function ($vote) {
|
|
return in_array($vote->einundzwanzigPleb->npub, config('einundzwanzig.config.current_board'));
|
|
});
|
|
$approveCount = $boardVotes->where('value', 1)->count();
|
|
$disapproveCount = $boardVotes->where('value', 0)->count();
|
|
|
|
$shouldDisplay = match($section) {
|
|
'all' => true,
|
|
'new' => $approveCount < 3,
|
|
'supported' => $project->sats_paid > 0,
|
|
'rejected' => $disapproveCount >= 3,
|
|
'approved' => ($approveCount === 3 || $disapproveCount !== 3),
|
|
default => true,
|
|
};
|
|
@endphp
|
|
|
|
@if($shouldDisplay)
|
|
|
|
<flux:card class="flex flex-col sm:flex-row overflow-hidden" wire:key="project_{{ $project->id }}">
|
|
@if(!$project->sats_paid)
|
|
<a class="relative block w-full h-48 sm:w-56 sm:h-auto xl:sidebar-expanded:w-40 2xl:sidebar-expanded:w-56 shrink-0 sm:shrink-0"
|
|
href="{{ route('association.projectSupport.item', ['projectProposal' => $project]) }}">
|
|
<img class="absolute object-cover object-center w-full h-full"
|
|
src="{{ $project->getSignedMediaUrl('main') }}" alt="Meetup 01">
|
|
<button class="absolute top-0 right-0 mt-4 mr-4">
|
|
<img class="rounded-full h-8 w-8"
|
|
src="{{ $project->einundzwanzigPleb->profile?->picture }}"
|
|
onerror="this.src='{{ asset('einundzwanzig-alpha.jpg') }}'"
|
|
alt="">
|
|
</button>
|
|
</a>
|
|
@else
|
|
<a class="relative block w-full h-48 sm:w-56 sm:h-auto xl:sidebar-expanded:w-40 2xl:sidebar-expanded:w-56 shrink-0 sm:shrink-0"
|
|
href="{{ route('association.projectSupport.item', ['projectProposal' => $project]) }}">
|
|
<img class="absolute object-cover object-center w-full h-full"
|
|
src="{{ $project->getSignedMediaUrl('main') }}" alt="Meetup 01">
|
|
<button class="absolute top-0 right-0 mt-4 mr-4">
|
|
<img class="rounded-full h-8 w-8"
|
|
src="{{ $project->einundzwanzigPleb->profile?->picture }}"
|
|
onerror="this.src='{{ asset('einundzwanzig-alpha.jpg') }}'"
|
|
alt="">
|
|
</button>
|
|
</a>
|
|
@endif
|
|
<!-- Content -->
|
|
<div class="grow p-5 flex flex-col">
|
|
<div class="grow">
|
|
<div class="text-sm font-semibold text-amber-500 uppercase mb-2">
|
|
Eingereicht von: <flux:link href="https://njump.me/{{ $project->einundzwanzigPleb->npub }}" target="_blank">
|
|
{{ $project->einundzwanzigPleb->profile?->name ?? str($project->einundzwanzigPleb->npub)->limit(32) }}
|
|
</flux:link>
|
|
</div>
|
|
<div class="inline-flex mb-2">
|
|
<h3 class="text-lg font-bold text-gray-800 dark:text-gray-100">
|
|
{{ $project->name }}
|
|
</h3>
|
|
</div>
|
|
</div>
|
|
<!-- Footer -->
|
|
<div class="mt-3 space-y-3">
|
|
<!-- First row: Sats, Website, Supporters -->
|
|
<div class="flex flex-wrap items-center gap-2">
|
|
<flux:badge color="amber">{{ number_format($project->support_in_sats, 0, ',', '.') }} Sats</flux:badge>
|
|
<flux:link
|
|
href="{{ $project->website }}"
|
|
target="_blank">
|
|
Webseite
|
|
</flux:link>
|
|
@if($project->votes->where('value', true)->count() > 0)
|
|
<flux:badge color="blue">
|
|
+{{ $project->votes->where('value', true)->count() }} Unterstützer
|
|
</flux:badge>
|
|
@endif
|
|
</div>
|
|
|
|
<!-- Second row: Action buttons -->
|
|
<div class="flex flex-wrap gap-2">
|
|
@if(Illuminate\Support\Facades\Gate::forUser(App\Support\NostrAuth::user())->allows('delete', $project))
|
|
<flux:button
|
|
icon="trash"
|
|
size="xs"
|
|
variant="danger"
|
|
wire:click="$dispatch('confirmDeleteProject', { id: {{ $project->id }} })">
|
|
Löschen
|
|
</flux:button>
|
|
@endif
|
|
@if(Illuminate\Support\Facades\Gate::forUser(App\Support\NostrAuth::user())->allows('update', $project))
|
|
<flux:button
|
|
icon="pencil"
|
|
size="xs"
|
|
:href="route('association.projectSupport.edit', ['projectProposal' => $project])">
|
|
Editieren
|
|
</flux:button>
|
|
@endif
|
|
@if(($currentPleb && $currentPleb->association_status->value > 2) || $project->accepted)
|
|
<flux:button
|
|
icon="folder-open"
|
|
size="xs"
|
|
:href="route('association.projectSupport.item', ['projectProposal' => $project])">
|
|
Öffnen
|
|
</flux:button>
|
|
@endif
|
|
</div>
|
|
</div>
|
|
<div class="py-2">
|
|
@if($project->sats_paid)
|
|
<div class="inline-block">
|
|
<flux:badge color="green">Wurde mit {{ number_format($project->sats_paid, 0, ',', '.') }} Sats
|
|
unterstützt
|
|
</flux:badge>
|
|
</div>
|
|
@endif
|
|
</div>
|
|
</div>
|
|
</flux:card>
|
|
@endif
|