Skip to content

Commit e7297f1

Browse files
committed
fix: "except empty" means "except all"
This behavior is unexpected and not good for security. If a dev removes all items in `except` key, the filter is disabled. Now "except empty" means "except nothing".
1 parent bc1f279 commit e7297f1

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

system/Filters/Filters.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ protected function processGlobals(?string $uri = null)
416416
if (isset($rules['except'])) {
417417
// grab the exclusion rules
418418
$check = $rules['except'];
419-
if ($this->pathApplies($uri, $check)) {
419+
if ($this->checkExcept($uri, $check)) {
420420
$keep = false;
421421
}
422422
}
@@ -550,4 +550,39 @@ private function pathApplies(string $uri, $paths)
550550

551551
return false;
552552
}
553+
554+
/**
555+
* Check except paths
556+
*
557+
* @param string $uri URI to check
558+
* @param array|string $paths The except path patterns
559+
*
560+
* @return bool True if the URI matches except paths.
561+
*/
562+
private function checkExcept(string $uri, $paths): bool
563+
{
564+
// empty path does not match anything
565+
if (empty($paths)) {
566+
return false;
567+
}
568+
569+
// make sure the paths are iterable
570+
if (is_string($paths)) {
571+
$paths = [$paths];
572+
}
573+
574+
// treat each paths as pseudo-regex
575+
foreach ($paths as $path) {
576+
// need to escape path separators
577+
$path = str_replace('/', '\/', trim($path, '/ '));
578+
// need to make pseudo wildcard real
579+
$path = strtolower(str_replace('*', '.*', $path));
580+
// Does this rule apply here?
581+
if (preg_match('#^' . $path . '$#', $uri, $match) === 1) {
582+
return true;
583+
}
584+
}
585+
586+
return false;
587+
}
553588
}

0 commit comments

Comments
 (0)