-
Notifications
You must be signed in to change notification settings - Fork 2
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
4. Improve logging on session related errors #226
Merged
pmeulen
merged 1 commit into
feature/implement-session-required-atttribute
from
feature/4.log-more-session-errors
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Surfnet\Tiqr\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] | ||
class RequiresActiveSession | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/EventSubscriber/RequiresActiveSessionAttributeListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Surfnet\Tiqr\EventSubscriber; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use RuntimeException; | ||
use Surfnet\Tiqr\Attribute\RequiresActiveSession; | ||
use Surfnet\Tiqr\Service\SessionCorrelationIdService; | ||
use Surfnet\Tiqr\WithContextLogger; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException; | ||
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
use function is_array; | ||
|
||
/** | ||
* This listener acts when the given route has a #[RequiresActiveSession] attribute. | ||
* When a route is marked as to have a required active session this listener will deny access when there is none. | ||
*/ | ||
final readonly class RequiresActiveSessionAttributeListener implements EventSubscriberInterface | ||
{ | ||
private string $sessionName; | ||
|
||
/** | ||
* @param array<string, string> $sessionOptions | ||
*/ | ||
public function __construct( | ||
private LoggerInterface $logger, | ||
private SessionCorrelationIdService $sessionCorrelationIdService, | ||
private array $sessionOptions, | ||
) { | ||
if (!array_key_exists('name', $this->sessionOptions)) { | ||
throw new RuntimeException( | ||
'The session name (PHP session cookie identifier) could not be found in the session configuration.' | ||
); | ||
} | ||
$this->sessionName = $this->sessionOptions['name']; | ||
} | ||
|
||
public function onKernelControllerArguments(ControllerArgumentsEvent $event): void | ||
{ | ||
if (!is_array($event->getAttributes()[RequiresActiveSession::class] ?? null)) { | ||
return; | ||
} | ||
|
||
$logger = WithContextLogger::from($this->logger, [ | ||
'correlationId' => $this->sessionCorrelationIdService->generateCorrelationId() ?? '', | ||
'route' => $event->getRequest()->getRequestUri(), | ||
]); | ||
|
||
try { | ||
$sessionId = $event->getRequest()->getSession()->getId(); | ||
$sessionCookieId = $event->getRequest()->cookies->get($this->sessionName); | ||
|
||
if (!$sessionCookieId) { | ||
$logger->error('Route requires active session. Active session wasn\'t found. No session cookie was set.'); | ||
|
||
throw new AccessDeniedException(); | ||
} | ||
|
||
if ($sessionId !== $sessionCookieId) { | ||
$logger->error('Route requires active session. Session does not match session cookie.'); | ||
|
||
throw new AccessDeniedException(); | ||
} | ||
} catch (SessionNotFoundException) { | ||
$logger->error('Route requires active session. Active session wasn\'t found.'); | ||
|
||
throw new AccessDeniedException(); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 20]]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Surfnet\Tiqr\EventSubscriber; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use RuntimeException; | ||
use Surfnet\Tiqr\Service\SessionCorrelationIdService; | ||
use Surfnet\Tiqr\WithContextLogger; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
/** | ||
* Listen to all incoming requests and log the session state information. | ||
*/ | ||
final readonly class SessionStateListener implements EventSubscriberInterface | ||
{ | ||
private string $sessionName; | ||
|
||
/** | ||
* @param array<string, string> $sessionOptions | ||
*/ | ||
public function __construct( | ||
private LoggerInterface $logger, | ||
private SessionCorrelationIdService $sessionCorrelationIdService, | ||
private array $sessionOptions, | ||
) { | ||
if (!array_key_exists('name', $this->sessionOptions)) { | ||
throw new RuntimeException( | ||
'The session name (PHP session cookie identifier) could not be found in the session configuration.' | ||
); | ||
} | ||
$this->sessionName = $this->sessionOptions['name']; | ||
} | ||
|
||
public function onKernelRequest(RequestEvent $event): void | ||
{ | ||
$logger = WithContextLogger::from($this->logger, [ | ||
'correlationId' => $this->sessionCorrelationIdService->generateCorrelationId() ?? '', | ||
'route' => $event->getRequest()->getRequestUri(), | ||
]); | ||
|
||
$sessionCookieId = $event->getRequest()->cookies->get($this->sessionName); | ||
if ($sessionCookieId === null) { | ||
$logger->info('User made a request without a session cookie.'); | ||
return; | ||
} | ||
|
||
$logger->info('User made a request with a session cookie.'); | ||
|
||
try { | ||
$sessionId = $event->getRequest()->getSession()->getId(); | ||
$logger->info('User has a session.'); | ||
|
||
if ($sessionId !== $sessionCookieId) { | ||
$logger->error('The session cookie does not match the session id.'); | ||
return; | ||
} | ||
} catch (SessionNotFoundException) { | ||
$logger->info('Session not found.'); | ||
return; | ||
} | ||
|
||
$logger->info('User session matches the session cookie.'); | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [KernelEvents::REQUEST => ['onKernelRequest', 20]]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Copyright 2024 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Surfnet\Tiqr\Service; | ||
|
||
use RuntimeException; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
final readonly class SessionCorrelationIdService | ||
{ | ||
private string $sessionName; | ||
private ?string $correlationIdSalt; | ||
|
||
/** | ||
* @param array<string, string> $sessionOptions | ||
*/ | ||
public function __construct( | ||
private RequestStack $requestStack, | ||
array $sessionOptions, | ||
?string $correlationIdSalt = null, | ||
) { | ||
if (!array_key_exists('name', $sessionOptions)) { | ||
throw new RuntimeException( | ||
'The session name (PHP session cookie identifier) could not be found in the session configuration.' | ||
); | ||
} | ||
$this->correlationIdSalt = is_null($correlationIdSalt) || strlen($correlationIdSalt) < 16 ? null : $correlationIdSalt; | ||
$this->sessionName = $sessionOptions['name']; | ||
} | ||
|
||
public function generateCorrelationId(): ?string | ||
{ | ||
if ($this->correlationIdSalt === null) { | ||
return null; | ||
} | ||
|
||
$sessionCookie = $this->requestStack->getMainRequest()?->cookies->get($this->sessionName); | ||
|
||
if ($sessionCookie === null) { | ||
return null; | ||
} | ||
|
||
return substr(hash('sha256', $sessionCookie.$this->correlationIdSalt), 0, 8); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now in spec with @pmeulen s request found here: #210 (comment)