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

Appearance settings

Commit 5ba4925

Browse filesBrowse files
committed
bug #39859 [Security] Replace message data in JSON security error response (wouterj)
This PR was merged into the 5.2 branch. Discussion ---------- [Security] Replace message data in JSON security error response | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #39663 | License | MIT | Doc PR | n/a Commits ------- 5e5795a [Security] Replace message data in JSON security error response
2 parents 8fc46dc + 5e5795a commit 5ba4925
Copy full SHA for 5ba4925

File tree

2 files changed

+21
-2
lines changed
Filter options

2 files changed

+21
-2
lines changed

‎src/Symfony/Component/Security/Http/Authenticator/JsonLoginAuthenticator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Authenticator/JsonLoginAuthenticator.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
126126
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
127127
{
128128
if (null === $this->failureHandler) {
129-
$errorMessage = $exception->getMessageKey();
130-
131129
if (null !== $this->translator) {
132130
$errorMessage = $this->translator->trans($exception->getMessageKey(), $exception->getMessageData(), 'security');
131+
} else {
132+
$errorMessage = strtr($exception->getMessageKey(), $exception->getMessageData());
133133
}
134134

135135
return new JsonResponse(['error' => $errorMessage], JsonResponse::HTTP_UNAUTHORIZED);

‎src/Symfony/Component/Security/Http/Tests/Authenticator/JsonLoginAuthenticatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Tests/Authenticator/JsonLoginAuthenticatorTest.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ public function testAuthenticationFailureWithTranslator()
147147
$this->assertSame(['error' => 'foo'], json_decode($response->getContent(), true));
148148
}
149149

150+
public function testOnFailureReplacesMessageDataWithoutTranslator()
151+
{
152+
$this->setUpAuthenticator();
153+
154+
$response = $this->authenticator->onAuthenticationFailure(new Request(), new class() extends AuthenticationException {
155+
public function getMessageData(): array
156+
{
157+
return ['%failed_attempts%' => 3];
158+
}
159+
160+
public function getMessageKey(): string
161+
{
162+
return 'Session locked after %failed_attempts% failed attempts.';
163+
}
164+
});
165+
166+
$this->assertSame(['error' => 'Session locked after 3 failed attempts.'], json_decode($response->getContent(), true));
167+
}
168+
150169
private function setUpAuthenticator(array $options = [])
151170
{
152171
$this->authenticator = new JsonLoginAuthenticator(new HttpUtils(), $this->userProvider, null, null, $options);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.