Skip to content

[WIP] Do not expose routing 404 exception for protected route #11480

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
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ public function testRoutingErrorIsNotExposedForProtectedResourceWhenLoggedInWith
$this->assertNotEquals(404, $client->getResponse()->getStatusCode());
}

/**
* @dataProvider getConfigs
*/
public function testRoutingErrorIsNotExposedForNotExistingProtectedResource($config)
{
if (strpos(PHP_OS, "WIN") === 0 && version_compare(phpversion(), "5.3.9", "<")) {
Copy link
Member

Choose a reason for hiding this comment

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

Symfony code usually checks if the OS is Windows using this snippet:

if (defined('PHP_WINDOWS_VERSION_BUILD')) {
    // ...
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I just copied this from the test above this one, should I change it everywhere?

$this->markTestSkipped('Test hangs on Windows & PHP due to https://bugs.php.net/bug.php?id=60120 fixed in http://svn.php.net/viewvc?view=revision&revision=318366');
}

$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
$client->insulate();

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

$this->assertNotEquals(404, $client->getResponse()->getStatusCode());
}

/**
* @dataProvider getConfigs
* @group ip_whitelist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ security:
- { path: ^/secured-by-one-ip$, ip: 10.10.10.10, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/secured-by-two-ips$, ips: [1.1.1.1, 2.2.2.2], roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/highly_protected_resource$, roles: IS_ADMIN }
- { path: ^/protected_not_existing_resource$, roles: IS_ADMIN }
- { path: ^/protected-via-expression$, allow_if: "(is_anonymous() and request.headers.get('user-agent') matches '/Firefox/i') or has_role('ROLE_USER')" }
- { path: .*, roles: IS_AUTHENTICATED_FULLY }
17 changes: 14 additions & 3 deletions src/Symfony/Component/Security/Http/Firewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace Symfony\Component\Security\Http;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down Expand Up @@ -49,9 +51,9 @@ public function __construct(FirewallMapInterface $map, EventDispatcherInterface
/**
* Handles security.
*
* @param GetResponseEvent $event An GetResponseEvent instance
* @param KernelEvent $event An GetResponseEvent instance
*/
public function onKernelRequest(GetResponseEvent $event)
public function onKernelRequest(KernelEvent $event)
{
if (!$event->isMasterRequest()) {
return;
Expand Down Expand Up @@ -84,6 +86,14 @@ public function onKernelFinishRequest(FinishRequestEvent $event)
}
}

public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof HttpException && 404 === $exception->getStatusCode()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you might be able to use Response::HTTP_NOT_FOUND not found here.

Copy link
Contributor

Choose a reason for hiding this comment

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

The framework normally uses the status codes directly afair.

Copy link
Member

Choose a reason for hiding this comment

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

@apfelbox you are right. See #11401 as an example.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, sorry for noise

$this->onKernelRequest($event);
}
}

/**
* {@inheritdoc}
*/
Expand All @@ -92,6 +102,7 @@ public static function getSubscribedEvents()
return array(
KernelEvents::REQUEST => array('onKernelRequest', 8),
KernelEvents::FINISH_REQUEST => 'onKernelFinishRequest',
KernelEvents::EXCEPTION => array('onKernelException', 900),
);
}
}