Skip to content

Commit 01ce549

Browse files
committed
Redactor refactor
1 parent 59c6238 commit 01ce549

File tree

2 files changed

+78
-81
lines changed

2 files changed

+78
-81
lines changed

src/Support/LogRedactor.php

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,87 +7,6 @@
77
use Illuminate\Support\Facades\Config;
88
use Kirschbaum\Monitor\Facades\Monitor;
99

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-
9110
class LogRedactor
9211
{
9312
/** @var array<string> */

src/Support/RedactorConfig.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kirschbaum\Monitor\Support;
6+
7+
use Illuminate\Support\Facades\Config;
8+
9+
final class RedactorConfig
10+
{
11+
public function __construct(
12+
/** @var array<string> */
13+
public array $safeKeys = [],
14+
/** @var array<string> */
15+
public array $blockedKeys = [],
16+
/** @var array<string> */
17+
public array $patterns = [],
18+
public string $replacement = '[REDACTED]',
19+
public ?int $maxValueLength = null,
20+
public bool $redactLargeObjects = true,
21+
public int $maxObjectSize = 50,
22+
public bool $enableShannonEntropy = true,
23+
public float $entropyThreshold = 4.5,
24+
public int $minLength = 20,
25+
/** @var array<string> */
26+
public array $entropyExclusionPatterns = [],
27+
public bool $markRedacted = true,
28+
public bool $trackRedactedKeys = false,
29+
public string $nonRedactableObjectBehavior = 'preserve'
30+
) {}
31+
32+
public static function fromConfig(): self
33+
{
34+
/** @var array<string> $safeKeys */
35+
$safeKeys = Config::array('monitor.log_redactor.safe_keys', []);
36+
/** @var array<string> $blockedKeys */
37+
$blockedKeys = Config::array('monitor.log_redactor.blocked_keys', []);
38+
/** @var array<string> $patterns */
39+
$patterns = Config::array('monitor.log_redactor.patterns', []);
40+
41+
// Ensure we only have string values and apply transformations
42+
$safeKeysLower = array_map(function (string $key): string {
43+
return strtolower($key);
44+
}, $safeKeys);
45+
46+
$blockedKeysLower = array_map(function (string $key): string {
47+
return strtolower($key);
48+
}, $blockedKeys);
49+
50+
$validPatterns = array_filter($patterns, fn (string $pattern): bool => @preg_match($pattern, '') !== false);
51+
52+
// Handle nullable max value length
53+
$maxValueLength = Config::get('monitor.log_redactor.max_value_length');
54+
$maxValueLengthTyped = is_int($maxValueLength) ? $maxValueLength : null;
55+
56+
/** @var array<string> $entropyExclusionPatterns */
57+
$entropyExclusionPatterns = Config::array('monitor.log_redactor.shannon_entropy.exclusion_patterns');
58+
59+
$validExclusionPatterns = array_filter($entropyExclusionPatterns, fn (string $pattern): bool => @preg_match($pattern, '') !== false);
60+
61+
return new self(
62+
safeKeys: $safeKeysLower,
63+
blockedKeys: $blockedKeysLower,
64+
patterns: array_values($validPatterns), // Re-index array
65+
replacement: Config::string('monitor.log_redactor.replacement', '[REDACTED]'),
66+
maxValueLength: $maxValueLengthTyped,
67+
redactLargeObjects: Config::boolean('monitor.log_redactor.redact_large_objects', true),
68+
maxObjectSize: Config::integer('monitor.log_redactor.max_object_size', 50),
69+
enableShannonEntropy: Config::boolean('monitor.log_redactor.shannon_entropy.enabled', true),
70+
entropyThreshold: Config::float('monitor.log_redactor.shannon_entropy.threshold', 4.5),
71+
minLength: Config::integer('monitor.log_redactor.shannon_entropy.min_length', 20),
72+
entropyExclusionPatterns: array_values($validExclusionPatterns), // Re-index array
73+
markRedacted: Config::boolean('monitor.log_redactor.mark_redacted', true),
74+
trackRedactedKeys: Config::boolean('monitor.log_redactor.track_redacted_keys', false),
75+
nonRedactableObjectBehavior: Config::string('monitor.log_redactor.non_redactable_object_behavior', 'preserve')
76+
);
77+
}
78+
}

0 commit comments

Comments
 (0)