8000 [Validator] : Fix "PHP Warning: Undefined array key 1" in NotCompromisedPasswordValidator by KevinVanSonsbeek · Pull Request #46948 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] : Fix "PHP Warning: Undefined array key 1" in NotCompromisedPasswordValidator #46948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public function validate($value, Constraint $constraint)
}

foreach (explode("\r\n", $result) as $line) {
if (!str_contains($line, ':')) {
continue;
}

[$hashSuffix, $count] = explode(':', $line);

if ($hashPrefix.$hashSuffix === $hash && $constraint->threshold <= (int) $count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,31 @@ public function testInvalidPasswordCustomEndpoint()
->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);
Expand Down Expand Up @@ -202,11 +227,11 @@ public function provideErrorSkippingConstraints(): iterable
}
}

private function createHttpClientStub(): HttpClientInterface
private function createHttpClientStub(?string $returnValue = null): HttpClientInterface
{
$httpClientStub = $this->createMock(HttpClientInterface::class);
$httpClientStub->method('request')->willReturnCallback(
function (string $method, string $url): ResponseInterface {
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
Expand All @@ -219,7 +244,7 @@ public function getResponse(): ResponseInterface
$responseStub = $this->createMock(ResponseInterface::class);
$responseStub
->method('getContent')
->willReturn(implode("\r\n", self::RETURN));
->willReturn($returnValue ?? implode("\r\n", self::RETURN));

return $responseStub;
}
Expand Down
0