Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit b433980

Browse files
authored
Merge pull request #4 from interactive-solutions/blur-response-keys
Allow to filter values for keys when logging request
2 parents dd0c2b9 + eb32636 commit b433980

3 files changed

Lines changed: 82 additions & 2 deletions

File tree

config/config.php.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ return [
1818
ElasticsearchAdapter::class,
1919
],
2020
'alwaysLogRoutes' => [],
21+
'blurredKeys' => [
22+
'password'
23+
],
24+
'blurredKeysValue' => '***FILTERED***'
2125
],
2226

2327
ElasticsearchOptions::class => [

src/Options/LogHandlerOptions.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ final class LogHandlerOptions extends AbstractOptions
2222
*/
2323
protected $alwaysLogRoutes = [];
2424

25+
/**
26+
* @var array
27+
*/
28+
protected $blurredKeys = ['password'];
29+
30+
/**
31+
* @var string
32+
*/
33+
protected $blurredKeysValue = '***FILTERED***';
34+
2535
/**
2636
* @var array
2737
*/
@@ -116,4 +126,36 @@ public function setHost(string $host)
116126
{
117127
$this->host = $host;
118128
}
129+
130+
/**
131+
* @return array
132+
*/
133+
public function getBlurredKeys(): array
134+
{
135+
return $this->blurredKeys;
136+
}
137+
138+
/**
139+
* @param array $blurredKeys
140+
*/
141+
public function setBlurredKeys(array $blurredKeys)
142+
{
143+
$this->blurredKeys = $blurredKeys;
144+
}
145+
146+
/**
147+
* @return string
148+
*/
149+
public function getBlurredKeysValue(): string
150+
{
151+
return $this->blurredKeysValue;
152+
}
153+
154+
/**
155+
* @param string $blurredKeysValue
156+
*/
157+
public function setBlurredKeysValue(string $blurredKeysValue)
158+
{
159+
$this->blurredKeysValue = $blurredKeysValue;
160+
}
119161
}

src/Service/LogHandlerService.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public function handleRequestResponse(
9797
'headers' => $request->getHeaders()->toString(),
9898
'routeMatch' => $routeMatch ? $routeMatch->getMatchedRouteName() : '',
9999
'url' => $request->getUriString(),
100-
'body' => $request->getContent(),
101-
'query' => $request->getQuery()->toString(),
100+
'body' => $this->blurKeywords($request->getContent()),
101+
'query' => $this->blurKeywords($request->getQuery()->toString()),
102102
'method' => $request->getMethod(),
103103
],
104104
'response' => [
@@ -127,6 +127,40 @@ public function getAdapters(): array
127127
return $this->adapters;
128128
}
129129

130+
/**
131+
* Check if string contains keys that should be blurred out
132+
* This will only check top-level
133+
*
134+
* @param string $body
135+
* @return string
136+
*/
137+
private function blurKeywords(string $body): string
138+
{
139+
try {
140+
$isJson = true;
141+
$bodyAsArray = Json::decode($body, true);
142+
} catch (RuntimeException $e) {
143+
$isJson = false;
144+
parse_str($body, $bodyAsArray);
145+
}
146+
147+
if (!is_array($bodyAsArray)) {
148+
return $body;
149+
}
150+
151+
foreach ($bodyAsArray as $key => $value) {
152+
if (in_array($key, $this->options->getBlurredKeys(), false)) {
153+
$bodyAsArray[$key] = $this->options->getBlurredKeysValue();
154+
}
155+
}
156+
157+
if ($isJson) {
158+
return Json::encode($bodyAsArray);
159+
}
160+
161+
return http_build_query($bodyAsArray);
162+
}
163+
130164
/**
131165
* Check if matched route should always be logged for http request/response
132166
*

0 commit comments

Comments
 (0)