8000 Merge branch '2.7' into 2.8 · symfony/symfony@ce12665 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce12665

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: [Security] added more tests [Security] fixed default target path when referer contains a query string [Security] simplified tests [Security] refactored tests [VarDumper] Move locale sniffing to dump() time
2 parents f8f5c02 + f4172b0 commit ce12665

File tree

3 files changed

+91
-163
lines changed

3 files changed

+91
-163
lines changed

src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationSuccessHandler.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,14 @@ protected function determineTargetUrl(Request $request)
119119
return $targetUrl;
120120
}
121121

122-
if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
123-
return $targetUrl;
122+
if ($this->options['use_referer']) {
123+
$targetUrl = $request->headers->get('Referer');
124+
if (false !== $pos = strpos($targetUrl, '?')) {
125+
$targetUrl = substr($targetUrl, 0, $pos);
126+
}
127+
if ($targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
128+
return $targetUrl;
129+
}
124130
}
125131

126132
return $this->options['default_target_path'];

src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationSuccessHandlerTest.php

Lines changed: 78 additions & 159 deletions
18
Original file line numberDiff line numberDiff line change
@@ -12,173 +12,92 @@
1212
namespace Symfony\Component\Security\Http\Tests\Authentication;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
17+
use Symfony\Component\Security\Http\HttpUtils;
17

1819
class DefaultAuthenticationSuccessHandlerTest extends TestCase
1920
{
20-
private $httpUtils = null;
21-
22-
private $request = null;
23-
24-
private $token = null;
25-
26-
protected function setUp()
21+
/**
22+
* @dataProvider getRequestRedirections
23+
*/
24+
public function testRequestRedirections(Request $request, $options, $redirectedUrl)
2725
{
28-
$this->httpUtils = $this->getMockBuilder('Symfony\Component\Security\Http\HttpUtils')->getMock();
29-
$this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
30-
$this->request->headers = $this->getMockBuilder('Symfony\Component\HttpFoundation\HeaderBag')->getMock();
31-
$this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
26+
$urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock();
27+
$urlGenerator->expects($this->any())->method('generate')->will($this->returnValue('http://localhost/login'));
28+
$httpUtils = new HttpUtils($urlGenerator);
29+
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
30+
$handler = new DefaultAuthenticationSuccessHandler($httpUtils, $options);
31+
if ($request->hasSession()) {
32+
$handler->setProviderKey('admin');
33+
}
34+
$this->assertSame('http://localhost'.$redirectedUrl, $handler->onAuthenticationSuccess($request, $token)->getTargetUrl());
3235
}
3336

34-
public function testRequestIsRedirected()
35-
{
36-
$response = $this->expectRedirectResponse('/');
37-
38-
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
39-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
40-
41-
$this->assertSame($response, $result);
42-
}
43-
44-
public function testDefaultTargetPathCanBeForced()
45-
{
46-
$options = array(
47-
'always_use_default_target_path' => true,
48-
'default_target_path' => '/dashboard',
49-
);
50-
51-
$response = $this->expectRedirectResponse('/dashboard');
52-
53-
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
54-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
55-
56-
$this->assertSame($response, $result);
57-
}
58-
59-
public function testTargetPathIsPassedWithRequest()
60-
{
61-
$this->request->expects($this->once())
62-
->method('get')->with('_target_path')
63-
->will($this->returnValue('/dashboard'));
64-
65-
$response = $this->expectRedirectResponse('/dashboard');
66-
67-
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
68-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
69-
70-
$this->assertSame($response, $result);
71-
}
72-
73-
public function testTargetPathIsPassedAsNestedParameterWithRequest()
74-
{
75-
$this->request->expects($this->once())
76-
->method('get')->with('_target_path')
77-
->will($this->returnValue(array('value' => '/dashboard')));
78-
79-
$response = $this->expectRedirectResponse('/dashboard');
80-
81-
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array('target_path_parameter' => '_target_path[value]'));
82-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
83-
84-
$this->assertSame($response, $result);
85-
}
86-
87-
public function testTargetPathParameterIsCustomised()
88-
{
89-
$options = array('target_path_parameter' => '_my_target_path');
90-
91-
$this->request->expects($this->once())
92-
->method('get')->with('_my_target_path')
93-
->will($this->returnValue('/dashboard'));
94-
95-
$response = $this->expectRedirectResponse('/dashboard');
96-
97-
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
98-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
99-
100-
$this->assertSame($response, $result);
101-
}
102-
103-
public function testTargetPathIsTakenFromTheSession()
37+
public function getRequestRedirections()
10438
{
10539
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock();
106-
$session->expects($this->once())
107-
->method('get')->with('_security.admin.target_path')
108-
->will($this->returnValue('/admin/dashboard'));
109-
$session->expects($this->once())
110-
->method('remove')->with('_security.admin.target_path');
111-
112-
$this->request->expects($this->any())
113-
->method('getSession')
114-
->will($this->returnValue($session));
115-
116-
$response = $this->expectRedirectResponse('/admin/dashboard');
117-
118-
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
119-
$handler->setProviderKey('admin');
120-
121-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
122-
123-
$this->assertSame($response, $result);
124-
}
125-
126-
public function testTargetPathIsPassedAsReferer()
127-
{
128-
$options = array('use_referer' => true);
129-
130-
$this->request->headers->expects($this->once())
131-
->method('get')->with('Referer')
132-
->will($this->returnValue('/dashboard'));
133-
134-
$response = $this->expectRedirectResponse('/dashboard');
135-
136-
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
137-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
138-
139-
$this->assertSame($response, $result);
140-
}
141-
142-
public function testRefererHasToBeDifferentThatLoginUrl()
143-
{
144-
$options = array('use_referer' => true);
145-
146-
$this->request->headers->expects($this->any())
147-
->method('get')->with('Referer')
148-
->will($this->returnValue('/login'));
149-
150-
$this->httpUtils->expects($this->once())
151-
->method('generateUri')->with($this->request, '/login')
152-
->will($this->returnValue('/login'));
153-
154-
$response = $this->expectRedirectResponse('/');
155-
156-
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
157-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
158-
159-
$this->assertSame($response, $result);
160-
}
161-
162-
public function testRefererTargetPathIsIgnoredByDefault()
163-
{
164-
$this->request->headers->expects($this->never())->method('get');
165-
166-
$response = $this->expectRedirectResponse('/');
167-
168-
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
169-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
170-
171-
$this->assertSame($response, $result);
172-
}
173-
174-
private function expectRedirectResponse($path)
175-
{
176-
$response = new Response();
177-
$this->httpUtils->expects($this->once())
178-
->method('createRedirectResponse')
179-
->with($this->request, $path)
180-
->will($this->returnValue($response));
181-
182-
return $response;
40+
$session->expects($this->once())->method('get')->with('_security.admin.target_path')->will($this->returnValue('/admin/dashboard'));
41+
$session->expects($this->once())->method('remove')->with('_security.admin.target_path');
42+
$requestWithSession = Request::create('/');
43+
$requestWithSession->setSession($session);
44+
45+
return array(
46+
'default' => array(
47+
Request::create('/'),
48+
array(),
49+
'/',
50+
),
51+
'forced target path' => array(
52+
Request::create('/'),
53+
array('always_use_default_target_path' => true, 'default_target_path' => '/dashboard'),
54+
'/dashboard',
55+
),
56+
'target path as query string' => array(
57+
Request::create('/?_target_path=/dashboard'),
58+
array(),
59+
'/dashboard',
60+
),
61+
'target path name as query string is customized' => array(
62+
Request::create('/?_my_target_path=/dashboard'),
63+
array('target_path_parameter' => '_my_target_path'),
64+
'/dashboard',
65+
),
66+
'target path name as query string is customized and nested' => array(
67+
Request::create('/?_target_path[value]=/dashboard'),
68+
array('target_path_parameter' => '_target_path[value]'),
69+
'/dashboard',
70+
),
71+
'target path in session' => array(
72+
$requestWithSession,
73+
array(),
74+
'/admin/dashboard',
75+
),
76+
'target path as referer' => array(
77+
Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/dashboard')),
78+
array('use_referer' => true),
79+
'/dashboard',
80+
),
81+
'target path as referer is ignored if not configured' => array(
82+
Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/dashboard')),
83+
array(),
84+
'/',
85+
),
86+
'target path should be different than login URL' => array(
87+
Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/login')),
88+
array('use_referer' => true, 'login_path' => '/login'),
89+
'/',
90+
),
91+
'target path should be different than login URL (query string does not matter)' => array(
92+
Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/login?t=1&p=2')),
93+
array('use_referer' => true, 'login_path' => '/login'),
94+
'/',
95+
),
96+
'target path should be different than login URL (login_path as a route)' => array(
97+
Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/login?t=1&p=2')),
98+
array('use_referer' => true, 'login_path' => 'login_route'),
99+
'/',
100+
),
101+
);
183102
}
184103
}

src/Symfony/Component/VarDumper/Dumper/AbstractDumper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
3838
public function __construct($output = null, $charset = null)
3939
{
4040
$this->setCharset($charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8');
41-
$this->decimalPoint = (string) 0.5;
42-
$this->decimalPoint = $this->decimalPoint[1];
41+
$this->decimalPoint = localeconv();
42+
$this->decimalPoint = $this->decimalPoint['decimal_point'];
4343
$this->setOutput($output ?: static::$defaultOutput);
4444
if (!$output && is_string(static::$defaultOutput)) {
4545
static::$defaultOutput = $this->outputStream;
@@ -113,6 +113,9 @@ public function setIndentPad($pad)
113113
*/
114114
public function dump(Data $data, $output = null)
115115
{
116+
$this->decimalPoint = localeconv();
117+
$this->decimalPoint = $this->decimalPoint['decimal_point'];
118+
116119
$exception = null;
117120
if ($output) {
118121
$prevOutput = $this->setOutput($output);

0 commit comments

Comments
 (0)
0