File tree Expand file tree Collapse file tree 2 files changed +26
-11
lines changed Expand file tree Collapse file tree 2 files changed +26
-11
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
6
6
7
7
## [ UNRELEASED]
8
8
### Fixed
9
- - NaN
9
+ - Filename sanitization is now optional (enabled via default)
10
10
11
11
### Added
12
12
- NaN
Original file line number Diff line number Diff line change @@ -328,17 +328,10 @@ public function decodeName(?string $name): string {
328
328
$ name = EncodingAliases::convert ($ name , $ encoding );
329
329
}
330
330
331
- // sanitize $name
332
- $ replaces = [
333
- '/ \\\\/ ' => '' ,
334
- '/[\/\0:]+/ ' => '' ,
335
- '/\.+/ ' => '. ' ,
336
- ];
337
- $ name_starts_with_dots = str_starts_with ($ name , '.. ' );
338
- $ name = preg_replace (array_keys ($ replaces ), array_values ($ replaces ), $ name );
339
- if ($ name_starts_with_dots ) {
340
- return substr ($ name , 1 );
331
+ if ($ this ->config ->get ('security.sanitize_filenames ' , true )) {
332
+ $ name = $ this ->sanitizeName ($ name );
341
333
}
334
+
342
335
return $ name ;
343
336
}
344
337
return "" ;
@@ -497,4 +490,26 @@ public function setDecoder(DecoderInterface $decoder): static {
497
490
$ this ->decoder = $ decoder ;
498
491
return $ this ;
499
492
}
493
+
494
+ /**
495
+ * Sanitize a given name to prevent common attacks
496
+ * !!IMPORTANT!! Do not rely on this method alone - this is just the bare minimum. Additional measures should be taken
497
+ * to ensure that the file is safe to use.
498
+ * @param string $name
499
+ *
500
+ * @return string
501
+ */
502
+ private function sanitizeName (string $ name ): string {
503
+ $ replaces = [
504
+ '/ \\\\/ ' => '' ,
505
+ '/[\/\0:]+/ ' => '' ,
506
+ '/\.+/ ' => '. ' ,
507
+ ];
508
+ $ name_starts_with_dots = str_starts_with ($ name , '.. ' );
509
+ $ name = preg_replace (array_keys ($ replaces ), array_values ($ replaces ), $ name );
510
+ if ($ name_starts_with_dots ) {
511
+ return substr ($ name , 1 );
512
+ }
513
+ return $ name ;
514
+ }
500
515
}
You can’t perform that action at this time.
0 commit comments