1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Tests\Constraints;
use Symfony\Component\Validator\Constraints\Luhn;
use Symfony\Component\Validator\Constraints\NotCompromisedPassword;
use Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class NotCompromisedPasswordValidatorTest extends ConstraintValidatorTestCase
{
private const PASSWORD_TRIGGERING_AN_ERROR = 'apiError';
private const PASSWORD_TRIGGERING_AN_ERROR_RANGE_URL = 'https://api.pwnedpasswords.com/range/3EF27'; // https://api.pwnedpasswords.com/range/3EF27 is the range for the value "apiError"
private const PASSWORD_LEAKED = 'maman';
private const PASSWORD_NOT_LEAKED = ']<0585"%sb^5aa$w6!b38",,72?dp3r4\45b28Hy';
private const PASSWORD_NON_UTF8_LEAKED = 'мама';
private const PASSWORD_NON_UTF8_NOT_LEAKED = 'м<в0dp3r4\45b28Hy';
private const RETURN = [
'35E033023A46402F94CFB4F654C5BFE44A1:1',
'35F079CECCC31812288257CD770AA7968D7:53',
'36039744C253F9B2A4E90CBEDB02EBFB82D:5', // UTF-8 leaked password: maman
'273CA8A2A78C9B2D724144F4FAF4D221C86:6', // ISO-8859-5 leaked password: мама
'3686792BBC66A72D40D928ED15621124CFE:7',
'36EEC709091B810AA240179A44317ED415C:2',
'EE6EB9C0DFA0F07098CEDB11ECC7AFF9D4E:0', // UTF-8 not leaked password: ]<0585"%sb^5aa$w6!b38",,72?dp3r4\45b28Hy
'FC9F37E51AACD6B692A62769267590D46B8:0', // ISO-8859-5 non leaked password: м<в0dp3r4\45b28Hy
];
protected function createValidator(): ConstraintValidatorInterface
{
// Pass HttpClient::create() instead of the mock to run the tests against the real API
return new NotCompromisedPasswordValidator($this->createHttpClientStub());
}
public function testNullIsValid()
{
$this->validator->validate(null, new NotCompromisedPassword());
$this->assertNoViolation();
}
public function testEmptyStringIsValid()
{
$this->validator->validate('', new NotCompromisedPassword());
$this->assertNoViolation();
}
public function testInvalidPasswordButDisabled()
{
$r = new \ReflectionProperty($this->validator, 'enabled');
$r->setValue($this->validator, false);
$this->validator->validate(self::PASSWORD_LEAKED, new NotCompromisedPassword());
$this->assertNoViolation();
}
public function testInvalidPassword()
{
$constraint = new NotCompromisedPassword();
$this->validator->validate(self::PASSWORD_LEAKED, $constraint);
$this->buildViolation($constraint->message)
->setCode(NotCompromisedPassword::COMPROMISED_PASSWORD_ERROR)
->assertRaised();
}
public function testThresholdReached()
{
$constraint = new NotCompromisedPassword(threshold: 3);
$this->validator->validate(self::PASSWORD_LEAKED, $constraint);
$this->buildViolation($constraint->message)
->setCode(NotCompromisedPassword::COMPROMISED_PASSWORD_ERROR)
->assertRaised();
}
public function testThresholdNotReached()
{
$this->validator->validate(self::PASSWORD_LEAKED, new NotCompromisedPassword(threshold: 10));
$this->assertNoViolation();
}
#[\PHPUnit\Framework\Attributes\Group('legacy')]
public function testThresholdNotReachedDoctrineStyle()
{
$this->validator->validate(self::PASSWORD_LEAKED, new NotCompromisedPassword(['threshold' => 10]));
$this->assertNoViolation();
}
public function testValidPassword()
{
$this->validator->validate(self::PASSWORD_NOT_LEAKED, new NotCompromisedPassword());
$this->assertNoViolation();
}
public function testNonUtf8CharsetValid()
{
$validator = new NotCompromisedPasswordValidator($this->createHttpClientStub(), 'ISO-8859-5');
$validator->validate(mb_convert_encoding(self::PASSWORD_NON_UTF8_NOT_LEAKED, 'ISO-8859-5', 'UTF-8'), new NotCompromisedPassword());
$this->assertNoViolation();
}
public function testNonUtf8CharsetInvalid()
{
$constraint = new NotCompromisedPassword();
$this->context = $this->createContext();
$validator = new NotCompromisedPasswordValidator($this->createHttpClientStub(), 'ISO-8859-5');
$validator->initialize($this->context);
$validator->validate(mb_convert_encoding(self::PASSWORD_NON_UTF8_LEAKED, 'ISO-8859-5', 'UTF-8'), $constraint);
$this->buildViolation($constraint->message)
->setCode(NotCompromisedPassword::COMPROMISED_PASSWORD_ERROR)
->assertRaised();
}
public function testInvalidPasswordCustomEndpoint()
{
$endpoint = 'https://password-check.internal.example.com/range/%s';
// 50D74 - first 5 bytes of uppercase SHA1 hash of self::PASSWORD_LEAKED
$expectedEndpointUrl = 'https://password-check.internal.example.com/range/50D74';
$constraint = new NotCompromisedPassword();
$this->context = $this->createContext();
$validator = new NotCompromisedPasswordValidator(
$this->createHttpClientStubCustomEndpoint($expectedEndpointUrl),
'UTF-8',
true,
$endpoint
);
$validator->initialize($this->context);
$validator->validate(self::PASSWORD_LEAKED, $constraint);
$this->buildViolation($constraint->message)
->setCode(NotCompromisedPassword::COMPROMISED_PASSWORD_ERROR)
->assertRaised();
}
public function testEndpointWithInvalidValueInReturn()
{
$returnValue = implode(
"\r\n",
[
'36039744C253F9B2A4E90CBEDB02EBFB82D:5',
'This should not break the validator',
'3686792BBC66A72D40D928ED15621124CFE:7',
'36EEC709091B810AA240179A44317ED415C:2',
'',
]
);
$validator = new NotCompromisedPasswordValidator(
$this->createHttpClientStub($returnValue),
'UTF-8',
true,
'https://password-check.internal.example.com/range/%s'
);
$validator->validate(self::PASSWORD_NOT_LEAKED, new NotCompromisedPassword());
$this->assertNoViolation();
}
public function testInvalidConstraint()
{
$this->expectException(UnexpectedTypeException::class);
$this->validator->validate(null, new Luhn());
}
public function testInvalidValue()
{
$this->expectException(UnexpectedTypeException::class);
$this->validator->validate([], new NotCompromisedPassword());
}
public function testApiError()
{
$this->expectException(ExceptionInterface::class);
$this->expectExceptionMessage('Problem contacting the Have I been Pwned API.');
$this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotCompromisedPassword());
}
public function testApiErrorSkipped()
{
$this->expectNotToPerformAssertions();
$this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotCompromisedPassword(skipOnError: true));
}
#[\PHPUnit\Framework\Attributes\Group('legacy')]
public function testApiErrorSkippedDoctrineStyle()
{
$this->expectNotToPerformAssertions();
$this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotCompromisedPassword(['skipOnError' => true]));
}
private function createHttpClientStub(?string $returnValue = null): HttpClientInterface
{
$httpClientStub = $this->createMock(HttpClientInterface::class);
$httpClientStub->method('request')->willReturnCallback(
function (string $method, string $url) use ($returnValue): ResponseInterface {
if (self::PASSWORD_TRIGGERING_AN_ERROR_RANGE_URL === $url) {
throw new class('Problem contacting the Have I been Pwned API.') extends \Exception implements ServerExceptionInterface {
public function getResponse(): ResponseInterface
{
throw new \RuntimeException('Not implemented');
}
};
}
$responseStub = $this->createMock(ResponseInterface::class);
$responseStub
->method('getContent')
->willReturn($returnValue ?? implode("\r\n", self::RETURN));
return $responseStub;
}
);
return $httpClientStub;
}
private function createHttpClientStubCustomEndpoint($expectedEndpoint): HttpClientInterface
{
$httpClientStub = $this->createMock(HttpClientInterface::class);
$httpClientStub->method('request')->with('GET', $expectedEndpoint)->willReturnCallback(
function (string $method, string $url): ResponseInterface {
$responseStub = $this->createMock(ResponseInterface::class);
$responseStub
->method('getContent')
->willReturn(implode("\r\n", self::RETURN));
return $responseStub;
}
);
return $httpClientStub;
}
}
|