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
182 lines
5.9 KiB
PHP
182 lines
5.9 KiB
PHP
<?php
|
|
|
|
use App\Livewire\Traits\WithNostrAuth;
|
|
use App\Models\EinundzwanzigPleb;
|
|
use App\Models\ProjectProposal;
|
|
use App\Support\NostrAuth;
|
|
use Flux\Flux;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Livewire\Attributes\Locked;
|
|
use Livewire\Component;
|
|
|
|
new class extends Component {
|
|
use WithNostrAuth;
|
|
|
|
public string $activeFilter = 'all';
|
|
|
|
public ?string $confirmDeleteId = null;
|
|
|
|
public string $search = '';
|
|
|
|
#[Locked]
|
|
public Collection $projects;
|
|
|
|
#[Locked]
|
|
public bool $isAllowed = false;
|
|
|
|
#[Locked]
|
|
public ?string $currentPubkey = null;
|
|
|
|
#[Locked]
|
|
public ?ProjectProposal $projectToDelete = null;
|
|
|
|
protected $listeners = [
|
|
'confirmDeleteProject' => 'confirmDeleteProject',
|
|
];
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->loadProjects();
|
|
}
|
|
|
|
public function updatedSearch(): void
|
|
{
|
|
$this->loadProjects();
|
|
}
|
|
|
|
public function loadProjects(): void
|
|
{
|
|
$this->projects = ProjectProposal::query()
|
|
->with([
|
|
'einundzwanzigPleb.profile',
|
|
'votes',
|
|
])
|
|
->where(function ($query) {
|
|
$query
|
|
->where('name', 'ilike', '%'.$this->search.'%')
|
|
->orWhere('description', 'ilike', '%'.$this->search.'%')
|
|
->orWhereHas('einundzwanzigPleb.profile', function ($q) {
|
|
$q->where('name', 'ilike', '%'.$this->search.'%');
|
|
});
|
|
})
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
}
|
|
|
|
public function confirmDeleteProject($id): void
|
|
{
|
|
$this->projectToDelete = ProjectProposal::query()->findOrFail($id);
|
|
Flux::modal('delete-project')->show();
|
|
}
|
|
|
|
public function setFilter($filter): void
|
|
{
|
|
$this->activeFilter = $filter;
|
|
}
|
|
|
|
public function delete(): void
|
|
{
|
|
if ($this->projectToDelete) {
|
|
Gate::forUser(NostrAuth::user())->authorize('delete', $this->projectToDelete);
|
|
|
|
$this->projectToDelete->delete();
|
|
Flux::toast('Projektunterstützung gelöscht.');
|
|
$this->loadProjects();
|
|
Flux::modals()->close();
|
|
$this->projectToDelete = null;
|
|
}
|
|
}
|
|
|
|
};
|
|
?>
|
|
|
|
<div>
|
|
<div class="px-4 sm:px-6 lg:px-8 py-8 w-full max-w-9xl mx-auto">
|
|
<!-- Page header -->
|
|
<div class="sm:flex sm:justify-between sm:items-center mb-5">
|
|
|
|
<!-- Left: Title -->
|
|
<div class="mb-4 sm:mb-0">
|
|
<h1 class="text-2xl md:text-3xl text-zinc-800 dark:text-zinc-100 font-bold">
|
|
Einundzwanzig Projektunterstützungen
|
|
</h1>
|
|
</div>
|
|
|
|
<!-- Right: Actions -->
|
|
<div class="grid grid-cols-1 sm:grid-cols-2 justify-start sm:justify-end gap-2">
|
|
|
|
<!-- Search form -->
|
|
<form class="relative">
|
|
<flux:input type="search" wire:model.live.debounce="search"
|
|
placeholder="Suche" icon="magnifying-glass"/>
|
|
</form>
|
|
|
|
<!-- Add meetup button -->
|
|
@if(Gate::forUser(NostrAuth::user())->allows('create', App\Models\ProjectProposal::class))
|
|
<flux:button :href="route('association.projectSupport.create')" icon="plus" variant="primary">
|
|
Projekt einreichen
|
|
</flux:button>
|
|
@endif
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<!-- Filters -->
|
|
<div class="mb-5">
|
|
<ul class="flex flex-wrap -m-1">
|
|
<li class="m-1">
|
|
<flux:button wire:click="setFilter('all')" :variant="$activeFilter === 'all' ? 'primary' : 'ghost'">
|
|
Alle
|
|
</flux:button>
|
|
</li>
|
|
<li class="m-1">
|
|
<flux:button wire:click="setFilter('new')" :variant="$activeFilter === 'new' ? 'primary' : 'ghost'">
|
|
Neu
|
|
</flux:button>
|
|
</li>
|
|
<li class="m-1">
|
|
<flux:button wire:click="setFilter('supported')"
|
|
:variant="$activeFilter === 'supported' ? 'primary' : 'ghost'">
|
|
Unterstützt
|
|
</flux:button>
|
|
</li>
|
|
<li class="m-1">
|
|
<flux:button wire:click="setFilter('rejected')"
|
|
:variant="$activeFilter === 'rejected' ? 'primary' : 'ghost'">
|
|
Abgelehnt
|
|
</flux:button>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<div class="text-sm text-zinc-500 dark:text-zinc-400 italic mb-4">{{ $projects->count() }} Projekte</div>
|
|
|
|
<!-- Content -->
|
|
<div class="grid xl:grid-cols-2 gap-6 mb-8">
|
|
@foreach($this->projects as $project)
|
|
<x-project-card :project="$project" :currentPleb="$currentPleb" :section="$activeFilter"/>
|
|
@endforeach
|
|
</div>
|
|
|
|
<!-- Delete confirmation modal -->
|
|
<flux:modal name="delete-project" class="min-w-88">
|
|
<div class="space-y-6">
|
|
<div>
|
|
<flux:heading size="lg">Projektunterstützung löschen?</flux:heading>
|
|
<flux:text class="mt-2">
|
|
<p>Du bist dabei, diese Projektunterstützung zu löschen.</p>
|
|
<p>Diese Aktion kann nicht rückgängig gemacht werden.</p>
|
|
</flux:text>
|
|
</div>
|
|
<div class="flex gap-2">
|
|
<flux:spacer/>
|
|
<flux:modal.close>
|
|
<flux:button variant="ghost">Abbrechen</flux:button>
|
|
</flux:modal.close>
|
|
<flux:button wire:click="delete" variant="danger">Löschen</flux:button>
|
|
</div>
|
|
</div>
|
|
</flux:modal>
|
|
</div>
|
|
</div>
|