Skip to content

Commit e656757

Browse files
committed
Obfuscating request headers directly on PSR compliant request instance
make it easier to configure request header/body obfuscation
1 parent 257c91d commit e656757

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## [v0.9.1 (Unreleased)](https://github.com/onlime/laravel-http-client-global-logger/compare/v0.9...main)
44

5+
- Introducing `HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_HEADERS` and `HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_BODY_KEYS` env vars for easier configuration of request header/body obfuscation.
6+
- Obfuscating request headers directly on PSR compliant request instance instead of applying regex replacements on formatted log message.
7+
58
## [v0.9 (2021-07-12)](https://github.com/onlime/laravel-http-client-global-logger/releases/tag/v0.9)
69

710
- Initial release

config/http-client-global-logger.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,13 @@
9898
'obfuscate' => [
9999
'enabled' => env('HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_ENABLED', true),
100100
'replacement' => env('HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_REPLACEMENT', '**********'),
101-
'patterns' => [
102-
'/(?<="pass":").*(?=")/mU',
103-
'/(?<=Authorization:\sBearer ).*/m',
104-
],
101+
'headers' => explode(',', env(
102+
'HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_HEADERS',
103+
'Authorization'
104+
)),
105+
'body_keys' => explode(',', env(
106+
'HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_BODY_KEYS',
107+
'pass,password,token,apikey'
108+
)),
105109
]
106110
];

src/Listeners/LogRequestSending.php

+44-11
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use GuzzleHttp\MessageFormatter;
66
use Illuminate\Http\Client\Events\RequestSending;
7-
use Illuminate\Support\Arr;
87
use Illuminate\Support\Facades\Log;
8+
use Psr\Http\Message\RequestInterface;
99

1010
class LogRequestSending
1111
{
@@ -17,19 +17,52 @@ class LogRequestSending
1717
*/
1818
public function handle(RequestSending $event)
1919
{
20+
$obfuscate = config('http-client-global-logger.obfuscate.enabled');
21+
$psrRequest = $event->request->toPsrRequest();
22+
23+
if ($obfuscate) {
24+
$psrRequest = $this->obfuscateHeaders($psrRequest);
25+
}
26+
2027
$formatter = new MessageFormatter(config('http-client-global-logger.format.request'));
21-
$message = $formatter->format(
22-
$event->request->toPsrRequest()
23-
);
24-
25-
if (config('http-client-global-logger.obfuscate.enabled')) {
26-
$message = preg_replace(
27-
config('http-client-global-logger.obfuscate.patterns'),
28-
config('http-client-global-logger.obfuscate.replacement'),
29-
$message
30-
);
28+
$message = $formatter->format($psrRequest);
29+
30+
if ($obfuscate) {
31+
foreach (config('http-client-global-logger.obfuscate.body_keys') as $key) {
32+
$message = preg_replace(
33+
'/(?<="' . $key . '":").*(?=")/mU',
34+
config('http-client-global-logger.obfuscate.replacement'),
35+
$message
36+
);
37+
}
3138
}
3239

3340
Log::channel(config('http-client-global-logger.channel'))->info($message);
3441
}
42+
43+
/**
44+
* Obfuscate headers, e.g. Authorization header.
45+
*
46+
* @param RequestInterface $request
47+
* @return RequestInterface
48+
*/
49+
protected function obfuscateHeaders(RequestInterface $request): RequestInterface
50+
{
51+
$replacement = config('http-client-global-logger.obfuscate.replacement');
52+
53+
// TODO: Currently, there is no clean way of modifying the PendingRequest body, e.g. via Macros
54+
// see https://stackoverflow.com/q/60603066/5982842
55+
// Tried to modify data directly on HTTP Client Request object, but PsrRequest is already set
56+
// $data = $request->data();
57+
// data_set($data, 'params.pass', $replacement);
58+
// $request = $request->withData($data);
59+
60+
foreach (config('http-client-global-logger.obfuscate.headers') as $name) {
61+
if ($request->hasHeader($name)) {
62+
$request = $request->withHeader($name, $replacement);
63+
}
64+
}
65+
66+
return $request;
67+
}
3568
}

0 commit comments

Comments
 (0)