|
| 1 | +How Does the Security access_control Work? |
| 2 | +========================================== |
| 3 | + |
| 4 | +For each incoming request, Symfony checks each ``access_control`` entry |
| 5 | +to find *one* that matches the current request. As soon as it finds a matching |
| 6 | +``access_control`` entry, it stops - only the **first** matching ``access_control`` |
| 7 | +is used to enforce access. |
| 8 | + |
| 9 | +Each ``access_control`` has several options that configure two different |
| 10 | +things: |
| 11 | + |
| 12 | +#. :ref:`should the incoming request match this access control entry <security-book-access-control-matching-options>` |
| 13 | +#. :ref:`once it matches, should some sort of access restriction be enforced <security-book-access-control-enforcement-options>`: |
| 14 | + |
| 15 | +.. _security-book-access-control-matching-options: |
| 16 | + |
| 17 | +1. Matching Options |
| 18 | +------------------- |
| 19 | + |
| 20 | +Symfony creates an instance of :class:`Symfony\\Component\\HttpFoundation\\RequestMatcher` |
| 21 | +for each ``access_control`` entry, which determines whether or not a given |
| 22 | +access control should be used on this request. The following ``access_control`` |
| 23 | +options are used for matching: |
| 24 | + |
| 25 | +* ``path`` |
| 26 | +* ``ip`` or ``ips`` |
| 27 | +* ``host`` |
| 28 | +* ``methods`` |
| 29 | + |
| 30 | +Take the following ``access_control`` entries as an example: |
| 31 | + |
| 32 | +.. configuration-block:: |
| 33 | + |
| 34 | + .. code-block:: yaml |
| 35 | +
|
| 36 | + # app/config/security.yml |
| 37 | + security: |
| 38 | + # ... |
| 39 | + access_control: |
| 40 | + - { path: ^/admin, roles: ROLE_USER_IP, ip: 127.0.0.1 } |
| 41 | + - { path: ^/admin, roles: ROLE_USER_HOST, host: symfony\.com$ } |
| 42 | + - { path: ^/admin, roles: ROLE_USER_METHOD, methods: [POST, PUT] } |
| 43 | + - { path: ^/admin, roles: ROLE_USER } |
| 44 | +
|
| 45 | + .. code-block:: xml |
| 46 | +
|
| 47 | + <!-- app/config/security.xml --> |
| 48 | + <?xml version="1.0" encoding="UTF-8"?> |
| 49 | + <srv:container xmlns="http://symfony.com/schema/dic/security" |
| 50 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 51 | + xmlns:srv="http://symfony.com/schema/dic/services" |
| 52 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 53 | + http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 54 | +
|
| 55 | + <config> |
| 56 | + <!-- ... --> |
| 57 | + <access-control> |
| 58 | + <rule path="^/admin" role="ROLE_USER_IP" ip="127.0.0.1" /> |
| 59 | + <rule path="^/admin" role="ROLE_USER_HOST" host="symfony\.com$" /> |
| 60 | + <rule path="^/admin" role="ROLE_USER_METHOD" method="POST, PUT" /> |
| 61 | + <rule path="^/admin" role="ROLE_USER" /> |
| 62 | + </access-control> |
| 63 | + </config> |
| 64 | + </srv:container> |
| 65 | +
|
| 66 | + .. code-block:: php |
| 67 | +
|
| 68 | + // app/config/security.php |
| 69 | + $container->loadFromExtension('security', array( |
| 70 | + // ... |
| 71 | + 'access_control' => array( |
| 72 | + array( |
| 73 | + 'path' => '^/admin', |
| 74 | + 'role' => 'ROLE_USER_IP', |
| 75 | + 'ip' => '127.0.0.1', |
| 76 | + ), |
| 77 | + array( |
| 78 | + 'path' => '^/admin', |
| 79 | + 'role' => 'ROLE_USER_HOST', |
| 80 | + 'host' => 'symfony\.com$', |
| 81 | + ), |
| 82 | + array( |
| 83 | + 'path' => '^/admin', |
| 84 | + 'role' => 'ROLE_USER_METHOD', |
| 85 | + 'method' => 'POST, PUT', |
| 86 | + ), |
| 87 | + array( |
| 88 | + 'path' => '^/admin', |
| 89 | + 'role' => 'ROLE_USER', |
| 90 | + ), |
| 91 | + ), |
| 92 | + )); |
| 93 | +
|
| 94 | +For each incoming request, Symfony will decide which ``access_control`` |
| 95 | +to use based on the URI, the client's IP address, the incoming host name, |
| 96 | +and the request method. Remember, the first rule that matches is used, and |
| 97 | +if ``ip``, ``host`` or ``method`` are not specified for an entry, that ``access_control`` |
| 98 | +will match any ``ip``, ``host`` or ``method``: |
| 99 | + |
| 100 | ++-----------------+-------------+-------------+------------+--------------------------------+-------------------------------------------------------------+ |
| 101 | +| URI | IP | HOST | METHOD | ``access_control`` | Why? | |
| 102 | ++=================+=============+=============+============+================================+=============================================================+ |
| 103 | +| ``/admin/user`` | 127.0.0.1 | example.com | GET | rule #1 (``ROLE_USER_IP``) | The URI matches ``path`` and the IP matches ``ip``. | |
| 104 | ++-----------------+-------------+-------------+------------+--------------------------------+-------------------------------------------------------------+ |
| 105 | +| ``/admin/user`` | 127.0.0.1 | symfony.com | GET | rule #1 (``ROLE_USER_IP``) | The ``path`` and ``ip`` still match. This would also match | |
| 106 | +| | | | | | the ``ROLE_USER_HOST`` entry, but *only* the **first** | |
| 107 | +| | | | | | ``access_control`` match is used. | |
| 108 | ++-----------------+-------------+-------------+------------+--------------------------------+-------------------------------------------------------------+ |
| 109 | +| ``/admin/user`` | 168.0.0.1 | symfony.com | GET | rule #2 (``ROLE_USER_HOST``) | The ``ip`` doesn't match the first rule, so the second | |
| 110 | +| | | | | | rule (which matches) is used. | |
| 111 | ++-----------------+-------------+-------------+------------+--------------------------------+-------------------------------------------------------------+ |
| 112 | +| ``/admin/user`` | 168.0.0.1 | symfony.com | POST | rule #2 (``ROLE_USER_HOST``) | The second rule still matches. This would also match the | |
| 113 | +| | | | | | third rule (``ROLE_USER_METHOD``), but only the **first** | |
| 114 | +| | | | | | matched ``access_control`` is used. | |
| 115 | ++-----------------+-------------+-------------+------------+--------------------------------+-------------------------------------------------------------+ |
| 116 | +| ``/admin/user`` | 168.0.0.1 | example.com | POST | rule #3 (``ROLE_USER_METHOD``) | The ``ip`` and ``host`` don't match the first two entries, | |
| 117 | +| | | | | | but the third - ``ROLE_USER_METHOD`` - matches and is used. | |
| 118 | ++-----------------+-------------+-------------+------------+--------------------------------+-------------------------------------------------------------+ |
| 119 | +| ``/admin/user`` | 168.0.0.1 | example.com | GET | rule #4 (``ROLE_USER``) | The ``ip``, ``host`` and ``method`` prevent the first | |
| 120 | +| | | | | | three entries from matching. But since the URI matches the | |
| 121 | +| | | | | | ``path`` pattern of the ``ROLE_USER`` entry, it is used. | |
| 122 | ++-----------------+-------------+-------------+------------+--------------------------------+-------------------------------------------------------------+ |
| 123 | +| ``/foo`` | 127.0.0.1 | symfony.com | POST | matches no entries | This doesn't match any ``access_control`` rules, since its | |
| 124 | +| | | | | | URI doesn't match any of the ``path`` values. | |
| 125 | ++-----------------+-------------+-------------+------------+--------------------------------+-------------------------------------------------------------+ |
| 126 | + |
| 127 | +.. _security-book-access-control-enforcement-options: |
| 128 | + |
| 129 | +2. Access Enforcement |
| 130 | +--------------------- |
| 131 | + |
| 132 | +Once Symfony has decided which ``access_control`` entry matches (if any), |
| 133 | +it then *enforces* access restrictions based on the ``roles`` and ``requires_channel`` |
| 134 | +options: |
| 135 | + |
| 136 | +* ``role`` If the user does not have the given role(s), then access is denied |
| 137 | + (internally, an :class:`Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException` |
| 138 | + is thrown); |
| 139 | + |
| 140 | +* ``requires_channel`` If the incoming request's channel (e.g. ``http``) |
| 141 | + does not match this value (e.g. ``https``), the user will be redirected |
| 142 | + (e.g. redirected from ``http`` to ``https``, or vice versa). |
| 143 | + |
| 144 | +.. tip:: |
| 145 | + |
| 146 | + If access is denied, the system will try to authenticate the user if not |
| 147 | + already (e.g. redirect the user to the login page). If the user is already |
| 148 | + logged in, the 403 "access denied" error page will be shown. See |
| 149 | + :doc:`/cookbook/controller/error_pages` for more information. |
| 150 | + |
| 151 | +.. _book-security-securing-ip: |
| 152 | + |
| 153 | +Matching access_control By IP |
| 154 | +----------------------------- |
| 155 | + |
| 156 | +Certain situations may arise when you need to have an ``access_control`` |
| 157 | +entry that *only* matches requests coming from some IP address or range. |
| 158 | +For example, this *could* be used to deny access to a URL pattern to all |
| 159 | +requests *except* those from a trusted, internal server. |
| 160 | + |
| 161 | +.. caution:: |
| 162 | + |
| 163 | + As you'll read in the explanation below the example, the ``ips`` option |
| 164 | + does not restrict to a specific IP address. Instead, using the ``ips`` |
| 165 | + key means that the ``access_control`` entry will only match this IP address, |
| 166 | + and users accessing it from a different IP address will continue down |
| 167 | + the ``access_control`` list. |
| 168 | + |
| 169 | +Here is an example of how you configure some example ``/internal*`` URL |
| 170 | +pattern so that it is only accessible by requests from the local server itself: |
| 171 | + |
| 172 | +.. configuration-block:: |
| 173 | + |
| 174 | + .. code-block:: yaml |
| 175 | +
|
| 176 | + # app/config/security.yml |
| 177 | + security: |
| 178 | + # ... |
| 179 | + access_control: |
| 180 | + # |
| 181 | + - { path: ^/internal, roles: IS_AUTHENTICATED_ANONYMOUSLY, ips: [127.0.0.1, ::1] } |
| 182 | + - { path: ^/internal, roles: ROLE_NO_ACCESS } |
| 183 | +
|
| 184 | + .. code-block:: xml |
| 185 | +
|
| 186 | + <!-- app/config/security.xml --> |
| 187 | + <?xml version="1.0" encoding="UTF-8"?> |
| 188 | + <srv:container xmlns="http://symfony.com/schema/dic/security" |
| 189 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 190 | + xmlns:srv="http://symfony.com/schema/dic/services" |
| 191 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 192 | + http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 193 | +
|
| 194 | + <config> |
| 195 | + <!-- ... --> |
| 196 | + <access-control> |
| 197 | + <rule path="^/esi" role="IS_AUTHENTICATED_ANONYMOUSLY" |
| 198 | + ips="127.0.0.1, ::1" /> |
| 199 | + <rule path="^/esi" role="ROLE_NO_ACCESS" /> |
| 200 | + </access-control> |
| 201 | + </config> |
| 202 | + </srv:container> |
| 203 | +
|
| 204 | + .. code-block:: php |
| 205 | +
|
| 206 | + // app/config/security.php |
| 207 | + $container->loadFromExtension('security', array( |
| 208 | + // ... |
| 209 | + 'access_control' => array( |
| 210 | + array( |
| 211 | + 'path' => '^/esi', |
| 212 | + 'role' => 'IS_AUTHENTICATED_ANONYMOUSLY', |
| 213 | + 'ips' => '127.0.0.1, ::1' |
| 214 | + ), |
| 215 | + array( |
| 216 | + 'path' => '^/esi', |
| 217 | + 'role' => 'ROLE_NO_ACCESS' |
| 218 | + ), |
| 219 | + ), |
| 220 | + )); |
| 221 | +
|
| 222 | +Here is how it works when the path is ``/internal/something`` coming from |
| 223 | +the external IP address ``10.0.0.1``: |
| 224 | + |
| 225 | +* The first access control rule is ignored as the ``path`` matches but the |
| 226 | + IP address does not match either of the IPs listed; |
| 227 | + |
| 228 | +* The second access control rule is enabled (the only restriction being the |
| 229 | + ``path``) and so it matches. If you make sure that no users ever have |
| 230 | + ``ROLE_NO_ACCESS``, then access is denied (``ROLE_NO_ACCESS`` can be anything |
| 231 | + that does not match an existing role, it just serves as a trick to always |
| 232 | + deny access). |
| 233 | + |
| 234 | +But if the same request comes from ``127.0.0.1`` or ``::1`` (the IPv6 loopback |
| 235 | +address): |
| 236 | + |
| 237 | +* Now, the first access control rule is enabled as both the ``path`` and the |
| 238 | + ``ip`` match: access is allowed as the user always has the |
| 239 | + ``IS_AUTHENTICATED_ANONYMOUSLY`` role. |
| 240 | + |
| 241 | +* The second access rule is not examined as the first rule matched. |
| 242 | + |
| 243 | +.. _book-security-securing-channel: |
| 244 | + |
| 245 | +Forcing a Channel (http, https) |
| 246 | +------------------------------- |
| 247 | + |
| 248 | +You can also require a user to access a URL via SSL; just use the |
| 249 | +``requires_channel`` argument in any ``access_control`` entries. If this |
| 250 | +``access_control`` is matched and the request is using the ``http`` channel, |
| 251 | +the user will be redirected to ``https``: |
| 252 | + |
| 253 | +.. configuration-block:: |
| 254 | + |
| 255 | + .. code-block:: yaml |
| 256 | +
|
| 257 | + # app/config/security.yml |
| 258 | + security: |
| 259 | + # ... |
| 260 | + access_control: |
| 261 | + - { path: ^/cart/checkout, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https } |
| 262 | +
|
| 263 | + .. code-block:: xml |
| 264 | +
|
| 265 | + <!-- app/config/security.xml --> |
| 266 | + <?xml version="1.0" encoding="UTF-8"?> |
| 267 | + <srv:container xmlns="http://symfony.com/schema/dic/security" |
| 268 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 269 | + xmlns:srv="http://symfony.com/schema/dic/services" |
| 270 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 271 | + http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 272 | +
|
| 273 | + <access-control> |
| 274 | + <rule path="^/cart/checkout" |
| 275 | + role="IS_AUTHENTICATED_ANONYMOUSLY" |
| 276 | + requires-channel="https" /> |
| 277 | + </access-control> |
| 278 | + </srv:container> |
| 279 | +
|
| 280 | + .. code-block:: php |
| 281 | +
|
| 282 | + // app/config/security.php |
| 283 | + $container->loadFromExtension('security', array( |
| 284 | + 'access_control' => array( |
| 285 | + array( |
| 286 | + 'path' => '^/cart/checkout', |
| 287 | + 'role' => 'IS_AUTHENTICATED_ANONYMOUSLY', |
| 288 | + 'requires_channel' => 'https', |
| 289 | + ), |
| 290 | + ), |
| 291 | + )); |