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.

dev-main 2025-07-17 21:48 UTC

This package is auto-updated.

Last update: 2025-07-17 21:48:32 UTC


README

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.

Latest Version Total Downloads License PHP Version Laravel Version Tests Code Quality Code Style

๐Ÿง  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 production
  • env_has_all_required_keys โ€” Validates all .env.example keys exist in .env
  • no_secrets_in_code โ€” Detects hardcoded secrets in your codebase
  • no_unused_env_keys โ€” Identifies unused environment variables
  • no_example_mismatch โ€” Ensures .env and .env.example are in sync

โš™๏ธ Application Configuration

  • app_key_is_set โ€” Verifies Laravel APP_KEY is generated
  • no_test_routes_in_production โ€” Prevents test routes in production
  • storage_writable โ€” Checks storage directories are writable

๐Ÿ›ก๏ธ Laravel Security

  • csrf_enabled โ€” Ensures CSRF protection is active
  • secure_cookies_in_production โ€” Validates secure cookie settings
  • session_secure_in_production โ€” Checks session security configuration
  • https_enforced_in_production โ€” Verifies HTTPS enforcement
  • no_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:

๐Ÿ’ก Examples

The examples/ directory contains practical examples and code samples:

๐Ÿ“ˆ 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 โœ