Skip to content

[HttpKernel] Do not enable HtmlDumper if the request is issued by curl #13915

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 2 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 @@ -41,6 +41,11 @@ public function getConfigTreeBuilder()
->min(-1)
->defaultValue(-1)
->end()
->scalarNode('disable_html_for')
->info('Disable HTMLdumper in favor of CliDumper based on an expression')
->example("0 === strpos(request.headers.get('user-agent'), 'curl/')")
->defaultNull()
Copy link
Contributor

Choose a reason for hiding this comment

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

why not use "0 === strpos(request.headers.get('user-agent'), 'curl/')" as default value ?

->end()
->end()
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

Expand All @@ -37,6 +38,13 @@ public function load(array $configs, ContainerBuilder $container)
$container->getDefinition('var_dumper.cloner')
->addMethodCall('setMaxItems', array($config['max_items']))
->addMethodCall('setMaxString', array($config['max_string_length']));

if ($config['disable_html_for']) {
$container->getDefinition('debug.dump_listener')
->replaceArgument(2, new Definition('Symfony\Bundle\DebugBundle\ExpressionLanguage'))
->replaceArgument(3, $config['disable_html_for'])
;
}
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Bundle/DebugBundle/ExpressionLanguage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Bundle\DebugBundle;

use Symfony\Component\Security\Core\Authorization\ExpressionLanguage as BaseExpressionLanguage;
Copy link

Choose a reason for hiding this comment

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

is there a reason for not using Symfony\Component\ExpressionLanguage ?

Copy link
Member

Choose a reason for hiding this comment

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

no good reason

Copy link
Member Author

Choose a reason for hiding this comment

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

good catch ;)


/**
* Adds some functions to the default Symfony Debug ExpressionLanguage.
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class ExpressionLanguage extends BaseExpressionLanguage
{
protected function registerFunctions()
{
parent::registerFunctions();
$this->register('strpos', function ($haystack, $needle) {
Copy link
Member

Choose a reason for hiding this comment

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

this should a function provider

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure to understand. Can you give me more details?

Copy link
Member

Choose a reason for hiding this comment

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

Since Symfony 2.6, we introduce a function provider in the ExpressionLanguage component, and it should be used.

See https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Security/Core/Authorization/ExpressionLanguage.php and https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Security/Core/Authorization/ExpressionLanguageProvider.php for the usage in the security component.
The custom ExpressionLanguage is not even needed here; we could just register the provider in the service definition, using a normal ExpressionLanguage instance (the security component must have it for BC)

return sprintf('strpos(%s, %s)', $haystack, $needle);
}, function (array $variables, $haystack, $needle) {
return strpos($haystack, $needle);
});
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/DebugBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<tag name="kernel.event_subscriber" />
<argument type="service" id="var_dumper.cloner" />
<argument type="service" id="data_collector.dump" />
<argument>null</argument><!-- Expression language -->
<argument>null</argument><!-- Expression -->
</service>

<service id="var_dumper.cloner" class="Symfony\Component\VarDumper\Cloner\VarCloner" />
Expand Down
32 changes: 28 additions & 4 deletions src/Symfony/Component/HttpKernel/EventListener/DumpListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
namespace Symfony\Component\HttpKernel\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\SyntaxError;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
use Symfony\Component\VarDumper\VarDumper;

Expand All @@ -26,19 +30,39 @@ class DumpListener implements EventSubscriberInterface
{
private $cloner;
private $dumper;
private $expressionLanguage;
private $expression;

/**
* @param ClonerInterface $cloner Cloner service.
* @param DataDumperInterface $dumper Dumper service.
* @param ClonerInterface $cloner Cloner service.
* @param DataDumperInterface $dumper Dumper service.
* @param ExpressionLanguage|null $expressionLanguage Expression Language service.
* @param string|null $expression The expression.
*/
public function __construct(ClonerInterface $cloner, DataDumperInterface $dumper)
public function __construct(ClonerInterface $cloner, DataDumperInterface $dumper, ExpressionLanguage $expressionLanguage = null, $expression = null)
{
if (null !== $expression && null === $expressionLanguage) {
throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
}

$this->cloner = $cloner;
$this->dumper = $dumper;
$this->expressionLanguage = $expressionLanguage;
$this->expression = $expression;
}

public function configure()
public function configure(GetResponseEvent $event)
{
if ($this->expression) {
$result = $this->expressionLanguage->evaluate($this->expression, array('request' => $event->getRequest()));
if ($result) {
CliDumper::$defaultColors = true;
CliDumper::$defaultOutput = 'php://output';

return;
}
}

$cloner = $this->cloner;
$dumper = $this->dumper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\HttpKernel\Tests\EventListener;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\EventListener\DumpListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
use Symfony\Component\VarDumper\Cloner\Data;
Expand Down Expand Up @@ -45,8 +47,20 @@ public function testConfigure()
$exception = null;
$listener = new DumpListener($cloner, $dumper);

$response = new Response();
$event = $this
->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
->disableOriginalConstructor()
->getMock()
;
$event
->expects($this->any())
->method('getRequest')
->will($this->returnValue($response))
;

try {
$listener->configure();
$listener->configure($event);

VarDumper::dump('foo');
VarDumper::dump('bar');
Expand Down