Repository navigation
Expand file tree
/
Copy pathQueryParametersRedaction.php
More file actions
57 lines (49 loc) · 1.87 KB
/
Copy pathQueryParametersRedaction.php
File metadata and controls
57 lines (49 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace TinyBlocks\Logger\Redactions;
/**
* Masks the query parameters of a logged request, keeping only the ones named.
*
* <p>A query parameter is a value someone put in a URL, so it carries whatever the caller sent: an
* identifier, a document, a token. Naming what stays readable removes the leak by omission, since a
* parameter added later is masked until it is allowed.</p>
*
* <p>It reaches the <code>query_parameters</code> branch, which is where the request log middleware
* of this ecosystem puts them. For a payload that carries them under another name, compose
* {@see GenericRedaction::under()} with {@see GenericRedaction::keeping()} instead.</p>
*/
final readonly class QueryParametersRedaction implements Redaction
{
private const string PARENT = 'query_parameters';
private Redaction $redactor;
private function __construct(array $fields)
{
$this->redactor = GenericRedaction::under(
parent: self::PARENT,
redaction: GenericRedaction::keeping(fields: $fields)
);
}
/**
* Builds a QueryParametersRedaction masking every query parameter.
*
* @return QueryParametersRedaction The created instance.
*/
public static function default(): QueryParametersRedaction
{
return QueryParametersRedaction::keeping(fields: []);
}
/**
* Creates a QueryParametersRedaction keeping only the named parameters readable.
*
* @param string[] $fields The parameter names left untouched, wildcards accepted.
* @return QueryParametersRedaction The created instance.
*/
public static function keeping(array $fields): QueryParametersRedaction
{
return new QueryParametersRedaction(fields: $fields);
}
public function redact(array $payload): array
{
return $this->redactor->redact(payload: $payload);
}
}