1515use Config \Autoload ;
1616use Config \Modules ;
1717use InvalidArgumentException ;
18+ use RuntimeException ;
1819
1920/**
2021 * An autoloader that uses both PSR4 autoloading, and traditional classmaps.
@@ -290,9 +291,9 @@ protected function includeFile(string $file)
290291 }
291292
292293 /**
293- * Sanitizes a filename, replacing spaces with dashes .
294+ * Check file path .
294295 *
295- * Removes special characters that are illegal in filenames on certain
296+ * Checks special characters that are illegal in filenames on certain
296297 * operating systems and special characters requiring special escaping
297298 * to manipulate at the command line. Replaces spaces and consecutive
298299 * dashes with a single dash. Trim period, dash and underscore from beginning
@@ -306,10 +307,34 @@ public function sanitizeFilename(string $filename): string
306307 // Plus the forward slash for directory separators since this might be a path.
307308 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_278
308309 // Modified to allow backslash and colons for on Windows machines.
309- $ filename = preg_replace ('/[^0-9\p{L}\s\/\-\_\.\: \\\\]/u ' , '' , $ filename );
310+ $ result = preg_match_all ('/[^0-9\p{L}\s\/\-_.: \\\\]/u ' , $ filename , $ matches );
311+
312+ if ($ result > 0 ) {
313+ $ chars = implode ('' , $ matches [0 ]);
314+
315+ throw new InvalidArgumentException (
316+ 'The file path contains special characters " ' . $ chars
317+ . '" that are not allowed: " ' . $ filename . '" '
318+ );
319+ }
320+ if ($ result === false ) {
321+ if (version_compare (PHP_VERSION , '8.0.0 ' , '>= ' )) {
322+ $ message = preg_last_error_msg ();
323+ } else {
324+ $ message = 'Regex error. error code: ' . preg_last_error ();
325+ }
326+
327+ throw new RuntimeException ($ message . '. filename: " ' . $ filename . '" ' );
328+ }
310329
311330 // Clean up our filename edges.
312- return trim ($ filename , '.-_ ' );
331+ $ cleanFilename = trim ($ filename , '.-_ ' );
332+
333+ if ($ filename !== $ cleanFilename ) {
334+ throw new InvalidArgumentException ('The characters ".-_" are not allowed in filename edges: " ' . $ filename . '" ' );
335+ }
336+
337+ return $ cleanFilename ;
313338 }
314339
315340 private function loadComposerNamespaces (ClassLoader $ composer ): void
0 commit comments