Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit fd31f7c

Browse files
Merge branch '4.2'
* 4.2: (26 commits) Apply php-cs-fixer rule for array_key_exists() [Cache] fix warming up cache.system and apcu [Security] Change FormAuthenticator if condition handles multi-byte characters in autocomplete speed up tests running them without debug flag [Translations] added missing Croatian validators Fix getItems() performance issue with RedisCluster (php-redis) [VarDumper] Keep a ref to objects to ensure their handle cannot be reused while cloning IntegerType: reject submitted non-integer numbers be keen to newcomers [HttpKernel] Fix possible infinite loop of exceptions fixed CS [Validator] Added missing translations for Afrikaans do not validate non-submitted form fields in PATCH requests Update usage example in ArrayInput doc block. [Console] Prevent ArgvInput::getFirstArgument() from returning an option value [Validator] Fixed duplicate UUID fixed CS [EventDispatcher] Fix unknown priority Avoid mutating the Finder when building the iterator ...
2 parents baa22f6 + 2adf893 commit fd31f7c

File tree

6 files changed

+88
-7
lines changed

6 files changed

+88
-7
lines changed

Core/Authentication/Token/AbstractToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public function setAttributes(array $attributes)
225225
*/
226226
public function hasAttribute($name)
227227
{
228-
return array_key_exists($name, $this->attributes);
228+
return \array_key_exists($name, $this->attributes);
229229
}
230230

231231
/**
@@ -239,7 +239,7 @@ public function hasAttribute($name)
239239
*/
240240
public function getAttribute($name)
241241
{
242-
if (!array_key_exists($name, $this->attributes)) {
242+
if (!\array_key_exists($name, $this->attributes)) {
243243
throw new \InvalidArgumentException(sprintf('This token has no "%s" attribute.', $name));
244244
}
245245

Core/Authorization/AccessDecisionManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class AccessDecisionManager implements AccessDecisionManagerInterface
4242
public function __construct(iterable $voters = [], string $strategy = self::STRATEGY_AFFIRMATIVE, bool $allowIfAllAbstainDecisions = false, bool $allowIfEqualGrantedDeniedDecisions = true)
4343
{
4444
$strategyMethod = 'decide'.ucfirst($strategy);
45-
if (!\is_callable([$this, $strategyMethod])) {
45+
if ('' === $strategy || !\is_callable([$this, $strategyMethod])) {
4646
throw new \InvalidArgumentException(sprintf('The strategy "%s" is not supported.', $strategy));
4747
}
4848

Core/Encoder/EncoderFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function getEncoder($user)
3333
$encoderKey = null;
3434

3535
if ($user instanceof EncoderAwareInterface && (null !== $encoderName = $user->getEncoderName())) {
36-
if (!array_key_exists($encoderName, $this->encoders)) {
36+
if (!\array_key_exists($encoderName, $this->encoders)) {
3737
throw new \RuntimeException(sprintf('The encoder "%s" was not configured.', $encoderName));
3838
}
3939

Http/Firewall/SimpleFormAuthenticationListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function attemptAuthentication(Request $request)
9797
$password = ParameterBagUtils::getRequestParameterValue($request, $this->options['password_parameter']);
9898
}
9999

100-
if (!\is_string($username) || (\is_object($username) && !\method_exists($username, '__toString'))) {
100+
if (!\is_string($username) && (!\is_object($username) || !\method_exists($username, '__toString'))) {
101101
throw new BadRequestHttpException(sprintf('The key "%s" must be a string, "%s" given.', $this->options['username_parameter'], \gettype($username)));
102102
}
103103

Http/Firewall/UsernamePasswordFormAuthenticationListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function attemptAuthentication(Request $request)
8585
$password = ParameterBagUtils::getRequestParameterValue($request, $this->options['password_parameter']);
8686
}
8787

88-
if (!\is_string($username) || (\is_object($username) && !\method_exists($username, '__toString'))) {
88+
if (!\is_string($username) && (!\is_object($username) || !\method_exists($username, '__toString'))) {
8989
throw new BadRequestHttpException(sprintf('The key "%s" must be a string, "%s" given.', $this->options['username_parameter'], \gettype($username)));
9090
}
9191

Http/Tests/Firewall/UsernamePasswordFormAuthenticationListenerTest.php

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function testHandleWhenUsernameLength($username, $ok)
8181
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
8282
* @expectedExceptionMessage The key "_username" must be a string, "array" given.
8383
*/
84-
public function testHandleNonStringUsername($postOnly)
84+
public function testHandleNonStringUsernameWithArray($postOnly)
8585
{
8686
$request = Request::create('/login_check', 'POST', ['_username' => []]);
8787
$request->setSession($this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock());
@@ -99,6 +99,79 @@ public function testHandleNonStringUsername($postOnly)
9999
$listener->handle($event);
100100
}
101101

102+
/**
103+
* @dataProvider postOnlyDataProvider
104+
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
105+
* @expectedExceptionMessage The key "_username" must be a string, "integer" given.
106+
*/
107+
public function testHandleNonStringUsernameWithInt($postOnly)
108+
{
109+
$request = Request::create('/login_check', 'POST', ['_username' => 42]);
110+
$request->setSession($this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock());
111+
$listener = new UsernamePasswordFormAuthenticationListener(
112+
new TokenStorage(),
113+
$this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(),
114+
new SessionAuthenticationStrategy(SessionAuthenticationStrategy::NONE),
115+
$httpUtils = new HttpUtils(),
116+
'foo',
117+
new DefaultAuthenticationSuccessHandler($httpUtils),
118+
new DefaultAuthenticationFailureHandler($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $httpUtils),
119+
['require_previous_session' => false, 'post_only' => $postOnly]
120+
);
121+
$event = new GetResponseEvent($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $request, HttpKernelInterface::MASTER_REQUEST);
122+
$listener->handle($event);
123+
}
124+
125+
/**
126+
* @dataProvider postOnlyDataProvider
127+
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
128+
* @expectedExceptionMessage The key "_username" must be a string, "object" given.
129+
*/
130+
public function testHandleNonStringUsernameWithObject($postOnly)
131+
{
132+
$request = Request::create('/login_check', 'POST', ['_username' => new \stdClass()]);
133+
$request->setSession($this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock());
134+
$listener = new UsernamePasswordFormAuthenticationListener(
135+
new TokenStorage(),
136+
$this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(),
137+
new SessionAuthenticationStrategy(SessionAuthenticationStrategy::NONE),
138+
$httpUtils = new HttpUtils(),
139+
'foo',
140+
new DefaultAuthenticationSuccessHandler($httpUtils),
141+
new DefaultAuthenticationFailureHandler($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $httpUtils),
142+
['require_previous_session' => false, 'post_only' => $postOnly]
143+
);
144+
$event = new GetResponseEvent($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $request, HttpKernelInterface::MASTER_REQUEST);
145+
$listener->handle($event);
146+
}
147+
148+
/**
149+
* @dataProvider postOnlyDataProvider
150+
*/
151+
public function testHandleNonStringUsernameWith__toString($postOnly)
152+
{
153+
$usernameClass = $this->getMockBuilder(DummyUserClass::class)->getMock();
154+
$usernameClass
155+
->expects($this->atLeastOnce())
156+
->method('__toString')
157+
->will($this->returnValue('someUsername'));
158+
159+
$request = Request::create('/login_check', 'POST', ['_username' => $usernameClass]);
160+
$request->setSession($this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock());
161+
$listener = new UsernamePasswordFormAuthenticationListener(
162+
new TokenStorage(),
163+
$this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(),
164+
new SessionAuthenticationStrategy(SessionAuthenticationStrategy::NONE),
165+
$httpUtils = new HttpUtils(),
166+
'foo',
167+
new DefaultAuthenticationSuccessHandler($httpUtils),
168+
new DefaultAuthenticationFailureHandler($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $httpUtils),
169+
['require_previous_session' => false, 'post_only' => $postOnly]
170+
);
171+
$event = new GetResponseEvent($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $request, HttpKernelInterface::MASTER_REQUEST);
172+
$listener->handle($event);
173+
}
174+
102175
public function postOnlyDataProvider()
103176
{
104177
return [
@@ -115,3 +188,11 @@ public function getUsernameForLength()
115188
];
116189
}
117190
}
191+
192+
class DummyUserClass
193+
{
194+
public function __toString()
195+
{
196+
return '';
197+
}
198+
}

0 commit comments

Comments
 (0)