-
-
Notifications
You must be signed in to change notification settings - Fork 0
Rules Reference
Laravel Safeguard includes comprehensive security rules organized by category. Each rule can be enabled/disabled in your configuration and provides specific security validations for your Laravel application.
Purpose: Ensures APP_DEBUG
is disabled in production environments
Severity: π¨ Critical
Environments: production, staging
Debug mode in production can expose sensitive information including stack traces, environment variables, and internal application structure.
// β
Good - Production configuration
APP_DEBUG=false
// β Bad - Debug enabled in production
APP_DEBUG=true
Configuration:
'app-debug-false-in-production' => true,
Purpose: Verifies that Laravel application key is generated
Severity: π¨ Critical
Environments: all
The application key is used for encrypting sessions, cookies, and other sensitive data. Without it, your application is vulnerable to security attacks.
# Generate application key if missing
php artisan key:generate
Configuration:
'app-key-is-set' => true,
Purpose: Validates all required environment variables are present
Severity: β Error
Environments: all
Ensures all critical environment variables are defined to prevent application errors and security misconfigurations.
Configuration:
'env-has-all-required-keys' => true,
// Define required variables
'required_env_vars' => [
'APP_KEY',
'APP_ENV',
'APP_DEBUG',
'APP_URL',
'DB_CONNECTION',
'DB_HOST',
'DB_DATABASE',
'DB_USERNAME',
'DB_PASSWORD',
'MAIL_MAILER',
'SESSION_DRIVER',
],
Purpose: Detects hardcoded secrets in your codebase
Severity: π¨ Critical
Environments: all
Scans your codebase for hardcoded secrets, API keys, passwords, and other sensitive information that should be in environment variables.
Common patterns detected:
$apiKey = 'sk_live_...'
'password' => 'hardcoded123'
AWS_ACCESS_KEY = 'AKIA...'
define('SECRET_KEY', 'abc123');
Configuration:
'no-secrets-in-code' => true,
// Customize patterns to detect
'secret_patterns' => [
'*_KEY',
'*_SECRET',
'*_TOKEN',
'*_PASSWORD',
'API_*',
'AWS_*',
'STRIPE_*',
],
Purpose: Ensures CSRF protection is enabled
Severity: π¨ Critical
Environments: all
Cross-Site Request Forgery protection prevents malicious websites from performing actions on behalf of authenticated users.
Configuration:
'csrf-enabled' => true,
Checks:
-
VerifyCsrfToken
middleware is active - CSRF tokens are properly generated
- Web routes are protected
Purpose: Validates composer packages for known security vulnerabilities
Severity: β Error
Environments: all
Scans your composer.lock
file for packages with known security vulnerabilities and recommends updates.
Configuration:
'composer-package-security' => true,
Purpose: Ensures cookies are secure in production
Severity: π¨ Critical
Environments: production, staging
Validates that cookies have proper security flags set in production environments.
Configuration:
'secure-cookies-in-production' => true,
Checks:
-
SESSION_SECURE_COOKIE=true
in production -
SESSION_SAME_SITE
properly configured SESSION_HTTP_ONLY=true
Purpose: Verifies HTTPS is enforced in production
Severity: π¨ Critical
Environments: production
Ensures all traffic is encrypted with HTTPS in production environments.
Configuration:
'https-enforced-in-production' => true,
Purpose: Ensures .env
file has proper permissions
Severity: π¨ Critical
Environments: production, staging
Checks that .env
files are not world-readable and contain sensitive configuration data.
Configuration:
'env-file-permissions' => true,
Recommended permissions:
# Correct permissions (600 = owner read/write only)
chmod 600 .env
# Incorrect permissions (644 = world readable)
chmod 644 .env # β Avoid this
Purpose: Validates that sensitive files are not web-accessible
Severity: β Error
Environments: all
Ensures sensitive files cannot be accessed directly via web requests.
Configuration:
'sensitive-files-hidden' => true,
'sensitive_files' => [
'.env',
'.env.example',
'composer.json',
'composer.lock',
'package.json',
'artisan',
'phpunit.xml',
'README.md',
],
Purpose: Verifies storage directories are writable
Severity: β Error
Environments: all
Ensures Laravel can write to required directories for logs, cache, and sessions.
Configuration:
'storage-writable' => true,
Purpose: Verifies that database connections use SSL/TLS encryption
Severity: π¨ Critical
Environments: production, staging
Ensures database connections are encrypted in transit to protect sensitive data.
Configuration:
'database-connection-encrypted' => true,
Example secure database configuration:
// config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
// ... other config
'options' => [
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => true,
],
],
Purpose: Checks for default or weak database credentials
Severity: π¨ Critical
Environments: all
Validates that database passwords are not empty, default, or commonly used weak passwords.
Configuration:
'database-credentials-not-default' => true,
Common issues detected:
- Empty passwords
- Username/password combinations like
root
/root
- Default MySQL passwords
- Common weak passwords
Purpose: Validates database backup security configuration
Severity: β Error
Environments: production
Checks backup encryption, access controls, and retention policies.
Configuration:
'database-backup-security' => true,
Purpose: Ensures database query logging is properly configured
Severity:
Environments: all
Validates query logging settings for security monitoring and debugging.
Configuration:
'database-query-logging' => true,
Purpose: Verifies that password policy configuration meets security standards
Severity: π¨ Critical
Environments: all
Checks password requirements including length, complexity, and validation rules.
Configuration:
'password-policy-compliance' => true,
Validates:
- Minimum password length (recommended: 8+ characters)
- Password complexity requirements
- Password history prevention
- Account lockout policies
Purpose: Validates two-factor authentication configuration
Severity:
Environments: production
Ensures 2FA is properly configured for enhanced security.
Configuration:
'two-factor-auth-enabled' => true,
Purpose: Validates session security configuration
Severity: β Error
Environments: production, staging
Checks session lifetime, security flags, and storage configuration.
Configuration:
'session-security-settings' => true,
Validates:
- Session lifetime configuration
- Session storage security
- Session regeneration policies
- Session cookie security flags
Purpose: Validates encryption key management and rotation policies
Severity:
Environments: production
Checks for proper key rotation practices and provides recommendations.
Configuration:
'encryption-key-rotation' => true,
Purpose: Ensures sensitive data is properly encrypted
Severity: π¨ Critical
Environments: all
Validates encryption of sensitive database fields and stored data.
Configuration:
'sensitive-data-encryption' => true,
Issues that pose immediate security risks and must be fixed before production deployment. These are deal-breakers that can expose your application to serious attacks.
Examples:
- Debug mode enabled in production
- Missing application key
- Hardcoded secrets in code
Important security issues that should be addressed but may not prevent deployment in all cases. These reduce your security posture significantly.
Examples:
- Missing required environment variables
- Weak database credentials
- Insecure file permissions
Recommendations for improved security posture. These are best practices that enhance security but aren't critical vulnerabilities.
Examples:
- Missing 2FA configuration
- Suboptimal session settings
- Encryption key rotation reminders
Informational messages about security configuration that provide helpful context or recommendations.
Examples:
- Configuration optimization suggestions
- Security best practice reminders
- Performance impact notifications
Configure different rules for different environments to balance security with development productivity:
'environments' => [
'production' => [
// Strict security rules for production
'app-debug-false-in-production',
'app-key-is-set',
'env-file-permissions',
'database-connection-encrypted',
'database-credentials-not-default',
'password-policy-compliance',
'encryption-key-rotation',
'secure-cookies-in-production',
'https-enforced-in-production',
],
'staging' => [
// Production-like but slightly relaxed
'app-debug-false-in-production',
'csrf-enabled',
'database-connection-encrypted',
'app-key-is-set',
],
'local' => [
// Development-friendly rules
'app-key-is-set',
'env-has-all-required-keys',
'csrf-enabled',
'storage-writable',
],
],
Test individual rules in isolation for debugging and validation:
# Test a specific rule
php artisan safeguard:test-rule app-debug-false-in-production
# Test multiple rules
php artisan safeguard:test-rule app-key-is-set csrf-enabled
# Test all rules for an environment
php artisan safeguard:test-environment production
# Get detailed output for a rule
php artisan safeguard:test-rule no-secrets-in-code --verbose
When creating custom rules, consider organizing them by these categories:
- Password policies
- Session management
- User permissions
- Role-based access control
- Encryption settings
- Database security
- File upload validation
- PII handling
- API security
- Third-party integrations
- OAuth configurations
- Webhook security
- Server configuration
- Network security
- Monitoring setup
- Logging configuration
- GDPR requirements
- PCI-DSS compliance
- HIPAA compliance
- Industry-specific regulations
Use this checklist to ensure comprehensive security coverage:
-
app-key-is-set
-
app-debug-false-in-production
-
csrf-enabled
-
no-secrets-in-code
-
secure-cookies-in-production
-
https-enforced-in-production
-
env-file-permissions
-
database-connection-encrypted
-
password-policy-compliance
-
session-security-settings
-
two-factor-auth-enabled
(if applicable)
-
storage-writable
-
sensitive-files-hidden
-
database-credentials-not-default
-
database-backup-security
- π Start with Critical Rules: Enable all critical (π¨) rules first
- π Environment-Specific: Use different rule sets per environment
- π Regular Reviews: Periodically review and update your rule configuration
- ποΈ Custom Rules: Create rules specific to your application's security requirements
- π Documentation: Document any custom or disabled rules for your team
- π Continuous Integration: Include security checks in your CI/CD pipeline
- π₯ Team Training: Ensure your team understands the purpose of each rule
- ποΈ Custom Rules Guide - Create your own security rules
- π Environment Rules - Configure per-environment rule sets
- βοΈ Configuration Reference - Complete configuration options
- π CI/CD Integration - Automate security checks
- π‘ Examples Collection - Real-world usage examples
Next Step: ποΈ Learn to create custom security rules
π Home | β‘ Quick Start | βοΈ Configuration | ποΈ Custom Rules
Laravel Safeguard - Configurable Security Checks for Laravel Applications
π Home | π¦ Installation | β‘ Quick Start | π‘ Examples | π Full Docs
Made with β€οΈ for the Laravel community
Β© 2025 - Laravel Safeguard by Grazulex