Confine every HRM query to the authenticated user's tenant - #1
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/destroyactions, 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 sharedApp\Models\Concerns\TenantScoped(zerp-pk/zerp#5). A foreign id now resolves tonull, 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,idis deliberately left alone: a company's owner row has a differentcreated_bythan its staff, so the same guard would break user assignment.No provisioning carve-out needed —
HrmModel::defaultdata()only touches SpatieRole/Permissionrows, 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,
blockedon the other's, and the list shows 1 of 2 rows — so it isolates without over-filtering, which was the real risk. Covered bytests/Unit/TenantScopeTest.php; removing the scope fails it.