-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass-wp-codebox-redaction-policy.php
More file actions
77 lines (65 loc) · 2.33 KB
/
Copy pathclass-wp-codebox-redaction-policy.php
File metadata and controls
77 lines (65 loc) · 2.33 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Shared redaction policy profiles for WordPress-side DTOs and proxies.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
class WP_Codebox_Redaction_Policy {
public const REDACTED_VALUE = '[redacted]';
/** @return array<string,array{exact_keys:array<int,string>,sensitive_key_tokens:array<int,string>,allowed_keys?:array<int,string>}> */
private static function profiles(): array {
return array(
'audit_metadata' => array(
'exact_keys' => array( 'authorization', 'key', 'value' ),
'sensitive_key_tokens' => array( 'secret', 'token', 'password', 'credential', 'private_key', 'api_key' ),
),
'provider_proxy' => array(
'exact_keys' => array( 'authorization', 'key', 'value' ),
'sensitive_key_tokens' => array( 'secret', 'token', 'password', 'credential', 'private_key', 'api_key' ),
),
'browser_event' => array(
'exact_keys' => array( 'authorization' ),
'sensitive_key_tokens' => array( 'secret', 'token', 'password', 'credential', 'private_key', 'api_key', 'cookie' ),
),
'public_session_dto' => array(
'exact_keys' => array(),
'sensitive_key_tokens' => array( 'secret', 'token', 'password', 'private_key', 'api_key', 'credential' ),
'allowed_keys' => array( 'secret_env', 'secretenv', 'secret_env_names' ),
),
);
}
public static function key_should_redact( string $profile_name, string $key ): bool {
$profile = self::profiles()[ $profile_name ] ?? null;
if ( null === $profile ) {
return false;
}
$normalized_key = strtolower( $key );
if ( in_array( $normalized_key, $profile['allowed_keys'] ?? array(), true ) ) {
return false;
}
if ( in_array( $normalized_key, $profile['exact_keys'], true ) ) {
return true;
}
foreach ( $profile['sensitive_key_tokens'] as $token ) {
if ( str_contains( $normalized_key, $token ) ) {
return true;
}
}
return false;
}
public static function redact_array( string $profile_name, mixed $value ): mixed {
if ( ! is_array( $value ) ) {
return $value;
}
$redacted = array();
foreach ( $value as $key => $item ) {
if ( self::key_should_redact( $profile_name, (string) $key ) ) {
$redacted[ $key ] = self::REDACTED_VALUE;
continue;
}
$redacted[ $key ] = self::redact_array( $profile_name, $item );
}
return $redacted;
}
}