Symfony:在SecurityContext中找不到用于防火墙后面的路由的令牌

时间:2015-05-28 09:29:48

标签: php symfony error-handling bots

在我的Symfony2应用程序中,我构建了一个异常监听器,让我了解未处理的错误。

当机器人访问防火墙后面的页面时,我收到有关以下错误的消息:

A Token was not found in the SecurityContext.

我还检索以下数据:

User agent  Mozilla/5.0 (compatible; AhrefsBot/5.0; +http://ahrefs.com/robot/)
Trace as string #0 /home/foodmeup.net/production/releases/20150527141710/app/cache/prod/classes.php(2951): Symfony\Component\Security\Http\Firewall\AccessListener->handle(Object(Symfony\Component\HttpKernel\Event\GetResponseEvent))
#1 [internal function]: Symfony\Component\Security\Http\Firewall->onKernelRequest(Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher))
#2 /home/foodmeup.net/production/releases/20150527141710/app/cache/prod/classes.php(2205): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher))
#3 /home/foodmeup.net/production/releases/20150527141710/app/cache/prod/classes.php(2138): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, 'kernel.request', Object(Symfony\Component\HttpKernel\Event\GetResponseEvent))
#4 /home/foodmeup.net/production/releases/20150527141710/app/cache/prod/classes.php(2299): Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.request', Object(Symfony\Component\HttpKernel\Event\GetResponseEvent))
#5 /home/foodmeup.net/production/releases/20150527141710/app/bootstrap.php.cache(3017): Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.request', Object(Symfony\Component\HttpKernel\Event\GetResponseEvent))
#6 /home/foodmeup.net/production/releases/20150527141710/app/bootstrap.php.cache(2990): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#7 /home/foodmeup.net/production/releases/20150527141710/app/bootstrap.php.cache(3139): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#8 /home/foodmeup.net/production/releases/20150527141710/app/bootstrap.php.cache(2383): Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#9 /home/foodmeup.net/production/releases/20150527141710/web/app.php(28): Symfony\Component\HttpKernel\Kernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#10 {main}

这发生在我上次更新以来的所有网站页面上,但我无法弄清楚问题是什么。 如果我自己访问该页面,则没有问题,也没有例外。

我对我设置的防火墙的理解是,如果有人试图访问受保护资源,他会被重定向到登录页面而不会抛出任何错误。在这里,我担心一些用户可能会登陆错误页面而不是被重定向到登录页面。当我想通过从抛出错误时访问referer来复制错误时,我被正确地重定向,所以我不明白抛出错误与重定向用户的情况是什么。

编辑:

我的异常侦听器服务:

exception_listener:
    class: %exception_listener.class%
    arguments: [@router, @session, @security.token_storage, @email_manager, @doctrine, "@=service('kernel').getEnvironment()", @security.authorization_checker]
    tags:
        - { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: 250 }

我的异常监听器代码:

<?php

namespace AppBundle\EventListener;

use AppBundle\Application\Core\EmailManager;
use AppBundle\Application\Core\JournalManager;
use AppBundle\Entity\User\User;
use AppBundle\Security\Voter\SubscriptionVoter;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;


class ExceptionListener
{
    /**
     * @var Router
     */
    private $router;

    /**
     * @var Session
     */
    private $session;

    /**
     * @var TokenStorage
     */
    private $tokenStorage;

    /**
     * @var EmailManager
     */
    private $emailManager;

    /**
     * @var null
     */
    private $environment;

    /**
     * @var AuthorizationChecker
     */
    private $authorizationChecker;
    /**
     * @var Registry
     */
    private $doctrine;

    public function __construct(Router $router, Session $session, TokenStorage $tokenStorage, EmailManager $emailManager, Registry $doctrine ,$environment=null, AuthorizationChecker $authorizationChecker)
    {
        $this->router = $router;
        $this->session = $session;
        $this->tokenStorage = $tokenStorage;
        $this->emailManager = $emailManager;
        $this->environment = $environment;
        $this->authorizationChecker = $authorizationChecker;
        $this->doctrine = $doctrine;
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        try {
            /** @var $exception */
            $exception = $event->getException();
            $request = $event->getRequest();
            $referer = $request->headers->get('referer');

            $manager = $this->doctrine->getManager('logging');
            $journalManager = new JournalManager($manager);

            if($exception->getCode() == 403)
            {
                if ($this->authorizationChecker->isGranted(SubscriptionVoter::HAS_SUBSCRIPTION) && !$this->authorizationChecker->isGranted(SubscriptionVoter::SUBSCRIPTION_VALID))
                {
                    $this->session->getFlashBag()->add('warning',"La page précédente n'est pas accessible avec ce portfolio car le paiement n'est pas à jour. Vous devez actualiser votre paiement.");
                    $response = new RedirectResponse($this->router->generate('renew_subscription'));
                }
                else
                {
                    $this->session->getFlashBag()->add('warning',"La page précédente n'est pas accessible avec vos droits d'accès et vous avez été redirigé vers l'accueil du site.");
                    $response = new RedirectResponse($this->router->generate('home'));
                }

                $event->setResponse($response);
            }
            elseif ($exception->getMessage() == "Couldn't connect to host, Elasticsearch down?" || $exception->getCode() == 52)
            {
                $this->session->getFlashBag()->add('warning', "La service de recherche du site a arrêté de fonctionner. Renouvellez votre dernière action si celle si n'a pas été suivie d'effet d'ici 2 minutes.");
                $event->setResponse(new RedirectResponse($request->headers->get('referer') ?: $this->router->generate('home')));
            }
            elseif (
                !($exception->getCode() == 404 && !$referer) &&
                !($this->contains($exception->getMessage(), array('object not found', 'A Token was not found in the SecurityContext', 'No route found for')) && !strpos($referer, 'foodmeup')) &&
                !in_array($this->environment, array('dev', 'test')) &&
                !$journalManager->errorExists($exception, $request->getUri(), 1)
            )
            {
                $user = is_object($this->tokenStorage->getToken()) ? $this->tokenStorage->getToken()->getUser() : null;
                $user = $user instanceOf User ? $user : null;
                $code = $exception->getCode();

                $this->emailManager->sendEmail(
                    'error@foodmeup.net',
                    'foodmeup@foodmeup.net',
                    ':Core/Email:error.html.twig',
                    "Une erreur $code s'est produite sur le site",
                    array(
                        'date' => new \DateTime(),
                        'user' => $user,
                        'exception' => $exception,
                        'referer' => $request->headers->get('referer'),
                        'current' => $request->getUri(),
                        'user_agent' => $_SERVER['HTTP_USER_AGENT']
                    )
                );

                $journalManager->addErrorLog($exception, $request->getUri());
            }
        } catch (\Exception $e)
        {
        }

    }

    private function contains($str, array $arr)
    {
        foreach($arr as $a) {
            if (stripos($str,$a) !== false) return true;
        }
        return false;
    }
}

我的防火墙:

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false
    guest:
        pattern:        /(public/|$|genemu-captcha-refresh|media/cache/)
        anonymous:      true
        context:        main_auth
    main:
        pattern:        ^/
        anonymous:      false
        provider:       main
        context:        main_auth
        switch_user: { role: ROLE_ADMIN, parameter: _switch_user_parameter }
        form_login:
            login_path: fos_user_security_login
            check_path: fos_user_security_check
            success_handler: authentication_site_handler
        logout:
            path:       fos_user_security_logout
            target:     /
        remember_me:
            key:      "%secret%"
            lifetime: 86400 #en secondes
            path:     /
            domain:   ~ # Prend la valeur par défaut du domaine courant depuis $_SERVER
        oauth:
            remember_me: true
            resource_owners:
                facebook:           "/loginhwi/check-facebook"
                github:             "/loginhwi/check-github"
                google:             "/loginhwi/check-google"
                twitter:            "/loginhwi/check-twitter"
                linkedin:           "/loginhwi/check-linkedin"
                flickr:             "/loginhwi/check-flickr"
            login_path:        fos_user_security_login
            check_path:        fos_user_security_check
            failure_path:      fos_user_security_login
            success_handler:  authentication_site_handler
            oauth_user_provider:
                service: fosubuser.provider

精确度:

  • emailManager只是发送一封来自给定参数的电子邮件。 它工作正常
  • journalManager只记录错误并帮助我 过滤掉已记录的错误

1 个答案:

答案 0 :(得分:4)

不要担心这种情况/错误。当你自己测试你的情况时,你会得到预期的行为(重定向到登录页面),就像其他任何&#34;真实的&#34;冲浪者。

然后

  

用户代理Mozilla / 5.0(兼容; AhrefsBot / 5.0;   + http://ahrefs.com/robot/

你可以看到Ahrefs bot要求该页面。当你作为真正的冲浪者重定向到其他页面时,它会使用&#34; header&#34;行动。但是机器人不会处理标题。所以实际上这是业余程序员经常犯的错误。他们提出了像

这样的东西
if($notallowed){
 header('Location: /login');
}
//... only logged stuff ...// 

然后它适用于&#34;真实&#34;冲浪者,但机器人可以通过并达到记录的东西&#34;。因此,在这种情况下,它需要&#34; die&#34;标题后面的命令(必须永远不是用户的坏样式)或抛出异常(良好的风格)。

结论:你提到你最近在更改代码后得到了异常,但很可能机器人开始抓取以前没有抓过的新内容。所以很可能你应该添加一个规则来跳过该异常,因为它只适用于机器人。但是你当然也应该检查你最近的变化。您还可以查看此类异常的日志并检查用户代理,以确保它仅应用于机器人。