| Total Complexity | 55 |
| Total Lines | 521 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Checker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Checker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class Checker { |
||
| 55 | const CACHE_KEY = 'oc.integritycheck.checker'; |
||
| 56 | /** @var EnvironmentHelper */ |
||
| 57 | private $environmentHelper; |
||
| 58 | /** @var AppLocator */ |
||
| 59 | private $appLocator; |
||
| 60 | /** @var FileAccessHelper */ |
||
| 61 | private $fileAccessHelper; |
||
| 62 | /** @var IConfig */ |
||
| 63 | private $config; |
||
| 64 | /** @var ICache */ |
||
| 65 | private $cache; |
||
| 66 | /** @var IAppManager */ |
||
| 67 | private $appManager; |
||
| 68 | /** @var ITempManager */ |
||
| 69 | private $tempManager; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param EnvironmentHelper $environmentHelper |
||
| 73 | * @param FileAccessHelper $fileAccessHelper |
||
| 74 | * @param AppLocator $appLocator |
||
| 75 | * @param IConfig $config |
||
| 76 | * @param ICacheFactory $cacheFactory |
||
| 77 | * @param IAppManager $appManager |
||
| 78 | * @param ITempManager $tempManager |
||
| 79 | */ |
||
| 80 | public function __construct(EnvironmentHelper $environmentHelper, |
||
| 81 | FileAccessHelper $fileAccessHelper, |
||
| 82 | AppLocator $appLocator, |
||
| 83 | IConfig $config = null, |
||
| 84 | ICacheFactory $cacheFactory, |
||
| 85 | IAppManager $appManager = null, |
||
| 86 | ITempManager $tempManager) { |
||
| 87 | $this->environmentHelper = $environmentHelper; |
||
| 88 | $this->fileAccessHelper = $fileAccessHelper; |
||
| 89 | $this->appLocator = $appLocator; |
||
| 90 | $this->config = $config; |
||
| 91 | $this->cache = $cacheFactory->createDistributed(self::CACHE_KEY); |
||
| 92 | $this->appManager = $appManager; |
||
| 93 | $this->tempManager = $tempManager; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Whether code signing is enforced or not. |
||
| 98 | * |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public function isCodeCheckEnforced(): bool { |
||
| 102 | $notSignedChannels = [ '', 'git']; |
||
| 103 | if (\in_array($this->environmentHelper->getChannel(), $notSignedChannels, true)) { |
||
| 104 | return false; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * This config option is undocumented and supposed to be so, it's only |
||
| 109 | * applicable for very specific scenarios and we should not advertise it |
||
| 110 | * too prominent. So please do not add it to config.sample.php. |
||
| 111 | */ |
||
| 112 | $isIntegrityCheckDisabled = false; |
||
| 113 | if ($this->config !== null) { |
||
| 114 | $isIntegrityCheckDisabled = $this->config->getSystemValue('integrity.check.disabled', false); |
||
| 115 | } |
||
| 116 | if ($isIntegrityCheckDisabled === true) { |
||
| 117 | return false; |
||
| 118 | } |
||
| 119 | |||
| 120 | return true; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Enumerates all files belonging to the folder. Sensible defaults are excluded. |
||
| 125 | * |
||
| 126 | * @param string $folderToIterate |
||
| 127 | * @param string $root |
||
| 128 | * @return \RecursiveIteratorIterator |
||
| 129 | * @throws \Exception |
||
| 130 | */ |
||
| 131 | private function getFolderIterator(string $folderToIterate, string $root = ''): \RecursiveIteratorIterator { |
||
| 147 | ); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns an array of ['filename' => 'SHA512-hash-of-file'] for all files found |
||
| 152 | * in the iterator. |
||
| 153 | * |
||
| 154 | * @param \RecursiveIteratorIterator $iterator |
||
| 155 | * @param string $path |
||
| 156 | * @return array Array of hashes. |
||
| 157 | */ |
||
| 158 | private function generateHashes(\RecursiveIteratorIterator $iterator, |
||
| 159 | string $path): array { |
||
| 160 | $hashes = []; |
||
| 161 | |||
| 162 | $baseDirectoryLength = \strlen($path); |
||
| 163 | foreach($iterator as $filename => $data) { |
||
| 164 | /** @var \DirectoryIterator $data */ |
||
| 165 | if($data->isDir()) { |
||
| 166 | continue; |
||
| 167 | } |
||
| 168 | |||
| 169 | $relativeFileName = substr($filename, $baseDirectoryLength); |
||
| 170 | $relativeFileName = ltrim($relativeFileName, '/'); |
||
| 171 | |||
| 172 | // Exclude signature.json files in the appinfo and root folder |
||
| 173 | if($relativeFileName === 'appinfo/signature.json') { |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | // Exclude signature.json files in the appinfo and core folder |
||
| 177 | if($relativeFileName === 'core/signature.json') { |
||
| 178 | continue; |
||
| 179 | } |
||
| 180 | |||
| 181 | // The .htaccess file in the root folder of ownCloud can contain |
||
| 182 | // custom content after the installation due to the fact that dynamic |
||
| 183 | // content is written into it at installation time as well. This |
||
| 184 | // includes for example the 404 and 403 instructions. |
||
| 185 | // Thus we ignore everything below the first occurrence of |
||
| 186 | // "#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####" and have the |
||
| 187 | // hash generated based on this. |
||
| 188 | if($filename === $this->environmentHelper->getServerRoot() . '/.htaccess') { |
||
| 189 | $fileContent = file_get_contents($filename); |
||
| 190 | $explodedArray = explode('#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####', $fileContent); |
||
| 191 | if(\count($explodedArray) === 2) { |
||
| 192 | $hashes[$relativeFileName] = hash('sha512', $explodedArray[0]); |
||
| 193 | continue; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | $hashes[$relativeFileName] = hash_file('sha512', $filename); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $hashes; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Creates the signature data |
||
| 205 | * |
||
| 206 | * @param array $hashes |
||
| 207 | * @param X509 $certificate |
||
| 208 | * @param RSA $privateKey |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | private function createSignatureData(array $hashes, |
||
| 212 | X509 $certificate, |
||
| 213 | RSA $privateKey): array { |
||
| 214 | ksort($hashes); |
||
| 215 | |||
| 216 | $privateKey->setSignatureMode(RSA::SIGNATURE_PSS); |
||
| 217 | $privateKey->setMGFHash('sha512'); |
||
| 218 | // See https://tools.ietf.org/html/rfc3447#page-38 |
||
| 219 | $privateKey->setSaltLength(0); |
||
| 220 | $signature = $privateKey->sign(json_encode($hashes)); |
||
| 221 | |||
| 222 | return [ |
||
| 223 | 'hashes' => $hashes, |
||
| 224 | 'signature' => base64_encode($signature), |
||
| 225 | 'certificate' => $certificate->saveX509($certificate->currentCert), |
||
| 226 | ]; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Write the signature of the app in the specified folder |
||
| 231 | * |
||
| 232 | * @param string $path |
||
| 233 | * @param X509 $certificate |
||
| 234 | * @param RSA $privateKey |
||
| 235 | * @throws \Exception |
||
| 236 | */ |
||
| 237 | public function writeAppSignature($path, |
||
| 238 | X509 $certificate, |
||
| 239 | RSA $privateKey) { |
||
| 240 | $appInfoDir = $path . '/appinfo'; |
||
| 241 | try { |
||
| 242 | $this->fileAccessHelper->assertDirectoryExists($appInfoDir); |
||
| 243 | |||
| 244 | $iterator = $this->getFolderIterator($path); |
||
| 245 | $hashes = $this->generateHashes($iterator, $path); |
||
| 246 | $signature = $this->createSignatureData($hashes, $certificate, $privateKey); |
||
| 247 | $this->fileAccessHelper->file_put_contents( |
||
| 248 | $appInfoDir . '/signature.json', |
||
| 249 | json_encode($signature, JSON_PRETTY_PRINT) |
||
| 250 | ); |
||
| 251 | } catch (\Exception $e){ |
||
| 252 | if (!$this->fileAccessHelper->is_writable($appInfoDir)) { |
||
| 253 | throw new \Exception($appInfoDir . ' is not writable'); |
||
| 254 | } |
||
| 255 | throw $e; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Write the signature of core |
||
| 261 | * |
||
| 262 | * @param X509 $certificate |
||
| 263 | * @param RSA $rsa |
||
| 264 | * @param string $path |
||
| 265 | * @throws \Exception |
||
| 266 | */ |
||
| 267 | public function writeCoreSignature(X509 $certificate, |
||
| 268 | RSA $rsa, |
||
| 269 | $path) { |
||
| 270 | $coreDir = $path . '/core'; |
||
| 271 | try { |
||
| 272 | |||
| 273 | $this->fileAccessHelper->assertDirectoryExists($coreDir); |
||
| 274 | $iterator = $this->getFolderIterator($path, $path); |
||
| 275 | $hashes = $this->generateHashes($iterator, $path); |
||
| 276 | $signatureData = $this->createSignatureData($hashes, $certificate, $rsa); |
||
| 277 | $this->fileAccessHelper->file_put_contents( |
||
| 278 | $coreDir . '/signature.json', |
||
| 279 | json_encode($signatureData, JSON_PRETTY_PRINT) |
||
| 280 | ); |
||
| 281 | } catch (\Exception $e){ |
||
| 282 | if (!$this->fileAccessHelper->is_writable($coreDir)) { |
||
| 283 | throw new \Exception($coreDir . ' is not writable'); |
||
| 284 | } |
||
| 285 | throw $e; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Verifies the signature for the specified path. |
||
| 291 | * |
||
| 292 | * @param string $signaturePath |
||
| 293 | * @param string $basePath |
||
| 294 | * @param string $certificateCN |
||
| 295 | * @return array |
||
| 296 | * @throws InvalidSignatureException |
||
| 297 | * @throws \Exception |
||
| 298 | */ |
||
| 299 | private function verify(string $signaturePath, string $basePath, string $certificateCN): array { |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Whether the code integrity check has passed successful or not |
||
| 395 | * |
||
| 396 | * @return bool |
||
| 397 | */ |
||
| 398 | public function hasPassedCheck(): bool { |
||
| 399 | $results = $this->getResults(); |
||
| 400 | if(empty($results)) { |
||
| 401 | return true; |
||
| 402 | } |
||
| 403 | |||
| 404 | return false; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | public function getResults(): array { |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Stores the results in the app config as well as cache |
||
| 424 | * |
||
| 425 | * @param string $scope |
||
| 426 | * @param array $result |
||
| 427 | */ |
||
| 428 | private function storeResults(string $scope, array $result) { |
||
| 429 | $resultArray = $this->getResults(); |
||
| 430 | unset($resultArray[$scope]); |
||
| 431 | if(!empty($result)) { |
||
| 432 | $resultArray[$scope] = $result; |
||
| 433 | } |
||
| 434 | if ($this->config !== null) { |
||
| 435 | $this->config->setAppValue('core', self::CACHE_KEY, json_encode($resultArray)); |
||
| 436 | } |
||
| 437 | $this->cache->set(self::CACHE_KEY, json_encode($resultArray)); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * |
||
| 442 | * Clean previous results for a proper rescanning. Otherwise |
||
| 443 | */ |
||
| 444 | private function cleanResults() { |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Verify the signature of $appId. Returns an array with the following content: |
||
| 451 | * [ |
||
| 452 | * 'FILE_MISSING' => |
||
| 453 | * [ |
||
| 454 | * 'filename' => [ |
||
| 455 | * 'expected' => 'expectedSHA512', |
||
| 456 | * 'current' => 'currentSHA512', |
||
| 457 | * ], |
||
| 458 | * ], |
||
| 459 | * 'EXTRA_FILE' => |
||
| 460 | * [ |
||
| 461 | * 'filename' => [ |
||
| 462 | * 'expected' => 'expectedSHA512', |
||
| 463 | * 'current' => 'currentSHA512', |
||
| 464 | * ], |
||
| 465 | * ], |
||
| 466 | * 'INVALID_HASH' => |
||
| 467 | * [ |
||
| 468 | * 'filename' => [ |
||
| 469 | * 'expected' => 'expectedSHA512', |
||
| 470 | * 'current' => 'currentSHA512', |
||
| 471 | * ], |
||
| 472 | * ], |
||
| 473 | * ] |
||
| 474 | * |
||
| 475 | * Array may be empty in case no problems have been found. |
||
| 476 | * |
||
| 477 | * @param string $appId |
||
| 478 | * @param string $path Optional path. If none is given it will be guessed. |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | public function verifyAppSignature(string $appId, string $path = ''): array { |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Verify the signature of core. Returns an array with the following content: |
||
| 506 | * [ |
||
| 507 | * 'FILE_MISSING' => |
||
| 508 | * [ |
||
| 509 | * 'filename' => [ |
||
| 510 | * 'expected' => 'expectedSHA512', |
||
| 511 | * 'current' => 'currentSHA512', |
||
| 512 | * ], |
||
| 513 | * ], |
||
| 514 | * 'EXTRA_FILE' => |
||
| 515 | * [ |
||
| 516 | * 'filename' => [ |
||
| 517 | * 'expected' => 'expectedSHA512', |
||
| 518 | * 'current' => 'currentSHA512', |
||
| 519 | * ], |
||
| 520 | * ], |
||
| 521 | * 'INVALID_HASH' => |
||
| 522 | * [ |
||
| 523 | * 'filename' => [ |
||
| 524 | * 'expected' => 'expectedSHA512', |
||
| 525 | * 'current' => 'currentSHA512', |
||
| 526 | * ], |
||
| 527 | * ], |
||
| 528 | * ] |
||
| 529 | * |
||
| 530 | * Array may be empty in case no problems have been found. |
||
| 531 | * |
||
| 532 | * @return array |
||
| 533 | */ |
||
| 534 | public function verifyCoreSignature(): array { |
||
| 535 | try { |
||
| 536 | $result = $this->verify( |
||
| 537 | $this->environmentHelper->getServerRoot() . '/core/signature.json', |
||
| 538 | $this->environmentHelper->getServerRoot(), |
||
| 539 | 'core' |
||
| 540 | ); |
||
| 541 | } catch (\Exception $e) { |
||
| 542 | $result = [ |
||
| 543 | 'EXCEPTION' => [ |
||
| 544 | 'class' => \get_class($e), |
||
| 545 | 'message' => $e->getMessage(), |
||
| 546 | ], |
||
| 547 | ]; |
||
| 548 | } |
||
| 549 | $this->storeResults('core', $result); |
||
| 550 | |||
| 551 | return $result; |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Verify the core code of the instance as well as all applicable applications |
||
| 556 | * and store the results. |
||
| 557 | */ |
||
| 558 | public function runInstanceVerification() { |
||
| 575 | } |
||
| 576 | } |
||
| 579 |