|
6 | 6 |
|
7 | 7 | final class FieldMasker |
8 | 8 | { |
9 | | - /** |
10 | | - * Create a new instance of the FieldMasker. |
11 | | - * @param array<int,string|int|bool|array> $fields |
12 | | - */ |
13 | 9 | public function __construct( |
14 | 10 | public array $fields = [], |
15 | 11 | ) { |
16 | 12 | } |
17 | 13 |
|
18 | | - /** |
19 | | - * Mask the inputted data. |
20 | | - * @param array<string,string|array> $data |
21 | | - * @return array |
22 | | - */ |
23 | 14 | public function mask(array $data): array |
24 | 15 | { |
25 | 16 | $collector = []; |
26 | 17 | foreach ($data as $key => $value) { |
27 | | - if (is_array($value)) { |
28 | | - $collector[$key] = $this->mask( |
| 18 | + $collector[$key] = match (true) { |
| 19 | + is_array($value) => $this->mask( |
29 | 20 | data: $value, |
30 | | - ); |
31 | | - } |
| 21 | + ), |
| 22 | + is_string($value) => $this->handleString( |
| 23 | + key: $key, |
| 24 | + value: $value, |
| 25 | + ), |
| 26 | + default => $value, |
| 27 | + }; |
| 28 | + } |
32 | 29 |
|
33 | | - if (is_bool($value) || is_int($value) || is_float($value) || is_null($value)) { |
34 | | - $collector[$key] = $value; |
35 | | - } |
| 30 | + return $collector; |
| 31 | + } |
36 | 32 |
|
37 | | - // we should know it is a string. |
38 | | - if (is_string($value)) { |
39 | | - // check if this is an auth header or api key header etc |
40 | | - // is the key a header we want to mask? |
41 | | - if ($this->isHeader( |
42 | | - name: $key, |
43 | | - )) { |
44 | | - // grab the sensitive part of the value and mask. |
45 | | - if ($this->isAuth( |
46 | | - value: $value, |
47 | | - )) { |
48 | | - $parts = explode( |
49 | | - separator: ' ', |
50 | | - string: $value, |
51 | | - ); |
52 | | - |
53 | | - if (count($parts) >= 2) { |
54 | | - for ($i = 1; $i < count($parts); $i++) { |
55 | | - $parts[$i] = $this->star( |
56 | | - string: $parts[$i] |
57 | | - ); |
58 | | - } |
59 | | - } else { |
60 | | - $parts[0] = $this->star($parts[0]); |
61 | | - } |
62 | | - |
63 | | - $value = implode(' ', $parts); |
64 | | - } else { |
65 | | - $value = $this->star( |
66 | | - string: $value, |
67 | | - ); |
68 | | - } |
69 | | - } |
70 | | - |
71 | | - if (in_array($key, $this->fields, true)) { |
72 | | - $collector[$key] = $this->star( |
73 | | - string: $value, |
74 | | - ); |
75 | | - } else { |
76 | | - $collector[$key] = $value; |
77 | | - } |
78 | | - } |
| 33 | + private function handleString(string $key, string $value): string |
| 34 | + { |
| 35 | + static $lowerFields = null; |
| 36 | + if ($lowerFields === null) { |
| 37 | + $lowerFields = array_map('strtolower', $this->fields); |
79 | 38 | } |
80 | 39 |
|
81 | | - return $collector; |
| 40 | + $lowerKey = strtolower($key); |
| 41 | + |
| 42 | + if (in_array($lowerKey, $lowerFields, true)) { |
| 43 | + return $this->star($value); |
| 44 | + } |
| 45 | + |
| 46 | + if ($this->isSensitiveHeader($lowerKey)) { |
| 47 | + return $this->maskAuthorization($value); |
| 48 | + } |
| 49 | + |
| 50 | + if ($this->isBase64($value)) { |
| 51 | + return 'base64 encoded images are too big to process'; |
| 52 | + } |
| 53 | + |
| 54 | + return $value; |
82 | 55 | } |
83 | 56 |
|
84 | | - /** |
85 | | - * Check if the field is a Header. |
86 | | - * @param int|bool|float|string|null $name |
87 | | - * @return bool |
88 | | - */ |
89 | | - private function isHeader(int|bool|float|null|string $name): bool |
| 57 | + private function maskAuthorization(string $value): string |
90 | 58 | { |
91 | | - return in_array( |
92 | | - needle: $name, |
93 | | - haystack: [ |
94 | | - 'auth', |
95 | | - 'Auth', |
96 | | - 'Authorization', |
97 | | - 'authorization', |
98 | | - 'X-API-KEY', |
99 | | - 'x-api-key', |
100 | | - ], |
101 | | - strict: true, |
102 | | - ); |
| 59 | + $parts = explode(' ', $value, 2); |
| 60 | + if (isset($parts[1])) { |
| 61 | + $authTypeLower = strtolower($parts[0]); |
| 62 | + if (in_array($authTypeLower, ['bearer', 'basic', 'digest'])) { |
| 63 | + return $parts[0].' '.$this->star($parts[1]); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return $this->star($value); |
103 | 68 | } |
104 | 69 |
|
105 | | - /** |
106 | | - * Check is the value is part of an Auth header. |
107 | | - * @param string $value |
108 | | - * @return bool |
109 | | - */ |
110 | | - private function isAuth(string $value): bool |
| 70 | + private function isSensitiveHeader(string $key): bool |
111 | 71 | { |
112 | | - return in_array( |
113 | | - needle: explode( |
114 | | - separator: ' ', |
115 | | - string: $value, |
116 | | - )[0], |
117 | | - haystack: [ |
118 | | - 'Bearer', |
119 | | - 'bearer', |
120 | | - 'Basic', |
121 | | - 'basic', |
122 | | - ], |
123 | | - strict: true, |
124 | | - ); |
| 72 | + return in_array($key, ['authorization', 'x-api-key'], true); |
125 | 73 | } |
126 | 74 |
|
127 | | - /** |
128 | | - * Replace a string input with a star. |
129 | | - * @param string $string |
130 | | - * @return string |
131 | | - */ |
132 | 75 | public function star(string $string): string |
133 | 76 | { |
134 | | - return str_repeat( |
135 | | - string: '*', |
136 | | - times: strlen( |
137 | | - string: $string, |
138 | | - ), |
139 | | - ); |
| 77 | + return str_repeat('*', strlen($string)); |
| 78 | + } |
| 79 | + |
| 80 | + private function isBase64(string $string): bool |
| 81 | + { |
| 82 | + return str_starts_with($string, 'data:image/') && str_contains($string, ';base64,'); |
140 | 83 | } |
141 | 84 | } |
0 commit comments