|
| 1 | +<?php |
| 2 | + |
| 3 | +use STS\FilamentPHPInfo\Redaction; |
| 4 | +use STS\Phpinfo\Info; |
| 5 | + |
| 6 | +it('replaces the value of a secret environment variable', function () { |
| 7 | + expect(Redaction::value('Environment', 'APP_KEY', 'base64:abc123'))->toBe('[redacted]'); |
| 8 | +}); |
| 9 | + |
| 10 | +it('redacts every row form phpinfo prints for one variable', function ($module, $name) { |
| 11 | + expect(Redaction::isSensitive($module, $name))->toBeTrue(); |
| 12 | +})->with([ |
| 13 | + ['Environment', 'APP_KEY'], |
| 14 | + ['PHP Variables', "\$_ENV['APP_KEY']"], |
| 15 | + ['PHP Variables', '$_SERVER["APP_KEY"]'], |
| 16 | +]); |
| 17 | + |
| 18 | +it('redacts on every pattern', function ($name) { |
| 19 | + expect(Redaction::isSensitive('Environment', $name))->toBeTrue(); |
| 20 | +})->with([ |
| 21 | + 'AWS_SECRET_ACCESS_KEY', |
| 22 | + 'DB_PASSWORD', |
| 23 | + 'NIGHTWATCH_TOKEN', |
| 24 | + 'GOOGLE_APPLICATION_CREDENTIALS', |
| 25 | + 'OAUTH_PRIVATE_KEY', |
| 26 | + 'JWT_SIGNING_SALT', |
| 27 | + 'JWT_RECIPIENT_SIGNATURE', |
| 28 | + 'SENTRY_DSN', |
| 29 | + 'NOVA_LICENSE_KEY', |
| 30 | + 'SLACK_WEBHOOK', |
| 31 | +]); |
| 32 | + |
| 33 | +it('keeps ordinary environment variables', function ($name) { |
| 34 | + expect(Redaction::value('Environment', $name, 'kept'))->toBe('kept'); |
| 35 | +})->with(['APP_ENV', 'DB_HOST', 'MAIL_FROM_ADDRESS', 'AWS_DEFAULT_REGION', 'PATH']); |
| 36 | + |
| 37 | +it('keeps php settings whose name matches a pattern', function ($module, $name) { |
| 38 | + expect(Redaction::value($module, $name, 'kept'))->toBe('kept'); |
| 39 | +})->with([ |
| 40 | + ['Zend OPcache', 'Max keys'], |
| 41 | + ['Zend OPcache', 'Cached keys'], |
| 42 | + ['Zend OPcache', 'Hash keys restarts'], |
| 43 | + ['tokenizer', 'Tokenizer Support'], |
| 44 | + ['Core', 'highlight.keyword'], |
| 45 | +]); |
| 46 | + |
| 47 | +it('leaves an unset variable alone, so the page still shows it is empty', function ($value) { |
| 48 | + expect(Redaction::value('Environment', 'APP_KEY', $value))->toBe($value); |
| 49 | +})->with([null, '']); |
| 50 | + |
| 51 | +it('can be turned off', function () { |
| 52 | + config()->set('filament-phpinfo.redact-environment', false); |
| 53 | + |
| 54 | + expect(Redaction::value('Environment', 'APP_KEY', 'base64:abc123'))->toBe('base64:abc123'); |
| 55 | +}); |
| 56 | + |
| 57 | +it('respects custom patterns from config', function () { |
| 58 | + config()->set('filament-phpinfo.redact-patterns', ['tenant']); |
| 59 | + |
| 60 | + expect(Redaction::value('Environment', 'TENANT_ID', 'acme'))->toBe('[redacted]'); |
| 61 | + expect(Redaction::value('Environment', 'APP_KEY', 'base64:abc123'))->toBe('base64:abc123'); |
| 62 | +}); |
| 63 | + |
| 64 | +it('respects a custom placeholder from config', function () { |
| 65 | + config()->set('filament-phpinfo.redact-placeholder', '***'); |
| 66 | + |
| 67 | + expect(Redaction::value('Environment', 'APP_KEY', 'base64:abc123'))->toBe('***'); |
| 68 | +}); |
| 69 | + |
| 70 | +it('redacts nothing outside the environment modules of a real capture', function () { |
| 71 | + $redacted = []; |
| 72 | + |
| 73 | + foreach (Info::capture()->modules() as $module) { |
| 74 | + if (in_array($module->name(), Redaction::ENVIRONMENT_MODULES, true)) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + foreach ($module->configs() as $config) { |
| 79 | + if (Redaction::isSensitive($module->name(), $config->name())) { |
| 80 | + $redacted[] = $module->name() . ' / ' . $config->name(); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + expect($redacted)->toBeEmpty(); |
| 86 | +}); |
| 87 | + |
| 88 | +it('redacts a secret in a real capture', function () { |
| 89 | + putenv('FILAMENT_PHPINFO_TEST_SECRET=must-not-render'); |
| 90 | + |
| 91 | + $names = []; |
| 92 | + |
| 93 | + foreach (Info::capture()->modules() as $module) { |
| 94 | + foreach ($module->configs() as $config) { |
| 95 | + if (str_contains($config->name(), 'FILAMENT_PHPINFO_TEST_SECRET')) { |
| 96 | + $names[] = $config->name(); |
| 97 | + expect(Redaction::value($module->name(), $config->name(), $config->localValue())) |
| 98 | + ->toBe('[redacted]'); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + expect($names)->not->toBeEmpty(); |
| 104 | + |
| 105 | + putenv('FILAMENT_PHPINFO_TEST_SECRET'); |
| 106 | +}); |
0 commit comments