8000 [Security] Fixed persistence of AuthenticationException by rpg600 · Pull Request #15557 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Fixed persistence of AuthenticationException #15557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
< 10000 /tr>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Security\User;

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;

class FilesystemUserProvider extends InMemoryUserProvider
{
public static function getFilename($testCase)
{
return sys_get_temp_dir().'/'.Kernel::VERSION.'/'. $testCase .'/user.txt';
}

public function __construct($testCase)
{
$users = json_decode(file_get_contents(self::getFilename($testCase)), true);

parent::__construct($users);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Bundle\SecurityBundle\Tests\Functional;

use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Security\User\FilesystemUserProvider;

class FormLoginTest extends WebTestCase
{
/**
Expand Down Expand Up @@ -76,6 +78,37 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin($config)
$this->assertContains('You\'re browsing to path "/protected_resource".', $text);
}

public function testFormLoginRedirectsToEntryPointWithAuthenticationExceptionMessage()
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'custom_provider.yml'));
$client->insulate();

// An user is added to the FilesystemUserProvider
file_put_contents(FilesystemUserProvider::getFilename('StandardFormLogin'), json_encode(
array('johannes' => array('password' => 'test', 'roles' => array('ROLE_USER')))
));

$form = $client->request('GET', '/login')->selectButton('login')->form();

$form['_username'] = 'johannes';
$form['_password'] = 'test';
$client->submit($form);

$client->followRedirect();

// The username is modified between two requests
file_put_contents(FilesystemUserProvider::getFilename('StandardFormLogin'), json_encode(
array('johannes_' => array('password' => 'test', 'roles' => array('ROLE_USER')))
));

$client->request('GET', '/profile');

$crawler = $client->followRedirect();

// The message from the authentication exception thrown during the refreshUser is displayed
$this->assertContains('Username "johannes" does not exist.', $crawler->text());
}

public function getConfigs()
{
return array(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
imports:
- { resource: ./../config/default.yml }

services:
filesystem_user_provider:
class: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Security\User\FilesystemUserProvider
arguments:
- %kernel.test_case%

security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext

providers:
filesystem:
id: filesystem_user_provider

firewalls:
login_form:
pattern: ^/login$
security: false

default:
form_login:
check_path: /login_check
default_target_path: /profile
anonymous: ~
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public function onKernelResponse(FilterResponseEvent $event)
*
* @return TokenInterface|null
*
* @throws UsernameNotFoundException
* @throws \RuntimeException
*/
private function refreshUser(TokenInterface $token)
Expand Down Expand Up @@ -174,7 +175,7 @@ private function refreshUser(TokenInterface $token)
$this->logger->warning(sprintf('Username "%s" could not be found.', $e->getUsername()));
}

return;
throw $e;
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException;
use Symfony\Component\Security\Core\Exception\LogoutException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\HttpFoundation\Request;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -100,6 +101,15 @@ private function handleAuthenticationException(GetResponseForExceptionEvent $eve
$this->logger->info(sprintf('Authentication exception occurred; redirecting to authentication entry point (%s)', $exception->getMessage()));
}

if ($event->getRequest()->hasSession() && !$this->stateless) {
$session = $event->getRequest()->getSession();
$session->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception);

if ($exception instanceof UsernameNotFoundException) {
$session->set(SecurityContextInterface::LAST_USERNAME, $exception->getUsername());
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting things in the session must not be done like this all the time:

  • the request may not have a Session in it, which triggers a fatal error in your code
  • if the firewall is configured as stateless, nothing should be set in the session even if there is a session


try {
$event->setResponse($this->startAuthentication($event->getRequest(), $exception));
} catch (\Exception $e) {
Expand Down
0