|
7 | 7 | use Illuminate\Support\Facades\Config; |
8 | 8 | use Kirschbaum\Monitor\Facades\Monitor; |
9 | 9 |
|
10 | | -final class RedactorConfig |
11 | | -{ |
12 | | - public function __construct( |
13 | | - /** @var array<string> */ |
14 | | - public array $safeKeys = [], |
15 | | - /** @var array<string> */ |
16 | | - public array $blockedKeys = [], |
17 | | - /** @var array<string> */ |
18 | | - public array $patterns = [], |
19 | | - public string $replacement = '[REDACTED]', |
20 | | - public ?int $maxValueLength = null, |
21 | | - public bool $redactLargeObjects = true, |
22 | | - public int $maxObjectSize = 50, |
23 | | - public bool $enableShannonEntropy = true, |
24 | | - public float $entropyThreshold = 4.5, |
25 | | - public int $minLength = 20, |
26 | | - /** @var array<string> */ |
27 | | - public array $entropyExclusionPatterns = [], |
28 | | - public bool $markRedacted = true, |
29 | | - public bool $trackRedactedKeys = false, |
30 | | - public string $nonRedactableObjectBehavior = 'preserve' |
31 | | - ) {} |
32 | | - |
33 | | - public static function fromConfig(): self |
34 | | - { |
35 | | - /** @var array<string> $safeKeys */ |
36 | | - $safeKeys = Config::array('monitor.log_redactor.safe_keys', []); |
37 | | - /** @var array<string> $blockedKeys */ |
38 | | - $blockedKeys = Config::array('monitor.log_redactor.blocked_keys', []); |
39 | | - /** @var array<string> $patterns */ |
40 | | - $patterns = Config::array('monitor.log_redactor.patterns', []); |
41 | | - |
42 | | - // Ensure we only have string values and apply transformations |
43 | | - $safeKeysLower = array_map(function (string $key): string { |
44 | | - return strtolower($key); |
45 | | - }, $safeKeys); |
46 | | - |
47 | | - $blockedKeysLower = array_map(function (string $key): string { |
48 | | - return strtolower($key); |
49 | | - }, $blockedKeys); |
50 | | - |
51 | | - $validPatterns = array_filter($patterns, fn (string $pattern): bool => @preg_match($pattern, '') !== false); |
52 | | - |
53 | | - // Handle nullable max value length |
54 | | - $maxValueLength = Config::get('monitor.log_redactor.max_value_length'); |
55 | | - $maxValueLengthTyped = is_int($maxValueLength) ? $maxValueLength : null; |
56 | | - |
57 | | - /** @var array<string> $entropyExclusionPatterns */ |
58 | | - $entropyExclusionPatterns = Config::array('monitor.log_redactor.shannon_entropy.exclusion_patterns', [ |
59 | | - '/^https?:\/\//', // URLs |
60 | | - '/^[\/\\\\].+[\/\\\\]/', // File paths |
61 | | - '/^\d{4}-\d{2}-\d{2}/', // Date formats |
62 | | - '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i', // UUIDs |
63 | | - '/^[0-9a-f]+$/i', // Hex strings (checked with length < 32) |
64 | | - '/^\s*$/', // Whitespace strings |
65 | | - '/^Mozilla\/\d\.\d|^[A-Za-z]+\/\d+\.\d+|AppleWebKit|Chrome|Safari|Firefox|Opera|Edge/', // User agents |
66 | | - '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', // IPv4 addresses |
67 | | - '/^[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}$/i', // MAC addresses |
68 | | - ]); |
69 | | - |
70 | | - $validExclusionPatterns = array_filter($entropyExclusionPatterns, fn (string $pattern): bool => @preg_match($pattern, '') !== false); |
71 | | - |
72 | | - return new self( |
73 | | - safeKeys: $safeKeysLower, |
74 | | - blockedKeys: $blockedKeysLower, |
75 | | - patterns: array_values($validPatterns), // Re-index array |
76 | | - replacement: Config::string('monitor.log_redactor.replacement', '[REDACTED]'), |
77 | | - maxValueLength: $maxValueLengthTyped, |
78 | | - redactLargeObjects: Config::boolean('monitor.log_redactor.redact_large_objects', true), |
79 | | - maxObjectSize: Config::integer('monitor.log_redactor.max_object_size', 50), |
80 | | - enableShannonEntropy: Config::boolean('monitor.log_redactor.shannon_entropy.enabled', true), |
81 | | - entropyThreshold: Config::float('monitor.log_redactor.shannon_entropy.threshold', 4.5), |
82 | | - minLength: Config::integer('monitor.log_redactor.shannon_entropy.min_length', 20), |
83 | | - entropyExclusionPatterns: array_values($validExclusionPatterns), // Re-index array |
84 | | - markRedacted: Config::boolean('monitor.log_redactor.mark_redacted', true), |
85 | | - trackRedactedKeys: Config::boolean('monitor.log_redactor.track_redacted_keys', false), |
86 | | - nonRedactableObjectBehavior: Config::string('monitor.log_redactor.non_redactable_object_behavior', 'preserve') |
87 | | - ); |
88 | | - } |
89 | | -} |
90 | | - |
91 | 10 | class LogRedactor |
92 | 11 | { |
93 | 12 | /** @var array<string> */ |
|
0 commit comments