|
3 | 3 | namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Unit\Field; |
4 | 4 |
|
5 | 5 | use EasyCorp\Bundle\EasyAdminBundle\Config\Action; |
| 6 | +use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; |
6 | 7 | use EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\UrlConfigurator; |
7 | 8 | use EasyCorp\Bundle\EasyAdminBundle\Field\UrlField; |
| 9 | +use Symfony\Component\Validator\Constraints\Url; |
8 | 10 |
|
9 | 11 | class UrlFieldTest extends AbstractFieldTest |
10 | 12 | { |
@@ -59,6 +61,244 @@ public function testFormattedValuesOnIndexAction(string $url, string $expectedRe |
59 | 61 | self::assertSame($expectedRenderedUrl, $fieldDto->getFormattedValue()); |
60 | 62 | } |
61 | 63 |
|
| 64 | + /** |
| 65 | + * @testWith ["javascript:alert(1)"] |
| 66 | + * ["JavaScript:alert(1)"] |
| 67 | + * ["JaVaScRiPt:alert(1)"] |
| 68 | + * ["data:text/html,<script>alert(1)</script>"] |
| 69 | + * ["vbscript:msgbox(1)"] |
| 70 | + * ["file:///etc/passwd"] |
| 71 | + * [" javascript:alert(1)"] |
| 72 | + * ["\tjavascript:alert(1)"] |
| 73 | + * ["\njavascript:alert(1)"] |
| 74 | + * ["\u0000javascript:alert(1)"] |
| 75 | + */ |
| 76 | + public function testDangerousSchemesAreMarkedUnsafe(string $url): void |
| 77 | + { |
| 78 | + $this->initializeConfigurator(); |
| 79 | + |
| 80 | + $field = UrlField::new('foo')->setValue($url); |
| 81 | + $fieldDto = $this->configure($field); |
| 82 | + |
| 83 | + self::assertTrue($fieldDto->getCustomOption(UrlField::OPTION_IS_UNSAFE)); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @testWith ["http://example.com"] |
| 88 | + * ["https://example.com"] |
| 89 | + * ["ftp://example.com"] |
| 90 | + * ["ftps://example.com"] |
| 91 | + * ["ssh://user@example.com"] |
| 92 | + * ["sftp://user@example.com"] |
| 93 | + * ["git://example.com/repo.git"] |
| 94 | + * ["mailto:user@example.com"] |
| 95 | + * ["webcal://example.com/cal.ics"] |
| 96 | + * ["/relative/path"] |
| 97 | + * ["relative/path"] |
| 98 | + * ["//example.com/protocol-relative"] |
| 99 | + * [""] |
| 100 | + */ |
| 101 | + public function testSafeSchemesAreNotMarkedUnsafe(string $url): void |
| 102 | + { |
| 103 | + $this->initializeConfigurator(); |
| 104 | + |
| 105 | + $field = UrlField::new('foo')->setValue($url); |
| 106 | + $fieldDto = $this->configure($field); |
| 107 | + |
| 108 | + self::assertFalse($fieldDto->getCustomOption(UrlField::OPTION_IS_UNSAFE)); |
| 109 | + } |
| 110 | + |
| 111 | + public function testTemplateRendersDangerousUrlAsSpanInsteadOfLink(): void |
| 112 | + { |
| 113 | + $this->initializeConfigurator(); |
| 114 | + |
| 115 | + $field = UrlField::new('foo')->setValue('javascript:alert(1)'); |
| 116 | + $fieldDto = $this->configure($field); |
| 117 | + |
| 118 | + $html = $this->renderFieldTemplate($fieldDto, $this->entityDto, $this->adminContext); |
| 119 | + |
| 120 | + // the dangerous scheme must not be rendered inside an anchor's href attribute |
| 121 | + self::assertStringNotContainsString('<a ', $html); |
| 122 | + self::assertStringNotContainsString('href=', $html); |
| 123 | + self::assertStringContainsString('<span', $html); |
| 124 | + self::assertStringContainsString('javascript:alert(1)', $html); |
| 125 | + } |
| 126 | + |
| 127 | + public function testTemplateEscapesDangerousUrlContainingHtml(): void |
| 128 | + { |
| 129 | + $this->initializeConfigurator(); |
| 130 | + |
| 131 | + $field = UrlField::new('foo')->setValue('data:text/html,<script>alert(1)</script>'); |
| 132 | + $fieldDto = $this->configure($field); |
| 133 | + |
| 134 | + $html = $this->renderFieldTemplate($fieldDto, $this->entityDto, $this->adminContext); |
| 135 | + |
| 136 | + self::assertStringNotContainsString('<script>', $html); |
| 137 | + self::assertStringContainsString('<script>', $html); |
| 138 | + } |
| 139 | + |
| 140 | + public function testTemplateRendersSafeUrlAsLink(): void |
| 141 | + { |
| 142 | + $this->initializeConfigurator(); |
| 143 | + |
| 144 | + $field = UrlField::new('foo')->setValue('https://example.com'); |
| 145 | + $fieldDto = $this->configure($field); |
| 146 | + |
| 147 | + $html = $this->renderFieldTemplate($fieldDto, $this->entityDto, $this->adminContext); |
| 148 | + |
| 149 | + self::assertStringContainsString('<a ', $html); |
| 150 | + self::assertStringContainsString('href="https://example.com"', $html); |
| 151 | + self::assertStringContainsString('target="_blank"', $html); |
| 152 | + self::assertStringContainsString('rel="noopener"', $html); |
| 153 | + } |
| 154 | + |
| 155 | + public function testTemplateRendersDangerousUrlAsSpanOnDetailAction(): void |
| 156 | + { |
| 157 | + $this->initializeConfigurator(); |
| 158 | + |
| 159 | + $field = UrlField::new('foo')->setValue('javascript:alert(1)'); |
| 160 | + $fieldDto = $this->configure($field, pageName: Crud::PAGE_DETAIL, actionName: Action::DETAIL); |
| 161 | + |
| 162 | + $html = $this->renderFieldTemplate($fieldDto, $this->entityDto, $this->adminContext); |
| 163 | + |
| 164 | + self::assertStringNotContainsString('<a ', $html); |
| 165 | + self::assertStringContainsString('<span', $html); |
| 166 | + } |
| 167 | + |
| 168 | + public function testAllowedProtocolsOptionDefaultsToNull(): void |
| 169 | + { |
| 170 | + $this->initializeConfigurator(); |
| 171 | + |
| 172 | + $field = UrlField::new('foo'); |
| 173 | + $fieldDto = $this->configure($field, actionName: Action::EDIT); |
| 174 | + |
| 175 | + self::assertNull($fieldDto->getCustomOption(UrlField::OPTION_ALLOWED_PROTOCOLS)); |
| 176 | + self::assertNull($fieldDto->getFormTypeOption('constraints')); |
| 177 | + } |
| 178 | + |
| 179 | + public function testAllowedProtocolsAddsUrlConstraint(): void |
| 180 | + { |
| 181 | + $this->initializeConfigurator(); |
| 182 | + |
| 183 | + $field = UrlField::new('foo')->allowedProtocols(['http', 'https']); |
| 184 | + $fieldDto = $this->configure($field, actionName: Action::EDIT); |
| 185 | + |
| 186 | + self::assertSame(['http', 'https'], $fieldDto->getCustomOption(UrlField::OPTION_ALLOWED_PROTOCOLS)); |
| 187 | + |
| 188 | + $constraints = $fieldDto->getFormTypeOption('constraints'); |
| 189 | + self::assertIsArray($constraints); |
| 190 | + self::assertCount(1, $constraints); |
| 191 | + self::assertInstanceOf(Url::class, $constraints[0]); |
| 192 | + self::assertSame(['http', 'https'], $constraints[0]->protocols); |
| 193 | + } |
| 194 | + |
| 195 | + public function testAllowedProtocolsAppendsToExistingConstraints(): void |
| 196 | + { |
| 197 | + $this->initializeConfigurator(); |
| 198 | + |
| 199 | + $existingConstraint = new Url(); |
| 200 | + $field = UrlField::new('foo') |
| 201 | + ->setFormTypeOption('constraints', [$existingConstraint]) |
| 202 | + ->allowedProtocols(['ftp', 'sftp']); |
| 203 | + $fieldDto = $this->configure($field, actionName: Action::EDIT); |
| 204 | + |
| 205 | + $constraints = $fieldDto->getFormTypeOption('constraints'); |
| 206 | + self::assertIsArray($constraints); |
| 207 | + self::assertCount(2, $constraints); |
| 208 | + self::assertSame($existingConstraint, $constraints[0]); |
| 209 | + self::assertInstanceOf(Url::class, $constraints[1]); |
| 210 | + self::assertSame(['ftp', 'sftp'], $constraints[1]->protocols); |
| 211 | + } |
| 212 | + |
| 213 | + public function testAllowedProtocolsReturnsSelfForFluentInterface(): void |
| 214 | + { |
| 215 | + $field = UrlField::new('foo'); |
| 216 | + |
| 217 | + self::assertSame($field, $field->allowedProtocols(['http', 'https'])); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * @testWith [["http"]] |
| 222 | + * [["https"]] |
| 223 | + * [["ftp", "ftps"]] |
| 224 | + * [["ssh", "sftp", "git"]] |
| 225 | + * [["http", "https", "ftp", "mailto", "webcal"]] |
| 226 | + */ |
| 227 | + public function testAllowedProtocolsAcceptsDifferentProtocolSets(array $protocols): void |
| 228 | + { |
| 229 | + $this->initializeConfigurator(); |
| 230 | + |
| 231 | + $field = UrlField::new('foo')->allowedProtocols($protocols); |
| 232 | + $fieldDto = $this->configure($field, actionName: Action::EDIT); |
| 233 | + |
| 234 | + self::assertSame($protocols, $fieldDto->getCustomOption(UrlField::OPTION_ALLOWED_PROTOCOLS)); |
| 235 | + |
| 236 | + $constraints = $fieldDto->getFormTypeOption('constraints'); |
| 237 | + self::assertCount(1, $constraints); |
| 238 | + self::assertInstanceOf(Url::class, $constraints[0]); |
| 239 | + self::assertSame($protocols, $constraints[0]->protocols); |
| 240 | + } |
| 241 | + |
| 242 | + public function testAllowedProtocolsWithEmptyArrayStillAddsConstraint(): void |
| 243 | + { |
| 244 | + $this->initializeConfigurator(); |
| 245 | + |
| 246 | + $field = UrlField::new('foo')->allowedProtocols([]); |
| 247 | + $fieldDto = $this->configure($field, actionName: Action::EDIT); |
| 248 | + |
| 249 | + self::assertSame([], $fieldDto->getCustomOption(UrlField::OPTION_ALLOWED_PROTOCOLS)); |
| 250 | + |
| 251 | + $constraints = $fieldDto->getFormTypeOption('constraints'); |
| 252 | + self::assertCount(1, $constraints); |
| 253 | + self::assertInstanceOf(Url::class, $constraints[0]); |
| 254 | + self::assertSame([], $constraints[0]->protocols); |
| 255 | + } |
| 256 | + |
| 257 | + public function testAllowedProtocolsLastCallWins(): void |
| 258 | + { |
| 259 | + $this->initializeConfigurator(); |
| 260 | + |
| 261 | + $field = UrlField::new('foo') |
| 262 | + ->allowedProtocols(['http', 'https']) |
| 263 | + ->allowedProtocols(['ftp']); |
| 264 | + $fieldDto = $this->configure($field, actionName: Action::EDIT); |
| 265 | + |
| 266 | + self::assertSame(['ftp'], $fieldDto->getCustomOption(UrlField::OPTION_ALLOWED_PROTOCOLS)); |
| 267 | + |
| 268 | + $constraints = $fieldDto->getFormTypeOption('constraints'); |
| 269 | + self::assertCount(1, $constraints); |
| 270 | + self::assertSame(['ftp'], $constraints[0]->protocols); |
| 271 | + } |
| 272 | + |
| 273 | + public function testAllowedProtocolsIsIndependentFromDefaultProtocol(): void |
| 274 | + { |
| 275 | + $this->initializeConfigurator(); |
| 276 | + |
| 277 | + $field = UrlField::new('foo') |
| 278 | + ->setDefaultProtocol('https') |
| 279 | + ->allowedProtocols(['http', 'https']); |
| 280 | + $fieldDto = $this->configure($field, actionName: Action::EDIT); |
| 281 | + |
| 282 | + self::assertSame('https', $fieldDto->getCustomOption(UrlField::OPTION_DEFAULT_PROTOCOL)); |
| 283 | + self::assertSame('https', $fieldDto->getFormTypeOption('default_protocol')); |
| 284 | + self::assertSame(['http', 'https'], $fieldDto->getCustomOption(UrlField::OPTION_ALLOWED_PROTOCOLS)); |
| 285 | + self::assertCount(1, $fieldDto->getFormTypeOption('constraints')); |
| 286 | + } |
| 287 | + |
| 288 | + public function testNoConstraintIsAddedWhenAllowedProtocolsIsNotCalled(): void |
| 289 | + { |
| 290 | + $this->initializeConfigurator(); |
| 291 | + |
| 292 | + $existingConstraint = new Url(); |
| 293 | + $field = UrlField::new('foo') |
| 294 | + ->setFormTypeOption('constraints', [$existingConstraint]); |
| 295 | + $fieldDto = $this->configure($field, actionName: Action::EDIT); |
| 296 | + |
| 297 | + $constraints = $fieldDto->getFormTypeOption('constraints'); |
| 298 | + self::assertCount(1, $constraints); |
| 299 | + self::assertSame($existingConstraint, $constraints[0]); |
| 300 | + } |
| 301 | + |
62 | 302 | private function initializeConfigurator(): void |
63 | 303 | { |
64 | 304 | self::bootKernel(); |
|
0 commit comments