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
227 lines
8.8 KiB
PHP
227 lines
8.8 KiB
PHP
<?php
|
|
|
|
use App\Livewire\Traits\WithNostrAuth;
|
|
use App\Models\ProjectProposal;
|
|
use App\Models\Vote;
|
|
use App\Support\NostrAuth;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Livewire\Attributes\Locked;
|
|
use Livewire\Component;
|
|
|
|
new class extends Component {
|
|
use WithNostrAuth;
|
|
|
|
#[Locked]
|
|
public $projectProposal;
|
|
|
|
#[Locked]
|
|
public bool $isAllowed = false;
|
|
|
|
#[Locked]
|
|
public ?string $currentPubkey = null;
|
|
|
|
#[Locked]
|
|
public ?object $currentPleb = null;
|
|
|
|
#[Locked]
|
|
public bool $ownVoteExists = false;
|
|
|
|
public function mount($projectProposal): void
|
|
{
|
|
$this->projectProposal = ProjectProposal::query()->where('slug', $projectProposal)->firstOrFail();
|
|
if (NostrAuth::check()) {
|
|
$this->currentPubkey = NostrAuth::pubkey();
|
|
$this->isAllowed = true;
|
|
$this->mountWithNostrAuth();
|
|
$this->ownVoteExists = Vote::query()
|
|
->where('project_proposal_id', $this->projectProposal->id)
|
|
->where('einundzwanzig_pleb_id', $this->currentPleb->id)
|
|
->exists();
|
|
}
|
|
}
|
|
|
|
public function getBoardVotesProperty()
|
|
{
|
|
return Vote::query()
|
|
->where('project_proposal_id', $this->projectProposal->id)
|
|
->whereHas('einundzwanzigPleb', fn($q) => $q->whereIn('npub', config('einundzwanzig.config.current_board')))
|
|
->get();
|
|
}
|
|
|
|
public function getOtherVotesProperty()
|
|
{
|
|
return Vote::query()
|
|
->where('project_proposal_id', $this->projectProposal->id)
|
|
->whereDoesntHave(
|
|
'einundzwanzigPleb',
|
|
fn($q) => $q->whereIn('npub', config('einundzwanzig.config.current_board'))
|
|
)
|
|
->get();
|
|
}
|
|
|
|
public function handleApprove(): void
|
|
{
|
|
$nostrUser = NostrAuth::user();
|
|
|
|
if (! $nostrUser || ! $nostrUser->getPleb()) {
|
|
return;
|
|
}
|
|
|
|
Gate::forUser($nostrUser)->authorize('create', [Vote::class, $this->projectProposal]);
|
|
|
|
$executed = RateLimiter::attempt(
|
|
'voting:'.request()->ip(),
|
|
10,
|
|
function () {},
|
|
);
|
|
|
|
if (! $executed) {
|
|
abort(429, 'Too many voting attempts.');
|
|
}
|
|
|
|
Vote::query()->updateOrCreate([
|
|
'project_proposal_id' => $this->projectProposal->id,
|
|
'einundzwanzig_pleb_id' => $this->currentPleb->id,
|
|
], [
|
|
'value' => true,
|
|
]);
|
|
$this->ownVoteExists = true;
|
|
}
|
|
|
|
public function handleNotApprove(): void
|
|
{
|
|
$nostrUser = NostrAuth::user();
|
|
|
|
if (! $nostrUser || ! $nostrUser->getPleb()) {
|
|
return;
|
|
}
|
|
|
|
Gate::forUser($nostrUser)->authorize('create', [Vote::class, $this->projectProposal]);
|
|
|
|
$executed = RateLimiter::attempt(
|
|
'voting:'.request()->ip(),
|
|
10,
|
|
function () {},
|
|
);
|
|
|
|
if (! $executed) {
|
|
abort(429, 'Too many voting attempts.');
|
|
}
|
|
|
|
Vote::query()->updateOrCreate([
|
|
'project_proposal_id' => $this->projectProposal->id,
|
|
'einundzwanzig_pleb_id' => $this->currentPleb->id,
|
|
], [
|
|
'value' => false,
|
|
]);
|
|
$this->ownVoteExists = true;
|
|
}
|
|
}
|
|
?>
|
|
|
|
<div>
|
|
<div class="px-4 sm:px-6 lg:px-8 py-8 w-full">
|
|
<div class="mx-auto flex flex-col lg:flex-row lg:space-x-8 xl:space-x-12">
|
|
<div class="flex-1">
|
|
<div class="mb-6">
|
|
<flux:button :href="route('association.projectSupport')" variant="primary" size="sm"
|
|
icon="chevron-left">
|
|
Zurück zur Übersicht
|
|
</flux:button>
|
|
</div>
|
|
<div class="text-sm font-semibold text-violet-500 uppercase mb-2">
|
|
{{ $projectProposal->created_at->translatedFormat('d.m.Y') }}
|
|
</div>
|
|
<header class="mb-4">
|
|
<h1 class="text-2xl md:text-3xl text-zinc-800 dark:text-zinc-100 font-bold mb-2">
|
|
{{ $projectProposal->name }}
|
|
</h1>
|
|
<x-markdown>
|
|
{!! $projectProposal->description !!}
|
|
</x-markdown>
|
|
</header>
|
|
|
|
<div class="space-y-3 sm:flex sm:items-center sm:justify-between sm:space-y-0 mb-6">
|
|
<div class="flex items-center sm:mr-4">
|
|
<a class="block mr-2 shrink-0" href="#0">
|
|
<img class="rounded-full"
|
|
src="{{ $projectProposal->einundzwanzigPleb->profile?->picture ?? asset('einundzwanzig-alpha.jpg') }}"
|
|
width="32" height="32" alt="User">
|
|
</a>
|
|
<div class="text-sm whitespace-nowrap">Eingereicht von
|
|
<div class="font-semibold text-zinc-800 dark:text-zinc-100">
|
|
{{ $projectProposal->einundzwanzigPleb?->profile->name ?? str($projectProposal->einundzwanzigPleb->npub)->limit(32) }}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="flex flex-wrap items-center sm:justify-end space-x-2">
|
|
<div
|
|
class="text-xs inline-flex items-center font-medium border border-zinc-200 dark:border-zinc-700/60 text-zinc-600 dark:text-zinc-400 rounded-full text-center px-2.5 py-1">
|
|
<a target="_blank" href="{{ $projectProposal->website }}">Webseite</a>
|
|
</div>
|
|
<div
|
|
class="text-xs inline-flex font-medium uppercase bg-green-500/20 text-green-700 rounded-full text-center px-2.5 py-1">
|
|
{{ number_format($projectProposal->support_in_sats, 0, ',', '.') }} Sats
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<figure class="mb-6">
|
|
<img class="rounded-sm h-48" src="{{ $projectProposal->getSignedMediaUrl('main') }}"
|
|
alt="Picture">
|
|
</figure>
|
|
|
|
<hr class="my-6 border-t border-zinc-100 dark:border-zinc-700/60">
|
|
</div>
|
|
|
|
<div class="lg:w-80 xl:w-96 shrink-0 space-y-4">
|
|
@if($isAllowed)
|
|
<div class="bg-white dark:bg-zinc-800 p-5 shadow-sm rounded-xl">
|
|
@if(!$ownVoteExists)
|
|
<div class="space-y-2">
|
|
<flux:button wire:click="handleApprove" class="w-full">
|
|
<i class="fill-current shrink-0 fa-sharp-duotone fa-solid fa-thumbs-up mr-2"></i>
|
|
Zustimmen
|
|
</flux:button>
|
|
<flux:button wire:click="handleNotApprove" variant="danger" class="w-full">
|
|
<i class="fill-current shrink-0 fa-sharp-duotone fa-solid fa-thumbs-down mr-2"></i>
|
|
Ablehnen
|
|
</flux:button>
|
|
</div>
|
|
@else
|
|
<p class="text-sm text-zinc-700 dark:text-zinc-300">Du hast bereits abgestimmt.</p>
|
|
@endif
|
|
</div>
|
|
@endif
|
|
|
|
<div class="bg-white dark:bg-zinc-800 p-5 shadow-sm rounded-xl">
|
|
<div class="text-sm font-semibold text-zinc-800 dark:text-zinc-100 mb-2">
|
|
Zustimmungen des Vorstands ({{ count($this->boardVotes->where('value', 1)) }})
|
|
</div>
|
|
</div>
|
|
|
|
<div class="bg-white dark:bg-zinc-800 p-5 shadow-sm rounded-xl">
|
|
<div class="text-sm font-semibold text-zinc-800 dark:text-zinc-100 mb-2">
|
|
Ablehnungen des Vorstands ({{ count($this->boardVotes->where('value', 0)) }})
|
|
</div>
|
|
</div>
|
|
|
|
<div class="bg-white dark:bg-zinc-800 p-5 shadow-sm rounded-xl">
|
|
<div class="text-sm font-semibold text-zinc-800 dark:text-zinc-100 mb-2">
|
|
Zustimmungen der übrigen Mitglieder
|
|
({{ count($this->otherVotes->where('value', 1)) }})
|
|
</div>
|
|
</div>
|
|
|
|
<div class="bg-white dark:bg-zinc-800 p-5 shadow-sm rounded-xl">
|
|
<div class="text-sm font-semibold text-zinc-800 dark:text-zinc-100 mb-2">
|
|
Ablehnungen der übrigen Mitglieder
|
|
({{ count($this->otherVotes->where('value', 0)) }})
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|