Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitddfd4e6

Browse files
committed
feature#11300 Added docs for the NotCompromisedPassword constraint (javiereguiluz)
This PR was squashed before being merged into the master branch (closes#11300).Discussion----------Added docs for the NotCompromisedPassword constraintDocumentssymfony/symfony#27738.Commits-------78a9387 Added docs for the NotCompromisedPassword constraint
2 parents00e8731 +78a9387 commitddfd4e6

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

‎reference/constraints.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Validation Constraints Reference
6565
constraints/Expression
6666
constraints/All
6767
constraints/UserPassword
68+
constraints/NotCompromisedPassword
6869
constraints/Valid
6970
constraints/Traverse
7071

‎reference/constraints/NotPwned.rst‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
NotCompromisedPassword
2+
======================
3+
4+
..versionadded::4.3
5+
6+
The ``NotCompromisedPassword`` constraint was introduced in Symfony 4.3.
7+
8+
Validates that the given password has not been compromised by checking that is
9+
not included in any of the public data breaches tracked by `haveibeenpwned.com`_.
10+
11+
========== ===================================================================
12+
Applies to:ref:`property or method<validation-property-target>`
13+
Options - `groups`_
14+
- `message`_
15+
- `payload`_
16+
- `skipOnError`_
17+
- `threshold`_
18+
Class:class:`Symfony\\Component\\Validator\\Constraints\\NotCompromisedPassword`
19+
Validator:class:`Symfony\\Component\\Validator\\Constraints\\NotCompromisedPasswordValidator`
20+
========== ===================================================================
21+
22+
Basic Usage
23+
-----------
24+
25+
The following constraint ensures that the ``rawPassword`` property of the
26+
``User`` class doesn't store a compromised password:
27+
28+
..configuration-block::
29+
30+
..code-block::php-annotations
31+
32+
// src/Entity/User.php
33+
namespace App\Entity;
34+
35+
use Symfony\Component\Validator\Constraints as Assert;
36+
37+
class User
38+
{
39+
// ...
40+
41+
/**
42+
* @Assert\NotCompromisedPassword
43+
*/
44+
protected $rawPassword;
45+
}
46+
47+
..code-block::yaml
48+
49+
# config/validator/validation.yaml
50+
App\Entity\User:
51+
properties:
52+
rawPassword:
53+
-NotCompromisedPassword
54+
55+
..code-block::xml
56+
57+
<!-- config/validator/validation.xml-->
58+
<?xml version="1.0" encoding="UTF-8" ?>
59+
<constraint-mappingxmlns="http://symfony.com/schema/dic/constraint-mapping"
60+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
61+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
62+
63+
<classname="App\Entity\User">
64+
<propertyname="rawPassword">
65+
<constraintname="NotCompromisedPassword"></constraint>
66+
</property>
67+
</class>
68+
</constraint-mapping>
69+
70+
..code-block::php
71+
72+
// src/Entity/User.php
73+
namespace App\Entity;
74+
75+
use Symfony\Component\Validator\Mapping\ClassMetadata;
76+
use Symfony\Component\Validator\Constraints as Assert;
77+
78+
class User
79+
{
80+
public static function loadValidatorMetadata(ClassMetadata $metadata)
81+
{
82+
$metadata->addPropertyConstraint('rawPassword', new Assert\NotCompromisedPassword());
83+
}
84+
}
85+
86+
In order to make the password validation, this constraint doesn't send the raw
87+
password value to the ``haveibeenpwned.com`` API. Instead, it follows a secure
88+
process known as `k-anonimity password validation`_.
89+
90+
In practice, the raw password is hashed using SHA-1 and only the first bytes of
91+
the hash are sent. Then, the ``haveibeenpwned.com`` API compares those bytes
92+
with the SHA-1 hashes of all leaked passwords and returns the list of hashes
93+
that start with those same bytes. That's how the constraint can check if the
94+
password has been compromised without fully disclosing it.
95+
96+
For example, if the password is ``test``, the entire SHA-1 hash is
97+
``a94a8fe5ccb19ba61c4c0873d391e987982fbbd3`` but the validator only sends
98+
``a94a8`` to the ``haveibeenpwned.com`` API.
99+
100+
Available Options
101+
-----------------
102+
103+
..include::/reference/constraints/_groups-option.rst.inc
104+
105+
message
106+
~~~~~~~
107+
108+
**type**: ``string`` **default**: ``This password has been leaked in a data breach, it must not be used. Please use another password.``
109+
110+
The default message supplied when the password has been compromised.
111+
112+
..include::/reference/constraints/_payload-option.rst.inc
113+
114+
skipOnError
115+
~~~~~~~~~~~
116+
117+
**type**: ``boolean`` **default**: ``false``
118+
119+
When the HTTP request made to the ``haveibeenpwned.com`` API fails for any
120+
reason, an exception is thrown (no validation error is displayed). Set this
121+
option to ``true`` to not throw the exception and consider the password valid.
122+
123+
threshold
124+
~~~~~~~~~
125+
126+
**type**: ``integer`` **default**: ``1``
127+
128+
This value defines the number of times a password should have been leaked
129+
publicly to consider it compromised. Think carefully before setting this option
130+
to a higher value because it could decrease the security of your application.
131+
132+
.. _`haveibeenpwned.com`:https://haveibeenpwned.com/
133+
.. _`k-anonimity password validation`:https://blog.cloudflare.com/validating-leaked-passwords-with-k-anonymity/

‎reference/constraints/map.rst.inc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ String Constraints
2323
* :doc:`Json</reference/constraints/Json>`
2424
* :doc:`Uuid</reference/constraints/Uuid>`
2525
* :doc:`UserPassword</reference/constraints/UserPassword>`
26+
* :doc:`NotCompromisedPassword</reference/constraints/NotCompromisedPassword>`
2627

2728
Comparison Constraints
2829
~~~~~~~~~~~~~~~~~~~~~~
@@ -85,7 +86,7 @@ Other Constraints
8586
* :doc:`Expression</reference/constraints/Expression>`
8687
* :doc:`All</reference/constraints/All>`
8788
* :doc:`Valid</reference/constraints/Valid>`
89+
* :doc:`Traverse</reference/constraints/Traverse>`
8890
* :doc:`Collection</reference/constraints/Collection>`
8991
* :doc:`Count</reference/constraints/Count>`
9092
* :doc:`UniqueEntity</reference/constraints/UniqueEntity>`
91-
* :doc:`Traverse</reference/constraints/Traverse>`

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp