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): validate event filters and preserve suppression state

Andras Bacsai4e40a2c05b8b
diff --git a/app/Http/Controllers/Api/AuditEventsController.php b/app/Http/Controllers/Api/AuditEventsController.php
index 03c1fcb49c..da452bb303 100644
--- a/app/Http/Controllers/Api/AuditEventsController.php
+++ b/app/Http/Controllers/Api/AuditEventsController.php
@@ -6,6 +6,8 @@ use App\Http\Controllers\Controller;
use App\Models\AuditEvent;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Validation\Rule;
class AuditEventsController extends Controller
{
@@ -20,8 +22,24 @@ class AuditEventsController extends Controller
return response()->json(['message' => 'Only team admins and owners can view audit logs.'], 403);
}
- $perPage = max(1, min(100, $request->integer('per_page', 25)));
- $search = trim((string) $request->query('search', ''));
+ $validator = Validator::make($request->all(), [
+ 'per_page' => ['sometimes', 'integer', 'min:1', 'max:100'],
+ 'page' => ['sometimes', 'integer', 'min:1'],
+ 'search' => ['sometimes', 'nullable', 'string', 'max:255'],
+ 'action' => ['sometimes', 'nullable', 'string', 'max:255'],
+ 'source' => ['sometimes', 'nullable', 'string', Rule::in(['all', 'ui', 'api', 'mcp', 'webhook', 'system', 'scheduler'])],
+ ]);
+
+ if ($validator->fails()) {
+ return response()->json([
+ 'message' => 'Validation failed.',
+ 'errors' => $validator->errors(),
+ ], 422);
+ }
+
+ $validated = $validator->validated();
+ $perPage = (int) ($validated['per_page'] ?? 25);
+ $search = trim((string) ($validated['search'] ?? ''));
$canReadSensitive = $request->attributes->get('can_read_sensitive', false) === true;
$events = AuditEvent::query()
->select([
@@ -50,13 +68,13 @@ class AuditEventsController extends Controller
->visibleToTeam($teamId)
->filtered(
search: $search,
- action: $request->string('action', 'all')->toString(),
- source: $request->string('source', 'all')->toString(),
+ action: (string) ($validated['action'] ?? 'all'),
+ source: (string) ($validated['source'] ?? 'all'),
searchSensitiveFields: $canReadSensitive,
)
->latestFirst()
->paginate($perPage);
- return response()->json($events);
+ return response()->json(serializeApiResponse($events));
}
}
diff --git a/app/Traits/Auditable.php b/app/Traits/Auditable.php
index 0b065db44e..878d46c1e4 100644
--- a/app/Traits/Auditable.php
+++ b/app/Traits/Auditable.php
@@ -59,12 +59,13 @@ trait Auditable
public function withoutAuditLogging(Closure $callback): mixed
{
+ $wasAuditLoggingEnabled = $this->auditLoggingEnabled;
$this->auditLoggingEnabled = false;
try {
return $callback();
} finally {
- $this->auditLoggingEnabled = true;
+ $this->auditLoggingEnabled = $wasAuditLoggingEnabled;
}
}
diff --git a/tests/Feature/AuditEventsTest.php b/tests/Feature/AuditEventsTest.php
index ebe7d8f94d..8851ea0dba 100644
--- a/tests/Feature/AuditEventsTest.php
+++ b/tests/Feature/AuditEventsTest.php
@@ -483,6 +483,31 @@ test('team resource models opt in to automatic auditing', function (string $mode
StandaloneClickhouse::class,
]);
+test('withoutAuditLogging suppresses mutations until the outer callback ends', function () {
+ $project = Project::factory()->create([
+ 'team_id' => $this->team->id,
+ 'name' => 'Original project',
+ 'description' => 'Original description',
+ ]);
+ AuditEvent::query()->delete();
+
+ $project->withoutAuditLogging(function () use ($project) {
+ $project->update(['name' => 'Outer suppressed']);
+
+ $project->withoutAuditLogging(function () use ($project) {
+ $project->update(['description' => 'Inner suppressed']);
+ });
+
+ $project->update(['name' => 'Still suppressed']);
+ });
+
+ expect(AuditEvent::query()->count())->toBe(0);
+
+ $project->update(['description' => 'Logged after suppression']);
+
+ expect(AuditEvent::query()->pluck('event')->all())->toBe(['ui.project.updated']);
+});
+
test('status-only database updates do not record last online audit changes', function () {
$project = Project::factory()->create(['team_id' => $this->team->id]);
$environment = Environment::factory()->create(['project_id' => $project->id]);
@@ -715,6 +740,53 @@ test('team members cannot query audit events through the api', function () {
->assertForbidden();
});
+test('audit events api rejects pagination and filter values outside the allowed bounds', function (string $query, string $field) {
+ $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/audit-events?'.$query)
+ ->assertUnprocessable()
+ ->assertJsonValidationErrors([$field]);
+})->with([
+ 'per_page below minimum' => ['per_page=0', 'per_page'],
+ 'per_page above maximum' => ['per_page=101', 'per_page'],
+ 'page below minimum' => ['page=0', 'page'],
+ 'unsupported source' => ['source=unknown', 'source'],
+ 'action longer than 255 characters' => ['action='.str_repeat('a', 256), 'action'],
+]);
+
+test('audit events api accepts valid pagination and filter values', function () {
+ AuditEvent::factory()->create([
+ 'team_id' => $this->team->id,
+ 'event' => 'api.project.updated',
+ 'source' => 'api',
+ 'action' => 'updated',
+ 'description' => 'Visible event',
+ ]);
+ AuditEvent::factory()->create([
+ 'team_id' => $this->team->id,
+ 'event' => 'ui.project.created',
+ 'source' => 'ui',
+ 'action' => 'created',
+ 'description' => 'Other event',
+ ]);
+
+ $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/audit-events?per_page=1&source=api&action=updated&search=Visible')
+ ->assertOk()
+ ->assertJsonCount(1, 'data')
+ ->assertJsonPath('data.0.description', 'Visible event')
+ ->assertJsonPath('per_page', 1);
+});
+
test('audit source filter omits the unused system source', function () {
Livewire::test(AuditLog::class)
->assertSee('All sources')