Skip to content

Commit 91e1fc7

Browse files
committed
minor #54 split the Security chapter (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- split the Security chapter Commits ------- f612b1c split the Security chapter
2 parents e003574 + f612b1c commit 91e1fc7

File tree

5 files changed

+94
-141
lines changed

5 files changed

+94
-141
lines changed

contributing/code/security.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Security Advisories
9898
.. tip::
9999

100100
You can check your Symfony application for known security vulnerabilities
101-
using the ``security:check`` command. See :ref:`book-security-checking-vulnerabilities`.
101+
using the ``security:check`` command (see :doc:`/security/security_checker`).
102102

103103
This section indexes security vulnerabilities that were fixed in Symfony
104104
releases, starting from Symfony 1.0.0:

security.rst

+11-139
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ sections:
2222

2323
These are followed by a number of small (but still captivating) sections,
2424
like :ref:`logging out <book-security-logging-out>` and
25-
:ref:`encoding user passwords <security-encoding-password>`.
25+
:doc:`encoding user passwords </security/password_encoding>`.
2626

2727
.. _book-security-firewalls:
2828

@@ -591,7 +591,7 @@ It will give you something like this:
591591
Everything will now work exactly like before. But if you have dynamic users
592592
(e.g. from a database), how can you programmatically encode the password
593593
before inserting them into the database? Don't worry, see
594-
:ref:`security-encoding-password` for details.
594+
:doc:`/security/password_encoding` for details.
595595

596596
.. tip::
597597

@@ -1216,48 +1216,6 @@ is defined by the ``target`` parameter above (e.g. the ``homepage``).
12161216
browser cache or restarting your browser usually helps. Some web developer
12171217
tools might be helpful here too.
12181218

1219-
.. _`security-encoding-password`:
1220-
1221-
Dynamically Encoding a Password
1222-
-------------------------------
1223-
1224-
.. note::
1225-
1226-
For historical reasons, Symfony uses the term *"password encoding"* when it
1227-
should really refer to *"password hashing"*. The "encoders" are in fact
1228-
`cryptographic hash functions`_.
1229-
1230-
If, for example, you're storing users in the database, you'll need to encode
1231-
the users' passwords before inserting them. No matter what algorithm you
1232-
configure for your user object, the hashed password can always be determined
1233-
in the following way from a controller::
1234-
1235-
// whatever *your* User object is
1236-
$user = new AppBundle\Entity\User();
1237-
$plainPassword = 'ryanpass';
1238-
$encoder = $this->container->get('security.password_encoder');
1239-
$encoded = $encoder->encodePassword($user, $plainPassword);
1240-
1241-
$user->setPassword($encoded);
1242-
1243-
.. versionadded:: 2.6
1244-
The ``security.password_encoder`` service was introduced in Symfony 2.6.
1245-
1246-
In order for this to work, just make sure that you have the encoder for your
1247-
user class (e.g. ``AppBundle\Entity\User``) configured under the ``encoders``
1248-
key in ``app/config/security.yml``.
1249-
1250-
The ``$encoder`` object also has an ``isPasswordValid`` method, which takes
1251-
the ``User`` object as the first argument and the plain password to check
1252-
as the second argument.
1253-
1254-
.. caution::
1255-
1256-
When you allow a user to submit a plaintext password (e.g. registration
1257-
form, change password form), you *must* have validation that guarantees
1258-
that the password is 4096 characters or fewer. Read more details in
1259-
:ref:`How to implement a simple Registration Form <cookbook-registration-password-max>`.
1260-
12611219
.. _security-role-hierarchy:
12621220

12631221
Hierarchical Roles
@@ -1315,98 +1273,6 @@ In the above configuration, users with ``ROLE_ADMIN`` role will also have the
13151273
``ROLE_USER`` role. The ``ROLE_SUPER_ADMIN`` role has ``ROLE_ADMIN``, ``ROLE_ALLOWED_TO_SWITCH``
13161274
and ``ROLE_USER`` (inherited from ``ROLE_ADMIN``).
13171275

1318-
Stateless Authentication
1319-
------------------------
1320-
1321-
By default, Symfony relies on a cookie (the Session) to persist the security
1322-
context of the user. But if you use certificates or HTTP authentication for
1323-
instance, persistence is not needed as credentials are available for each
1324-
request. In that case, and if you don't need to store anything else between
1325-
requests, you can activate the stateless authentication (which means that no
1326-
cookie will be ever created by Symfony):
1327-
1328-
.. configuration-block::
1329-
1330-
.. code-block:: yaml
1331-
1332-
# app/config/security.yml
1333-
security:
1334-
# ...
1335-
1336-
firewalls:
1337-
main:
1338-
http_basic: ~
1339-
stateless: true
1340-
1341-
.. code-block:: xml
1342-
1343-
<!-- app/config/security.xml -->
1344-
<?xml version="1.0" encoding="UTF-8"?>
1345-
<srv:container xmlns="http://symfony.com/schema/dic/security"
1346-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1347-
xmlns:srv="http://symfony.com/schema/dic/services"
1348-
xsi:schemaLocation="http://symfony.com/schema/dic/services
1349-
http://symfony.com/schema/dic/services/services-1.0.xsd">
1350-
1351-
<config>
1352-
<!-- ... -->
1353-
1354-
<firewall name="main" stateless="true">
1355-
<http-basic />
1356-
</firewall>
1357-
</config>
1358-
</srv:container>
1359-
1360-
.. code-block:: php
1361-
1362-
// app/config/security.php
1363-
$container->loadFromExtension('security', array(
1364-
// ...
1365-
1366-
'firewalls' => array(
1367-
'main' => array('http_basic' => null, 'stateless' => true),
1368-
),
1369-
));
1370-
1371-
.. note::
1372-
1373-
If you use a form login, Symfony will create a cookie even if you set
1374-
``stateless`` to ``true``.
1375-
1376-
.. _book-security-checking-vulnerabilities:
1377-
1378-
Checking for Known Security Vulnerabilities in Dependencies
1379-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1380-
1381-
When using lots of dependencies in your Symfony projects, some of them may
1382-
contain security vulnerabilities. That's why Symfony includes a command called
1383-
``security:check`` that checks your ``composer.lock`` file to find any known
1384-
security vulnerability in your installed dependencies:
1385-
1386-
.. code-block:: bash
1387-
1388-
$ php app/console security:check
1389-
1390-
A good security practice is to execute this command regularly to be able to
1391-
update or replace compromised dependencies as soon as possible. Internally,
1392-
this command uses the public `security advisories database`_ published by the
1393-
FriendsOfPHP organization.
1394-
1395-
.. tip::
1396-
1397-
The ``security:check`` command terminates with a non-zero exit code if
1398-
any of your dependencies is affected by a known security vulnerability.
1399-
Therefore, you can easily integrate it in your build process.
1400-
1401-
.. note::
1402-
1403-
To enable the ``security:check`` command, make sure the
1404-
`SensioDistributionBundle`_ is installed.
1405-
1406-
.. code-block:: bash
1407-
1408-
$ composer require 'sensio/distribution-bundle'
1409-
14101276
Final Words
14111277
-----------
14121278

@@ -1461,8 +1327,14 @@ Authorization (Denying Access)
14611327
security/securing_services
14621328
security/access_control
14631329

1330+
Other Security Related Topics
1331+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1332+
1333+
.. toctree::
1334+
:maxdepth:
1335+
1336+
password_encoding
1337+
security_checker
1338+
14641339
.. _`frameworkextrabundle documentation`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html
1465-
.. _`security advisories database`: https://github.com/FriendsOfPHP/security-advisories
1466-
.. _`cryptographic hash functions`: https://en.wikipedia.org/wiki/Cryptographic_hash_function
14671340
.. _`HWIOAuthBundle`: https://github.com/hwi/HWIOAuthBundle
1468-
.. _`SensioDistributionBundle`: https://packagist.org/packages/sensio/distribution-bundle

security/entity_provider.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ Creating your First User
299299
To add users, you can implement a :doc:`registration form </doctrine/registration_form>`
300300
or add some `fixtures`_. This is just a normal entity, so there's nothing
301301
tricky, *except* that you need to encode each user's password. But don't
302-
worry, Symfony gives you a service that will do this for you. See :ref:`security-encoding-password`
302+
worry, Symfony gives you a service that will do this for you. See :doc:`/security/password_encoding`
303303
for details.
304304

305305
Below is an export of the ``app_users`` table from MySQL with user ``admin``

security/password_encoding.rst

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
.. index::
2+
single: Security; Encoding Passwords
3+
4+
How to Manually Encode a Password
5+
=================================
6+
7+
.. note::
8+
9+
For historical reasons, Symfony uses the term *"password encoding"* when it
10+
should really refer to *"password hashing"*. The "encoders" are in fact
11+
`cryptographic hash functions`_.
12+
13+
If, for example, you're storing users in the database, you'll need to encode
14+
the users' passwords before inserting them. No matter what algorithm you
15+
configure for your user object, the hashed password can always be determined
16+
in the following way from a controller::
17+
18+
// whatever *your* User object is
19+
$user = new AppBundle\Entity\User();
20+
$plainPassword = 'ryanpass';
21+
$encoder = $this->container->get('security.password_encoder');
22+
$encoded = $encoder->encodePassword($user, $plainPassword);
23+
24+
$user->setPassword($encoded);
25+
26+
.. versionadded:: 2.6
27+
The ``security.password_encoder`` service was introduced in Symfony 2.6.
28+
29+
In order for this to work, just make sure that you have the encoder for your
30+
user class (e.g. ``AppBundle\Entity\User``) configured under the ``encoders``
31+
key in ``app/config/security.yml``.
32+
33+
The ``$encoder`` object also has an ``isPasswordValid`` method, which takes
34+
the ``User`` object as the first argument and the plain password to check
35+
as the second argument.
36+
37+
.. caution::
38+
39+
When you allow a user to submit a plaintext password (e.g. registration
40+
form, change password form), you *must* have validation that guarantees
41+
that the password is 4096 characters or fewer. Read more details in
42+
:ref:`How to implement a simple Registration Form <cookbook-registration-password-max>`.
43+
44+
.. _`cryptographic hash functions`: https://en.wikipedia.org/wiki/Cryptographic_hash_function

security/security_checker.rst

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
.. index::
2+
single: Security; Vulnerability Checker
3+
4+
How to Check for Known Security Vulnerabilities in Your Dependencies
5+
====================================================================
6+
7+
When using lots of dependencies in your Symfony projects, some of them may
8+
contain security vulnerabilities. That's why Symfony includes a command called
9+
``security:check`` that checks your ``composer.lock`` file to find any known
10+
security vulnerability in your installed dependencies:
11+
12+
.. code-block:: bash
13+
14+
$ php app/console security:check
15+
16+
A good security practice is to execute this command regularly to be able to
17+
update or replace compromised dependencies as soon as possible. Internally,
18+
this command uses the public `security advisories database`_ published by the
19+
FriendsOfPHP organization.
20+
21+
.. tip::
22+
23+
The ``security:check`` command terminates with a non-zero exit code if
24+
any of your dependencies is affected by a known security vulnerability.
25+
Therefore, you can easily integrate it in your build process.
26+
27+
.. note::
28+
29+
To enable the ``security:check`` command, make sure the
30+
`SensioDistributionBundle`_ is installed.
31+
32+
.. code-block:: bash
33+
34+
$ composer require 'sensio/distribution-bundle'
35+
36+
.. _`security advisories database`: https://github.com/FriendsOfPHP/security-advisories
37+
.. _`SensioDistributionBundle`: https://packagist.org/packages/sensio/distribution-bundle

0 commit comments

Comments
 (0)