Repository navigation
Expand file tree
/
Copy pathBrowserCache_RuleDirectives.php
More file actions
131 lines (121 loc) · 3.34 KB
/
Copy pathBrowserCache_RuleDirectives.php
File metadata and controls
131 lines (121 loc) · 3.34 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* File: BrowserCache_RuleDirectives.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class BrowserCache_RuleDirectives
*
* Render-time policy for BrowserCache server rules: normalize exception
* patterns and resolve select-backed security-header directives against
* an allowlist so Apache and Nginx emitters only write validated values.
*
* Write-time stripping for `directive_string` keys lives in Config::set();
* this class is the paired render-time consumer.
*
* @since 2.10.6
*/
class BrowserCache_RuleDirectives {
/**
* Select-backed directive metadata.
*
* Keys are short directive ids used by callers. Each entry lists the
* accepted raw config values (as stored by the admin UI). Callers map
* those accepted tokens onto platform-specific output.
*
* @since 2.10.6
*
* @var array<string, array{accepted: array<int, string>}>
*/
private static $directives = array(
'hsts' => array(
'accepted' => array( 'maxage', 'maxagepre', 'maxageinc', 'maxageincpre' ),
),
'xfo' => array(
'accepted' => array( 'same', 'deny', 'allow' ),
),
'xss' => array(
'accepted' => array( '0', '1', 'block' ),
),
'referrer_policy' => array(
'accepted' => array(
'0',
'no-referrer',
'no-referrer-when-downgrade',
'same-origin',
'origin',
'strict-origin',
'origin-when-cross-origin',
'strict-origin-when-cross-origin',
'unsafe-url',
),
),
'pkp_extra' => array(
'accepted' => array( 'maxage', 'maxageinc' ),
),
'pkp_report_only' => array(
'accepted' => array( '0', '1' ),
),
);
/**
* Returns accepted raw values for a named directive, or null when unknown.
*
* @since 2.10.6
*
* @param string $directive Directive id (e.g. `hsts`, `xfo`).
*
* @return array<int, string>|null
*/
public static function accepted_values( $directive ) {
if ( ! \is_string( $directive ) || ! isset( self::$directives[ $directive ] ) ) {
return null;
}
return self::$directives[ $directive ]['accepted'];
}
/**
* Resolves a select-backed directive to an accepted token.
*
* Strips directive metacharacters first, then allowlists. Unsupported
* or empty results return null so callers can skip rendering.
*
* @since 2.10.6
*
* @param string $directive Directive id.
* @param mixed $value Raw config value.
*
* @return string|null Accepted token, or null when invalid.
*/
public static function normalize_enum( $directive, $value ) {
$accepted = self::accepted_values( $directive );
if ( null === $accepted ) {
return null;
}
$candidate = Util_Rule::sanitize_directive_value(
\is_string( $value ) ? $value : ''
);
if ( '' === $candidate || ! \in_array( $candidate, $accepted, true ) ) {
return null;
}
return $candidate;
}
/**
* Normalizes no404wp exception patterns for rule rendering.
*
* Sanitizes each entry, drops empties/non-strings, de-duplicates, and
* sorts so equivalent inputs produce identical Apache/Nginx output.
*
* @since 2.10.6
*
* @param mixed $exceptions Raw `browsercache.no404wp.exceptions` value.
*
* @return array<int, string>
*/
public static function normalize_exceptions( $exceptions ) {
$out = Util_Rule::sanitize_directive_values( $exceptions );
$out = \array_values( \array_unique( $out ) );
\sort( $out, SORT_STRING );
return $out;
}
}