Convert InsufficientAuthenticationException to HttpAccessDeniedException · Issue #8467 · symfony/symfony · GitHub
Skip to content

Convert InsufficientAuthenticationException to HttpAccessDeniedException #8467

Closed

Description

@Koc
    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt|fragment)|css|images|js)/
            security: false

        main:
            pattern:    /
            ololo: ~
            provider: users_entity
            anonymous: ~

    access_control:
        - { path: ^/admin, roles: ROLE_ADMIN }

open as anonymous something like /admin/dashboard and got 500. Expected 403.

[2013-07-10 17:27:49] security.INFO: Populated SecurityContext with an anonymous Token [] []
[2013-07-10 17:27:49] security.INFO: No expression found; abstaining from voting. [] []
[2013-07-10 17:27:49] security.DEBUG: Access is denied (user is not fully authenticated) by "/vhosts/www.ololo.com/new/vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/AccessListener.php" at line 73; redirecting to authentication entry point [] []
[2013-07-10 17:27:49] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException: "Full authentication is required to access this resource." at /vhosts/www.ololo.com/new/vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php line 109 {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\InsufficientAuthenticationException: Full authentication is required to access this resource. at /vhosts/www.ololo.com/new/vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php:109, Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException: Access Denied at /vhosts/www.ololo.com/new/vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/AccessListener.php:73)"} []

Activity

  1. stof commented on Jul 10, 2013

    @stof
    Member

    This is weird. It should be handled by the ExceptionController of the security component already

  2. Koc commented on Jul 10, 2013

    @Koc
    ContributorAuthor

    Please explain a little bit particularly why.

  3. stof commented on Jul 10, 2013

    @stof
    Member

    https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php is the exception listener I'm talking about. The Firewall is registering on the kernel.exception event to handle these exceptions.

  4. Koc commented on Jul 10, 2013

    @Koc
    ContributorAuthor

    Oh, I see. It causes because my configuration hasn't login_form. But security component hasn't ExceptionController.

  5. jfsimon commented on Jul 15, 2013

    @jfsimon
    Contributor

    @Koc can you confirm there is no framework problem here and close this issue?

  6. Koc commented on Jul 15, 2013

    @Koc
    ContributorAuthor

    I cann't confitm that.

  7. jfsimon commented on Jul 15, 2013

    @jfsimon
    Contributor

    @Koc then can you point the real issue and write a failing test?

  8. Koc commented on Jul 15, 2013

    @Koc
    ContributorAuthor

    I think that better ask @schmittjoh is this fw problem or no.

  9. jakzal commented on Dec 19, 2013

    @jakzal
    Contributor

    Might be related: #9823

  10. added a commit that references this issue on Dec 29, 2013
  11. fabpot commented on Dec 29, 2013

    @fabpot
    Member

    @Koc #9879 should have fixed this issue. Can you confirm?

  12. Koc commented on Dec 29, 2013

    @Koc
    ContributorAuthor

    No, this issue isn't fixed. I got

    Full authentication is required to access this resource.
    500 Internal Server Error - InsufficientAuthenticationException
    1 linked Exception:  AccessDeniedException
    
  13. Koc commented on Dec 29, 2013

    @Koc
    ContributorAuthor

    Tryed on the 0285bfd revision

  14. lwojciechowski commented on Mar 8, 2014

    @lwojciechowski

    I can confirm this with Symfony 2.4.2 and using simple_preauth.

  15. pkruithof commented on Mar 14, 2014

    @pkruithof
    Contributor

    Also confirming for 2.4.2, I think I know how this occurs in my setup: I have an Api which uses a token supplied via a header to authenticate. There is no authentication entry point: if there's no token supplied, a 401 response should be returned directly.

    This line shows that the exception is re-thrown when this is the case.

  16. 32 remaining items

  17. goetas commented on Jun 21, 2017

    @goetas
    Contributor

    @maarekj solution works well :)
    @merk solution works well :)

  18. maarekj commented on Jun 21, 2017

    @maarekj
    Contributor

    @goetas Sorry, but which solution ?

  19. goetas commented on Jun 21, 2017

    @goetas
    Contributor

    @maarekj sorry typo... i meant @merk

  20. vpassapera commented on Jul 31, 2017

    @vpassapera

    I'm still seeing this issue in 3.3 using LexikJWTAuthenticationBundle

  21. dmaicher commented on Aug 21, 2017

    @dmaicher
    Contributor

    @weaverryan @chalasr @stof as you are mergers of the security component: What do you think?

    @jakzal proposed some options here: #8467 (comment)

    One more idea would be to throw an AccessDeniedHttpException here instead of re-throwing the AuthenticationException:

    https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php#L183

    I could work on a patch if we agree on a solution 😊

  22. Th3Mouk commented on Nov 11, 2017

    @Th3Mouk
    Contributor

    I had the same issue on 3.4.0-BETA3
    I resolved it putting the backend before the public firewall:

    Pattern always must be restrictive at the start, and be degressive

        firewalls:
            dev:
                pattern: ^/(_(profiler|wdt)|css|images|js)/
                security: false
    
            backend:
                anonymous: ~
                pattern:    ^/admin
                provider: admin
    
                form_login:
                    login_path: admin_login
                    check_path: admin_login
                    default_target_path: admin_dashboard
    
            public:
                anonymous: ~
                pattern:    ^/
                provider: customer
    
        access_control:
            - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/admin, roles: ROLE_ADMIN }
  23. Rebolon commented on Jan 11, 2018

    @Rebolon

    I get the same kind of error with Symfony4 + Flex (this is a proof of concept project)
    A route protected by a firewall and grant access return a 500 AccessDeniedException instead of a 403.

    A sample route that return the 500 :
    http://localhost:85/demo/login/json/isloggedin

    Here is my security.yaml

    security:
        encoders:
            # @todo should use password encoding, more info here: https://symfony.com/doc/current/security.html#c-encoding-the-user-s-password
            Symfony\Component\Security\Core\User\User: plaintext
        # https://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
        providers:
            in_memory:
                memory:
                    users:
                        test:
                            password:           test
                            roles:              ROLE_USER
                        admin:
                            password:           admin
                            roles:              [ROLE_USER, ROLE_ADMIN]
    
        firewalls:
            dev:
                pattern: ^/(_(profiler|wdt)|css|images|js)/
                security: false
            vuejs:
                pattern: ^/(demo/(vuejs|form|login/(json|json/isloggedin|json/logout))|api)
                anonymous: ~
                json_login:
                    check_path: /demo/login/json
                    # this doesn't work, see in the security.yaml:12 for more explanation
                    #check_path: demo_login_json_check
                logout:
                    path:   demo_login_json_logout
                    target: index
                    invalidate_session: true
            main:
                pattern: ^/demo/login/(secured|standard|authenticate|logout)
                anonymous: ~
    
                # activate different ways to authenticate
    
                # http_basic: ~
                # https://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate
    
                # form_login: ~
                # https://symfony.com/doc/current/cookbook/security/form_login_setup.html
    
                form_login:
                    default_target_path: demo_secured_page
                    login_path: demo_login_standard
                    check_path: demo_login_standard_check
                    # field names for the username and password fields
                    username_parameter: login_username
                    password_parameter: login_password
    
                    # csrf token options
                    csrf_parameter:       "%csrf_token_parameter%"
                    csrf_token_generator: security.csrf.token_manager
                    csrf_token_id:        "%csrf_token_id%"
    
                logout:
                    path:   demo_login_standard_logout
                    target: index
                    invalidate_session: true
    

    Here is the action controller:

        /**
         * Try to test this security when the one on the bottom works Security("is_granted('IS_AUTHENTICATED_FULLY')")
         *
         * call it with .json extension and check if you have a 200
         *
         * @todo: should we let it as is, or always return a 200 and in the Json content set the isLoggedIn to 0 or 1 ?
         * For instance i stay on my first choice
         *
         * @Security("is_granted('IS_AUTHENTICATED_FULLY')")
         * @Route(
         *     "/demo/login/json/isloggedin",
         *     name="demo_secured_page_is_logged_in",
         *     )
         * @Method({"GET"})
         */
        public function isLoggedIn() {
            // will be usefull if we decide to return always 200 + the real Json content represented by isLoggedIn: 0|1
            $authenticated = $this->isGranted('IS_AUTHENTICATED_FULLY');
    
            return new JsonResponse(['isLoggedIn' => (int)$authenticated, ]);
        }
    

    And finally the composer.json:

    {
        "type": "project",
        "license": "proprietary",
        "require": {
            "php": "^7.0",
            "ext-curl": "*",
            "ext-pdo_sqlite": "*",
            "doctrine/doctrine-migrations-bundle": "^1.3",
            "javiereguiluz/easyadmin-bundle": "^1.17",
            "php-http/httplug-pack": "^1.0",
            "rebolon/api-pack": "^1.0.2",
            "sensio/framework-extra-bundle": "^5.1.0",
            "symfony/console": "^4.0.0",
            "symfony/framework-bundle": "^4.0.0",
            "symfony/monolog-bundle": "^3.1.0",
            "symfony/profiler-pack": "^1.0",
            "symfony/yaml": "^4.0.0",
            "webonyx/graphql-php": "^0.11.5"
        },
        "require-dev": {
            "doctrine/doctrine-fixtures-bundle": "^3.0",
            "symfony/dotenv": "^4.0.0",
            "symfony/flex": "^1.0",
            "symfony/webpack-encore-pack": "^1.0"
        },
        "minimum-stability": "dev",
        "prefer-stable": true,
        "repositories": [
            {
                "type": "vcs",
                "url":  "git@github.com:Rebolon/api-pack.git"
            }
        ],
        "config": {
            "preferred-install": {
                "*": "dist"
            },
            "sort-packages": true
        },
        "autoload": {
            "psr-4": {
                "App\\": "src/"
            }
        },
        "autoload-dev": {
            "psr-4": {
                "App\\Tests\\": "tests/"
            }
        },
        "scripts": {
            "auto-scripts": {
                "cache:clear": "symfony-cmd",
                "assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
            },
            "post-install-cmd": [
                "@auto-scripts"
            ],
            "post-update-cmd": [
                "@auto-scripts"
            ]
        },
        "conflict": {
            "symfony/symfony": "*",
            "symfony/twig-bundle": "<3.3",
            "symfony/debug": "<3.3"
        },
        "extra": {
            "symfony": {
                "id": "01BXPDHBAP11MNR97CR7E97D3F",
                "allow-contrib": false
            }
        }
    }
    

    And maybe the way i installed the app:

    composer create-project symfony/skeleton sf-flex-encore-vuejs
    cd sf-flex-encore-vuejs
    composer req encore annotations twig api http profiler log doctrine-migrations admin webonyx/graphql-php
    composer require --dev doctrine/doctrine-fixtures-bundle
    

    Here is the project uri: https://github.com/Rebolon/php-sf-flex-webpack-encore-vuejs

  24. Rebolon commented on Jan 15, 2018

    @Rebolon

    when i look at the Security component documentation https://symfony.com/doc/master/bundles/SensioFrameworkExtraBundle/annotations/security.html

    I can see that we have to specify the status_code if we want an HTTP Exception instead of an AccessDeniedException

    For my configuration it seems that the json_login is the explanation of the problem because this is its own behavior (why ? i don't know the code always throw this exception, not an HttpException)

  25. devpetrov commented on Feb 2, 2018

    @devpetrov

    If this would help.

    I'm encountering this issue with the following configuration:

    providers:
        myProvider:
            id: myProviderService
    
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
    
        main:
            anonymous: ~
    
        application_main:
            pattern: ^/
            provider: myProvider:
            anonymous: ~
            myAuthenticator:
    	login_path: login_route
            logout:
                path: /logout
                target: index
                invalidate_session: false
                handlers: [myLogoutHandler]
    

    The problem is the presence main firewall and may be the way it is configured. When I remove it and leave only dev and application_main everything works as normal.

  26. xabbuh commented on Feb 2, 2018

    @xabbuh
    Member

    @chaos-drone your main firewall catches all requests that do not match the pattern of the dev firewall. Thus the application_main firewall is never reached.

  27. added a commit that references this issue on Oct 17, 2018
  28. Koc commented on Oct 17, 2018

    @Koc
    ContributorAuthor

    I hope #28801 fixes this issue

  29. Rebolon commented on Oct 18, 2018

    @Rebolon

    @Koc your fix is only for 2.8 or also for 3.4+ and 4+ ?

  30. dmaicher commented on Oct 18, 2018

    @dmaicher
    Contributor

    @Rebolon it will be merged up in the other branches 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions