Skip to content

[HttpFoundation] Add Request::isStateless method #57852

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

Closed
wants to merge 7 commits into from
Closed
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 @@ -67,7 +67,7 @@ private function getFirewallContext(Request $request): ?FirewallContext
$context = $this->container->get($contextId);

if ($context->getConfig()?->isStateless() && !$request->attributes->has('_stateless')) {
$request->attributes->set('_stateless', true);
$request->setStateless();
}

return $context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testGetListeners(Request $request, bool $expectedState)
$this->assertEquals([[$listener], $exceptionListener, $logoutListener], $firewallMap->getListeners($request));
$this->assertEquals($firewallConfig, $firewallMap->getFirewallConfig($request));
$this->assertEquals('security.firewall.map.context.foo', $request->attributes->get(self::ATTRIBUTE_FIREWALL_CONTEXT));
$this->assertEquals($expectedState, $request->attributes->get('_stateless'));
$this->assertEquals($expectedState, $request->isStateless());
}

public static function providesStatefulStatelessRequests(): \Generator
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/SecurityBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/event-dispatcher": "^6.4|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/http-foundation": "^6.4|^7.0",
"symfony/http-foundation": "^7.2",
"symfony/password-hasher": "^6.4|^7.0",
"symfony/security-core": "^7.2",
"symfony/security-csrf": "^6.4|^7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function toolbarAction(Request $request, ?string $token = null): Response
throw new NotFoundHttpException('The profiler must be enabled.');
}

if (!$request->attributes->getBoolean('_stateless') && $request->hasSession()
if (!$request->isStateless() && $request->hasSession()
&& ($session = $request->getSession())->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag
) {
// keep current flashes for one more request if using AutoExpireFlashBag
Expand Down Expand Up @@ -174,7 +174,7 @@ public function searchBarAction(Request $request): Response
$this->cspHandler?->disableCsp();

$session = null;
if (!$request->attributes->getBoolean('_stateless') && $request->hasSession()) {
if (!$request->isStateless() && $request->hasSession()) {
$session = $request->getSession();
}

Expand Down Expand Up @@ -254,7 +254,7 @@ public function searchAction(Request $request): Response
$token = $request->query->get('token');
$profileType = $request->query->get('type', 'request');

if (!$request->attributes->getBoolean('_stateless') && $request->hasSession()) {
if (!$request->isStateless() && $request->hasSession()) {
$session = $request->getSession();

$session->set('_profiler_search_ip', $ip);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/WebProfilerBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"symfony/config": "^6.4|^7.0",
"symfony/framework-bundle": "^6.4|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/http-foundation": "^7.2",
"symfony/routing": "^6.4|^7.0",
"symfony/twig-bundle": "^6.4|^7.0",
"twig/twig": "^3.10"
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add optional `$requests` argument to `RequestStack::__construct()`
* Add `Request::isStateless()` and `Request::setStateless()`

7.1
---
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,16 @@ public function get(string $key, mixed $default = null): mixed
return $default;
}

public function setStateless(bool $stateless = true): void
{
$this->attributes->set('_stateless', $stateless);
}

public function isStateless(): bool
{
return $this->attributes->getBoolean('_stateless');
}

/**
* Gets the Session.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function collect(Request $request, Response $response, ?\Throwable $excep
$sessionMetadata = [];
$sessionAttributes = [];
$flashes = [];
if (!$request->attributes->getBoolean('_stateless') && $request->hasSession()) {
if (!$request->isStateless() && $request->hasSession()) {
$session = $request->getSession();
if ($session->isStarted()) {
$sessionMetadata['Created'] = date(\DATE_RFC822, $session->getMetadataBag()->getCreated());
Expand Down Expand Up @@ -106,7 +106,7 @@ public function collect(Request $request, Response $response, ?\Throwable $excep
'session_metadata' => $sessionMetadata,
'session_attributes' => $sessionAttributes,
'session_usages' => array_values($this->sessionUsages),
'stateless_check' => $this->requestStack?->getMainRequest()?->attributes->get('_stateless') ?? false,
'stateless_check' => $this->requestStack?->getMainRequest()?->isStateless() ?? false,
'flashes' => $flashes,
'path_info' => $request->getPathInfo(),
'controller' => 'n/a',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function onKernelResponse(ResponseEvent $event): void
->headers->addCacheControlDirective('must-revalidate');
}

if (!$event->getRequest()->attributes->get('_stateless', false)) {
if (!$event->getRequest()->isStateless()) {
return;
}

Expand Down Expand Up @@ -239,7 +239,7 @@ public function onSessionUsage(): void
$stateless = false;
$clonedRequestStack = clone $requestStack;
while (null !== ($request = $clonedRequestStack->pop()) && !$stateless) {
$stateless = $request->attributes->get('_stateless');
$stateless = $request->isStateless();
}

if (!$stateless) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function onKernelResponse(ResponseEvent $event): void
return;
}

$session = !$request->attributes->getBoolean('_stateless') && $request->hasPreviousSession() ? $request->getSession() : null;
$session = !$request->isStateless() && $request->hasPreviousSession() ? $request->getSession() : null;

if ($session instanceof Session) {
$usageIndexValue = $usageIndexReference = &$session->getUsageIndex();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function createSubRequest(string $uri, Request $request): Request
$subRequest->setLocale($request->getLocale());
}
if ($request->attributes->has('_stateless')) {
$subRequest->attributes->set('_stateless', $request->attributes->get('_stateless'));
$subRequest->setStateless($request->isStateless());
}
if ($request->attributes->has('_check_controller_is_allowed')) {
$subRequest->attributes->set('_check_controller_is_allowed', $request->attributes->get('_check_controller_is_allowed'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public function testStatelessCheck()

$requestStack = new RequestStack();
$request = $this->createRequest();
$request->attributes->set('_stateless', true);
$request->setStateless();
$requestStack->push($request);

$collector = new RequestDataCollector($requestStack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ public function testSessionUsageExceptionIfStatelessAndSessionUsed()
$kernel = $this->createMock(HttpKernelInterface::class);

$request = new Request();
$request->attributes->set('_stateless', true);
$request->setStateless();
$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
$request->getSession();

Expand All @@ -780,7 +780,7 @@ public function testSessionUsageLogIfStatelessAndSessionUsed()
$kernel = $this->createMock(HttpKernelInterface::class);

$request = new Request();
$request->attributes->set('_stateless', true);
$request->setStateless();
$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
$request->getSession();

Expand All @@ -805,7 +805,7 @@ public function testSessionIsSavedWhenUnexpectedSessionExceptionThrown()
$kernel = $this->createMock(HttpKernelInterface::class);

$request = new Request();
$request->attributes->set('_stateless', true);
$request->setStateless();

$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
$request->getSession();
Expand All @@ -824,7 +824,7 @@ public function testSessionUsageCallbackWhenDebugAndStateless()
$requestStack = new RequestStack();

$request = new Request();
$request->attributes->set('_stateless', true);
$request->setStateless();

$requestStack->push(new Request());
$requestStack->push($request);
Expand All @@ -849,7 +849,7 @@ public function testSessionUsageCallbackWhenNoDebug()
$session->expects($this->exactly(0))->method('save');

$request = new Request();
$request->attributes->set('_stateless', true);
$request->setStateless();

$requestStack = new RequestStack();
$requestStack->push($request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ public function testIpAddressOfRangedTrustedProxyIsSetAsRemote()
public function testStatelessAttributeIsForwardedByDefault()
{
$request = Request::create('/');
$request->attributes->set('_stateless', true);
$request->setStateless();

$kernel = $this->createMock(HttpKernelInterface::class);
$kernel
->expects($this->once())
->method('handle')
->with($this->callback(static fn (Request $subRequest) => $subRequest->attributes->get('_stateless')))
->with($this->callback(static fn (Request $subRequest) => $subRequest->isStateless()))
;
$strategy = new InlineFragmentRenderer($kernel);
$strategy->render('/', $request);
Expand All @@ -290,13 +290,13 @@ public function testStatelessAttributeIsForwardedByDefault()
public function testStatelessAttributeCanBeDisabled()
{
$request = Request::create('/');
$request->attributes->set('_stateless', true);
$request->setStateless();

$kernel = $this->createMock(HttpKernelInterface::class);
$kernel
->expects($this->once())
->method('handle')
->with($this->callback(static fn (Request $subRequest) => !$subRequest->attributes->get('_stateless')))
->with($this->callback(static fn (Request $subRequest) => !$subRequest->isStateless()))
;
$strategy = new InlineFragmentRenderer($kernel);
$strategy->render(new ControllerReference('main_controller', ['_stateless' => false]), $request);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/error-handler": "^6.4|^7.0",
"symfony/event-dispatcher": "^6.4|^7.0",
"symfony/http-foundation": "^6.4|^7.0",
"symfony/http-foundation": "^7.2",
"symfony/polyfill-ctype": "^1.8",
"psr/log": "^1|^2|^3"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio

$this->logger?->debug('Authentication failure, redirect triggered.', ['failure_path' => $options['failure_path']]);

if (!$request->attributes->getBoolean('_stateless')) {
if (!$request->isStateless()) {
$request->getSession()->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, $exception);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected function determineTargetUrl(Request $request): string
}

$firewallName = $this->getFirewallName();
if (null !== $firewallName && !$request->attributes->getBoolean('_stateless') && $targetUrl = $this->getTargetPath($request->getSession(), $firewallName)) {
if (null !== $firewallName && !$request->isStateless() && $targetUrl = $this->getTargetPath($request->getSession(), $firewallName)) {
$this->removeTargetPath($request->getSession(), $firewallName);

return $targetUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function authenticate(RequestEvent $event): void
}

$request = $event->getRequest();
$session = !$request->attributes->getBoolean('_stateless') && $request->hasPreviousSession() ? $request->getSession() : null;
$session = !$request->isStateless() && $request->hasPreviousSession() ? $request->getSession() : null;

$request->attributes->set('_security_firewall_run', $this->sessionKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ protected function setUp(): void

$this->session = $this->createMock(SessionInterface::class);
$this->request = $this->createMock(Request::class);
$this->request->attributes = new ParameterBag(['_stateless' => false]);
$this->request->expects($this->any())->method('getSession')->willReturn($this->session);
$this->exception = $this->getMockBuilder(AuthenticationException::class)->onlyMethods(['getMessage'])->getMock();
}
Expand Down Expand Up @@ -93,7 +92,7 @@ public function testExceptionIsPersistedInSession()

public function testExceptionIsNotPersistedInSessionOnStatelessRequest()
{
$this->request->attributes = new ParameterBag(['_stateless' => true]);
$this->request->method('isStateless')->willReturn(true);

$this->session->expects($this->never())
->method('set')->with(SecurityRequestAttributes::AUTHENTICATION_ERROR, $this->exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testStatelessRequestRedirections()
$session->expects($this->never())->method('remove')->with('_security.admin.target_path');
$statelessRequest = Request::create('/');
$statelessRequest->setSession($session);
$statelessRequest->attributes->set('_stateless', true);
$statelessRequest->setStateless();

$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->expects($this->any())->method('generate')->willReturn('http://localhost/login');
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Security/Http/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=8.2",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/http-foundation": "^6.4|^7.0",
"symfony/http-foundation": "^7.2",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/polyfill-mbstring": "~1.0",
"symfony/property-access": "^6.4|^7.0",
Expand Down