Skip to content

Commit f5dd368

Browse files
committed
Filename sanitization made optional
1 parent 45f03cf commit f5dd368

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
66

77
## [UNRELEASED]
88
### Fixed
9-
- NaN
9+
- Filename sanitization is now optional (enabled via default)
1010

1111
### Added
1212
- NaN

src/Attachment.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,17 +328,10 @@ public function decodeName(?string $name): string {
328328
$name = EncodingAliases::convert($name, $encoding);
329329
}
330330

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);
341333
}
334+
342335
return $name;
343336
}
344337
return "";
@@ -497,4 +490,26 @@ public function setDecoder(DecoderInterface $decoder): static {
497490
$this->decoder = $decoder;
498491
return $this;
499492
}
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+
}
500515
}

0 commit comments

Comments
 (0)