grazulex / laravel-safeguard
Configurable security checks for Laravel applications. Run safety audits on environment variables, configuration files, and hidden routes to prevent common mistakes before going live.
Requires
- php: ^8.3
- illuminate/contracts: ^12.0
- illuminate/support: ^12.19
- nesbot/carbon: ^3.10
- symfony/yaml: ^7.3
Requires (Dev)
- doctrine/dbal: ^4.2
- larastan/larastan: ^3.4
- laravel/pint: ^1.22
- orchestra/testbench: ^10.0
- pestphp/pest: ^3.8
- pestphp/pest-plugin-laravel: ^3.2
- rector/rector: ^2.0
This package is auto-updated.
Last update: 2025-07-17 21:48:32 UTC
README

Configurable security checks for Laravel applications โ Run safety audits on environment variables, configuration files, and hidden routes to prevent common mistakes before going live.
๐ง Problem Solved
In real-world Laravel applications, many production issues come from misconfigured environments and security oversights:
- โ Missing critical variables (APP_KEY, DB_PASSWORD, etc.)
- ๐ Hardcoded secrets in code instead of environment variables
- ๐จ Inconsistencies between
.env.example
and.env
- ๐๏ธ Unused or legacy keys inherited from other projects
- โ ๏ธ Security misconfigurations (ex:
APP_DEBUG=true
in production) - ๐ Insecure defaults that should be changed before going live
Laravel Safeguard acts like Pint, PHPStan, or Rector but for security and configuration auditing โ with configurable rules you can enable/disable based on your needs.
โจ Features
๐ง Configurable Rules System โ Enable/disable security checks via config/safeguard.php
๐ Environment Security โ Verify .env
files, detect secrets in code, validate required keys
โ๏ธ Application Configuration โ Check Laravel-specific security settings
๐ก๏ธ Production Safety โ Prevent common production mistakes before deployment
๐ Multiple Output Formats โ CLI, JSON, or CI-friendly reporting
๐ CI/CD Integration โ Perfect for GitHub Actions, GitLab CI, and other pipelines
๐ฆ Installation
Install the package via Composer:
composer require --dev grazulex/laravel-safeguard
Publish the configuration file:
php artisan vendor:publish --tag=safeguard-config
๐ง Configuration
The package includes a comprehensive configuration file at config/safeguard.php
:
<?php return [ 'rules' => [ // ๐ Environment & Secrets 'env_debug_false_in_production' => true, 'env_has_all_required_keys' => true, 'no_secrets_in_code' => true, 'no_unused_env_keys' => false, 'no_example_mismatch' => true, // โ๏ธ Application Configuration 'app_key_is_set' => true, 'no_test_routes_in_production' => true, 'storage_writable' => true, // ๐ก๏ธ Laravel Security 'csrf_enabled' => true, 'secure_cookies_in_production' => true, 'no_forgotten_admin_routes' => true, 'session_secure_in_production' => true, 'https_enforced_in_production' => false, ], // ๐ฏ Environment-specific rules 'environments' => [ 'production' => [ 'env_debug_false_in_production', 'secure_cookies_in_production', 'https_enforced_in_production', ], 'staging' => [ 'env_debug_false_in_production', 'csrf_enabled', ], ], // ๐ Paths to scan for secrets and unused variables 'scan_paths' => [ 'app/', 'config/', 'routes/', 'resources/views/', ], // ๐ Secret patterns to detect in code 'secret_patterns' => [ '*_KEY', '*_SECRET', '*_TOKEN', '*_PASSWORD', 'API_*', ], ];
๐ฅ๏ธ Usage
Basic Security Check
Run all enabled security rules:
php artisan safeguard:check
Environment-Specific Checks
Run checks for a specific environment:
php artisan safeguard:check --env=production
Single Rule Testing
Test a specific rule in isolation:
php artisan safeguard:test-rule env_debug_false_in_production
List Available Rules
See all available rules and their status:
php artisan safeguard:list
๐ Example Output
๐ Laravel Safeguard Security Check
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Environment: production
โ
APP_KEY is set
โ
.env has all required keys from .env.example
โ APP_DEBUG is true in production
โ Secret found in config/services.php (TWILIO_SECRET)
โ
CSRF protection enabled
โ
Storage directories are writable
โ ๏ธ Rule "no_unused_env_keys" is disabled
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ฏ 2 issues found, 4 checks passed
JSON Output
For programmatic use or CI integration:
php artisan safeguard:check --format=json
{ "status": "failed", "environment": "production", "summary": { "total": 6, "passed": 4, "failed": 2, "disabled": 1 }, "results": [ { "rule": "env_debug_false_in_production", "status": "failed", "message": "APP_DEBUG is true in production", "severity": "error" } ] }
๐งช CI/CD Integration
GitHub Actions
name: Security Audit on: [push, pull_request] jobs: security: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: 8.3 - name: Install dependencies run: composer install - name: Run Laravel Safeguard run: php artisan safeguard:check --ci --fail-on-error
GitLab CI
security_audit: stage: test script: - composer install - php artisan safeguard:check --ci --fail-on-error only: - merge_requests - main
๐ Available Rules
๐ Environment & Secrets
env_debug_false_in_production
โ Ensures APP_DEBUG is false in productionenv_has_all_required_keys
โ Validates all .env.example keys exist in .envno_secrets_in_code
โ Detects hardcoded secrets in your codebaseno_unused_env_keys
โ Identifies unused environment variablesno_example_mismatch
โ Ensures .env and .env.example are in sync
โ๏ธ Application Configuration
app_key_is_set
โ Verifies Laravel APP_KEY is generatedno_test_routes_in_production
โ Prevents test routes in productionstorage_writable
โ Checks storage directories are writable
๐ก๏ธ Laravel Security
csrf_enabled
โ Ensures CSRF protection is activesecure_cookies_in_production
โ Validates secure cookie settingssession_secure_in_production
โ Checks session security configurationhttps_enforced_in_production
โ Verifies HTTPS enforcementno_forgotten_admin_routes
โ Detects potentially dangerous admin routes
๐จ Custom Rules
Create your own security rules by extending the base rule class:
php artisan safeguard:make-rule CustomSecurityRule
This generates a new rule class in app/SafeguardRules/
:
<?php namespace App\SafeguardRules; use Grazulex\LaravelSafeguard\Contracts\SafeguardRule; use Grazulex\LaravelSafeguard\Results\SafeguardResult; class CustomSecurityRule implements SafeguardRule { public function id(): string { return 'custom_security_rule'; } public function description(): string { return 'Custom security validation'; } public function check(): SafeguardResult { // Your custom logic here return SafeguardResult::passed('Custom check passed'); } }
๐งช Testing
Run the test suite:
composer test
Run with coverage:
composer test -- --coverage
๐ Documentation
For comprehensive documentation, see the docs/
directory:
- Installation Guide - Step-by-step installation and setup
- Quick Start - Get up and running in minutes
- Configuration Guide - Comprehensive configuration options
- Security Rules Reference - Complete list of available rules
- Custom Rules Guide - Create your own security rules
- CI/CD Integration - GitHub Actions, GitLab CI, and more
- Commands Reference - All available artisan commands
- Output Formats - CLI, JSON, and CI-friendly outputs
- FAQ - Frequently asked questions
- Troubleshooting - Common issues and solutions
๐ก Examples
The examples/
directory contains practical examples and code samples:
- Basic Usage - Simple examples to get started
- Custom Rules - Real-world custom security rules
- Configuration - Various configuration setups
- CI/CD - Ready-to-use CI/CD pipeline configurations
- Scripts - Utility scripts for automation
๐ Changelog
Please see CHANGELOG for more information on what has changed recently.
๐ค Contributing
Please see CONTRIBUTING for details.
๐ Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
๐ License
The MIT License (MIT). Please see License File for more information.
๐ก Credits
Laravel Safeguard โ Because security should be as simple as running php artisan safeguard:check
โ