Skip to content

Commit 7f5134b

Browse files
committed
Merge pull request #642 from willdurand/jsonp-validator
Use JsonpCallbackValidator lib
2 parents 77569cd + 6398b1d commit 7f5134b

File tree

9 files changed

+28
-25
lines changed

9 files changed

+28
-25
lines changed

DependencyInjection/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function getConfigTreeBuilder()
8282
->end()
8383
->arrayNode('serializer')
8484
->validate()
85-
->ifTrue(function($v) { return !empty($v['version']) && !empty($v['groups']); })
85+
->ifTrue(function ($v) { return !empty($v['version']) && !empty($v['groups']); })
8686
->thenInvalid('Only either a version or a groups exclusion strategy can be set')
8787
->end()
8888
->addDefaultsIfNotSet()
@@ -197,7 +197,7 @@ private function addFormatListenerSection(ArrayNodeDefinition $rootNode)
197197
->booleanNode('prefer_extension')->defaultTrue()->end()
198198
->scalarNode('fallback_format')->defaultValue('html')->end()
199199
->arrayNode('priorities')
200-
->beforeNormalization()->ifString()->then(function($v) { return preg_split('/\s*,\s*/', $v); })->end()
200+
->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()
201201
->prototype('scalar')->end()
202202
->end()
203203
->end()

DependencyInjection/FOSRestExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1818
use Symfony\Component\DependencyInjection\DefinitionDecorator;
19-
use Symfony\Component\DependencyInjection\Definition;
2019
use Symfony\Component\DependencyInjection\Reference;
21-
use Symfony\Component\HttpKernel\Kernel;
2220

2321
use FOS\RestBundle\Util\Codes;
2422

@@ -165,7 +163,10 @@ public function load(array $configs, ContainerBuilder $container)
165163
$container->setDefinition($this->getAlias().'.view_handler', $handler);
166164

167165
$container->setParameter($this->getAlias().'.view_handler.jsonp.callback_param', $config['view']['jsonp_handler']['callback_param']);
168-
$container->setParameter($this->getAlias().'.view_handler.jsonp.callback_filter', $config['view']['jsonp_handler']['callback_filter']);
166+
167+
if ('/(^[a-z0-9_]+$)|(^YUI\.Env\.JSONP\._[0-9]+$)/i' !== $config['view']['jsonp_handler']['callback_filter']) {
168+
throw new \LogicException('As of 1.2.0, the "callback_filter" parameter is deprecated, and is not used anymore. For more information, read: https://github.com/FriendsOfSymfony/FOSRestBundle/pull/642.');
169+
}
169170

170171
if (empty($config['view']['mime_types']['jsonp'])) {
171172
$config['view']['mime_types']['jsonp'] = $config['view']['jsonp_handler']['mime_type'];

Resources/config/view.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<parameter key="fos_rest.serializer.exclusion_strategy.version" />
99
<parameter key="fos_rest.serializer.exclusion_strategy.groups"/>
1010
<parameter key="fos_rest.view_handler.jsonp.callback_param"/>
11-
<parameter key="fos_rest.view_handler.jsonp.callback_filter"/>
1211
<parameter key="fos_rest.view.exception_wrapper_handler" />
1312
<parameter key="fos_rest.view_handler.default.class">FOS\RestBundle\View\ViewHandler</parameter>
1413
<parameter key="fos_rest.view_handler.jsonp.class">FOS\RestBundle\View\JsonpHandler</parameter>
@@ -39,7 +38,6 @@
3938

4039
<service id="fos_rest.view_handler.jsonp" class="%fos_rest.view_handler.jsonp.class%" public="false">
4140
<argument>%fos_rest.view_handler.jsonp.callback_param%</argument>
42-
<argument>%fos_rest.view_handler.jsonp.callback_filter%</argument>
4341
</service>
4442

4543
<service id="fos_rest.view.exception_wrapper_handler" class="%fos_rest.view.exception_wrapper_handler%" />

Resources/doc/2-the-view-layer.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ fos_rest:
292292
view:
293293
jsonp_handler:
294294
callback_param: mycallback
295-
callback_filter: /^[a-z0-9_]+$/i
296295
```
297296
298297
Finally the filter can also be disabled by setting it to false.

Resources/doc/configuration-reference.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ fos_rest:
5454
serialize_null: false
5555
jsonp_handler:
5656
callback_param: callback
57-
callback_filter: /(^[a-z0-9_]+$)|(^YUI\.Env\.JSONP\._[0-9]+$)/i
5857
mime_type: application/javascript+jsonp
5958
exception:
6059
codes:
@@ -77,13 +76,13 @@ fos_rest:
7776
-
7877
# URL path info
7978
path: ~
80-
79+
8180
# URL host name
8281
host: ~
8382
prefer_extension: true
8483
fallback_format: html
8584
priorities:
86-
85+
8786
# Prototype
8887
name: []
8988
media_type:

Tests/DependencyInjection/FOSRestExtensionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,12 @@ public function testCheckExceptionWrapperHandler()
474474
$exceptionWrapperHandler = $this->container->getDefinition('fos_rest.view.exception_wrapper_handler');
475475
$this->assertEquals('%fos_rest.view.exception_wrapper_handler%', $exceptionWrapperHandler->getClass());
476476
}
477+
478+
/**
479+
* @expectedException \LogicException
480+
*/
481+
public function testExceptionThrownIfCallbackFilterIsUsed()
482+
{
483+
$this->extension->load(array('fos_rest' => array('view' => array('jsonp_handler' => array('callback_filter' => 'foo')))), $this->container);
484+
}
477485
}

Tests/View/JsonpHandlerTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class JsonpHandlerTest extends \PHPUnit_Framework_TestCase
2828
/**
2929
* @dataProvider handleDataProvider
3030
*/
31-
public function testHandle($query, $callbackFilter = '/(^[a-z0-9_]+$)|(^YUI\.Env\.JSONP\._[0-9]+$)/i')
31+
public function testHandle($query)
3232
{
3333
$data = array('foo' => 'bar');
3434

3535
$viewHandler = new ViewHandler(array('jsonp' => false));
36-
$jsonpHandler = new JsonpHandler(key($query), $callbackFilter);
36+
$jsonpHandler = new JsonpHandler(key($query));
3737
$viewHandler->registerHandler('jsonp', array($jsonpHandler, 'createResponse'));
3838

3939
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
@@ -76,15 +76,15 @@ public static function handleDataProvider()
7676
}
7777

7878
/**
79-
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
79+
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
8080
* @dataProvider getCallbackFailureDataProvider
8181
*/
82-
public function testGetCallbackFailure(Request $request, $callbackFilter = '/(^[a-z0-9_]+$)|(^YUI\.Env\.JSONP\._[0-9]+$)/i')
82+
public function testGetCallbackFailure(Request $request)
8383
{
8484
$data = array('foo' => 'bar');
8585

8686
$viewHandler = new ViewHandler(array('jsonp' => false));
87-
$jsonpHandler = new JsonpHandler('callback', $callbackFilter);
87+
$jsonpHandler = new JsonpHandler('callback');
8888
$viewHandler->registerHandler('jsonp', array($jsonpHandler, 'createResponse'));
8989

9090
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
@@ -121,7 +121,6 @@ public function getCallbackFailureDataProvider()
121121
'incorrect callback param name' => array(new Request(array('foo' => 'bar'))),
122122
'incorrect callback param value' => array(new Request(array('callback' => 'ding.dong'))),
123123
'incorrect callback param name and value' => array(new Request(array('foo' => 'bar'))),
124-
'incorrect callback param value with a custom filter' => array(new Request(array('foo' => 'bar')), '/[0-9]+/'),
125124
);
126125
}
127126
}

View/JsonpHandler.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,19 @@
2323
class JsonpHandler
2424
{
2525
protected $callbackParam;
26-
protected $callbackFilter;
2726

28-
public function __construct($callbackParam, $callbackFilter)
27+
public function __construct($callbackParam)
2928
{
3029
$this->callbackParam = $callbackParam;
31-
$this->callbackFilter = $callbackFilter;
3230
}
3331

3432
protected function getCallback(Request $request)
3533
{
36-
$callback = $request->query->get($this->callbackParam);
34+
$callback = $request->query->get($this->callbackParam);
35+
$validator = new \JsonpCallbackValidator();
3736

38-
if ($this->callbackFilter && !preg_match($this->callbackFilter, $callback)) {
39-
$msg = "Callback '$callback' does not match '{$this->callbackFilter}'";
40-
throw new BadRequestHttpException($msg);
37+
if (!$validator->validate($this->callbackParam)) {
38+
throw new BadRequestHttpException('Invalid JSONP callback value');
4139
}
4240

4341
return $callback;

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"php": ">=5.3.2",
2525
"symfony/framework-bundle": "~2.2",
2626
"doctrine/inflector": "1.0.*",
27-
"willdurand/negotiation": "~1.2.0"
27+
"willdurand/negotiation": "~1.2.0",
28+
"willdurand/jsonp-callback-validator": "~1.0.0"
2829
},
2930

3031
"require-dev": {

0 commit comments

Comments
 (0)