ILIAS  trunk Revision v11.0_alpha-1831-g8615d53dadb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizerImpl Class Reference

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

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

Public Member Functions

 __construct (private array $whitelist)
 FilenameSanitizerImpl constructor. More...
 
 isClean (string $filename)
 Checks if the filename is prefixed with a valid whitelisted ending. More...
 
 sanitize (string $filename)
 

Private Member Functions

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

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 31 of file FilenameSanitizerImpl.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizerImpl::__construct ( private array  $whitelist)

FilenameSanitizerImpl constructor.

Parameters
string[]$whitelist
Parameters
$whitelistContains the whitelisted file suffixes.

Definition at line 37 of file FilenameSanitizerImpl.php.

References ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizer\CLEAN_FILE_SUFFIX.

42  {
43  // the secure file ending must be valid, therefore add it if it got removed from the white list.
44  if (!in_array(FilenameSanitizer::CLEAN_FILE_SUFFIX, $this->whitelist, true)) {
45  $this->whitelist[] = FilenameSanitizer::CLEAN_FILE_SUFFIX;
46  }
47  }
const CLEAN_FILE_SUFFIX
This file suffix will be used to sanitize not whitelisted file names.

Member Function Documentation

◆ extractFileSuffix()

ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizerImpl::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 93 of file FilenameSanitizerImpl.php.

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

93  : string
94  {
95  return strtolower(pathinfo($filename, PATHINFO_EXTENSION));
96  }
$filename
Definition: buildRTE.php:78
+ Here is the caller graph for this function:

◆ isClean()

ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizerImpl::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 50 of file FilenameSanitizerImpl.php.

References ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizerImpl\extractFileSuffix().

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

50  : bool
51  {
52  $suffix = $this->extractFileSuffix($filename);
53  if (preg_match('/^ph(p[3457]?|t|tml|ar)$/i', $suffix)) {
54  return false;
55  }
56 
57  return in_array($suffix, $this->whitelist, true);
58  }
$filename
Definition: buildRTE.php:78
extractFileSuffix(string $filename)
Extracts the suffix from the given filename.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sanitize()

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

Implements ILIAS\Filesystem\Security\Sanitizing\FilenameSanitizer.

Definition at line 63 of file FilenameSanitizerImpl.php.

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

Referenced by ilFileUtils\getValidFilename().

63  : string
64  {
66 
67  if ($this->isClean($filename)) {
68  return $filename;
69  }
70 
71  $pathInfo = pathinfo($filename);
72  $basename = $pathInfo['basename'];
73  $parentPath = $pathInfo['dirname'] === '.' ? '' : $pathInfo['dirname'];
74 
75  $filename = str_replace('.', '', $basename);
77 
78  // there is no parent
79  if ($parentPath === '') {
80  return $filename;
81  }
82 
83  return "$parentPath/$filename";
84  }
static sanitizeFileName(string $filename)
Definition: Util.php:48
isClean(string $filename)
Checks if the filename is prefixed with a valid whitelisted ending.
const CLEAN_FILE_SUFFIX
This file suffix will be used to sanitize not whitelisted file names.
$filename
Definition: buildRTE.php:78
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

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