-
Notifications
You must be signed in to change notification settings - Fork 15
Enforce company membership check on tenant switch #703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1170e42
7de31de
7f9bf22
b3bc789
293b2d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| --- | ||
| name: test-gaps | ||
| description: Flags security- or correctness-critical logic (auth checks, guards, validation) added or changed without a test proving both its allow and its deny path | ||
| --- | ||
|
|
||
| # Purpose | ||
|
|
||
| Catches the specific failure mode where a real behavior change ships with no test proving it | ||
| works: code added to prevent something bad, with nothing that proves the bad thing is actually | ||
| prevented. Triggered by this incident: an `abort_unless`/authorization guard was added to | ||
| `MyCompanies::switch` with zero test coverage — it could have been silently deleted or inverted | ||
| in a later change and nothing would fail. | ||
|
|
||
| This is narrower than `security-review` (which finds *missing* guards in code) and unrelated to | ||
| `test-honesty` (which is about schema/factory/seeder alignment). This skill assumes the guard | ||
| already exists and asks: is there a test that would fail if the guard were removed? | ||
|
|
||
| --- | ||
|
|
||
| # 1. Trigger Conditions | ||
|
|
||
| Apply this check whenever a diff adds or modifies any of: | ||
|
|
||
| - an authorization/ownership check (`abort_if`/`abort_unless`, `Gate::`, `->can()`, a Policy | ||
| method, a custom `assertBelongsTo*`/`assertOwns*`-style guard) | ||
| - input validation added specifically to reject a class of bad input (not just Filament's | ||
| built-in `->required()`/`->rule()` form validation, which already has its own test convention) | ||
| - a permission/role check gating an action, route, or Livewire method | ||
|
|
||
| --- | ||
|
|
||
| # 2. Coverage Rule | ||
|
|
||
| Every guard covered by Rule 1 needs **two** tests, not one: | ||
|
|
||
| - **Allow path**: the legitimate case still succeeds through the guard. | ||
| - **Deny path**: the guard actually blocks the illegitimate case — asserts the specific | ||
| exception/response the guard produces, not just "doesn't crash." | ||
|
|
||
| A guard with only an allow-path test (or no test) is a gap: nothing would catch the guard being | ||
| weakened, removed, or silently made a no-op in a later refactor. | ||
|
|
||
| --- | ||
|
|
||
| # 3. Test Placement Rule | ||
|
|
||
| If the guard lives inline inside a Filament/Livewire action closure, page method, or controller, | ||
| and testing it directly would require going through framework machinery that doesn't reliably | ||
| reach the unauthorized case (e.g. a table's own query already scopes out records the user | ||
| couldn't select in the first place, so a Feature test via `callTableAction()` never actually | ||
| exercises the deny path), that's a signal the check belongs in an extracted, directly-testable | ||
| method — a service method, a Policy, a dedicated class — not a reason to skip the deny-path test. | ||
|
|
||
| --- | ||
|
|
||
| # 4. What This Skill Does NOT Do | ||
|
|
||
| - Does not invent new authorization requirements — only checks that guards which already exist | ||
| in the diff are proven by tests. | ||
| - Does not replace `security-review`'s job of spotting where a guard is *missing* entirely. | ||
| - Does not apply to routine Filament form validation (`->required()`, `->rule()`, etc.) — that | ||
| has its own established test conventions in this codebase and isn't the failure mode this | ||
| skill targets. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,13 +2,15 @@ | |||||
|
|
||||||
| namespace Modules\Core\Services; | ||||||
|
|
||||||
| use Illuminate\Auth\Access\AuthorizationException; | ||||||
| use Illuminate\Support\Arr; | ||||||
| use Illuminate\Support\Facades\DB; | ||||||
| use Illuminate\Support\Facades\Hash; | ||||||
| use Illuminate\Support\Facades\Storage; | ||||||
| use Illuminate\Support\Str; | ||||||
| use Modules\Core\Events\UserWasCreated; | ||||||
| use Modules\Core\Events\UserWasUpdated; | ||||||
| use Modules\Core\Models\Company; | ||||||
| use Modules\Core\Models\Upload; | ||||||
| use Modules\Core\Models\User; | ||||||
| use Throwable; | ||||||
|
|
@@ -134,4 +136,18 @@ public function removeAvatar(User $user): bool | |||||
|
|
||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Guard against switching a user's active tenant to a company they aren't a | ||||||
| * member of. Called from the record resolved by Filament's table-action | ||||||
| * dispatch, which is not something callers otherwise verify — see #687. | ||||||
| * | ||||||
| * @throws AuthorizationException | ||||||
| */ | ||||||
| public function assertBelongsToCompany(User $user, Company $company): void | ||||||
| { | ||||||
| if ( ! $user->companies()->whereKey($company->id)->exists()) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Fix PSR-12 whitespace before the negation operator. Remove the space after the opening parenthesis. This change must pass Laravel Pint before merge. Proposed fix- if ( ! $user->companies()->whereKey($company->id)->exists()) {
+ if (! $user->companies()->whereKey($company->id)->exists()) {As per coding guidelines, “All code must conform to PSR-12 before merge — 📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||
| throw new AuthorizationException("User {$user->id} is not a member of company {$company->id}."); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| namespace Modules\Core\Tests\Unit\Services; | ||
|
|
||
| use Illuminate\Auth\Access\AuthorizationException; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Modules\Core\Models\Company; | ||
| use Modules\Core\Models\User; | ||
| use Modules\Core\Services\UserService; | ||
| use Modules\Core\Tests\AbstractAdminPanelTestCase; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
|
|
||
| #[CoversClass(UserService::class)] | ||
| class UserServiceTest extends AbstractAdminPanelTestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| private UserService $service; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->service = app(UserService::class); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function it_allows_a_user_to_switch_to_a_company_they_belong_to(): void | ||
|
Comment on lines
+28
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Add a security group to both authorization tests. Add As per coding guidelines, “Use Also applies to: 42-43 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| { | ||
| /* Arrange */ | ||
| $user = User::factory()->withCompany(['search_code' => 'MEMBER'])->create(); | ||
|
|
||
| /** @var Company $company */ | ||
| $company = $user->companies()->first(); | ||
|
|
||
| /* Act & Assert */ | ||
| $this->service->assertBelongsToCompany($user, $company); | ||
| $this->addToAssertionCount(1); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function it_refuses_to_switch_to_a_company_the_user_does_not_belong_to(): void | ||
| { | ||
| /* Arrange */ | ||
| $user = User::factory()->withCompany(['search_code' => 'MEMBER'])->create(); | ||
| $foreignCompany = Company::factory()->create(['search_code' => 'FOREIGN']); | ||
|
|
||
| /* Assert */ | ||
| $this->expectException(AuthorizationException::class); | ||
|
|
||
| /* Act */ | ||
| $this->service->assertBelongsToCompany($user, $foreignCompany); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Add a language identifier to the command block.
Line 202 starts an unlabeled fenced code block. Change it to
```bashto resolve markdownlint rule MD040.🧰 Tools
🪛 markdownlint-cli2 (0.23.1)
[warning] 202-202: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🤖 Prompt for AI Agents
Source: Linters/SAST tools