8000 [FrameworkBundle][HttpKernel] Restrict stateless reporting to exception only by mtarld · Pull Request #36321 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle][HttpKernel] Restrict stateless reporting to exception only #36321

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
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CHANGELOG
* Made `BrowserKitAssertionsTrait` report the original error message in case of a failure
* Added ability for `config:dump-reference` and `debug:config` to dump and debug kernel container extension configuration.
* Deprecated `session.attribute_bag` service and `session.flash_bag` service.
* Added `session.strict_statless` option to configure the strictness of stateless reporting

5.0.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
->min(4)
->max(6)
->end()
->booleanNode('strict_stateless')->defaultValue('%kernel.debug%')->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,8 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
$container->setParameter('session.save_path', $config['save_path']);

$container->setParameter('session.metadata.update_threshold', $config['metadata_update_threshold']);

$container->setParameter('session.strict_stateless', $config['strict_stateless']);
}

private function registerRequestConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<xsd:attribute name="metadata-update-threshold" type="xsd:nonNegativeInteger" />
<xsd:attribute name="sid-length" type="sid_length" />
<xsd:attribute name="sid-bits-per-character" type="sid_bits_per_character" />
<xsd:attribute name="strict-stateless" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="request">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<argument key="initialized_session" type="service" id="session" on-invalid="ignore_uninitialized" />
<argument key="logger" type="service" id="logger" on-invalid="ignore" />
</argument>
<argument>%kernel.debug%</argument>
<argument>%session.strict_stateless%</argument>
</service>

<!-- for BC -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ protected static function getBundleDefaultConfig()
'gc_probability' => 1,
'save_path' => '%kernel.cache_dir%/sessions',
'metadata_update_threshold' => 0,
'strict_stateless' => '%kernel.debug%',
],
'request' => [
'enabled' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'sid_length' => 22,
'sid_bits_per_character' => 4,
'save_path' => '/path/to/sessions',
'strict_stateless' => true,
],
'assets' => [
'version' => 'v1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<framework:ssi enabled="true" />
<framework:profiler only-exceptions="true" enabled="false" />
<framework:router resource="%kernel.project_dir%/config/routing.xml" type="xml" utf8="true" />
<framework:session gc-maxlifetime="90000" gc-probability="1" gc-divisor="108" storage-id="session.storage.native" handler-id="session.handler.native_file" name="_SYMFONY" cookie-lifetime="86400" cookie-path="/" cookie-domain="example.com" cookie-secure="true" cookie-httponly="false" use-cookies="true" save-path="/path/to/sessions" sid-length="22" sid-bits-per-character="4" />
<framework:session gc-maxlifetime="90000" gc-probability="1" gc-divisor="108" storage-id="session.storage.native" handler-id="session.handler.native_file" name="_SYMFONY" cookie-lifetime="86400" cookie-path="/" cookie-domain="example.com" cookie-secure="true" cookie-httponly="false" use-cookies="true" save-path="/path/to/sessions" sid-length="22" sid-bits-per-character="4" strict-stateless="true" />
<framework:request>
<framework:format name="csv">
<framework:mime-type>text/csv</framework:mime-type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ framework:
sid_length: 22
sid_bits_per_character: 4
save_path: /path/to/sessions
strict_stateless: true
assets:
version: v1
translator:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ public function testSession()
$this->assertEquals('fr', $container->getParameter('kernel.default_locale'));
$this->assertEquals('session.storage.native', (string) $container->getAlias('session.storage'));
$this->assertEquals('session.handler.native_file', (string) $container->getAlias('session.handler'));
$this->assertTrue($container->getParameter('session.strict_stateless'));

$options = $container->getParameter('session.storage.options');
$this->assertEquals('_SYMFONY', $options['name']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ abstract class AbstractSessionListener implements EventSubscriberInterface

protected $container;
private $sessionUsageStack = [];
private $debug;
private $strictStatelessReport;

public function __construct(ContainerInterface $container = null, bool $debug = false)
public function __construct(ContainerInterface $container = null, bool $strictStatelessReport = false)
{
$this->container = $container;
$this->debug = $debug;
$this->strictStatelessReport = $strictStatelessReport;
}

public function onKernelRequest(RequestEvent $event)
Expand Down Expand Up @@ -130,7 +130,7 @@ public function onKernelResponse(ResponseEvent $event)
return;
}

if ($this->debug) {
if ($this->strictStatelessReport) {
throw new UnexpectedSessionUsageException('Session was used while the request was declared stateless.');
}

Expand All @@ -148,7 +148,7 @@ public function onFinishRequest(FinishRequestEvent $event)

public function onSessionUsage(): void
{
if (!$this->debug) {
if (!$this->strictStatelessReport) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
*/
class SessionListener extends AbstractSessionListener
{
public function __construct(ContainerInterface $container, bool $debug = false)
public function __construct(ContainerInterface $container, bool $strictStatelessReport = false)
{
parent::__construct($container, $debug);
parent::__construct($container, $strictStatelessReport);
}

protected function getSession(): ?SessionInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function testSurrogateMasterRequestIsPublic()
$this->assertLessThanOrEqual((new \DateTime('now', new \DateTimeZone('UTC'))), (new \DateTime($response->headers->get('Expires'))));
}

public function testSessionUsageExceptionIfStatelessAndSessionUsed()
public function testSessionUsageExceptionWhenStrictStatelessAndSessionUsed()
{
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
Expand All @@ -200,7 +200,7 @@ public function testSessionUsageExceptionIfStatelessAndSessionUsed()
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, new Response()));
}

public function testSessionUsageLogIfStatelessAndSessionUsed()
public function testSessionUsageLogWhenNotStrictStatelessAndSessionUsed()
{
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
Expand Down Expand Up @@ -245,7 +245,7 @@ public function testSessionIsSavedWhenUnexpectedSessionExceptionThrown()
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response));
}

public function testSessionUsageCallbackWhenDebugAndStateless()
public function testSessionUsageCallbackWhenStrictAndStateless()
{
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
$session->method('isStarted')->willReturn(true);
Expand All @@ -268,7 +268,7 @@ public function testSessionUsageCallbackWhenDebugAndStateless()
(new SessionListener($container, true))->onSessionUsage();
}

public function testSessionUsageCallbackWhenNoDebug()
public function testSessionUsageCallbackWhenNotStrict()
{
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
$session->method('isStarted')->willReturn(true);
Expand Down
0