test: add admin access and API scope security tests (360 total)
Security/AdminAccessTest: verifies non-admin users are blocked from
all admin web routes (dashboard, users, reports, settings, instances,
curated onboarding) and admin API endpoints.
Security/ApiScopeSecurityTest: verifies read-only tokens cannot write
(follow, favourite, delete, mute, block), write tokens can read+write,
cross-user access is denied, and private statuses are protected.
BUG FOUND: CheckForAnyScope middleware (referenced in v1/admin routes)
was removed in Passport 13. All /api/v1/admin/* routes throw
BindingResolutionException. 6 tests skipped pending fix.
4 weeks ago
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use App\Models\Status;
|
|
|
|
|
use App\Models\User;
|
test: add admin access and API scope security tests (360 total)
Security/AdminAccessTest: verifies non-admin users are blocked from
all admin web routes (dashboard, users, reports, settings, instances,
curated onboarding) and admin API endpoints.
Security/ApiScopeSecurityTest: verifies read-only tokens cannot write
(follow, favourite, delete, mute, block), write tokens can read+write,
cross-user access is denied, and private statuses are protected.
BUG FOUND: CheckForAnyScope middleware (referenced in v1/admin routes)
was removed in Passport 13. All /api/v1/admin/* routes throw
BindingResolutionException. 6 tests skipped pending fix.
4 weeks ago
|
|
|
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
|
|
|
|
|
use Laravel\Passport\Passport;
|
|
|
|
|
|
|
|
|
|
uses(LazilyRefreshDatabase::class);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| API Scope Security Tests
|
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
| These tests verify that API tokens with limited scopes cannot perform
|
|
|
|
|
| actions outside their granted permissions.
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
describe('read-only token cannot write', function () {
|
|
|
|
|
it('cannot follow with read-only scope', function () {
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$user->refresh();
|
|
|
|
|
$target = User::factory()->create();
|
|
|
|
|
$target->refresh();
|
|
|
|
|
Passport::actingAs($user, ['read']);
|
|
|
|
|
|
|
|
|
|
$this->postJson("/api/v1/accounts/{$target->profile_id}/follow")
|
|
|
|
|
->assertForbidden();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cannot favourite with read-only scope', function () {
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$user->refresh();
|
|
|
|
|
$status = Status::factory()->create(['type' => 'photo', 'scope' => 'public']);
|
|
|
|
|
Passport::actingAs($user, ['read']);
|
|
|
|
|
|
|
|
|
|
$this->postJson("/api/v1/statuses/{$status->id}/favourite")
|
|
|
|
|
->assertForbidden();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cannot delete a status with read-only scope', function () {
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$user->refresh();
|
|
|
|
|
$status = Status::factory()->create([
|
|
|
|
|
'profile_id' => $user->profile_id,
|
|
|
|
|
'type' => 'photo',
|
|
|
|
|
]);
|
|
|
|
|
Passport::actingAs($user, ['read']);
|
|
|
|
|
|
|
|
|
|
$this->deleteJson("/api/v1/statuses/{$status->id}")
|
|
|
|
|
->assertForbidden();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cannot mute with read-only scope', function () {
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$user->refresh();
|
|
|
|
|
$target = User::factory()->create();
|
|
|
|
|
$target->refresh();
|
|
|
|
|
Passport::actingAs($user, ['read']);
|
|
|
|
|
|
|
|
|
|
$this->postJson("/api/v1/accounts/{$target->profile_id}/mute")
|
|
|
|
|
->assertForbidden();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cannot block with read-only scope', function () {
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$user->refresh();
|
|
|
|
|
$target = User::factory()->create();
|
|
|
|
|
$target->refresh();
|
|
|
|
|
Passport::actingAs($user, ['read']);
|
|
|
|
|
|
|
|
|
|
$this->postJson("/api/v1/accounts/{$target->profile_id}/block")
|
|
|
|
|
->assertForbidden();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('write token can read and write', function () {
|
|
|
|
|
it('can read verify credentials with write scope', function () {
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$user->refresh();
|
|
|
|
|
Passport::actingAs($user, ['read', 'write']);
|
|
|
|
|
|
|
|
|
|
$this->getJson('/api/v1/accounts/verify_credentials')
|
|
|
|
|
->assertOk();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can follow with write scope', function () {
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$user->refresh();
|
|
|
|
|
$target = User::factory()->create();
|
|
|
|
|
$target->refresh();
|
|
|
|
|
Passport::actingAs($user, ['read', 'write', 'follow']);
|
|
|
|
|
|
|
|
|
|
$this->postJson("/api/v1/accounts/{$target->profile_id}/follow")
|
|
|
|
|
->assertOk();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('non-admin tokens cannot access admin endpoints', function () {
|
|
|
|
|
it('rejects admin:read scope for non-admin user', function () {
|
|
|
|
|
$user = User::factory()->create(['is_admin' => false]);
|
|
|
|
|
$user->refresh();
|
|
|
|
|
Passport::actingAs($user, ['admin:read']);
|
|
|
|
|
|
|
|
|
|
$this->getJson('/api/admin/stats')
|
|
|
|
|
->assertNotFound();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('rejects admin:write scope for non-admin user', function () {
|
|
|
|
|
$user = User::factory()->create(['is_admin' => false]);
|
|
|
|
|
$user->refresh();
|
|
|
|
|
Passport::actingAs($user, ['admin:write']);
|
|
|
|
|
|
|
|
|
|
$this->postJson('/api/v1/admin/domain_blocks', [
|
|
|
|
|
'domain' => 'test.example.com',
|
|
|
|
|
])->assertForbidden();
|
|
|
|
|
});
|
test: add admin access and API scope security tests (360 total)
Security/AdminAccessTest: verifies non-admin users are blocked from
all admin web routes (dashboard, users, reports, settings, instances,
curated onboarding) and admin API endpoints.
Security/ApiScopeSecurityTest: verifies read-only tokens cannot write
(follow, favourite, delete, mute, block), write tokens can read+write,
cross-user access is denied, and private statuses are protected.
BUG FOUND: CheckForAnyScope middleware (referenced in v1/admin routes)
was removed in Passport 13. All /api/v1/admin/* routes throw
BindingResolutionException. 6 tests skipped pending fix.
4 weeks ago
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('cross-user access prevention', function () {
|
|
|
|
|
it('cannot delete another users status', function () {
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$user->refresh();
|
|
|
|
|
$other = User::factory()->create();
|
|
|
|
|
$other->refresh();
|
|
|
|
|
$status = Status::factory()->create([
|
|
|
|
|
'profile_id' => $other->profile_id,
|
|
|
|
|
'type' => 'photo',
|
|
|
|
|
]);
|
|
|
|
|
Passport::actingAs($user, ['write']);
|
|
|
|
|
|
|
|
|
|
$this->deleteJson("/api/v1/statuses/{$status->id}")
|
|
|
|
|
->assertNotFound();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cannot access private status without following', function () {
|
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$user->refresh();
|
|
|
|
|
$other = User::factory()->create();
|
|
|
|
|
$other->refresh();
|
|
|
|
|
$status = Status::factory()->private()->create([
|
|
|
|
|
'profile_id' => $other->profile_id,
|
|
|
|
|
'type' => 'photo',
|
|
|
|
|
]);
|
|
|
|
|
Passport::actingAs($user, ['read']);
|
|
|
|
|
|
|
|
|
|
$this->getJson("/api/v1/statuses/{$status->id}")
|
|
|
|
|
->assertForbidden();
|
|
|
|
|
});
|
|
|
|
|
});
|