PHP code example of yorcreative / laravel-scrubber

1. Go to this page and download the library: Download yorcreative/laravel-scrubber library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yorcreative / laravel-scrubber example snippets


return [
    /**
     * Specify the string to use to redact the data
     */
    'redaction' => '**redacted**',

    'secret_manager' => [
        'key' => env('APP_KEY'),
        'cipher' => 'AES-256-CBC',
        'enabled' => false,
        'providers' => [
            // See "Secret Manager Providers" section for full configuration options
            'gitlab' => ['enabled' => false, /* ... */],
            'aws' => ['enabled' => false, /* ... */],
            'vault' => ['enabled' => false, /* ... */],
            'azure' => ['enabled' => false, /* ... */],
            'google' => ['enabled' => false, /* ... */],
        ],
    ],

    /**
     * Specify the regexes to load
     * You can use a wildcard (*) to load all regexes in all `custom_regex_namespaces` and the default core regexes.
     * Otherwise, specify the regexes you want to load either by qualified class name or by unqualified (base) class name,
     * which will then search the `custom_regex_namespaces` and the default core regexes for a match.
     */
    'regex_loader' => ['*'],
    
    /**
     * Specify regex patterns to exclude from loading when using the regex loader
     * This allows fine-grained control over which regex patterns are loaded, especially useful when using wildcard (*) in regex_loader
     * 
     * You can exclude patterns using any of these formats:
     * - Fully qualified class name (e.g., 'YorCreative\Scrubber\RegexCollection\GoogleApi')
     * - Base class name (e.g., 'GoogleApi', 'EmailAddress')
     * - Pattern constant from RegexCollection (e.g., RegexCollection::$GOOGLE_API)
     * - Custom namespace class (e.g., 'App\Scrubber\RegexCollection\HerokuApiKey')
     * 
     * Example:
     * [
     *     'GoogleApi',
     *     'YorCreative\Scrubber\RegexCollection\EmailAddress',
     *     RegexCollection::$HEROKU_API_KEY,
     *     'App\Scrubber\RegexCollection\HerokuApiKey'
     * ]
     */
    'exclude_regex' => [],


    /**
     * Specify namespaces from which regexes will be loaded when using the wildcard (*)
     * for the regex_loader or where you use unqualified class names.
     */
    'custom_regex_namespaces' => [
       'App\\Scrubber\\RegexCollection',
    ],

    /**
     * Specify config keys for which the values will be scrubbed
     * You should use the dot notation to specify the keys
     * You can use wildcards (*) to match multiple keys
     *
     *  - 'database.connections.*.password'
     *  - 'app.secrets.*'
     *  - 'app.some.nested.key'
     */
    'config_loader' => [
        '*token',
        '*key',
        '*secret',
        '*password',
    ],

    /**
     * Minimum character length for config values to be treated as scrubbable.
     * Values shorter than this will be ignored to prevent overly aggressive
     * scrubbing (e.g., Livewire's release_token defaults to 'a').
     */
    'config_loader_min_length' => 4,

    /**
     * Config key patterns to exclude from scrubbing.
     * Supports wildcards (*) via Str::is().
     */
    'config_loader_exclusions' => [],

    /**
     * Specify the channels to tap into
     * You can use wildcards (*) to match multiple channels
     */ 
    'tap_channels' => false,
];

Log::info('some message', [
    'context' => 'accidental',
    'leak_of' => [
        'jwt' => '<insert jwt token here>'
    ]
])

// testing.INFO: some message {"context":"accidental","leak_of":{"jwt": '**redacted**'}} 

Log::info('<insert jwt token here>')

// testing.INFO: **redacted**  

Scrubber::processMessage([
    'context' => 'accidental',
    'leak_of' => [
        'jwt' => '<insert jwt token here>'
    ]
]);
// [
//     "context" => "accidental"
//     "leak_of" => [
//         "jwt" => "**redacted**"
//     ]
// ];

Scrubber::processMessage('<insert jwt token here>');
// **redacted**

// Get scrubbing statistics for the current request
$stats = Scrubber::getStats();
// ['total_scrubs' => 5, 'patterns_matched' => ['JsonWebToken' => 2, 'EmailAddress' => 3]]

// Test a string without modifying stats - useful for debugging
$result = Scrubber::test('Contact: [email protected], SSN: 123-45-6789');
// [
//     'matched' => true,
//     'patterns' => ['EmailAddress' => 1, 'SocialSecurityNumber' => 1],
//     'scrubbed' => 'Contact: **redacted**, SSN: ***-**-****'
// ]

// Reset statistics between requests
Scrubber::resetStats();

'events' => [
    'enabled' => true,
],

use YorCreative\Scrubber\Events\SensitiveDataDetected;

Event::listen(SensitiveDataDetected::class, function (SensitiveDataDetected $event) {
    // $event->patternName  — e.g. 'JsonWebToken'
    // $event->hitCount     — e.g. 2
    // $event->context      — 'log' or 'manual'
});

'tap_channels' => [
    'single',
    'papertrail'
]

'tap_channels' => false

 'regex_loader' => [
        RegexCollection::$GOOGLE_API,
        RegexCollection::$AUTHORIZATION_BEARER,
        RegexCollection::$CREDIT_CARD_AMERICAN_EXPRESS,
        RegexCollection::$CREDIT_CARD_DISCOVER,
        RegexCollection::$CREDIT_CARD_VISA,
        RegexCollection::$JSON_WEB_TOKEN
    ],

Scrubber::processMessage('SSN: 123-45-6789, Phone: (555) 123-4567');
// "SSN: ***-**-****, Phone: (***) ***-****"

Scrubber::processMessage('Server IP: 192.168.1.1');
// "Server IP: ***.***.***.***"



namespace App\Scrubber\RegexCollection;

use YorCreative\Scrubber\Interfaces\RegexCollectionInterface;

class TestRegex implements RegexCollectionInterface
{
    public function getPattern(): string
    {
        /**
         * @note return a regex pattern to detect a specific piece of sensitive data.
         */
        return '(?<=basic) [a-zA-Z0-9=:\\+\/-]{5,100}';
    }

    public function getTestableString(): string
    {
        /**
         * @note return a string that can be used to verify the regex pattern provided.
         */
        return 'basic f9Iu+YwMiJEsQu/vBHlbUNZRkN/ihdB1sNTU';
    }
    
    public function getReplacementValue(): string
    {
        
        /**
         * @note return a string that replaces the regex pattern provided.
         */
        return config('scrubber.redaction');
    }

    public function isSecret(): bool
    {
        return false;
    }
}

 'regex_loader' => [
        RegexCollection::$GOOGLE_API,
        RegexCollection::$AUTHORIZATION_BEARER,
        RegexCollection::$CREDIT_CARD_AMERICAN_EXPRESS,
        RegexCollection::$CREDIT_CARD_DISCOVER,
        RegexCollection::$CREDIT_CARD_VISA,
        RegexCollection::$JSON_WEB_TOKEN,
        'TestRegex'
    ],

'exclude_regex' => [
    // Exclude by base class name
    'GoogleApi',
    
    // Exclude by fully qualified class name
    'YorCreative\Scrubber\RegexCollection\EmailAddress',
    
    // Exclude using RegexCollection constant
    RegexCollection::$HEROKU_API_KEY,
    
    // Exclude from custom namespace
    'App\Scrubber\RegexCollection\HerokuApiKey'
],

// Default: ignore values shorter than 4 characters
'config_loader_min_length' => 4,

// Set to 0 to disable the minimum length filter
'config_loader_min_length' => 0,

'config_loader_exclusions' => [
    // Exclude a specific key
    'livewire.release_token',

    // Exclude all keys under a namespace
    'livewire.*',
],

'gitlab' => [
    'enabled' => true,
    'project_id' => env('GITLAB_PROJECT_ID'),
    'token' => env('GITLAB_TOKEN'),
    'host' => 'https://gitlab.com', // Or your self-hosted GitLab URL
    'keys' => ['*'], // Or specific variable names: ['DB_PASSWORD', 'API_KEY']
],

'aws' => [
    'enabled' => true,
    'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
    'version' => 'latest',
    'credentials' => [
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
    ],
    'keys' => ['*'], // Or specific secret names/ARNs
],

'vault' => [
    'enabled' => true,
    'host' => env('VAULT_ADDR', 'http://127.0.0.1:8200'),
    'token' => env('VAULT_TOKEN'),
    'namespace' => env('VAULT_NAMESPACE'), // Enterprise feature (optional)
    'engine' => env('VAULT_ENGINE', 'secret'), // KV engine mount path
    'path' => env('VAULT_PATH', ''), // Base path within the engine
    'version' => env('VAULT_KV_VERSION', 2), // KV engine version (1 or 2)
    'keys' => ['*'], // Or specific secret paths
],

'azure' => [
    'enabled' => true,
    'vault_url' => env('AZURE_VAULT_URL'), // https://my-vault.vault.azure.net
    // Authentication options (in order of precedence):
    // Option 1: Direct access token
    'access_token' => env('AZURE_VAULT_ACCESS_TOKEN'),
    // Option 2: Client credentials (service principal)
    'tenant_id' => env('AZURE_TENANT_ID'),
    'client_id' => env('AZURE_CLIENT_ID'),
    'client_secret' => env('AZURE_CLIENT_SECRET'),
    'keys' => ['*'], // Or specific secret names
],

'google' => [
    'enabled' => true,
    'project_id' => env('GOOGLE_CLOUD_PROJECT'),
    'access_token' => env('GOOGLE_SECRET_MANAGER_TOKEN'), // Optional
    'keys' => ['*'], // Or specific secret names
],
bash
php artisan vendor:publish --provider="YorCreative\Scrubber\ScrubberServiceProvider"
bash
composer 
bash
php artisan scrubber:validate