PharMetaDataInterceptor.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace TYPO3\PharStreamWrapper\Interceptor;
  3. /*
  4. * This file is part of the TYPO3 project.
  5. *
  6. * It is free software; you can redistribute it and/or modify it under the terms
  7. * of the MIT License (MIT). For the full copyright and license information,
  8. * please read the LICENSE file that was distributed with this source code.
  9. *
  10. * The TYPO3 project - inspiring people to share!
  11. */
  12. use TYPO3\PharStreamWrapper\Assertable;
  13. use TYPO3\PharStreamWrapper\Exception;
  14. use TYPO3\PharStreamWrapper\Manager;
  15. use TYPO3\PharStreamWrapper\Phar\DeserializationException;
  16. use TYPO3\PharStreamWrapper\Phar\Reader;
  17. /**
  18. * @internal Experimental implementation of checking against serialized objects in Phar meta-data
  19. * @internal This functionality has not been 100% pentested...
  20. */
  21. class PharMetaDataInterceptor implements Assertable
  22. {
  23. /**
  24. * Determines whether the according Phar archive contains
  25. * (potential insecure) serialized objects.
  26. *
  27. * @param string $path
  28. * @param string $command
  29. * @return bool
  30. * @throws Exception
  31. */
  32. public function assert($path, $command)
  33. {
  34. if ($this->baseFileDoesNotHaveMetaDataIssues($path)) {
  35. return true;
  36. }
  37. throw new Exception(
  38. sprintf(
  39. 'Problematic meta-data in "%s"',
  40. $path
  41. ),
  42. 1539632368
  43. );
  44. }
  45. /**
  46. * @param string $path
  47. * @return bool
  48. */
  49. private function baseFileDoesNotHaveMetaDataIssues($path)
  50. {
  51. $invocation = Manager::instance()->resolve($path);
  52. if ($invocation === null) {
  53. return false;
  54. }
  55. // directly return in case invocation was checked before
  56. if ($invocation->getVariable(__CLASS__) === true) {
  57. return true;
  58. }
  59. // otherwise analyze meta-data
  60. try {
  61. $reader = new Reader($invocation->getBaseName());
  62. $reader->resolveContainer()->getManifest()->deserializeMetaData();
  63. $invocation->setVariable(__CLASS__, true);
  64. } catch (DeserializationException $exception) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. }