Skip to content

Confine every HRM query to the authenticated user's tenant - #1

Merged
HafizMMoaz merged 1 commit into
masterfrom
tenant-scope
Jul 14, 2026
Merged

Confine every HRM query to the authenticated user's tenant#1
HafizMMoaz merged 1 commit into
masterfrom
tenant-scope

Conversation

@HafizMMoaz

Copy link
Copy Markdown
Member

Closes the cross-tenant IDOR in this module. This is the highest-risk one — it holds employee PII, salary and payroll data.

The bug

78 update/destroy actions, and ownership was checked in files covering just 5 of them. Authorisation was a capability check alone — can("edit-employees") — which every tenant's staff passes. Another company's id resolved fine and was then read, edited or deleted.

The fix

All 41 HRM tables carry created_by, so the boundary goes on the models via the shared App\Models\Concerns\TenantScoped (zerp-pk/zerp#5). A foreign id now resolves to null, so route-model binding 404s before a controller runs — no per-action guards to forget.

63 exists: rules on HRM tables are scoped by hand. These go through the query builder and never see an Eloquent scope, so without this you could still attach a record to another company's department, branch or employee. exists:users,id is deliberately left alone: a company's owner row has a different created_by than its staff, so the same guard would break user assignment.

No provisioning carve-out neededHrmModel::defaultdata() only touches Spatie Role/Permission rows, which this scope does not apply to. (Lead needed one; this module does not.)

Every find() site here is already null-guarded, so nothing newly 500s.

Verification

Proven against real MySQL inside a rolled-back transaction: two tenants, each sees its own branch and department, blocked on the other's, and the list shows 1 of 2 rows — so it isolates without over-filtering, which was the real risk. Covered by tests/Unit/TenantScopeTest.php; removing the scope fails it.

This module has 78 update/destroy actions and almost no ownership checks:
authorisation was a capability check alone (`can('edit-employees')`), which
every tenant's staff passes. Another company's id resolved fine and was then
read, edited or deleted — across employees, payroll, salary, documents and
leave, i.e. employee PII and financial data.

The boundary now lives on the models via App\Models\Concerns\TenantScoped:
all 41 tables here carry created_by, so every query is confined to it and a
foreign id 404s at route-model binding before a controller runs.

The 63 exists: rules on HRM tables are scoped by hand — they go through the
query builder and never see an Eloquent scope, so without this a record could
still be attached to another company's department, branch or employee.

No provisioning carve-out is needed: HrmModel::defaultdata() only touches
Spatie Role/Permission rows, which this scope does not apply to.
@HafizMMoaz
HafizMMoaz requested a review from a team as a code owner July 13, 2026 22:58
@HafizMMoaz HafizMMoaz self-assigned this Jul 14, 2026
@HafizMMoaz
HafizMMoaz merged commit 547106c into master Jul 14, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant