-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathSecurity.php
More file actions
762 lines (674 loc) · 29.8 KB
/
Copy pathSecurity.php
File metadata and controls
762 lines (674 loc) · 29.8 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
<?php
/**
* @package Grav\Common
*
* @copyright Copyright (c) 2015 - 2026 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Common;
use Exception;
use Grav\Common\Config\Config;
use Grav\Common\Filesystem\Folder;
use Grav\Common\Page\Pages;
use Grav\Common\Twig\Sandbox\GravSecurityPolicy;
use Rhukster\DomSanitizer\DOMSanitizer;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\File\YamlFile;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use RuntimeException;
use Twig\Sandbox\SecurityPolicyInterface;
use function chr;
use function count;
use function is_array;
use function is_string;
/**
* Class Security
* @package Grav\Common
*/
class Security
{
/**
* @param string $filepath
* @param array|null $options
* @return string|null
*/
public static function detectXssFromSvgFile(string $filepath, ?array $options = null): ?string
{
if (file_exists($filepath) && Grav::instance()['config']->get('security.sanitize_svg')) {
$content = file_get_contents($filepath);
return static::detectXss($content, $options);
}
return null;
}
/**
* Sanitize SVG string for XSS code
*
* @param string $svg
* @return string
*/
public static function sanitizeSvgString(string $svg): string
{
if (Grav::instance()['config']->get('security.sanitize_svg')) {
$sanitizer = new DOMSanitizer(DOMSanitizer::SVG);
$sanitizer->addDisallowedAttributes(['href', 'xlink:href']);
$sanitized = $sanitizer->sanitize($svg);
if (is_string($sanitized)) {
$svg = $sanitized;
}
}
return $svg;
}
/**
* Sanitize SVG for XSS code
*
* @param string $file
* @return void
*/
public static function sanitizeSVG(string $file): void
{
if (file_exists($file) && Grav::instance()['config']->get('security.sanitize_svg')) {
$sanitizer = new DOMSanitizer(DOMSanitizer::SVG);
$sanitizer->addDisallowedAttributes(['href', 'xlink:href']);
$original_svg = file_get_contents($file);
$clean_svg = $sanitizer->sanitize($original_svg);
// Quarantine bad SVG files and throw exception
if ($clean_svg !== false ) {
file_put_contents($file, $clean_svg);
} else {
$quarantine_file = Utils::basename($file);
$quarantine_dir = 'log://quarantine';
Folder::mkdir($quarantine_dir);
file_put_contents("$quarantine_dir/$quarantine_file", $original_svg);
unlink($file);
throw new Exception('SVG could not be sanitized, it has been moved to the logs/quarantine folder');
}
}
}
/**
* Detect XSS code in Grav pages
*
* @param Pages $pages
* @param bool $route
* @param callable|null $status
* @return array
*/
public static function detectXssFromPages(Pages $pages, $route = true, ?callable $status = null)
{
$routes = $pages->getList(null, 0, true);
// Remove duplicate for homepage
unset($routes['/']);
$list = [];
// This needs Symfony 4.1 to work
$status && $status([
'type' => 'count',
'steps' => count($routes),
]);
foreach (array_keys($routes) as $route) {
$status && $status([
'type' => 'progress',
]);
try {
$page = $pages->find($route);
if ($page->exists()) {
// call the content to load/cache it
$header = (array) $page->header();
$content = $page->value('content');
$data = ['header' => $header, 'content' => $content];
$results = static::detectXssFromArray($data);
if (!empty($results)) {
$list[$page->rawRoute()] = $results;
}
}
} catch (Exception) {
continue;
}
}
return $list;
}
/**
* Detect XSS in an array or strings such as $_POST or $_GET
*
* @param array $array Array such as $_POST or $_GET
* @param array|null $options Extra options to be passed.
* @param string $prefix Prefix for returned values.
* @return array Returns flatten list of potentially dangerous input values, such as 'data.content'.
*/
public static function detectXssFromArray(array $array, string $prefix = '', ?array $options = null)
{
if (null === $options) {
$options = static::getXssDefaults();
}
$list = [[]];
foreach ($array as $key => $value) {
if (is_array($value)) {
$list[] = static::detectXssFromArray($value, $prefix . $key . '.', $options);
}
if ($result = static::detectXss($value, $options)) {
$list[] = [$prefix . $key => $result];
}
}
return array_merge(...$list);
}
/**
* Determine if string potentially has a XSS attack. This simple function does not catch all XSS and it is likely to
*
* return false positives because of it tags all potentially dangerous HTML tags and attributes without looking into
* their content.
*
* @param string|null $string The string to run XSS detection logic on
* @param array|null $options
* @return string|null Type of XSS vector if the given `$string` may contain XSS, false otherwise.
*
* Copies the code from: https://github.com/symphonycms/xssfilter/blob/master/extension.driver.php#L138
*/
public static function detectXss($string, ?array $options = null): ?string
{
// Skip any null or non string values
if (null === $string || !is_string($string) || empty($string)) {
return null;
}
if (null === $options) {
$options = static::getXssDefaults();
}
$enabled_rules = (array)($options['enabled_rules'] ?? null);
$dangerous_tags = (array)($options['dangerous_tags'] ?? null);
if (!$dangerous_tags) {
$enabled_rules['dangerous_tags'] = false;
}
$invalid_protocols = (array)($options['invalid_protocols'] ?? null);
if (!$invalid_protocols) {
$enabled_rules['invalid_protocols'] = false;
}
$enabled_rules = array_filter($enabled_rules, static fn($val) => !empty($val));
if (!$enabled_rules) {
return null;
}
// Keep a copy of the original string before cleaning up
$orig = $string;
// URL decode
$string = urldecode($string);
// Convert Hexadecimals
$string = (string)preg_replace_callback('!(&#|\\\)[xX]([0-9a-fA-F]+);?!u', static fn($m) => chr(hexdec((string) $m[2])), $string);
// Clean up entities
$string = preg_replace('!(&#[0-9]+);?!u', '$1;', $string);
// Decode entities
$string = html_entity_decode((string) $string, ENT_NOQUOTES | ENT_HTML5, 'UTF-8');
// Strip whitespace characters
$string = preg_replace('!\s!u', ' ', $string);
$stripped = preg_replace('!\s!u', '', (string) $string);
// Set the patterns we'll test against
$patterns = [
// Match any attribute starting with "on" or xmlns (must be preceded by an
// attribute boundary: whitespace, NUL, quote or slash). We deliberately
// do NOT try to match the attribute value itself — the previous regex
// required quotes-or-spaces around the `=` sign and was bypassed by
// unquoted handlers like `<img src=x onerror=alert(1)>`
// (GHSA-9695-8fr9-hw5q, also exploited by GHSA-c2q3-p4jr-c55f and
// GHSA-w8cg-7jcj-4vv2). Detecting the attribute name + `=` is enough
// for a tripwire; trade-off is occasional false positives when an
// unrelated `on*=` substring appears inside another attribute's value.
'on_events' => '#<[^>]*?[\s\x00-\x20\"\'\/](on\s*[a-z]+|xmlns)\s*=#iu',
// Match javascript:, livescript:, vbscript:, mocha:, feed: and data: protocols
'invalid_protocols' => '#(' . implode('|', array_map('preg_quote', $invalid_protocols, ['#'])) . ')(:|\&\#58)\S.*?#iUu',
// Match -moz-bindings
'moz_binding' => '#-moz-binding[a-z\x00-\x20]*:#u',
// Match style attributes
'html_inline_styles' => '#(<[^>]+[a-z\x00-\x20\"\'\/])(style=[^>]*(url\:|x\:expression).*)>?#iUu',
// Match potentially dangerous tags
'dangerous_tags' => '#</*(' . implode('|', array_map('preg_quote', $dangerous_tags, ['#'])) . ')[^>]*>?#ui'
];
// Iterate over rules and return label if fail
foreach ($patterns as $name => $regex) {
if (!empty($enabled_rules[$name])) {
// Skip testing 'on_events' against stripped version to avoid false positives
// with tags like <caption>, <button>, <section> that end with 'on' or contain 'on'
if ($name === 'on_events') {
if (preg_match($regex, (string) $string) || preg_match($regex, $orig)) {
return $name;
}
} else {
if (preg_match($regex, (string) $string) || preg_match($regex, (string) $stripped) || preg_match($regex, $orig)) {
return $name;
}
}
}
}
return null;
}
public static function getXssDefaults(): array
{
/** @var Config $config */
$config = Grav::instance()['config'];
return [
'enabled_rules' => $config->get('security.xss_enabled'),
'dangerous_tags' => array_map('trim', $config->get('security.xss_dangerous_tags')),
'invalid_protocols' => array_map('trim', $config->get('security.xss_invalid_protocols')),
];
}
/** @var SecurityPolicyInterface|null Cached policy for current request */
private static ?SecurityPolicyInterface $twigSandboxPolicy = null;
/** @var string|null Cache key (hash of policy config) */
private static ?string $twigSandboxPolicyKey = null;
/**
* Build (or return cached) Twig sandbox SecurityPolicy from security.twig_sandbox.* config.
* Cached per-request, invalidated when the config hash changes.
*/
public static function buildTwigSandboxPolicy(): SecurityPolicyInterface
{
/** @var Config $config */
$config = Grav::instance()['config'];
// Raw, as-authored allowlists from config (system + user merged). The
// friendly shapes here — flat lists for tags/filters/functions, the
// list-of-rows shape for methods/properties — are exactly what the
// onBuildTwigSandboxPolicy event hands to plugins, so a plugin appends
// entries the same way they're written in security.yaml.
$rawTags = $config->get('security.twig_sandbox.allowed_tags', []);
$rawFilters = $config->get('security.twig_sandbox.allowed_filters', []);
$rawFunctions = $config->get('security.twig_sandbox.allowed_functions', []);
$rawMethods = $config->get('security.twig_sandbox.allowed_methods', []);
$rawProperties = $config->get('security.twig_sandbox.allowed_properties', []);
$configAccess = (bool) $config->get('security.twig_content.config_access', false);
$cacheKey = md5(serialize([$rawTags, $rawFilters, $rawFunctions, $rawMethods, $rawProperties, $configAccess]));
if (self::$twigSandboxPolicy !== null && self::$twigSandboxPolicyKey === $cacheKey) {
return self::$twigSandboxPolicy;
}
// Let plugins extend the allowlists for their own safe Twig members so
// editor-authored page content can use them under the sandbox. A plugin
// that ships a Twig function subscribes to `onBuildTwigSandboxPolicy`
// and appends to the relevant list — it is asserting that member is
// safe to expose to content authors, the same trust boundary as
// registering it in the first place. Example handler (read-modify-write
// because the event arguments are returned by value):
//
// $functions = $event['functions'];
// $functions[] = 'unite_gallery';
// $event['functions'] = $functions;
//
// $methods = $event['methods'];
// $methods[] = ['class' => Gallery::class, 'methods' => 'render'];
// $event['methods'] = $methods;
//
// Fired only on a genuine (re)build, never on the memoized path above:
// the policy is built once per request and the active plugin set is
// constant for the request, so the config-derived key memoizes the
// event's additions correctly with no per-render cost.
$event = new Event([
'tags' => $rawTags,
'filters' => $rawFilters,
'functions' => $rawFunctions,
'methods' => $rawMethods,
'properties' => $rawProperties,
]);
Grav::instance()->fireEvent('onBuildTwigSandboxPolicy', $event);
// Method names get lowercased to match Twig's sandbox comparison.
// Property names are CASE-SENSITIVE and preserved as-authored.
$tags = self::normalizeStringList($event['tags']);
$filters = self::normalizeStringList($event['filters']);
$functions = self::normalizeStringList($event['functions']);
$methods = self::normalizeMethodsMap($event['methods'], true);
$properties = self::normalizeMethodsMap($event['properties'], false);
// security.twig_content.config_access also closes the `grav.config`
// back-door: with the toggle off, the injected `config` variable is a
// deny-all SandboxConfig (handled in Twig::buildSandboxConfig), but
// `grav.config` and `grav['config']` still resolve to the raw Config
// service. Strip the Config and Data class entries from the sandbox's
// method allowlist so any reach into the raw container soft-fails via
// SecurityError instead of leaking values. The SandboxConfig class
// entry stays — that's the variable editors are meant to read.
if (!$configAccess) {
unset(
$methods['Grav\\Common\\Config\\Config'],
$methods['Grav\\Common\\Data\\Data']
);
}
self::$twigSandboxPolicy = new GravSecurityPolicy($tags, $filters, $methods, $properties, $functions);
self::$twigSandboxPolicyKey = $cacheKey;
return self::$twigSandboxPolicy;
}
/**
* Log a Twig sandbox violation via the security log channel. Called from the
* SecurityError handler in Twig::processPage() / processString().
*/
public static function logTwigSandboxViolation(string $rule, string $token, string $className = '', string $extra = ''): void
{
try {
/** @var Config $config */
$config = Grav::instance()['config'];
if (!$config->get('security.twig_sandbox.logging', true)) {
return;
}
$grav = Grav::instance();
if (!$grav->offsetExists('log.security')) {
return;
}
$logger = $grav['log.security'];
$route = 'unknown';
if ($grav->offsetExists('page')) {
$page = $grav['page'];
if ($page && method_exists($page, 'route')) {
$route = (string) ($page->route() ?? 'unknown');
}
}
$hint = self::twigSandboxHint($rule, $token, $className);
$logger->warning(
sprintf('[TwigSandbox] blocked rule=%s token=%s route=%s', $rule, $token, $route),
[
'rule' => $rule,
'token' => $token,
'class' => $className,
'route' => $route,
'extra' => $extra,
'hint' => $hint,
]
);
} catch (Exception) {
// Never let a logging failure break rendering.
}
}
/**
* Options resolver for the `header.process` checkboxes field in the page
* editor blueprint. Removes the `twig` checkbox when either the master
* gate is off or the current user lacks permission to enable Twig in
* content. Wired via `data-options@` in system/blueprints/pages/default.yaml.
*
* Visibility rules (any failure → twig option omitted):
* - security.twig_content.process_enabled must be true
* - security.twig_content.editor_enabled must be true OR the current
* user must hold `admin.super` or `admin.pages_twig`
*
* @return array<string,string>
*/
public static function pageProcessOptions(): array
{
$options = ['markdown' => 'Markdown'];
try {
$grav = Grav::instance();
/** @var Config $config */
$config = $grav['config'];
if ((bool) $config->get('security.twig_content.process_enabled', false) === false) {
return $options;
}
if ((bool) $config->get('security.twig_content.editor_enabled', false) === true) {
$options['twig'] = 'Twig';
return $options;
}
$user = $grav['user'] ?? null;
if ($user !== null && (
$user->authorize('admin.super') === true
|| $user->authorize('admin.pages_twig') === true
)) {
$options['twig'] = 'Twig';
}
} catch (Exception) {
// Conservative default: markdown only.
}
return $options;
}
/**
* Default the per-page `process.twig` flag from
* `security.twig_content.process_enabled` when the key isn't explicitly
* set in the configured `process` array. The security gate is the single
* source of truth for editor-Twig in content; an explicit value (true or
* false) in `system.pages.process` or per-page frontmatter still wins.
*
* Treats explicit YAML null (`twig: ~`) as "unset" so it inherits the gate.
*
* @param array<string,mixed> $process Configured process array (may be empty).
* @return array<string,mixed> Same array with `twig` populated when it was missing or null.
*/
public static function applyTwigContentDefault(array $process): array
{
if (isset($process['twig'])) {
return $process;
}
try {
$process['twig'] = (bool) Grav::instance()['config']->get('security.twig_content.process_enabled', false);
} catch (\Throwable) {
$process['twig'] = false;
}
return $process;
}
/**
* Per-page `process` field defaults for the page editor blueprint.
* Pulls markdown/twig defaults from `system.pages.process`, defaults
* `twig` from the security gate when unset, and intersects the result
* down to the keys advertised by pageProcessOptions() so plugin-
* contributed keys outside the {markdown, twig} contract don't leak
* into the form's `default:` block. Wired via `data-default@` in
* pages/default.yaml.
*
* @return array<string,bool>
*/
public static function pageProcessDefaults(): array
{
$defaults = ['markdown' => true, 'twig' => false];
try {
$config = Grav::instance()['config'];
// Apply the gate fallback to the configured value FIRST so an
// unset twig key inherits from the gate; only then overlay onto
// the schema seed (markdown defaulting to true).
$configured = self::applyTwigContentDefault((array) $config->get('system.pages.process', []));
$merged = array_replace($defaults, $configured);
// Restrict to the keys pageProcessOptions() actually renders so
// stray plugin-contributed keys don't appear in the form default.
// Always keep markdown + twig in the schema even if the current
// user's options view hides twig — the field still expects both
// checkboxes' defaults available.
$allowed = array_unique(array_merge(array_keys(self::pageProcessOptions()), ['markdown', 'twig']));
$defaults = array_intersect_key($merged, array_flip($allowed));
} catch (\Throwable) {
// Conservative default already set above.
}
foreach ($defaults as $key => $val) {
$defaults[$key] = (bool) $val;
}
return $defaults;
}
/**
* Log when the security.twig_content.process_enabled gate blocks page-content
* Twig processing. Called from Page::content() and Page::processFrontmatter()
* paths. Deduped per-route per-request so a single page render emits one entry.
*/
public static function logTwigContentGateBlocked(string $route, string $source = 'content'): void
{
try {
$grav = Grav::instance();
if (!$grav->offsetExists('log.security')) {
return;
}
static $logged = [];
$key = $source . '|' . $route;
if (isset($logged[$key])) {
return;
}
$logged[$key] = true;
$grav['log.security']->warning(
sprintf('[TwigContentGate] blocked source=%s route=%s', $source, $route),
[
'source' => $source,
'route' => $route,
'hint' => 'Enable security.twig_content.process_enabled to allow Twig processing in page content.',
]
);
} catch (Exception) {
// Never let a logging failure break rendering.
}
}
private static function twigSandboxHint(string $rule, string $token, string $className): string
{
return match ($rule) {
'tag' => "To allow this tag, add '{$token}' to security.twig_sandbox.allowed_tags — OR disable the sandbox via security.twig_sandbox.enabled: false.",
'filter' => "To allow this filter, add '{$token}' to security.twig_sandbox.allowed_filters — OR disable the sandbox via security.twig_sandbox.enabled: false.",
'function' => "To allow this function, add '{$token}' to security.twig_sandbox.allowed_functions — OR disable the sandbox via security.twig_sandbox.enabled: false.",
'method' => "To allow this method, add '{$token}' under security.twig_sandbox.allowed_methods['{$className}'] — OR disable the sandbox via security.twig_sandbox.enabled: false.",
'property' => "To allow this property, add '{$token}' under security.twig_sandbox.allowed_properties['{$className}'] — OR disable the sandbox via security.twig_sandbox.enabled: false.",
default => 'Review the blocked Twig construct in logs/security.log.',
};
}
/**
* @param mixed $value
* @return list<string>
*/
private static function normalizeStringList($value): array
{
if (!is_array($value)) {
return [];
}
$out = [];
foreach ($value as $v) {
if (is_string($v) && $v !== '') {
$out[] = $v;
}
}
return $out;
}
/**
* Normalize a class => [members] map read from config. Accepts two input
* shapes for operator convenience:
*
* Nested map (hand-edited YAML):
* 'Grav\Common\Config\Config': [get, toarray]
*
* List-of-rows (admin UI list field; shipped as default):
* - class: 'Grav\Common\Config\Config'
* methods: 'get, toarray' # string OR list
*
* @param mixed $value
* @param bool $lowercase If true, lowercase every name (use for method
* allowlists to match Twig's case-insensitive
* method comparison). Properties are case-sensitive.
* @return array<class-string, list<string>>
*/
private static function normalizeMethodsMap($value, bool $lowercase = true): array
{
if (!is_array($value)) {
return [];
}
$out = [];
foreach ($value as $key => $entry) {
// List-of-rows row: each entry is ['class' => '...', 'methods' => '...']
if (is_int($key) && is_array($entry) && isset($entry['class'])) {
$class = (string) $entry['class'];
$methods = $entry['methods'] ?? [];
$clean = self::splitMethodNames($methods, $lowercase);
if ($class !== '' && $clean) {
$out[$class] = array_values(array_unique(array_merge($out[$class] ?? [], $clean)));
}
continue;
}
// Nested-map entry: key is the class, entry is the methods list
if (is_string($key) && is_array($entry)) {
$clean = self::splitMethodNames($entry, $lowercase);
if ($clean) {
$out[$key] = array_values(array_unique(array_merge($out[$key] ?? [], $clean)));
}
}
}
return $out;
}
/**
* Accept a methods list as either an array of strings, a CSV string, or
* a mix; return a flat list of member names.
*
* @param mixed $methods
* @return list<string>
*/
private static function splitMethodNames($methods, bool $lowercase = true): array
{
if (is_string($methods)) {
$methods = preg_split('/\s*,\s*/', trim($methods)) ?: [];
}
if (!is_array($methods)) {
return [];
}
$clean = [];
foreach ($methods as $m) {
if (is_string($m)) {
$m = trim($m);
if ($m !== '') {
$clean[] = $lowercase ? strtolower($m) : $m;
}
}
}
return $clean;
}
/** @var string|null in-process cache for the nonce key */
private static ?string $nonceKey = null;
/**
* Per-site HMAC key used for CSRF nonce signing, admin rate-limit key hashing,
* and (when configured) session-name derivation. Backed by a local PHP file
* outside the Config tree, so sandboxed Twig cannot reach it via
* `grav.config.get('security.salt')` or `Config::toArray()` (GHSA-3f29-pqwf-v4j4).
*
* Migration: if the legacy `security.salt` key is present in the loaded Config
* (i.e. from an older install's `user/config/security.yaml`), its value is
* copied into the private file on first call and scrubbed from both the live
* Config and the on-disk YAML. Existing CSRF nonces and sessions survive the
* upgrade because the key value is preserved.
*
* To rotate the key manually, delete `user/config/security-private.php`; the
* next request generates a fresh 64-char random value. Rotation invalidates
* in-flight CSRF nonces and — if `system.session.uniqueness` is set to
* `security` — existing sessions.
*/
public static function getNonceKey(): string
{
if (self::$nonceKey !== null) {
return self::$nonceKey;
}
$grav = Grav::instance();
/** @var UniformResourceLocator $locator */
$locator = $grav['locator'];
$configFolder = $locator->findResource('config://', true) ?: $locator->findResource('config://', true, true);
$privateFile = "{$configFolder}/security-private.php";
if (is_file($privateFile)) {
$value = @include $privateFile;
if (is_string($value) && $value !== '') {
return self::$nonceKey = $value;
}
// Corrupt/empty file — fall through to regenerate.
}
// One-time migration out of Config for sites upgrading from <= v2.0.0-beta.2.
/** @var Config $config */
$config = $grav['config'];
$legacy = $config->get('security.salt');
if (is_string($legacy) && $legacy !== '') {
self::writeNonceKey($privateFile, $legacy);
$config->set('security.salt', null);
$securityYaml = "{$configFolder}/security.yaml";
if (is_file($securityYaml)) {
$file = YamlFile::instance($securityYaml);
$content = (array) $file->content();
if (array_key_exists('salt', $content)) {
unset($content['salt']);
$file->content($content);
$file->save();
$file->free();
}
}
return self::$nonceKey = $legacy;
}
$generated = bin2hex(random_bytes(32));
self::writeNonceKey($privateFile, $generated);
return self::$nonceKey = $generated;
}
private static function writeNonceKey(string $path, string $value): void
{
$escaped = var_export($value, true);
$contents = "<?php\n\n// Auto-generated private secret. Do NOT commit to version control.\n// Used for CSRF nonce signing and admin rate-limit hashing. Regenerate by\n// deleting this file; the next request will write a new value.\n\nreturn {$escaped};\n";
$dir = dirname($path);
if (!is_dir($dir)) {
Folder::create($dir);
}
// Atomic write: stage to a temp file, fsync via rename.
$tmp = $path . '.tmp';
if (@file_put_contents($tmp, $contents, LOCK_EX) === false) {
throw new RuntimeException('Failed to write nonce key file');
}
@chmod($tmp, 0600);
if (!@rename($tmp, $path)) {
@unlink($tmp);
throw new RuntimeException('Failed to commit nonce key file');
}
}
}