1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Security\Http\SecurityRequestAttributes;
class HttpUtilsTest extends TestCase
{
public function testCreateRedirectResponseWithPath()
{
$utils = new HttpUtils($this->getUrlGenerator());
$response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
$this->assertTrue($response->isRedirect('http://localhost/foobar'));
$this->assertEquals(302, $response->getStatusCode());
}
public function testCreateRedirectResponseWithAbsoluteUrl()
{
$utils = new HttpUtils($this->getUrlGenerator());
$response = $utils->createRedirectResponse($this->getRequest(), 'http://symfony.com/');
$this->assertTrue($response->isRedirect('http://symfony.com/'));
}
public function testCreateRedirectResponseWithDomainRegexp()
{
$utils = new HttpUtils($this->getUrlGenerator(), null, '#^https?://symfony\.com$#i');
$response = $utils->createRedirectResponse($this->getRequest(), 'http://symfony.com/blog');
$this->assertTrue($response->isRedirect('http://symfony.com/blog'));
}
public function testCreateRedirectResponseWithRequestsDomain()
{
$utils = new HttpUtils($this->getUrlGenerator(), null, '#^https?://%s$#i');
$response = $utils->createRedirectResponse($this->getRequest(), 'http://localhost/blog');
$this->assertTrue($response->isRedirect('http://localhost/blog'));
}
#[\PHPUnit\Framework\Attributes\DataProvider('validRequestDomainUrls')]
public function testCreateRedirectResponse(?string $domainRegexp, string $path, string $expectedRedirectUri)
{
$utils = new HttpUtils($this->getUrlGenerator(), null, $domainRegexp);
$response = $utils->createRedirectResponse($this->getRequest(), $path);
$this->assertTrue($response->isRedirect($expectedRedirectUri));
$this->assertEquals(302, $response->getStatusCode());
}
public static function validRequestDomainUrls()
{
return [
'/foobar' => [
null,
'/foobar',
'http://localhost/foobar',
],
'http://symfony.com/ without domain regex' => [
null,
'http://symfony.com/',
'http://symfony.com/',
],
'http://localhost/blog with #^https?://symfony\.com$#i' => [
'#^https?://symfony\.com$#i',
'http://symfony.com/blog',
'http://symfony.com/blog',
],
'http://localhost/blog with #^https?://%s$#i' => [
'#^https?://%s$#i',
'http://localhost/blog',
'http://localhost/blog',
],
'custom scheme' => [
null,
'android-app://com.google.android.gm/',
'android-app://com.google.android.gm/',
],
'custom scheme with all URL components' => [
null,
'android-app://foo:bar@www.example.com:8080/software/index.html?lite=true#section1',
'android-app://foo:bar@www.example.com:8080/software/index.html?lite=true#section1',
],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('badRequestDomainUrls')]
public function testCreateRedirectResponseWithBadRequestsDomain($url)
{
$utils = new HttpUtils($this->getUrlGenerator(), null, '#^https?://%s$#i');
$response = $utils->createRedirectResponse($this->getRequest(), $url);
$this->assertTrue($response->isRedirect('http://localhost/'));
}
public static function badRequestDomainUrls()
{
return [
['http://pirate.net/foo'],
['http:\\\\pirate.net/foo'],
['http:/\\pirate.net/foo'],
['http:\\/pirate.net/foo'],
['http://////pirate.net/foo'],
['http:///foo'],
];
}
public function testCreateRedirectResponseWithProtocolRelativeTarget()
{
$utils = new HttpUtils($this->getUrlGenerator(), null, '#^https?://%s$#i');
$response = $utils->createRedirectResponse($this->getRequest(), '//evil.com/do-bad-things');
$this->assertTrue($response->isRedirect('http://localhost//evil.com/do-bad-things'), 'Protocol-relative redirection should not be supported for security reasons');
}
public function testCreateRedirectResponseWithRouteName()
{
$utils = new HttpUtils($urlGenerator = $this->createMock(UrlGeneratorInterface::class));
$urlGenerator
->expects($this->any())
->method('generate')
->with('foobar', [], UrlGeneratorInterface::ABSOLUTE_URL)
->willReturn('http://localhost/foo/bar')
;
$urlGenerator
->expects($this->any())
->method('getContext')
->willReturn($this->createMock(RequestContext::class))
;
$response = $utils->createRedirectResponse($this->getRequest(), 'foobar');
$this->assertTrue($response->isRedirect('http://localhost/foo/bar'));
}
public function testCreateRequestWithPath()
{
$request = $this->getRequest();
$request->server->set('Foo', 'bar');
$utils = new HttpUtils($this->getUrlGenerator());
$subRequest = $utils->createRequest($request, '/foobar');
$this->assertEquals('GET', $subRequest->getMethod());
$this->assertEquals('/foobar', $subRequest->getPathInfo());
$this->assertEquals('bar', $subRequest->server->get('Foo'));
}
public function testCreateRequestWithRouteName()
{
$utils = new HttpUtils($urlGenerator = $this->createMock(UrlGeneratorInterface::class));
$urlGenerator
->expects($this->once())
->method('generate')
->willReturn('/foo/bar')
;
$urlGenerator
->expects($this->any())
->method('getContext')
->willReturn($this->createMock(RequestContext::class))
;
$subRequest = $utils->createRequest($this->getRequest(), 'foobar');
$this->assertEquals('/foo/bar', $subRequest->getPathInfo());
}
public function testCreateRequestWithAbsoluteUrl()
{
$utils = new HttpUtils($this->createMock(UrlGeneratorInterface::class));
$subRequest = $utils->createRequest($this->getRequest(), 'http://symfony.com/');
$this->assertEquals('/', $subRequest->getPathInfo());
}
public function testCreateRequestPassesSessionToTheNewRequest()
{
$request = $this->getRequest();
$request->setSession($session = $this->createMock(SessionInterface::class));
$utils = new HttpUtils($this->getUrlGenerator());
$subRequest = $utils->createRequest($request, '/foobar');
$this->assertSame($session, $subRequest->getSession());
}
#[\PHPUnit\Framework\Attributes\DataProvider('provideSecurityRequestAttributes')]
public function testCreateRequestPassesSecurityRequestAttributesToTheNewRequest($attribute)
{
$request = $this->getRequest();
$request->attributes->set($attribute, 'foo');
$utils = new HttpUtils($this->getUrlGenerator());
$subRequest = $utils->createRequest($request, '/foobar');
$this->assertSame('foo', $subRequest->attributes->get($attribute));
}
public static function provideSecurityRequestAttributes()
{
return [
[SecurityRequestAttributes::AUTHENTICATION_ERROR],
[SecurityRequestAttributes::ACCESS_DENIED_ERROR],
[SecurityRequestAttributes::LAST_USERNAME],
];
}
public function testCheckRequestPath()
{
$utils = new HttpUtils($this->getUrlGenerator());
$this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
$this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));
$this->assertTrue($utils->checkRequestPath($this->getRequest('/foo%20bar'), '/foo bar'));
// Plus must not decoded to space
$this->assertTrue($utils->checkRequestPath($this->getRequest('/foo+bar'), '/foo+bar'));
// Checking unicode
$this->assertTrue($utils->checkRequestPath($this->getRequest('/'.urlencode('вход')), '/вход'));
}
public function testCheckRequestPathWithUrlMatcherAndResourceNotFound()
{
$urlMatcher = $this->createMock(UrlMatcherInterface::class);
$urlMatcher
->expects($this->any())
->method('match')
->with('/')
->willThrowException(new ResourceNotFoundException())
;
$utils = new HttpUtils(null, $urlMatcher);
$this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
}
public function testCheckRequestPathWithUrlMatcherAndMethodNotAllowed()
{
$request = $this->getRequest();
$urlMatcher = $this->createMock(RequestMatcherInterface::class);
$urlMatcher
->expects($this->any())
->method('matchRequest')
->with($request)
->willThrowException(new MethodNotAllowedException([]))
;
$utils = new HttpUtils(null, $urlMatcher);
$this->assertFalse($utils->checkRequestPath($request, 'foobar'));
}
public function testCheckRequestPathWithUrlMatcherAndResourceFoundByUrl()
{
$urlMatcher = $this->createMock(UrlMatcherInterface::class);
$urlMatcher
->expects($this->any())
->method('match')
->with('/foo/bar')
->willReturn(['_route' => 'foobar'])
;
$utils = new HttpUtils(null, $urlMatcher);
$this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
}
public function testCheckRequestPathWithUrlMatcherAndResourceFoundByRequest()
{
$request = $this->getRequest();
$urlMatcher = $this->createMock(RequestMatcherInterface::class);
$urlMatcher
->expects($this->any())
->method('matchRequest')
->with($request)
->willReturn(['_route' => 'foobar'])
;
$utils = new HttpUtils(null, $urlMatcher);
$this->assertTrue($utils->checkRequestPath($request, 'foobar'));
}
public function testCheckRequestPathWithUrlMatcherLoadingException()
{
$this->expectException(\RuntimeException::class);
$urlMatcher = $this->createMock(UrlMatcherInterface::class);
$urlMatcher
->expects($this->any())
->method('match')
->willThrowException(new \RuntimeException())
;
$utils = new HttpUtils(null, $urlMatcher);
$utils->checkRequestPath($this->getRequest(), 'foobar');
}
public function testCheckRequestPathWithRequestAlreadyMatchedBefore()
{
$urlMatcher = $this->createMock(RequestMatcherInterface::class);
$urlMatcher
->expects($this->never())
->method('matchRequest')
;
$request = $this->getRequest();
$request->attributes->set('_route', 'route_name');
$utils = new HttpUtils(null, $urlMatcher);
$this->assertTrue($utils->checkRequestPath($request, 'route_name'));
$this->assertFalse($utils->checkRequestPath($request, 'foobar'));
}
public function testCheckPathWithoutRouteParam()
{
$urlMatcher = $this->createMock(UrlMatcherInterface::class);
$urlMatcher
->expects($this->any())
->method('match')
->willReturn(['_controller' => 'PathController'])
;
$utils = new HttpUtils(null, $urlMatcher);
$this->assertFalse($utils->checkRequestPath($this->getRequest(), 'path/index.html'));
}
public function testGenerateUriRemovesQueryString()
{
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar'));
$this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value'));
$this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
}
public function testGenerateUriPreservesFragment()
{
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value#fragment'));
$this->assertEquals('/foo/bar#fragment', $utils->generateUri(new Request(), 'route_name'));
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar#fragment'));
$this->assertEquals('/foo/bar#fragment', $utils->generateUri(new Request(), 'route_name'));
}
public function testUrlGeneratorIsRequiredToGenerateUrl()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('You must provide a UrlGeneratorInterface instance to be able to use routes.');
$utils = new HttpUtils();
$utils->generateUri(new Request(), 'route_name');
}
private function getUrlGenerator($generatedUrl = '/foo/bar')
{
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator
->expects($this->any())
->method('generate')
->willReturn($generatedUrl)
;
return $urlGenerator;
}
private function getRequest($path = '/')
{
return Request::create($path, 'get');
}
}
|