|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the TYPO3 CMS project. |
| 7 | + * |
| 8 | + * It is free software; you can redistribute it and/or modify it under |
| 9 | + * the terms of the GNU General Public License, either version 2 |
| 10 | + * of the License, or any later version. |
| 11 | + * |
| 12 | + * For the full copyright and license information, please read the |
| 13 | + * LICENSE.txt file that was distributed with this source code. |
| 14 | + * |
| 15 | + * The TYPO3 project - inspiring people to share! |
| 16 | + */ |
| 17 | + |
| 18 | +namespace TYPO3\CMS\Core\Serializer; |
| 19 | + |
| 20 | +use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; |
| 21 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 22 | +use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend; |
| 23 | +use TYPO3\CMS\Core\Crypto\HashService; |
| 24 | +use TYPO3\CMS\Core\Security\BlockSerializationTrait; |
| 25 | +use TYPO3\CMS\Core\Serializer\Exception\DeserializerException; |
| 26 | + |
| 27 | +/** |
| 28 | + * Deserializes a PHP-serialized payload while refusing any class that carries |
| 29 | + * a user-defined __destruct() or an exploitable __wakeup() (one not provided |
| 30 | + * solely by BlockSerializationTrait). |
| 31 | + * |
| 32 | + * The per-class deny/allow decision is made lazily via ReflectionClass at the |
| 33 | + * first encounter of each class name, then cached in cache:core so that |
| 34 | + * reflection is never repeated for the same class within a cache lifetime. |
| 35 | + * |
| 36 | + * Use this instead of a raw unserialize() call when the set of expected classes |
| 37 | + * is not known upfront but dangerous gadget classes must still be excluded. |
| 38 | + * |
| 39 | + * @internal Only to be used by TYPO3 core |
| 40 | + */ |
| 41 | +#[Autoconfigure(public: true)] |
| 42 | +final readonly class DenyListDeserializer |
| 43 | +{ |
| 44 | + /** |
| 45 | + * @var list<string> |
| 46 | + */ |
| 47 | + private array $allowedClassNames; |
| 48 | + private \ReflectionMethod $blockSerializationWakeup; |
| 49 | + |
| 50 | + public function __construct( |
| 51 | + #[Autowire(service: 'cache.core')] |
| 52 | + private PhpFrontend $cache, |
| 53 | + private HashService $hashService, |
| 54 | + private DeserializationService $deserializationService, |
| 55 | + ) { |
| 56 | + $allowedClassNames = $GLOBALS['TYPO3_CONF_VARS']['SYS']['deserialization']['allowedClassNames'] ?? null; |
| 57 | + $this->allowedClassNames = is_array($allowedClassNames) ? $allowedClassNames : []; |
| 58 | + $this->blockSerializationWakeup = (new \ReflectionClass(BlockSerializationTrait::class))->getMethod('__wakeup'); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Deserializes $payload, throwing DeserializerException if any class name |
| 63 | + * found in the payload is a deserialization gadget, or if the payload is |
| 64 | + * syntactically malformed. |
| 65 | + */ |
| 66 | + public function deserialize(string $payload): mixed |
| 67 | + { |
| 68 | + $classNames = $this->deserializationService->parseClassNames($payload); |
| 69 | + foreach ($classNames as $className) { |
| 70 | + if ($this->shallClassBeDenied($className)) { |
| 71 | + throw new DeserializerException( |
| 72 | + 'Denied class name "' . $className . '" found in payload', |
| 73 | + 1778594101 |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return $this->deserializationService->deserialize($payload, $classNames ?: false); |
| 79 | + } |
| 80 | + |
| 81 | + private function shallClassBeDenied(string $className): bool |
| 82 | + { |
| 83 | + if (in_array($className, $this->allowedClassNames, true)) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + $cacheKey = 'DenyListDeserializer_' . hash('xxh128', $className); |
| 88 | + if ($this->cache->has($cacheKey)) { |
| 89 | + $entry = $this->cache->require($cacheKey); |
| 90 | + if (is_array($entry) |
| 91 | + && isset($entry['denied'], $entry['hmac']) |
| 92 | + && $this->hashService->validateHmac( |
| 93 | + $this->createHmacPayload($className, (bool)$entry['denied']), |
| 94 | + DenyListDeserializer::class, |
| 95 | + $entry['hmac'] |
| 96 | + ) |
| 97 | + ) { |
| 98 | + return (bool)$entry['denied']; |
| 99 | + } |
| 100 | + // Tampered or stale entry — fall through to recompute |
| 101 | + } |
| 102 | + |
| 103 | + $denied = $this->resolveClassDenyStatus($className); |
| 104 | + $hmac = $this->hashService->hmac($this->createHmacPayload($className, $denied), DenyListDeserializer::class); |
| 105 | + $this->cache->set($cacheKey, 'return ' . var_export(['denied' => $denied, 'hmac' => $hmac], true) . ';'); |
| 106 | + return $denied; |
| 107 | + } |
| 108 | + |
| 109 | + private function createHmacPayload(string $className, bool $denied): string |
| 110 | + { |
| 111 | + return $className . ':' . ($denied ? '1' : '0'); |
| 112 | + } |
| 113 | + |
| 114 | + private function resolveClassDenyStatus(string $className): bool |
| 115 | + { |
| 116 | + try { |
| 117 | + $rc = new \ReflectionClass($className); |
| 118 | + } catch (\ReflectionException) { |
| 119 | + // The class does not exist or cannot be reflected (and not instantiated). |
| 120 | + // Thus, the class is allowed, since it cannot be a gadget and would |
| 121 | + // result in a `__PHP_Incomplete_Class` during deserialization. |
| 122 | + return false; |
| 123 | + } |
| 124 | + if ($rc->isInterface() || $rc->isTrait()) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + return $this->getUserDefinedMethod($rc, '__destruct') !== null |
| 128 | + || $this->hasDeniableWakeupMethod($rc); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns the method when $methodName is declared in user-defined (non-internal) code |
| 133 | + * somewhere in the class hierarchy. This excludes methods like Exception::__wakeup() |
| 134 | + * that PHP declares internally and that are harmless for deserialization purposes. |
| 135 | + */ |
| 136 | + private function getUserDefinedMethod(\ReflectionClass $rc, string $methodName): ?\ReflectionMethod |
| 137 | + { |
| 138 | + if (!$rc->hasMethod($methodName)) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + $method = $rc->getMethod($methodName); |
| 142 | + if ($method->getDeclaringClass()->isInternal()) { |
| 143 | + return null; |
| 144 | + } |
| 145 | + return $method; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Returns true when the class has a user-defined __wakeup() that is NOT |
| 150 | + * BlockSerializationTrait::__wakeup(). Classes whose only __wakeup comes |
| 151 | + * from BlockSerializationTrait are already protected against deserialization |
| 152 | + * (the trait throws unconditionally) and must not be treated as gadgets. |
| 153 | + * |
| 154 | + * Note: for trait methods getDeclaringClass() returns the using class, not the |
| 155 | + * trait — so the origin is identified by comparing the method's source file and line |
| 156 | + * against the trait's own __wakeup declaration. |
| 157 | + */ |
| 158 | + private function hasDeniableWakeupMethod(\ReflectionClass $rc): bool |
| 159 | + { |
| 160 | + $method = $this->getUserDefinedMethod($rc, '__wakeup'); |
| 161 | + if ($method === null) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + return $method->getFileName() !== $this->blockSerializationWakeup->getFileName() |
| 165 | + || $method->getStartLine() !== $this->blockSerializationWakeup->getStartLine(); |
| 166 | + } |
| 167 | +} |
0 commit comments