Skip to content

Commit 5bce0d8

Browse files
committed
Strip configured query items from URI for secure logging (also affects the logged uri in the context)
1 parent d78fddb commit 5bce0d8

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/LoggerMiddleware.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ApiClients\Foundation\Middleware\MiddlewareInterface;
77
use Psr\Http\Message\RequestInterface;
88
use Psr\Http\Message\ResponseInterface;
9+
use Psr\Http\Message\UriInterface;
910
use Psr\Log\LoggerInterface;
1011
use React\Promise\CancellablePromiseInterface;
1112
use Throwable;
@@ -43,7 +44,7 @@ public function pre(
4344
}
4445

4546
$this->context[$transactionId][self::REQUEST]['method'] = $request->getMethod();
46-
$this->context[$transactionId][self::REQUEST]['uri'] = (string)$request->getUri();
47+
$this->context[$transactionId][self::REQUEST]['uri'] = (string)$this->stripQueryItems($request->getUri(), $options);
4748
$this->context[$transactionId][self::REQUEST]['protocol_version'] = (string)$request->getProtocolVersion();
4849
$ignoreHeaders = $options[self::class][Options::IGNORE_HEADERS] ?? [];
4950
$this->context[$transactionId] = $this->iterateHeaders(
@@ -171,4 +172,14 @@ private function addResponseToContext(
171172

172173
return $context;
173174
}
175+
176+
private function stripQueryItems(UriInterface $uri, array $options): UriInterface
177+
{
178+
parse_str($uri->getQuery(), $query);
179+
foreach ($options[self::class][Options::IGNORE_URI_QUERY_ITEMS] ?? [] as $item) {
180+
unset($query[$item], $query[$item . '[]']);
181+
}
182+
183+
return $uri->withQuery(http_build_query($query));
184+
}
174185
}

src/Options.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
final class Options
66
{
7-
public const IGNORE_HEADERS = 'ignore_headers';
8-
public const LEVEL = 'level';
9-
public const ERROR_LEVEL = 'error_level';
10-
public const URL_LEVEL = 'url_level';
7+
public const IGNORE_HEADERS = 'ignore_headers';
8+
public const IGNORE_URI_QUERY_ITEMS = 'ignoreuri_query_items';
9+
public const LEVEL = 'level';
10+
public const ERROR_LEVEL = 'error_level';
11+
public const URL_LEVEL = 'url_level';
1112
}

tests/LoggerMiddlewareTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ public function testLog()
5555
'X-Ignore-Request',
5656
'X-Ignore-Response',
5757
],
58+
Options::IGNORE_URI_QUERY_ITEMS => [
59+
'strip_this_item',
60+
],
5861
],
5962
];
6063
$request = new Request(
6164
'GET',
62-
'https://example.com/',
65+
'https://example.com/?strip_this_item=0&dont_strip_this_item=1',
6366
[
6467
'X-Foo' => 'bar',
6568
'X-Ignore-Request' => 'nope',
@@ -79,11 +82,11 @@ public function testLog()
7982
$logger = $this->prophesize(LoggerInterface::class);
8083
$logger->log(
8184
LogLevel::DEBUG,
82-
'Requesting: https://example.com/',
85+
'Requesting: https://example.com/?dont_strip_this_item=1',
8386
[
8487
'request' => [
8588
'method' => 'GET',
86-
'uri' => 'https://example.com/',
89+
'uri' => 'https://example.com/?dont_strip_this_item=1',
8790
'protocol_version' => '1.1',
8891
'headers' => [
8992
'Host' => ['example.com'],
@@ -98,7 +101,7 @@ public function testLog()
98101
[
99102
'request' => [
100103
'method' => 'GET',
101-
'uri' => 'https://example.com/',
104+
'uri' => 'https://example.com/?dont_strip_this_item=1',
102105
'protocol_version' => '1.1',
103106
'headers' => [
104107
'Host' => ['example.com'],

0 commit comments

Comments
 (0)