|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Module\casserver\Tests\Cas\Ticket; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use SimpleSAML\Configuration; |
| 10 | +use SimpleSAML\Module\casserver\Cas\Ticket\FileSystemTicketStore; |
| 11 | + |
| 12 | +final class FileSystemTicketStoreTest extends TestCase |
| 13 | +{ |
| 14 | + private string $ticketDir; |
| 15 | + |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + $base = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) |
| 22 | + . DIRECTORY_SEPARATOR |
| 23 | + . 'casserver-ticketstore-test-' |
| 24 | + . bin2hex(random_bytes(8)); |
| 25 | + |
| 26 | + if (!mkdir($base, 0700, true) && !is_dir($base)) { |
| 27 | + self::fail('Failed to create test directory: ' . $base); |
| 28 | + } |
| 29 | + |
| 30 | + $this->ticketDir = $base; |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + protected function tearDown(): void |
| 35 | + { |
| 36 | + $this->rmrf($this->ticketDir); |
| 37 | + parent::tearDown(); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + public function testPathTraversalCannotReadOrUnserializeOutsideTicketDirectory(): void |
| 42 | + { |
| 43 | + $outsideFile = dirname($this->ticketDir) . DIRECTORY_SEPARATOR . 'outside-' . bin2hex(random_bytes(6)) . '.ser'; |
| 44 | + file_put_contents($outsideFile, serialize(['pwn' => true])); |
| 45 | + |
| 46 | + $store = $this->createStore($this->ticketDir); |
| 47 | + |
| 48 | + $this->expectException(Exception::class); |
| 49 | + $this->expectExceptionMessage('Invalid ticketId provided.'); |
| 50 | + $store->getTicket('../' . basename($outsideFile)); |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + public function testValidateTicketPathThrowsOnEmptyTicketId(): void |
| 55 | + { |
| 56 | + $store = $this->createStore($this->ticketDir); |
| 57 | + |
| 58 | + $this->expectException(Exception::class); |
| 59 | + $this->expectExceptionMessage('Invalid ticketId provided.'); |
| 60 | + $this->invokeValidateTicketPath($store, ''); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + public function testValidateTicketPathThrowsOnForwardSlash(): void |
| 65 | + { |
| 66 | + $store = $this->createStore($this->ticketDir); |
| 67 | + |
| 68 | + $this->expectException(Exception::class); |
| 69 | + $this->expectExceptionMessage('Invalid ticketId provided.'); |
| 70 | + $this->invokeValidateTicketPath($store, 'ST-123/456'); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + public function testValidateTicketPathThrowsOnBackslash(): void |
| 75 | + { |
| 76 | + $store = $this->createStore($this->ticketDir); |
| 77 | + |
| 78 | + $this->expectException(Exception::class); |
| 79 | + $this->expectExceptionMessage('Invalid ticketId provided.'); |
| 80 | + $this->invokeValidateTicketPath($store, 'ST-123\\456'); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + public function testValidateTicketPathThrowsOnDotDot(): void |
| 85 | + { |
| 86 | + $store = $this->createStore($this->ticketDir); |
| 87 | + |
| 88 | + $this->expectException(Exception::class); |
| 89 | + $this->expectExceptionMessage('Invalid ticketId provided.'); |
| 90 | + $this->invokeValidateTicketPath($store, 'ST-..-123'); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + public function testValidateTicketPathThrowsWhenTicketStorageDirectoryIsNotAccessible(): void |
| 95 | + { |
| 96 | + $store = $this->createStore($this->ticketDir); |
| 97 | + |
| 98 | + $rp = new \ReflectionProperty($store, 'pathToTicketDirectory'); |
| 99 | + $rp->setAccessible(true); |
| 100 | + $rp->setValue($store, $this->ticketDir . DIRECTORY_SEPARATOR . 'does-not-exist-' . bin2hex(random_bytes(4))); |
| 101 | + |
| 102 | + $this->expectException(Exception::class); |
| 103 | + $this->expectExceptionMessage('Ticket storage directory is not accessible.'); |
| 104 | + $this->invokeValidateTicketPath($store, 'ST-12345'); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + public function testValidateTicketPathReturnsAFileInsideTicketDirectory(): void |
| 109 | + { |
| 110 | + $store = $this->createStore($this->ticketDir); |
| 111 | + |
| 112 | + $ticketId = 'ST-' . bin2hex(random_bytes(8)); |
| 113 | + $path = $this->invokeValidateTicketPath($store, $ticketId); |
| 114 | + |
| 115 | + $realBase = realpath($this->ticketDir); |
| 116 | + self::assertIsString($realBase); |
| 117 | + |
| 118 | + self::assertStringStartsWith($realBase . DIRECTORY_SEPARATOR, $path); |
| 119 | + self::assertSame($realBase . DIRECTORY_SEPARATOR . sha1($ticketId), $path); |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + private function createStore(string $ticketDir): FileSystemTicketStore |
| 124 | + { |
| 125 | + $config = Configuration::loadFromArray([ |
| 126 | + 'ticketstore' => [ |
| 127 | + 'directory' => $ticketDir, |
| 128 | + ], |
| 129 | + ]); |
| 130 | + |
| 131 | + return new FileSystemTicketStore($config); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + private function invokeValidateTicketPath(FileSystemTicketStore $store, string $ticketId): string |
| 136 | + { |
| 137 | + $rm = new \ReflectionMethod($store, 'validateTicketPath'); |
| 138 | + $rm->setAccessible(true); |
| 139 | + |
| 140 | + $result = $rm->invoke($store, $ticketId); |
| 141 | + self::assertIsString($result); |
| 142 | + |
| 143 | + return $result; |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + private function rmrf(string $path): void |
| 148 | + { |
| 149 | + if ($path === '' || !file_exists($path)) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + if (is_file($path) || is_link($path)) { |
| 154 | + @unlink($path); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + $items = scandir($path); |
| 159 | + if ($items === false) { |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + foreach ($items as $item) { |
| 164 | + if ($item === '.' || $item === '..') { |
| 165 | + continue; |
| 166 | + } |
| 167 | + $this->rmrf($path . DIRECTORY_SEPARATOR . $item); |
| 168 | + } |
| 169 | + |
| 170 | + @rmdir($path); |
| 171 | + } |
| 172 | +} |
0 commit comments