captainhook/secrets
Detect secrets like passwords and API keys in code to prevent accidental commits. Use built-in regex suppliers (AWS, Google, GitHub, etc.) or provide your own patterns, plus a whitelist for allowed matches. Includes a simple Detector API.
Start by installing the package via Composer:
composer require --dev captainhook/secrets
The primary entry point is the Detector class. Its detectIn() method scans a string for secrets using pre-defined regex suppliers (e.g., Aws, Google, GitHub, Gitlab) or custom patterns. After installation, the first practical use case is integrating it into a local pre-commit hook (e.g., with CaptainHook) to block commits containing credentials — but even without the full hook runner, you can use the detector standalone in CI scripts or artisan commands.
Detector in your commit-msg or prepare-commit-msg hook to scan staged files for secrets before allowing the commit. Example: scan diff hunks or full file contents.Detector over changed files or the whole codebase, failing the build if any secrets are found.Regex\Supplier to define domain-specific patterns (e.g., internal API keys like MYAPP_SECRET_KEY\s*=), then pass them to useSuppliers().allow() with exact match strings or regexes (e.g., test fixtures like #root#) to avoid false positives.php artisan secrets:scan), outputting matches or generating reports.mb_detect_encoding() for safety, but ensure ext-mbstring is enabled (as required in composer.json)..env.testing, database.php) often contain placeholder secrets. Use allow() for known safe patterns or skip scanning non-production paths.detectIn(). Use file-by-file scanning in loops or integrate with tools that isolate changes (e.g., git diff output).Aws, Google, Gitlab) and includes comprehensive regex covering common formats (e.g., AKIA..., AIzaSy..., glpat-...).var_dump($result->matches()) — sometimes the detected string may be empty if the pattern group is misconfigured (e.g., non-capturing groups without match() extraction). Also verify the input encoding — UTF-8-only patterns may miss non-ASCII chars.How can I help you explore Laravel packages today?