Overview
The package implements defense-in-depth security through:- Relation whitelisting
- Column validation
- Operator restrictions
- Query complexity limits
- SQL injection prevention
- Permission-based access control
Relation Allowlist
The service enforces allowlisted relations via theRELATIONS constant on your models.
How It Works
filtering.strict_relations is enabled (default), any relation not in the allowlist will trigger an error:
Configuration
Best Practice
Only include relations that should be accessible via the API:Column Validation
The package validates column names before applying filters to prevent SQL errors and potential injection attempts.Configuration
How It Works
- Column names are validated against the actual database schema
- Valid columns are cached to avoid repeated schema queries
- Invalid column attempts are rejected before query execution
Operator Allowlist
Only operators defined infiltering.allowed_operators are accepted in oper filters.
Default Allowed Operators
Operator Validation
Unsupported or potentially dangerous operators throw an exception:The allowlist prevents injection of dangerous SQL operators. Only add operators to the allowlist if you have a specific, validated use case.
Filter Complexity Limits
The package limitsoper depth and total conditions to protect against abusive queries.
Default Limits
Why This Matters
Deeply nested or excessively complex filters can:- Cause database timeouts
- Consume excessive memory
- Enable denial-of-service attacks
Protection in Action
SQL Injection Prevention
The package uses Laravel’s query builder with parameter binding throughout:Safe Query Building
Defense Layers
- Column validation - Ensures column names are valid
- Operator allowlist - Only permits safe operators
- Parameter binding - All values are bound, never concatenated
- Input validation - Values are validated before query building
While the package provides strong SQL injection protection, always validate and sanitize input in your application layer as well. Defense in depth is essential.
Permission-Based Access Control
When using Spatie’slaravel-permission package, ensure proper authorization flow.
Middleware Order
Permission Cache
The package uses Spatie’s permission cache to avoid repeated database queries:Multi-Tenant Isolation
Ensure guard and team ID are set correctly:Environment Configuration
Environment variables are only referenced in the config file, making the package compatible with Laravel’s config caching.Config Cache Safety
env() directly in application code, only in configuration files. This follows Laravel best practices and prevents security issues with cached configs.
Security Best Practices
1. Always Whitelist Relations
2. Keep Validation Enabled
3. Use Appropriate Limits
4. Enable Query Logging (Development Only)
5. Implement Authorization
6. Sanitize Export Data
When using export features:7. Rate Limiting
Apply rate limiting to API endpoints:8. HTTPS Only
Enforce HTTPS in production:Reporting Security Issues
If you discover a security vulnerability:- Do not open a public GitHub issue
- Report privately to the maintainer email listed in package metadata
- Include:
- Description of the vulnerability
- Steps to reproduce
- Potential impact
- Suggested fix (if available)
Security Checklist
Before deploying to production:-
strict_relationsis enabled -
validate_columnsis enabled -
strict_column_validationis enabled - Appropriate
max_depthandmax_conditionslimits are set - Authorization middleware is correctly ordered
- Query logging is disabled (
LOG_QUERY=false) - HTTPS is enforced
- Rate limiting is configured
- Permission cache is properly managed
- Export endpoints are authorized
- Sensitive fields are excluded from API responses
Learn More
- Troubleshooting - Common security-related errors
- Configuration Overview - All security settings
- API Reference - Security-related methods