Skip to content

Add event listener to check that the kernel.secret parameter isn't set to the default #6480

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 1 commit 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
8 changes: 8 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<parameter key="debug.container.dump">%kernel.cache_dir%/%kernel.container_class%.xml</parameter>
<parameter key="debug.controller_resolver.class">Symfony\Component\HttpKernel\Controller\TraceableControllerResolver</parameter>
<parameter key="debug.deprecation_logger_listener.class">Symfony\Component\HttpKernel\EventListener\DeprecationLoggerListener</parameter>
<parameter key="debug.default_secret_listener.class">Symfony\Component\HttpKernel\EventListener\DefaultSecretListener</parameter>
</parameters>

<services>
Expand All @@ -33,5 +34,12 @@
<tag name="monolog.logger" channel="deprecation" />
<argument type="service" id="logger" on-invalid="null" />
</service>

<service id="debug.default_secret_listener" class="%debug.default_secret_listener.class%">
<tag name="kernel.event_subscriber" />
<tag name="monolog.logger" />
<argument type="service" id="logger" on-invalid="null" />
<argument>%kernel.secret%</argument>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\EventListener;

use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* Checks that the kernel.secret parameter isn't set to the default.
*
* @author Lee McDermott <github@leemcdermott.co.uk>
*/
class DefaultSecretListener implements EventSubscriberInterface
{
private $logger;
private $secret;

public function __construct(LoggerInterface $logger = null, $secret)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$logger can't be null. because you will have error on $this->logger->alert

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the logic should be fixed. The logger is optional everywhere in Symfony.

{
$this->logger = $logger;
$this->secret = $secret;
}

public function onKernelRequest()
{
if ('ThisTokenIsNotSoSecretChangeIt' === $this->secret) {
$this->logger->alert('The "kernel.secret" parameter is currently set to the default. It is important that you change it');
}
}

public static function getSubscribedEvents()
{
return array(KernelEvents::REQUEST => 'onKernelRequest');
}
}