Mitigation Strategy: Principle of Least Privilege and Granular Permissions
-
Description:
- Identify Actions: List all distinct actions users can perform within the application.
- Create Permissions (using
spatie/laravel-permission
): Use the package's methods (Permission::create(['name' => '...'])
) to create specific permissions for each action. Avoid broad permissions. - Create Roles (using
spatie/laravel-permission
, if needed): Use the package's methods (Role::create(['name' => '...'])
) to group related permissions into roles only if it simplifies management without granting excessive access. - Assign Permissions/Roles (using
spatie/laravel-permission
): Use the package's methods ($user->givePermissionTo(...)
,$user->assignRole(...)
) to grant only the minimum required permissions/roles to users. - Avoid Default Permissions: Do not assign any permissions by default.
- Regular Review: Regularly review permissions and roles created and assigned using the package.
-
Threats Mitigated:
- Incorrect Role/Permission Assignment (Human Error): (Severity: High)
- "Super Admin" Bypass (Partial): (Severity: High)
- Insider Threats: (Severity: Medium)
-
Impact:
- Incorrect Role/Permission Assignment: Risk significantly reduced.
- "Super Admin" Bypass: Partial risk reduction.
- Insider Threats: Significant risk reduction.
-
Currently Implemented:
- Permissions and roles are defined using
spatie/laravel-permission
's methods in seeders. - Basic user role assignment is done using
$user->assignRole(...)
.
- Permissions and roles are defined using
-
Missing Implementation:
- Granular permissions are missing for some features.
- Regular, scheduled reviews are not formalized.
- No automated tests specifically verify minimum permissions.
Mitigation Strategy: Mandatory Code Reviews for Authorization Changes (Focusing on spatie/laravel-permission
Usage)
-
Description:
- Policy Enforcement: Enforce code reviews for any code change involving
spatie/laravel-permission
's methods or Blade directives. - Checklist (Specific to
spatie/laravel-permission
): Include in the checklist:- Correct usage of
can
,@can
,hasRole
,hasPermissionTo
,givePermissionTo
,assignRole
,revokePermissionTo
,removeRole
, etc. - Proper cache invalidation using
forgetCachedPermissions
. - No hardcoded role or permission names (use constants or configuration).
- Correct usage of
- Reviewer Training: Ensure reviewers are familiar with
spatie/laravel-permission
.
- Policy Enforcement: Enforce code reviews for any code change involving
-
Threats Mitigated:
- Incorrect Role/Permission Assignment (Human Error): (Severity: High)
- Improper use of Blade Directives: (Severity: Medium)
- Relying solely on
hasRole
/hasPermissionTo
: (Severity: Medium) - "Super Admin" Bypass (Partial): (Severity: High)
-
Impact:
- Incorrect Role/Permission Assignment: High risk reduction.
- Improper use of Blade Directives: High risk reduction.
- Relying solely on
hasRole
/hasPermissionTo
: High risk reduction. - "Super Admin" Bypass: Moderate risk reduction.
-
Currently Implemented:
- Code reviews are mandatory.
-
Missing Implementation:
- A dedicated authorization checklist (focused on
spatie/laravel-permission
) is missing. - Formal training on the package for reviewers is inconsistent.
- A dedicated authorization checklist (focused on
Mitigation Strategy: Automated Authorization Testing (Targeting spatie/laravel-permission
)
-
Description:
- Test Types: Create tests that specifically use
spatie/laravel-permission
's methods and features:- Test
can
,hasRole
,hasPermissionTo
in various scenarios. - Test Blade directives (
@can
,@role
, etc.). - Test permission/role assignment and revocation.
- Test cache invalidation (
forgetCachedPermissions
).
- Test
- Test Data: Create test users and assign roles/permissions using the package's methods.
- Assertions: Verify the expected behavior of the package's methods and directives.
- Continuous Integration: Integrate tests into the CI pipeline.
- Test Types: Create tests that specifically use
-
Threats Mitigated:
- Incorrect Role/Permission Assignment (Human Error): (Severity: High)
- Improper use of Blade Directives: (Severity: Medium)
- Relying solely on
hasRole
/hasPermissionTo
: (Severity: Medium) - Caching Issues: (Severity: Medium)
- Regression Bugs: (Severity: Medium)
-
Impact:
- Incorrect Role/Permission Assignment: High risk reduction.
- Improper use of Blade Directives: High risk reduction.
- Relying solely on
hasRole
/hasPermissionTo
: High risk reduction. - Caching Issues: Moderate risk reduction.
- Regression Bugs: High risk reduction.
-
Currently Implemented:
- Some feature tests use
@can
and related methods.
- Some feature tests use
-
Missing Implementation:
- Comprehensive negative tests are lacking.
- Dedicated test users with specific roles/permissions are not consistently used.
- Tests for cache invalidation are missing.
Mitigation Strategy: Proper Usage of Blade Directives (Provided by spatie/laravel-permission
)
-
Description:
- Understand Directives: Thoroughly understand
@can
,@cannot
,@role
,@hasrole
,@hasanyrole
,@hasallroles
,@unlessrole
,@haspermissionto
,@hasanypermission
,@hasallpermissions
. Refer to the package documentation. - Always Pass Model Instances: When checking permissions on a specific model instance, always pass the instance to
@can
. - Avoid Logic in Views: Minimize complex authorization logic directly within Blade templates. Use Policies and helper methods in your controllers or models.
- Code Reviews: Review Blade templates for correct directive usage.
- Understand Directives: Thoroughly understand
-
Threats Mitigated:
- Improper use of Blade Directives: (Severity: Medium) - Prevents authorization bypasses due to incorrect directive usage.
-
Impact:
- Improper use of Blade Directives: High risk reduction.
-
Currently Implemented:
- Blade directives are used for authorization checks in views.
-
Missing Implementation:
- Consistent enforcement of passing model instances to
@can
is not always followed. - Code reviews don't always catch subtle errors in directive usage.
- Consistent enforcement of passing model instances to
Mitigation Strategy: Proper Cache Management (Using spatie/laravel-permission
's Features)
-
Description:
- Understand Caching: Understand how the package caches permissions.
- Cache Clearing: Whenever roles or permissions are modified (using the package's methods), explicitly call
forgetCachedPermissions()
. - Testing: Include automated tests that specifically verify cache invalidation:
- Modify roles/permissions using the package's methods.
- Call
forgetCachedPermissions()
. - Verify changes are reflected in authorization checks.
-
Threats Mitigated:
- Caching Issues: (Severity: Medium)
-
Impact:
- Caching Issues: High risk reduction.
-
Currently Implemented:
forgetCachedPermissions()
is called in some places.
-
Missing Implementation:
- Consistent cache clearing is not implemented everywhere.
- Automated tests for cache invalidation are missing.
Mitigation Strategy: Prefer can
and Policies over hasRole
/hasPermissionTo
-
Description:
- Understand the Difference: Understand that
can
(and@can
) checks against model policies, whilehasRole
andhasPermissionTo
only check for the presence of a role or permission. - Use
can
by Default: In most cases, usecan
(and@can
) for authorization checks. This ensures that model-specific authorization logic (defined in Policies) is considered. - Use
hasRole
/hasPermissionTo
Sparingly: UsehasRole
andhasPermissionTo
only when you specifically need to check if a user possesses a role or permission, and you are certain no model policies are involved. - Code Reviews: Ensure code reviews check for appropriate use of these methods.
- Understand the Difference: Understand that
-
Threats Mitigated:
- Relying solely on
hasRole
/hasPermissionTo
: (Severity: Medium) - Prevents authorization bypasses due to neglecting model policies.
- Relying solely on
-
Impact:
- Relying solely on
hasRole
/hasPermissionTo
: High risk reduction.
- Relying solely on
-
Currently Implemented:
can
and@can
are used in many places.
-
Missing Implementation:
- Consistent use of
can
overhasRole
/hasPermissionTo
is not always followed. - Code reviews don't always catch incorrect usage.
- Consistent use of
Mitigation Strategy: Avoid Wildcard Permissions
-
Description:
- Understand Wildcard: The wildcard permission (
*
) grants access to everything. - Avoid Wildcard: Do not assign the wildcard permission (
*
) to any role, especially the "Super Admin" role. - Explicit Permissions: Explicitly assign the necessary permissions to roles, even for administrative roles.
- Review Existing Roles: Review all existing roles (created using
spatie/laravel-permission
) and remove the wildcard permission if it exists.
- Understand Wildcard: The wildcard permission (
-
Threats Mitigated:
- "Super Admin" Bypass (Partial): (Severity: High) - Reduces the impact of a compromised super admin account.
- Incorrect Role/Permission Assignment (Human Error): (Severity: High) - Limits the scope of damage if a role with the wildcard permission is accidentally assigned.
-
Impact:
- "Super Admin" Bypass: Moderate risk reduction.
- Incorrect Role/Permission Assignment: High risk reduction.
-
Currently Implemented:
- None.
-
Missing Implementation:
- The
Super Admin
role currently has the wildcard permission. This needs to be removed and replaced with explicit permissions.
- The