Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix(audit): restrict sensitive fields and centralize event filtering

Andras Bacsai57a322ba11ea
diff --git a/app/Http/Controllers/Api/AuditEventsController.php b/app/Http/Controllers/Api/AuditEventsController.php
index 741aa8be3b..03c1fcb49c 100644
--- a/app/Http/Controllers/Api/AuditEventsController.php
+++ b/app/Http/Controllers/Api/AuditEventsController.php
@@ -22,21 +22,39 @@ class AuditEventsController extends Controller
$perPage = max(1, min(100, $request->integer('per_page', 25)));
$search = trim((string) $request->query('search', ''));
+ $canReadSensitive = $request->attributes->get('can_read_sensitive', false) === true;
$events = AuditEvent::query()
- ->where('team_id', $teamId)
- ->when($request->filled('source'), fn ($query) => $query->where('source', $request->string('source')->toString()))
- ->when($request->filled('action'), fn ($query) => $query->where('action', $request->string('action')->toString()))
- ->when($search !== '', function ($query) use ($search): void {
- $query->where(function ($query) use ($search): void {
- $query->where('event', 'like', "%{$search}%")
- ->orWhere('description', 'like', "%{$search}%")
- ->orWhere('resource_name', 'like', "%{$search}%")
- ->orWhere('actor_name', 'like', "%{$search}%")
- ->orWhere('actor_email', 'like', "%{$search}%");
- });
- })
- ->latest('created_at')
- ->latest('id')
+ ->select([
+ 'id',
+ 'team_id',
+ 'event',
+ 'source',
+ 'action',
+ 'actor_type',
+ 'actor_id',
+ 'actor_name',
+ 'resource_type',
+ 'resource_uuid',
+ 'resource_name',
+ 'description',
+ 'created_at',
+ ])
+ ->when($canReadSensitive, fn ($query) => $query->addSelect([
+ 'actor_email',
+ 'actor_token_id',
+ 'actor_token_name',
+ 'metadata',
+ 'ip_address',
+ 'user_agent',
+ ]))
+ ->visibleToTeam($teamId)
+ ->filtered(
+ search: $search,
+ action: $request->string('action', 'all')->toString(),
+ source: $request->string('source', 'all')->toString(),
+ searchSensitiveFields: $canReadSensitive,
+ )
+ ->latestFirst()
->paginate($perPage);
return response()->json($events);
diff --git a/app/Livewire/Team/AuditLog.php b/app/Livewire/Team/AuditLog.php
index 91dfbc35c5..53cb203eff 100644
--- a/app/Livewire/Team/AuditLog.php
+++ b/app/Livewire/Team/AuditLog.php
@@ -4,6 +4,7 @@ namespace App\Livewire\Team;
use App\Models\AuditEvent;
use Illuminate\Contracts\View\View;
+use Illuminate\Support\Str;
use Livewire\Component;
use Livewire\WithPagination;
@@ -50,26 +51,23 @@ class AuditLog extends Component
$search = trim($this->search);
$teamId = currentTeam()->id;
$canViewInstanceEvents = $teamId === 0 && isInstanceAdmin();
+ $visibleEvents = AuditEvent::query()->visibleToTeam($teamId, $canViewInstanceEvents);
+ $actionOptions = [
+ ['value' => 'all', 'label' => 'All actions'],
+ ...$visibleEvents->clone()
+ ->select('action')
+ ->distinct()
+ ->orderBy('action')
+ ->pluck('action')
+ ->map(fn (string $action): array => ['value' => $action, 'label' => Str::headline($action)])
+ ->all(),
+ ];
$events = AuditEvent::query()
- ->where(function ($query) use ($canViewInstanceEvents, $teamId): void {
- $query->where('team_id', $teamId)
- ->when($canViewInstanceEvents, fn ($query) => $query->orWhereNull('team_id'));
- })
- ->when($this->action !== 'all', fn ($query) => $query->where('action', $this->action))
- ->when($this->source !== 'all', fn ($query) => $query->where('source', $this->source))
- ->when($search !== '', function ($query) use ($search): void {
- $query->where(function ($query) use ($search): void {
- $query->where('description', 'like', "%{$search}%")
- ->orWhere('resource_name', 'like', "%{$search}%")
- ->orWhere('actor_name', 'like', "%{$search}%")
- ->orWhere('actor_email', 'like', "%{$search}%")
- ->orWhere('event', 'like', "%{$search}%");
- });
- })
- ->latest('created_at')
- ->latest('id')
+ ->visibleToTeam($teamId, $canViewInstanceEvents)
+ ->filtered($search, $this->action, $this->source)
+ ->latestFirst()
->paginate($this->perPage);
- return view('livewire.team.audit-log', ['events' => $events]);
+ return view('livewire.team.audit-log', ['actionOptions' => $actionOptions, 'events' => $events]);
}
}
diff --git a/app/Models/AuditEvent.php b/app/Models/AuditEvent.php
index a7ec905f29..2383dee267 100644
--- a/app/Models/AuditEvent.php
+++ b/app/Models/AuditEvent.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
@@ -45,6 +46,40 @@ class AuditEvent extends Model
];
}
+ public function scopeVisibleToTeam(Builder $query, int $teamId, bool $includeInstanceEvents = false): Builder
+ {
+ return $query->where(function (Builder $query) use ($includeInstanceEvents, $teamId): void {
+ $query->where('team_id', $teamId)
+ ->when($includeInstanceEvents, fn (Builder $query) => $query->orWhereNull('team_id'));
+ });
+ }
+
+ public function scopeFiltered(
+ Builder $query,
+ string $search = '',
+ string $action = 'all',
+ string $source = 'all',
+ bool $searchSensitiveFields = true,
+ ): Builder {
+ return $query
+ ->when($action !== 'all', fn (Builder $query) => $query->where('action', $action))
+ ->when($source !== 'all', fn (Builder $query) => $query->where('source', $source))
+ ->when($search !== '', function (Builder $query) use ($search, $searchSensitiveFields): void {
+ $query->where(function (Builder $query) use ($search, $searchSensitiveFields): void {
+ $query->where('event', 'like', "%{$search}%")
+ ->orWhere('description', 'like', "%{$search}%")
+ ->orWhere('resource_name', 'like', "%{$search}%")
+ ->orWhere('actor_name', 'like', "%{$search}%")
+ ->when($searchSensitiveFields, fn (Builder $query) => $query->orWhere('actor_email', 'like', "%{$search}%"));
+ });
+ });
+ }
+
+ public function scopeLatestFirst(Builder $query): Builder
+ {
+ return $query->latest('created_at')->latest('id');
+ }
+
/**
* @param array<string, mixed> $context
*/
@@ -53,10 +88,6 @@ class AuditEvent extends Model
try {
$attributes = self::attributesFor($event, $context);
- if ($attributes === null) {
- return;
- }
-
DB::afterCommit(function () use ($attributes): void {
defer(function () use ($attributes): void {
try {
@@ -90,8 +121,8 @@ class AuditEvent extends Model
$parts = explode('.', $event);
$source = $parts[0] ?? 'system';
- $resourceType = $parts[1] ?? null;
- $action = end($parts) ?: 'event';
+ $resourceType = data_get($context, 'resource') ?? ($parts[1] ?? null);
+ $action = data_get($context, 'action') ?? (end($parts) ?: 'event');
$resourceUuid = self::firstContextValue($context, $resourceType ? "{$resourceType}_uuid" : null, '_uuid');
$resourceName = self::firstContextValue($context, $resourceType ? "{$resourceType}_name" : null, '_name');
$user = auth()->user();
@@ -163,7 +194,7 @@ class AuditEvent extends Model
private static function redact(mixed $value, ?string $key = null): mixed
{
- if ($key !== null && preg_match('/password|secret|token|private_key|signature|credential|invitation_email/i', $key)) {
+ if ($key !== null && preg_match('/password|secret|token|private_key|signature|credential|invitation_email|api_key|access_key|authorization|cookie/i', $key)) {
return '[REDACTED]';
}
diff --git a/database/migrations/2026_08_20_000000_create_audit_events_table.php b/database/migrations/2026_08_20_000000_create_audit_events_table.php
index f3a268f00a..0ace21f229 100644
--- a/database/migrations/2026_08_20_000000_create_audit_events_table.php
+++ b/database/migrations/2026_08_20_000000_create_audit_events_table.php
@@ -29,8 +29,10 @@ return new class extends Migration
$table->string('user_agent', 200)->nullable();
$table->timestamp('created_at')->useCurrent();
- $table->index(['team_id', 'created_at']);
- $table->index(['team_id', 'action', 'created_at']);
+ $table->index('created_at');
+ $table->index(['team_id', 'created_at', 'id']);
+ $table->index(['team_id', 'action', 'created_at', 'id']);
+ $table->index(['team_id', 'source', 'created_at', 'id']);
$table->index(['team_id', 'resource_type', 'resource_uuid', 'created_at']);
$table->index(['team_id', 'actor_id', 'created_at']);
});
diff --git a/resources/views/livewire/team/audit-log.blade.php b/resources/views/livewire/team/audit-log.blade.php
index adbc8e7358..d561e586a6 100644
--- a/resources/views/livewire/team/audit-log.blade.php
+++ b/resources/views/livewire/team/audit-log.blade.php
@@ -18,21 +18,8 @@
</div>
<div class="grid grid-cols-2 gap-2 sm:flex">
<div class="sm:w-36">
- <x-forms.listbox id="action" live canGate="viewAdmin" :canResource="currentTeam()" :options="[
- ['value' => 'all', 'label' => 'All actions'],
- ['value' => 'created', 'label' => 'Created'],
- ['value' => 'updated', 'label' => 'Updated'],
- ['value' => 'deleted', 'label' => 'Deleted'],
- ['value' => 'deployed', 'label' => 'Deployed'],
- ['value' => 'started', 'label' => 'Started'],
- ['value' => 'stopped', 'label' => 'Stopped'],
- ['value' => 'restarted', 'label' => 'Restarted'],
- ['value' => 'cancelled', 'label' => 'Cancelled'],
- ['value' => 'rollback', 'label' => 'Rollback'],
- ['value' => 'executed', 'label' => 'Executed'],
- ['value' => 'revoked', 'label' => 'Revoked'],
- ['value' => 'imported', 'label' => 'Imported'],
- ]" />
+ <x-forms.listbox id="action" live canGate="viewAdmin" :canResource="currentTeam()"
+ :options="$actionOptions" />
</div>
<div class="sm:w-36">
<x-forms.listbox id="source" live canGate="viewAdmin" :canResource="currentTeam()" :options="[
diff --git a/tests/Feature/AuditEventsTest.php b/tests/Feature/AuditEventsTest.php
index b34a548362..923d36c0e6 100644
--- a/tests/Feature/AuditEventsTest.php
+++ b/tests/Feature/AuditEventsTest.php
@@ -503,6 +503,36 @@ test('audit log redacts sensitive metadata', function () {
->and($metadata['nested']['safe'])->toBe('visible');
});
+test('audit log redacts common credential metadata keys', function (string $key) {
+ auditLog('api.application.updated', [
+ 'team_id' => $this->team->id,
+ $key => 'sensitive-value',
+ ]);
+
+ expect(AuditEvent::query()->sole()->metadata[$key])->toBe('[REDACTED]');
+})->with([
+ 'api_key',
+ 'access_key',
+ 'authorization',
+ 'cookie',
+ 'client_secret',
+]);
+
+test('audit event classification can use explicit resource and action context', function () {
+ auditLog('mcp.control', [
+ 'team_id' => $this->team->id,
+ 'resource' => 'application',
+ 'action' => 'restart',
+ 'resource_uuid' => 'app-123',
+ ]);
+
+ $event = AuditEvent::query()->sole();
+
+ expect($event->resource_type)->toBe('application')
+ ->and($event->action)->toBe('restart')
+ ->and($event->resource_uuid)->toBe('app-123');
+});
+
test('team invitation audit logs redact the invitation email', function () {
auditLog('ui.team_invitation.created', [
'team_id' => $this->team->id,
@@ -567,6 +597,11 @@ test('team admins can query only their team audit events through the api', funct
'source' => 'api',
'action' => 'updated',
'description' => 'Visible event',
+ 'actor_email' => '[email protected]',
+ 'actor_token_name' => 'production token',
+ 'metadata' => ['changed_fields' => ['name']],
+ 'ip_address' => '192.0.2.1',
+ 'user_agent' => 'Sensitive user agent',
]);
AuditEvent::factory()->create([
'team_id' => Team::factory()->create()->id,
@@ -585,7 +620,56 @@ test('team admins can query only their team audit events through the api', funct
->getJson('/api/v1/audit-events?source=api&action=updated')
->assertOk()
->assertJsonCount(1, 'data')
- ->assertJsonPath('data.0.description', 'Visible event');
+ ->assertJsonPath('data.0.description', 'Visible event')
+ ->assertJsonMissingPath('data.0.actor_email')
+ ->assertJsonMissingPath('data.0.actor_token_id')
+ ->assertJsonMissingPath('data.0.actor_token_name')
+ ->assertJsonMissingPath('data.0.metadata')
+ ->assertJsonMissingPath('data.0.ip_address')
+ ->assertJsonMissingPath('data.0.user_agent');
+});
+
+test('team admins with sensitive read access can query full audit event details', function () {
+ AuditEvent::factory()->create([
+ 'team_id' => $this->team->id,
+ 'actor_email' => '[email protected]',
+ 'actor_token_name' => 'production token',
+ 'metadata' => ['changed_fields' => ['name']],
+ 'ip_address' => '192.0.2.1',
+ 'user_agent' => 'Sensitive user agent',
+ ]);
+
+ $token = $this->user->createToken('audit-sensitive-read', ['read', 'read:sensitive']);
+ $token->accessToken->forceFill(['team_id' => $this->team->id])->save();
+ auth()->logout();
+ auth()->forgetGuards();
+
+ $this->withToken($token->plainTextToken)
+ ->getJson('/api/v1/audit-events')
+ ->assertOk()
+ ->assertJsonPath('data.0.actor_email', '[email protected]')
+ ->assertJsonPath('data.0.actor_token_name', 'production token')
+ ->assertJsonPath('data.0.metadata.changed_fields.0', 'name')
+ ->assertJsonPath('data.0.ip_address', '192.0.2.1')
+ ->assertJsonPath('data.0.user_agent', 'Sensitive user agent');
+});
+
+test('tokens without sensitive read access cannot search private audit fields', function () {
+ AuditEvent::factory()->create([
+ 'team_id' => $this->team->id,
+ 'actor_email' => '[email protected]',
+ 'description' => 'Public description',
+ ]);
+
+ $token = $this->user->createToken('audit-read', ['read']);
+ $token->accessToken->forceFill(['team_id' => $this->team->id])->save();
+ auth()->logout();
+ auth()->forgetGuards();
+
+ $this->withToken($token->plainTextToken)
+ ->getJson('/api/v1/[email protected]')
+ ->assertOk()
+ ->assertJsonCount(0, 'data');
});
test('team members cannot query audit events through the api', function () {
@@ -613,6 +697,19 @@ test('audit source filter omits the unused system source', function () {
->assertDontSee('System');
});
+test('audit action filter includes actions recorded for the current team', function () {
+ AuditEvent::factory()->create([
+ 'team_id' => $this->team->id,
+ 'action' => 'backup_schedule_deleted',
+ ]);
+
+ Livewire::test(AuditLog::class)
+ ->assertViewHas('actionOptions', fn (array $options): bool => in_array([
+ 'value' => 'backup_schedule_deleted',
+ 'label' => 'Backup Schedule Deleted',
+ ], $options, true));
+});
+
test('resource clone audit starts only after the destination server capability check', function () {
$source = file_get_contents(app_path('Livewire/Project/Shared/ResourceOperations.php'));