Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 30 | class Driver implements ExtendedCacheItemPoolInterface |
||
| 31 | { |
||
| 32 | use DriverBaseTrait, PathSeekerTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * |
||
| 36 | */ |
||
| 37 | const FILE_DIR = 'files'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Driver constructor. |
||
| 41 | * @param array $config |
||
| 42 | * @throws phpFastCacheDriverException |
||
| 43 | */ |
||
| 44 | public function __construct(array $config = []) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | public function driverCheck() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 63 | * @return mixed |
||
| 64 | * @throws \InvalidArgumentException |
||
| 65 | */ |
||
| 66 | protected function driverWrite(CacheItemInterface $item) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 90 | * @return mixed |
||
| 91 | */ |
||
| 92 | protected function driverRead(CacheItemInterface $item) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param \Psr\Cache\CacheItemInterface $item |
||
| 110 | * @return bool |
||
| 111 | * @throws \InvalidArgumentException |
||
| 112 | */ |
||
| 113 | protected function driverDelete(CacheItemInterface $item) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | protected function driverClear() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | protected function driverConnect() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $optionName |
||
| 148 | * @param mixed $optionValue |
||
| 149 | * @return bool |
||
| 150 | * @throws \InvalidArgumentException |
||
| 151 | */ |
||
| 152 | public static function isValidOption($optionName, $optionValue) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public static function getValidOptions() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public static function getRequiredOptions() |
||
| 191 | |||
| 192 | /******************** |
||
| 193 | * |
||
| 194 | * PSR-6 Extended Methods |
||
| 195 | * |
||
| 196 | *******************/ |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return driverStatistic |
||
| 200 | * @throws \phpFastCache\Exceptions\phpFastCacheCoreException |
||
| 201 | * @throws \phpFastCache\Exceptions\phpFastCacheDriverException |
||
| 202 | */ |
||
| 203 | View Code Duplication | public function getStats() |
|
| 219 | } |
$this->getFileDir()can contain request data and is used in file manipulation context(s) leading to a potential security vulnerability.1 path for user data to reach this point
HTTP_HOSTfrom$_SERVER,and$_SERVER['HTTP_HOST']is passed through str_replace(), andstr_replace(':', '_', $_SERVER['HTTP_HOST'])is passed through strtolower(), andstrtolower(str_replace(':', '_', $_SERVER['HTTP_HOST']))is passed through preg_replace(), and$securityKeyis assignedin src/phpFastCache/Core/Pool/IO/PathSeekerTrait.php on line 60
in vendor/src/phpFastCache/Core/Pool/IO/PathSeekerTrait.php on line 194
$securityKeyis assignedin src/phpFastCache/Core/Pool/IO/PathSeekerTrait.php on line 70
$full_pathis assignedin src/phpFastCache/Core/Pool/IO/PathSeekerTrait.php on line 87
$full_pathis passed through realpath()in src/phpFastCache/Core/Pool/IO/PathSeekerTrait.php on line 104
in src/phpFastCache/Core/Pool/IO/PathSeekerTrait.php on line 133
in src/phpFastCache/Drivers/Files/Driver.php on line 58
General Strategies to prevent injection
In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) { throw new \InvalidArgumentException('This input is not allowed.'); }For numeric data, we recommend to explicitly cast the data: