Skip to content

Commit 5757e80

Browse files
authored
Fix #20295: Add an ability to have wildcards in yii\log\Target::$maskVars array
1 parent 4816fbb commit 5757e80

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Yii Framework 2 Change Log
2222
- Bug #20140: Fix compatibility with PHP 8.4: calling `session_set_save_handler()` (Izumi-kun)
2323
- New #20185: Add `BackedEnum` support to `AttributeTypecastBehavior` (briedis)
2424
- Bug #17365: Fix "Trying to access array offset on null" warning (xcopy)
25+
- Enh #20295: Add an ability to have wildcards in `yii\log\Target::$maskVars` array (xcopy)
2526
- Bug #20296: Fix broken enum test (briedis)
2627
- Bug #20300: Clear stat cache in `FileCache::setValue()` (rob006)
2728

log/Target.php

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use yii\base\Component;
1212
use yii\base\InvalidConfigException;
1313
use yii\helpers\ArrayHelper;
14+
use yii\helpers\StringHelper;
1415
use yii\helpers\VarDumper;
1516
use yii\web\Request;
1617

@@ -91,6 +92,11 @@ abstract class Target extends Component
9192
* - `var` - `var` will be logged as `***`
9293
* - `var.key` - only `var[key]` will be logged as `***`
9394
*
95+
* In addition, this property accepts (case-insensitive) patterns. For example:
96+
* - `_SERVER.*_SECRET` matches all ending with `_SECRET`, such as `$_SERVER['TOKEN_SECRET']` etc.
97+
* - `_SERVER.SECRET_*` matches all starting with `SECRET_`, such as `$_SERVER['SECRET_TOKEN']` etc.
98+
* - `_SERVER.*SECRET*` matches all containing `SECRET` i.e. both of the above.
99+
*
94100
* @since 2.0.16
95101
*/
96102
public $maskVars = [
@@ -161,6 +167,54 @@ public function collect($messages, $final)
161167
}
162168
}
163169

170+
/**
171+
* Flattens a multidimensional array into a one-dimensional array.
172+
*
173+
* This method recursively traverses the input array and concatenates the keys
174+
* to form a new key in the resulting array.
175+
*
176+
* Example:
177+
*
178+
* ```php
179+
* $array = [
180+
* 'A' => [1, 2],
181+
* 'B' => [
182+
* 'C' => 1,
183+
* 'D' => 2,
184+
* ],
185+
* 'E' => 1,
186+
* ];
187+
* $result = \yii\log\Target::flatten($array);
188+
* // result will be:
189+
* // [
190+
* // 'A.0' => 1
191+
* // 'A.1' => 2
192+
* // 'B.C' => 1
193+
* // 'B.D' => 2
194+
* // 'E' => 1
195+
* // ]
196+
* ```
197+
*
198+
* @param array $array the input array to be flattened.
199+
* @param string $prefix the prefix to be added to each key in the resulting array.
200+
*
201+
* @return array the flattened array.
202+
*/
203+
private static function flatten($array, $prefix = ''): array
204+
{
205+
$result = [];
206+
207+
foreach ($array as $key => $value) {
208+
if (is_array($value)) {
209+
$result = array_merge($result, self::flatten($value, $prefix . $key . '.'));
210+
} else {
211+
$result[$prefix . $key] = $value;
212+
}
213+
}
214+
215+
return $result;
216+
}
217+
164218
/**
165219
* Generates the context information to be logged.
166220
* The default implementation will dump user information, system variables, etc.
@@ -169,9 +223,12 @@ public function collect($messages, $final)
169223
protected function getContextMessage()
170224
{
171225
$context = ArrayHelper::filter($GLOBALS, $this->logVars);
226+
$items = self::flatten($context);
172227
foreach ($this->maskVars as $var) {
173-
if (ArrayHelper::getValue($context, $var) !== null) {
174-
ArrayHelper::setValue($context, $var, '***');
228+
foreach ($items as $key => $value) {
229+
if (StringHelper::matchWildcard($var, $key, ['caseSensitive' => false])) {
230+
ArrayHelper::setValue($context, $key, '***');
231+
}
175232
}
176233
}
177234
$result = [];
@@ -292,7 +349,7 @@ public static function filterMessages($messages, $levels = 0, $categories = [],
292349
*/
293350
public function formatMessage($message)
294351
{
295-
list($text, $level, $category, $timestamp) = $message;
352+
[$text, $level, $category, $timestamp] = $message;
296353
$level = Logger::getLevelName($level);
297354
if (!is_string($text)) {
298355
// exceptions may not be serializable if in the call stack somewhere is a Closure

0 commit comments

Comments
 (0)