@@ -1228,6 +1228,92 @@ cookie will be ever created by Symfony):
12281228 If you use a form login, Symfony will create a cookie even if you set
12291229 ``stateless `` to ``true ``.
12301230
1231+ Utilities
1232+ ---------
1233+
1234+ .. versionadded :: 2.2
1235+ The ``StringUtils `` and ``SecureRandom `` classes were introduced in Symfony
1236+ 2.2
1237+
1238+ The Symfony Security component comes with a collection of nice utilities related
1239+ to security. These utilities are used by Symfony, but you should also use
1240+ them if you want to solve the problem they address.
1241+
1242+ Comparing Strings
1243+ ~~~~~~~~~~~~~~~~~
1244+
1245+ The time it takes to compare two strings depends on their differences. This
1246+ can be used by an attacker when the two strings represent a password for
1247+ instance; it is known as a `Timing attack `_.
1248+
1249+ Internally, when comparing two passwords, Symfony uses a constant-time
1250+ algorithm; you can use the same strategy in your own code thanks to the
1251+ :class: `Symfony\\ Component\\ Security\\ Core\\ Util\\ StringUtils ` class::
1252+
1253+ use Symfony\Component\Security\Core\Util\StringUtils;
1254+
1255+ // is password1 equals to password2?
1256+ $bool = StringUtils::equals($password1, $password2);
1257+
1258+ Generating a secure random Number
1259+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1260+
1261+ Whenever you need to generate a secure random number, you are highly
1262+ encouraged to use the Symfony
1263+ :class: `Symfony\\ Component\\ Security\\ Core\\ Util\\ SecureRandom ` class::
1264+
1265+ use Symfony\Component\Security\Core\Util\SecureRandom;
1266+
1267+ $generator = new SecureRandom();
1268+ $random = $generator->nextBytes(10);
1269+
1270+ The
1271+ :method: `Symfony\\ Component\\ Security\\ Core\\ Util\\ SecureRandom::nextBytes `
1272+ methods returns a random string composed of the number of characters passed as
1273+ an argument (10 in the above example).
1274+
1275+ The SecureRandom class works better when OpenSSL is installed but when it's
1276+ not available, it falls back to an internal algorithm, which needs a seed file
1277+ to work correctly. Just pass a file name to enable it::
1278+
1279+ $generator = new SecureRandom('/some/path/to/store/the/seed.txt');
1280+ $random = $generator->nextBytes(10);
1281+
1282+ .. note ::
1283+
1284+ You can also access a secure random instance directly from the Symfony
1285+ dependency injection container; its name is ``security.secure_random ``.
1286+
1287+ .. _book-security-checking-vulnerabilities :
1288+
1289+ Checking for Known Security Vulnerabilities in Dependencies
1290+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1291+
1292+ .. versionadded :: 2.5
1293+ The ``security:check `` command was introduced in Symfony 2.5. This command is
1294+ included in ``SensioDistributionBundle ``, which has to be registered in your
1295+ application in order to use this command.
1296+
1297+ When using lots of dependencies in your Symfony projects, some of them may
1298+ contain security vulnerabilities. That's why Symfony includes a command called
1299+ ``security:check `` that checks your ``composer.lock `` file to find any known
1300+ security vulnerability in your installed dependencies:
1301+
1302+ .. code-block :: bash
1303+
1304+ $ php app/console security:check
1305+
1306+ A good security practice is to execute this command regularly to be able to
1307+ update or replace compromised dependencies as soon as possible. Internally,
1308+ this command uses the public `security advisories database `_ published by the
1309+ FriendsOfPHP organization.
1310+
1311+ .. tip ::
1312+
1313+ The ``security:check `` command terminates with a non-zero exit code if
1314+ any of your dependencies is affected by a known security vulnerability.
1315+ Therefore, you can easily integrate it in your build process.
1316+
12311317Final Words
12321318-----------
12331319
@@ -1256,3 +1342,4 @@ Learn more from the Cookbook
12561342
12571343.. _`online tool` : https://www.dailycred.com/blog/12/bcrypt-calculator
12581344.. _`frameworkextrabundle documentation` : http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html
1345+ .. _`security advisories database` : https://github.com/FriendsOfPHP/security-advisories
0 commit comments