|
21 | 21 | use Slim\Tests\TestCase; |
22 | 22 | use stdClass; |
23 | 23 |
|
| 24 | +use function bin2hex; |
| 25 | +use function file_put_contents; |
| 26 | +use function get_class; |
24 | 27 | use function htmlspecialchars; |
25 | 28 | use function json_decode; |
26 | 29 | use function json_encode; |
| 30 | +use function mkdir; |
| 31 | +use function random_bytes; |
| 32 | +use function rmdir; |
27 | 33 | use function simplexml_load_string; |
| 34 | +use function sys_get_temp_dir; |
| 35 | +use function unlink; |
28 | 36 |
|
29 | 37 | use const ENT_QUOTES; |
30 | 38 | use const ENT_SUBSTITUTE; |
@@ -76,19 +84,68 @@ public function testHTMLErrorRendererRenderFragmentMethod() |
76 | 84 |
|
77 | 85 | public function testHTMLErrorRendererEscapesQuotesInErrorDetails() |
78 | 86 | { |
79 | | - $exception = new Exception("O'Brien <script>"); |
| 87 | + $unsafe = "O'Brien <script>"; |
| 88 | + $escaped = htmlspecialchars($unsafe, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
| 89 | + |
| 90 | + $exception = new class ($unsafe) extends Exception { |
| 91 | + public function __construct(string $unsafe) |
| 92 | + { |
| 93 | + parent::__construct($unsafe, 0); |
| 94 | + $this->file = '/tmp/' . $unsafe . '.php'; |
| 95 | + $this->code = "SQL'" . $unsafe; |
| 96 | + $this->line = 7; |
| 97 | + } |
| 98 | + }; |
| 99 | + |
80 | 100 | $renderer = new HtmlErrorRenderer(); |
81 | 101 | $reflectionRenderer = new ReflectionClass(HtmlErrorRenderer::class); |
82 | 102 |
|
83 | 103 | $method = $reflectionRenderer->getMethod('renderExceptionFragment'); |
84 | 104 | $this->setAccessible($method); |
85 | 105 | $output = $method->invoke($renderer, $exception); |
86 | 106 |
|
| 107 | + $this->assertStringNotContainsString($unsafe, $output); |
| 108 | + $this->assertStringContainsString($escaped, $output); |
87 | 109 | $this->assertStringContainsString( |
88 | | - htmlspecialchars("O'Brien <script>", ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'), |
89 | | - $output, |
90 | | - 'Message must be HTML-escaped including quotes' |
| 110 | + htmlspecialchars('/tmp/' . $unsafe . '.php', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'), |
| 111 | + $output |
91 | 112 | ); |
| 113 | + $this->assertStringContainsString( |
| 114 | + htmlspecialchars("SQL'" . $unsafe, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'), |
| 115 | + $output |
| 116 | + ); |
| 117 | + $this->assertStringContainsString('<div><strong>Line:</strong> 7</div>', $output); |
| 118 | + } |
| 119 | + |
| 120 | + public function testHTMLErrorRendererEscapesAnonymousClassType() |
| 121 | + { |
| 122 | + $unsafe = "O'Brien <script>"; |
| 123 | + $dir = sys_get_temp_dir() . '/slim-html-' . bin2hex(random_bytes(4)); |
| 124 | + $unsafeDir = $dir . '/' . $unsafe; |
| 125 | + mkdir($unsafeDir, 0700, true); |
| 126 | + $file = $unsafeDir . '/anon.php'; |
| 127 | + file_put_contents($file, '<?php return new class extends Exception {};'); |
| 128 | + |
| 129 | + try { |
| 130 | + /** @var Exception $exception */ |
| 131 | + $exception = require $file; |
| 132 | + $renderer = new HtmlErrorRenderer(); |
| 133 | + $reflectionRenderer = new ReflectionClass(HtmlErrorRenderer::class); |
| 134 | + |
| 135 | + $method = $reflectionRenderer->getMethod('renderExceptionFragment'); |
| 136 | + $this->setAccessible($method); |
| 137 | + $output = $method->invoke($renderer, $exception); |
| 138 | + |
| 139 | + $this->assertStringNotContainsString($unsafe, $output); |
| 140 | + $this->assertStringContainsString( |
| 141 | + htmlspecialchars(get_class($exception), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'), |
| 142 | + $output |
| 143 | + ); |
| 144 | + } finally { |
| 145 | + unlink($file); |
| 146 | + rmdir($unsafeDir); |
| 147 | + rmdir($dir); |
| 148 | + } |
92 | 149 | } |
93 | 150 |
|
94 | 151 | public function testHTMLErrorRendererRenderHttpException() |
|
0 commit comments