Skip to content

Commit 19b8d9e

Browse files
committed
Merge branch '2.8' into 3.0
* 2.8: limited the maximum length of a submitted username
2 parents 273688c + 6d20cee commit 19b8d9e

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

src/Symfony/Component/Security/Core/Security.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ final class Security
2121
const ACCESS_DENIED_ERROR = '_security.403_error';
2222
const AUTHENTICATION_ERROR = '_security.last_error';
2323
const LAST_USERNAME = '_security.last_username';
24+
const MAX_USERNAME_LENGTH = 4096;
2425
}

src/Symfony/Component/Security/Http/Firewall/SimpleFormAuthenticationListener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
2222
use Symfony\Component\Security\Http\Authentication\SimpleFormAuthenticatorInterface;
2323
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
24+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
2425
use Symfony\Component\Security\Core\Security;
2526
use Symfony\Component\Security\Http\HttpUtils;
2627
use Symfony\Component\Security\Http\ParameterBagUtils;
@@ -107,6 +108,10 @@ protected function attemptAuthentication(Request $request)
107108
$password = ParameterBagUtils::getRequestParameterValue($request, $this->options['password_parameter']);
108109
}
109110

111+
if (strlen($username) > Security::MAX_USERNAME_LENGTH) {
112+
throw new BadCredentialsException('Invalid username.');
113+
}
114+
110115
$request->getSession()->set(Security::LAST_USERNAME, $username);
111116

112117
$token = $this->simpleAuthenticator->createToken($request, $username, $password, $this->providerKey);

src/Symfony/Component/Security/Http/Firewall/UsernamePasswordFormAuthenticationListener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
2424
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2525
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
26+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
2627
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
2728
use Symfony\Component\Security\Core\Security;
2829
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -83,6 +84,10 @@ protected function attemptAuthentication(Request $request)
8384
$password = ParameterBagUtils::getRequestParameterValue($request, $this->options['password_parameter']);
8485
}
8586

87+
if (strlen($username) > Security::MAX_USERNAME_LENGTH) {
88+
throw new BadCredentialsException('Invalid username.');
89+
}
90+
8691
$request->getSession()->set(Security::LAST_USERNAME, $username);
8792

8893
return $this->authenticationManager->authenticate(new UsernamePasswordToken($username, $password, $this->providerKey));
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Tests\Http\Firewall;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener;
17+
use Symfony\Component\Security\Core\Security;
18+
19+
class UsernamePasswordFormAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* @dataProvider getUsernameForLength
23+
*/
24+
public function testHandleWhenUsernameLength($username, $ok)
25+
{
26+
$request = Request::create('/login_check', 'POST', array('_username' => $username));
27+
$request->setSession($this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
28+
29+
$httpUtils = $this->getMock('Symfony\Component\Security\Http\HttpUtils');
30+
$httpUtils
31+
->expects($this->any())
32+
->method('checkRequestPath')
33+
->will($this->returnValue(true))
34+
;
35+
36+
$failureHandler = $this->getMock('Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface');
37+
$failureHandler
38+
->expects($ok ? $this->never() : $this->once())
39+
->method('onAuthenticationFailure')
40+
->will($this->returnValue(new Response()))
41+
;
42+
43+
$authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager')->disableOriginalConstructor()->getMock();
44+
$authenticationManager
45+
->expects($ok ? $this->once() : $this->never())
46+
->method('authenticate')
47+
->will($this->returnValue(new Response()))
48+
;
49+
50+
$listener = new UsernamePasswordFormAuthenticationListener(
51+
$this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'),
52+
$authenticationManager,
53+
$this->getMock('Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface'),
54+
$httpUtils,
55+
'TheProviderKey',
56+
$this->getMock('Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface'),
57+
$failureHandler,
58+
array('require_previous_session' => false)
59+
);
60+
61+
$event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
62+
$event
63+
->expects($this->any())
64+
->method('getRequest')
65+
->will($this->returnValue($request))
66+
;
67+
68+
$listener->handle($event);
69+
}
70+
71+
public function getUsernameForLength()
72+
{
73+
return array(
74+
array(str_repeat('x', Security::MAX_USERNAME_LENGTH + 1), false),
75+
array(str_repeat('x', Security::MAX_USERNAME_LENGTH - 1), true),
76+
);
77+
}
78+
}

0 commit comments

Comments
 (0)