ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer Class Reference

Standard implementation of the filename sanitizing interface. More...

+ Inheritance diagram for ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer:
+ Collaboration diagram for ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer:

Public Member Functions

 __construct (private FilesystemConfig $settings)
 
 isClean (string $filename)
 Checks if the filename is prefixed with a valid whitelisted ending. More...
 
 sanitize (string $filename)
 @inheritDoc More...
 
 isClean (string $filename)
 Checks if the filename is prefixed with a valid whitelisted ending. More...
 
 sanitize (string $filename)
 Validates the file ending, with the filesystem whitelist provided by ILIAS. More...
 

Private Member Functions

 getWhitelistedSuffixes ()
 
 extractFileSuffix (string $filename)
 Extracts the suffix from the given filename. More...
 

Private Attributes

array $whitelist = null
 

Additional Inherited Members

- Data Fields inherited from ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizer
const CLEAN_FILE_SUFFIX = 'sec'
 This file suffix will be used to sanitize not whitelisted file names. More...
 

Detailed Description

Standard implementation of the filename sanitizing interface.

Author
Nicolas Schäfli ns@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch
Fabian Schmid fabia.nosp@m.n@sr.nosp@m..solu.nosp@m.tion.nosp@m.s

Definition at line 32 of file DefaultFilenameSanitizer.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer::__construct ( private FilesystemConfig  $settings)

Definition at line 36 of file DefaultFilenameSanitizer.php.

38 {
39
40 }

Member Function Documentation

◆ extractFileSuffix()

ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer::extractFileSuffix ( string  $filename)
private

Extracts the suffix from the given filename.

If no suffix was found an empty string will be returned.

Parameters
string$filenameThe filename which should be used to extract the file suffix.
Returns
string The file name suffix in lowercase.

Definition at line 123 of file DefaultFilenameSanitizer.php.

123 : string
124 {
125 return strtolower(pathinfo($filename, PATHINFO_EXTENSION));
126 }
$filename
Definition: buildRTE.php:78

References $filename.

Referenced by ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer\isClean().

+ Here is the caller graph for this function:

◆ getWhitelistedSuffixes()

ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer::getWhitelistedSuffixes ( )
private

Definition at line 42 of file DefaultFilenameSanitizer.php.

42 : array
43 {
44 if ($this->whitelist !== null) {
45 return $this->whitelist;
46 }
47
48 $this->whitelist = array_diff($this->settings->getWhiteListedSuffixes(), $this->settings->getBlackListedSuffixes());
49
50 // the negative list does not apply to users holding the bypass permission, the
51 // same rule FileServicesPolicy::isBlockedExtension() follows. Only suffixes the
52 // negative list removed from the default or the positive list come back; the
53 // prohibited suffixes and the hard coded protection against php suffixes stay
54 // in effect, see https://mantis.ilias.de/view.php?id=47828
55 if ($this->settings->isByPassAllowedForCurrentUser()) {
56 $removed_by_negative_list = array_intersect(
57 $this->settings->getWhiteListNegative(),
58 array_merge($this->settings->getDefaultWhitelist(), $this->settings->getWhiteListPositive())
59 );
60 $this->whitelist = array_merge(
61 $this->whitelist,
62 array_diff($removed_by_negative_list, $this->settings->getProhibited())
63 );
64 }
65
66 // the secure file ending must be valid, therefore add it if it got removed from the white list.
67 if (!in_array(FilenameSanitizer::CLEAN_FILE_SUFFIX, $this->whitelist, true)) {
68 $this->whitelist[] = FilenameSanitizer::CLEAN_FILE_SUFFIX;
69 }
70 return $this->whitelist;
71 }
const CLEAN_FILE_SUFFIX
This file suffix will be used to sanitize not whitelisted file names.

References ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer\$whitelist, ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizer\CLEAN_FILE_SUFFIX, and ILIAS\Repository\settings().

Referenced by ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer\isClean().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isClean()

ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer::isClean ( string  $filename)

Checks if the filename is prefixed with a valid whitelisted ending.

Parameters
string$filenameThe filename which should be checked for a whitelisted ending.
Returns
bool True if the filename ending is whitelisted otherwise false.

Implements ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizer.

Definition at line 74 of file DefaultFilenameSanitizer.php.

74 : bool
75 {
76 // check the raw and the percent-decoded spelling; both must be acceptable
77 foreach ([$filename, rawurldecode($filename)] as $candidate) {
78 $suffix = $this->extractFileSuffix($candidate);
79 if (preg_match('/^ph(p[3457]?|t|tml|ar)$/i', $suffix)) {
80 return false;
81 }
82 if (!in_array($suffix, $this->getWhitelistedSuffixes(), true)) {
83 return false;
84 }
85 }
86
87 return true;
88 }
extractFileSuffix(string $filename)
Extracts the suffix from the given filename.

References $filename, ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer\extractFileSuffix(), and ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer\getWhitelistedSuffixes().

Referenced by ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer\sanitize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sanitize()

ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer::sanitize ( string  $filename)

@inheritDoc

Implements ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizer.

Definition at line 93 of file DefaultFilenameSanitizer.php.

93 : string
94 {
96
97 if ($this->isClean($filename)) {
98 return $filename;
99 }
100
101 $pathInfo = pathinfo($filename);
102 $basename = $pathInfo['basename'];
103 $parentPath = $pathInfo['dirname'] === '.' ? '' : $pathInfo['dirname'];
104
105 $filename = str_replace('.', '', $basename);
107
108 // there is no parent
109 if ($parentPath === '') {
110 return $filename;
111 }
112
113 return "$parentPath/$filename";
114 }
isClean(string $filename)
Checks if the filename is prefixed with a valid whitelisted ending.
static sanitizeFileName(string $filename)
Definition: Util.php:48

References $filename, ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizer\CLEAN_FILE_SUFFIX, ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer\isClean(), and ILIAS\Filesystem\Util\sanitizeFileName().

+ Here is the call graph for this function:

Field Documentation

◆ $whitelist

array ILIAS\Filesystem\Security\Sanitizing\DefaultFilenameSanitizer::$whitelist = null
private

The documentation for this class was generated from the following file: