Skip to content

Commit ec90fcf

Browse files
committed
Improve traversal detection code.
1 parent 8bf959d commit ec90fcf

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,4 @@ __*Why "v3.0.0" instead of "v1.0.0?"*__ Prior to phpMussel v3, the "phpMussel Co
214214

215215
- [2025.08.31]: Slightly reworked the various front-end themes.
216216
- [2025.08.31]: Slightly reworked the logs page and the configuration page.
217+
- [2025.09.03]: Slightly improved the code for traversal detection.

src/FrontEnd.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: Front-end handler (last modified: 2025.09.01).
11+
* This file: Front-end handler (last modified: 2025.09.03).
1212
*/
1313

1414
namespace phpMussel\FrontEnd;
@@ -859,6 +859,20 @@ public function logsRecursiveList(): array
859859
return $Items;
860860
}
861861

862+
/**
863+
* Traversal detection.
864+
*
865+
* @param string $Path The path to check for traversal.
866+
* @return bool True when the path is traversal-free. False when traversal has been detected.
867+
*/
868+
public function freeFromTraversal(string $Path): bool
869+
{
870+
return !preg_match(
871+
'~//|(?:[^\da-z\p{L}\p{N}\p{M}\p{P}\p{S}\p{Z}.]|[\\/?&=]|^)\.\.+(?:[^\da-z\p{L}\p{N}\p{M}\p{P}\p{S}\p{Z}.]|[\\/?&=]|$)|/\.+(?:[^\da-z\p{L}\p{N}\p{M}\p{P}\p{S}\p{Z}.]|[\\/?&=]|$)|(?:[^\da-z\p{L}\p{N}\p{M}\p{P}\p{S}\p{Z}.]|[\\/?&=])\.+/|[\x01-\x1F]~i',
872+
str_ireplace(['%25', '%22', '%27', '%2e', '%2f', '%5b', '%5c', '%5d', '%5e', '%5f', '%60', '\\'], ['%', '"', '\'', '.', '/', '[', '/', ']', '^', '_', '`', '/'], $Path)
873+
);
874+
}
875+
862876
/**
863877
* Format filesize information.
864878
*
@@ -899,20 +913,6 @@ private function filterByDefined(string $ChoiceKey): bool
899913
return defined($ChoiceKey);
900914
}
901915

902-
/**
903-
* Traversal detection.
904-
*
905-
* @param string $Path The path to check for traversal.
906-
* @return bool True when the path is traversal-free. False when traversal has been detected.
907-
*/
908-
private function freeFromTraversal(string $Path): bool
909-
{
910-
return !preg_match(
911-
'~(?://|(?<![\da-z])\.\.(?![\da-z])|/\.(?![\da-z])|(?<![\da-z])\./|[\x01-\x1F\[-^`?*$])~i',
912-
str_replace('\\', '/', $Path)
913-
);
914-
}
915-
916916
/**
917917
* Get the appropriate path for a specified asset as per the defined theme.
918918
*

0 commit comments

Comments
 (0)