|
| 1 | +# Log cookie data |
| 2 | + |
| 3 | +Looking at `dot-errorhandler`'s config file, the array found at `CookieProvider::class` allows you to configure the behaviour of this provider: |
| 4 | + |
| 5 | +- **enabled**: enabled/disable this provider |
| 6 | +- **processor**: an array configuring the data processor to be used by the **CookieProvider**: |
| 7 | + - **class**: data processor class implementing `Dot\ErrorHandler\Extra\Processor\ProcessorInterface` |
| 8 | + - **replacementStrategy**: whether to replace specific cookie values completely or partially |
| 9 | + - **sensitiveParameters**: an array of cookies names that may contain sensitive information so their value should be masked partially/completely |
| 10 | + |
| 11 | +## Configure provider |
| 12 | + |
| 13 | +By default, **CookieProvider** is disabled. |
| 14 | +It can be enabled only by setting **enabled** to **true**. |
| 15 | + |
| 16 | +If **enabled** is set to **true**, your log file will contain an additional field under the `extra` key, called `cookie`. |
| 17 | +If **enabled** is set to **false**, no additional field is added under the `extra` key. |
| 18 | + |
| 19 | +## Configure processor |
| 20 | + |
| 21 | +From here, we assume that **enabled** is set to **true**. |
| 22 | + |
| 23 | +If **processor** is missing/empty, the processor is ignored the provider will log the raw data available. |
| 24 | +If **processor** is specified, but **class** is missing/invalid, the processor is ignored and the provider will log the raw data available. |
| 25 | + |
| 26 | +From here, we assume that **processor**.**class** is valid. |
| 27 | + |
| 28 | +### Replacement strategy |
| 29 | + |
| 30 | +This value should be an instance of `Dot\ErrorHandler\Extra\ReplacementStrategy`. |
| 31 | + |
| 32 | +If **replacementStrategy** is missing/invalid, the default **replacementStrategy** is used, which is `ReplacementStrategy::Full`. |
| 33 | +Else, the value used should be one of: |
| 34 | + |
| 35 | +- `ReplacementStrategy::Partial` for half-string replacements (e.g.: "abcdef" becomes "abc***") |
| 36 | +- `ReplacementStrategy::Full` for full-string replacements (e.g.: "abcdef" becomes "******") |
| 37 | + |
| 38 | +### Sensitive parameters |
| 39 | + |
| 40 | +If **sensitiveParameters** is missing/empty, the processor is ignored the provider will log the raw data available. |
| 41 | +This is because without a set of **sensitiveParameters**, the processor is unable to determine which key needs to be processed or left untouched. |
| 42 | +When specifying the array of **sensitiveParameters**, there are two possibilities: |
| 43 | + |
| 44 | +- use the constant `ProcessorInterface::ALL`, meaning alter all cookie values using the strategy specified by the **replacementStrategy** |
| 45 | + |
| 46 | +```php |
| 47 | +'sensitiveParameters' => [ |
| 48 | + Dot\ErrorHandler\Extra\Processor\ProcessorInterface::ALL, |
| 49 | +], |
| 50 | +``` |
| 51 | + |
| 52 | +- use exact strings to list the cookies for which the values should be altered using the strategy specified by the **replacementStrategy** |
| 53 | + |
| 54 | +```php |
| 55 | +'sensitiveParameters' => [ |
| 56 | + 'rememberMe', |
| 57 | +], |
| 58 | +``` |
| 59 | + |
| 60 | +> **CookieProcessor** uses EXACT cookie name lookups. |
| 61 | +> In order to alter the value of a cookie, you need to specify the exact cookie name. |
| 62 | +
|
| 63 | +> The config `sensitiveParameters` is case-insensitive. |
| 64 | +
|
| 65 | +## Why should I use a processor |
| 66 | + |
| 67 | +Consider the following request cookies: |
| 68 | + |
| 69 | +```text |
| 70 | +[ |
| 71 | + "sessionId" => "feb21b39f9c54e3a49af1f862acc8300", |
| 72 | + "language" => "en", |
| 73 | +] |
| 74 | +``` |
| 75 | + |
| 76 | +Without a **CookieProcessor**, the plain text session cookie identifier would end up saved in the log file: |
| 77 | + |
| 78 | +```text |
| 79 | +..."extra":{"file":"/path/to/some/class.php","line":314,"cookie":{"sessionId":"feb21b39f9c54e3a49af1f862acc8300","language":"en"},... |
| 80 | +``` |
| 81 | + |
| 82 | +But, with a properly configured **CookieProcessor**: |
| 83 | + |
| 84 | +```php |
| 85 | +'processor' => [ |
| 86 | + 'class' => CookieProcessor::class, |
| 87 | + 'replacementStrategy' => ReplacementStrategy::Full, |
| 88 | + 'sensitiveParameters' => [ |
| 89 | + 'sessionId', |
| 90 | + ], |
| 91 | +], |
| 92 | +``` |
| 93 | + |
| 94 | +the logged cookie data becomes: |
| 95 | + |
| 96 | +```text |
| 97 | +..."extra":{"file":"/path/to/some/class.php","line":314,"cookie":{"sessionId":"********************************","language":"en"},... |
| 98 | +``` |
| 99 | + |
| 100 | +## Custom processor |
| 101 | + |
| 102 | +If the existing processor does not offer enough features, you can create a custom processor. |
| 103 | +The custom processor must implement `Dot\ErrorHandler\Extra\Processor\ProcessorInterface` or extend `Dot\ErrorHandler\Extra\Processor\AbstractProcessor`, which already implements `Dot\ErrorHandler\Extra\Processor\ProcessorInterface`. |
| 104 | +Once the custom processor is ready, you need to configure **CookieProvider** to use it. |
| 105 | +For this, open `dot-errorhandler`'s config file and - under **CookieProvider::class** - set **processor**.**class** to the class string of your custom processor: |
| 106 | + |
| 107 | +```php |
| 108 | +CookieProvider::class => [ |
| 109 | + 'enabled' => false, |
| 110 | + 'processor' => [ |
| 111 | + 'class' => CustomCookieProcessor::class, |
| 112 | + 'replacementStrategy' => ReplacementStrategy::Full, |
| 113 | + 'sensitiveParameters' => [ |
| 114 | + ProcessorInterface::ALL, |
| 115 | + ], |
| 116 | + ], |
| 117 | +], |
| 118 | +``` |
| 119 | + |
| 120 | +Using this, cookie data will be processed by `CustomCookieProcessor` and logged as provided by this new processor. |
0 commit comments