How Does the Security access_control Work?

How Does the Security access_control Work?

For each incoming request, Symfony checks each access_control entry to find one that matches the current request. As soon as it finds a matching access_control entry, it stops - only the first matching access_control is used to enforce access.

Each access_control has several options that configure two different things:

  1. should the incoming request match this access control entry
  2. once it matches, should some sort of access restriction be enforced:

1. Matching Options

Symfony creates an instance of Symfony\Component\HttpFoundation\RequestMatcher for each access_control entry, which determines whether or not a given access control should be used on this request. The following access_control options are used for matching:

  • path: a regular expression (without delimiters)
  • ip or ips: netmasks are also supported
  • port: an integer
  • host: a regular expression
  • methods: one or many methods

Take the following access_control entries as an example:

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. access_control:
    5. - { path: '^/admin', roles: ROLE_USER_PORT, ip: 127.0.0.1, port: 8080 }
    6. - { path: '^/admin', roles: ROLE_USER_IP, ip: 127.0.0.1 }
    7. - { path: '^/admin', roles: ROLE_USER_HOST, host: symfony\.com$ }
    8. - { path: '^/admin', roles: ROLE_USER_METHOD, methods: [POST, PUT] }
    9. # when defining multiple roles, users must have at least one of them (it's like an OR condition)
    10. - { path: '^/admin', roles: [ROLE_MANAGER, ROLE_ADMIN] }
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <!-- ... -->
    10. <rule path="^/admin" role="ROLE_USER_PORT" ip="127.0.0.1" port="8080"/>
    11. <rule path="^/admin" role="ROLE_USER_IP" ip="127.0.0.1"/>
    12. <rule path="^/admin" role="ROLE_USER_HOST" host="symfony\.com$"/>
    13. <rule path="^/admin" role="ROLE_USER_METHOD" methods="POST, PUT"/>
    14. <!-- when defining multiple roles, users must have at least one of them (it's like an OR condition) -->
    15. <rule path="^/admin" roles="ROLE_ADMIN, ROLE_MANAGER"/>
    16. </config>
    17. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. $container->loadFromExtension('security', [
    3. // ...
    4. 'access_control' => [
    5. [
    6. 'path' => '^/admin',
    7. 'roles' => 'ROLE_USER_PORT',
    8. 'ip' => '127.0.0.1',
    9. 'port' => '8080',
    10. ],
    11. [
    12. 'path' => '^/admin',
    13. 'roles' => 'ROLE_USER_IP',
    14. 'ips' => '127.0.0.1',
    15. ],
    16. [
    17. 'path' => '^/admin',
    18. 'roles' => 'ROLE_USER_HOST',
    19. 'host' => 'symfony\.com$',
    20. ],
    21. [
    22. 'path' => '^/admin',
    23. 'roles' => 'ROLE_USER_METHOD',
    24. 'methods' => 'POST, PUT',
    25. ],
    26. [
    27. 'path' => '^/admin',
    28. // when defining multiple roles, users must have at least one of them (it's like an OR condition)
    29. 'roles' => ['ROLE_MANAGER', 'ROLE_ADMIN'],
    30. ],
    31. ],
    32. ]);

For each incoming request, Symfony will decide which access_control to use based on the URI, the client’s IP address, the incoming host name, and the request method. Remember, the first rule that matches is used, and if ip, port, host or method are not specified for an entry, that access_control will match any ip, port, host or method:

URIIPPORTHOSTMETHODaccess_controlWhy?
/admin/user127.0.0.180example.comGETrule #2 (ROLE_USER_IP)The URI matches path and the IP matches ip.
/admin/user127.0.0.180symfony.comGETrule #2 (ROLE_USER_IP)The path and ip still match. This would also match the ROLE_USER_HOST entry, but only the first access_control match is used.
/admin/user127.0.0.18080symfony.comGETrule #1 (ROLE_USER_PORT)The path, ip and port match.
/admin/user168.0.0.180symfony.comGETrule #3 (ROLE_USER_HOST)The ip doesn’t match the first rule, so the second rule (which matches) is used.
/admin/user168.0.0.180symfony.comPOSTrule #3 (ROLE_USER_HOST)The second rule still matches. This would also match the third rule (ROLE_USER_METHOD), but only the first matched access_control is used.
/admin/user168.0.0.180example.comPOSTrule #4 (ROLE_USER_METHOD)The ip and host don’t match the first two entries, but the third - ROLE_USER_METHOD - matches and is used.
/admin/user168.0.0.180example.comGETrule #4 (ROLE_MANAGER)The ip, host and method prevent the first three entries from matching. But since the URI matches the path pattern, then the ROLE_MANAGER (or the ROLE_ADMIN) is used.
/foo127.0.0.180symfony.comPOSTmatches no entriesThis doesn’t match any access_control rules, since its URI doesn’t match any of the path values.

Caution

Matching the URI is done without $_GET parameters. Deny access in PHP code if you want to disallow access based on $_GET parameter values.

2. Access Enforcement

Once Symfony has decided which access_control entry matches (if any), it then enforces access restrictions based on the roles, allow_if and requires_channel options:

  • roles If the user does not have the given role, then access is denied (internally, an Symfony\Component\Security\Core\Exception\AccessDeniedException is thrown). If this value is an array of multiple roles, the user must have at least one of them.
  • allow_if If the expression returns false, then access is denied;
  • requires_channel If the incoming request’s channel (e.g. http) does not match this value (e.g. https), the user will be redirected (e.g. redirected from http to https, or vice versa).

Tip

Behind the scenes, the array value of roles is passed as the $attributes argument to each voter in the application with the Symfony\Component\HttpFoundation\Request as $subject. You can learn how to use your custom attributes by reading How to Use Voters to Check User Permissions.

Caution

If you define both roles and allow_if, and your Access Decision Strategy is the default one (affirmative), then the user will be granted access if there’s at least one valid condition. If this behavior doesn’t fit your needs, change the Access Decision Strategy.

Tip

If access is denied, the system will try to authenticate the user if not already (e.g. redirect the user to the login page). If the user is already logged in, the 403 “access denied” error page will be shown. See How to Customize Error Pages for more information.

Matching access_control By IP

Certain situations may arise when you need to have an access_control entry that only matches requests coming from some IP address or range. For example, this could be used to deny access to a URL pattern to all requests except those from a trusted, internal server.

Caution

As you’ll read in the explanation below the example, the ips option does not restrict to a specific IP address. Instead, using the ips key means that the access_control entry will only match this IP address, and users accessing it from a different IP address will continue down the access_control list.

Here is an example of how you configure some example /internal* URL pattern so that it is only accessible by requests from the local server itself:

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. access_control:
    5. #
    6. # the 'ips' option supports IP addresses and subnet masks
    7. - { path: '^/internal', roles: IS_AUTHENTICATED_ANONYMOUSLY, ips: [127.0.0.1, ::1, 192.168.0.1/24] }
    8. - { path: '^/internal', roles: ROLE_NO_ACCESS }
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <!-- ... -->
    10. <!-- the 'ips' option supports IP addresses and subnet masks -->
    11. <rule path="^/internal" role="IS_AUTHENTICATED_ANONYMOUSLY">
    12. <ip>127.0.0.1</ip>
    13. <ip>::1</ip>
    14. </rule>
    15. <rule path="^/internal" role="ROLE_NO_ACCESS"/>
    16. </config>
    17. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. $container->loadFromExtension('security', [
    3. // ...
    4. 'access_control' => [
    5. [
    6. 'path' => '^/internal',
    7. 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY',
    8. // the 'ips' option supports IP addresses and subnet masks
    9. 'ips' => ['127.0.0.1', '::1'],
    10. ],
    11. [
    12. 'path' => '^/internal',
    13. 'roles' => 'ROLE_NO_ACCESS',
    14. ],
    15. ],
    16. ]);

Here is how it works when the path is /internal/something coming from the external IP address 10.0.0.1:

  • The first access control rule is ignored as the path matches but the IP address does not match either of the IPs listed;
  • The second access control rule is enabled (the only restriction being the path) and so it matches. If you make sure that no users ever have ROLE_NO_ACCESS, then access is denied (ROLE_NO_ACCESS can be anything that does not match an existing role, it only serves as a trick to always deny access).

But if the same request comes from 127.0.0.1 or ::1 (the IPv6 loopback address):

  • Now, the first access control rule is enabled as both the path and the ip match: access is allowed as the user always has the IS_AUTHENTICATED_ANONYMOUSLY role.
  • The second access rule is not examined as the first rule matched.

Securing by an Expression

Once an access_control entry is matched, you can deny access via the roles key or use more complex logic with an expression in the allow_if key:

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. access_control:
    5. -
    6. path: ^/_internal/secure
    7. # the 'role' and 'allow-if' options work like an OR expression, so
    8. # access is granted if the expression is TRUE or the user has ROLE_ADMIN
    9. roles: 'ROLE_ADMIN'
    10. allow_if: "'127.0.0.1' == request.getClientIp() or request.headers.has('X-Secure-Access')"
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <!-- ... -->
    10. <!-- the 'role' and 'allow-if' options work like an OR expression, so
    11. access is granted if the expression is TRUE or the user has ROLE_ADMIN -->
    12. <rule path="^/_internal/secure"
    13. role="ROLE_ADMIN"
    14. allow-if="'127.0.0.1' == request.getClientIp() or request.headers.has('X-Secure-Access')"/>
    15. </config>
    16. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. $container->loadFromExtension('security', [
    3. // ...
    4. 'access_control' => [
    5. [
    6. 'path' => '^/_internal/secure',
    7. // the 'role' and 'allow-if' options work like an OR expression, so
    8. // access is granted if the expression is TRUE or the user has ROLE_ADMIN
    9. 'roles' => 'ROLE_ADMIN',
    10. 'allow_if' => '"127.0.0.1" == request.getClientIp() or request.headers.has("X-Secure-Access")',
    11. ],
    12. ],
    13. ]);

In this case, when the user tries to access any URL starting with /_internal/secure, they will only be granted access if the IP address is 127.0.0.1 or a secure header, or if the user has the ROLE_ADMIN role.

Note

Internally allow_if triggers the built-in Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter as like it was part of the attributes defined in the roles option.

Inside the expression, you have access to a number of different variables and functions including request, which is the Symfony Symfony\Component\HttpFoundation\Request object (see Request).

For a list of the other functions and variables, see functions and variables.

Tip

The allow_if expressions can also contain custom functions registered with expression providers.

Restrict to a port

Add the port option to any access_control entries to require users to access those URLs via a specific port. This could be useful for example for localhost:8080.

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. access_control:
    5. - { path: ^/cart/checkout, roles: IS_AUTHENTICATED_ANONYMOUSLY, port: 8080 }
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <!-- ... -->
    10. <rule path="^/cart/checkout"
    11. role="IS_AUTHENTICATED_ANONYMOUSLY"
    12. port="8080"
    13. />
    14. </config>
    15. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. $container->loadFromExtension('security', [
    3. // ...
    4. 'access_control' => [
    5. [
    6. 'path' => '^/cart/checkout',
    7. 'role' => 'IS_AUTHENTICATED_ANONYMOUSLY',
    8. 'port' => '8080',
    9. ],
    10. ],
    11. ]);

Forcing a Channel (http, https)

You can also require a user to access a URL via SSL; use the requires_channel argument in any access_control entries. If this access_control is matched and the request is using the http channel, the user will be redirected to https:

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. access_control:
    5. - { path: ^/cart/checkout, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <!-- ... -->
    10. <rule path="^/cart/checkout"
    11. role="IS_AUTHENTICATED_ANONYMOUSLY"
    12. requires-channel="https"
    13. />
    14. </config>
    15. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. $container->loadFromExtension('security', [
    3. // ...
    4. 'access_control' => [
    5. [
    6. 'path' => '^/cart/checkout',
    7. 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY',
    8. 'requires_channel' => 'https',
    9. ],
    10. ],
    11. ]);

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.