Skip to content

Commit fd37b11

Browse files
committed
Fix: be less restrictive on callback validation
1 parent d861843 commit fd37b11

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Controller/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function indexAction(Request $request, $_format)
102102
$content = file_get_contents((string) $cache);
103103

104104
if (null !== $callback = $request->query->get('callback')) {
105-
if (false === ctype_alnum($callback)) {
105+
if (0 === preg_match('/^[a-zA-Z0-9\.$_]+$/', $callback)) {
106106
throw new HttpException(400, 'Invalid JSONP callback value');
107107
}
108108

Tests/Controller/ControllerTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,29 @@ public function testIndexAction()
3939
$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]+?","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""}', $response->getContent());
4040
}
4141

42-
public function testGenerateWithCallback()
42+
/**
43+
* @dataProvider dataProviderForTestGenerateWithCallback
44+
*/
45+
public function testGenerateWithCallback($callback)
4346
{
4447
$controller = new Controller($this->getSerializer(), $this->getExtractor());
45-
$response = $controller->indexAction($this->getRequest('/', 'GET', array('callback' => 'foo')), 'json');
48+
$response = $controller->indexAction($this->getRequest('/', 'GET', array('callback' => $callback)), 'json');
4649

47-
$this->assertEquals('foo({"base_url":"","routes":[],"prefix":"","host":"","scheme":""});', $response->getContent());
50+
$this->assertEquals(
51+
sprintf('%s({"base_url":"","routes":[],"prefix":"","host":"","scheme":""});', $callback),
52+
$response->getContent()
53+
);
54+
}
55+
56+
public static function dataProviderForTestGenerateWithCallback()
57+
{
58+
return array(
59+
array('foo'),
60+
array('foo123'),
61+
array('fos.Router.data'),
62+
array('$.callback'),
63+
array('_.callback'),
64+
);
4865
}
4966

5067
/**

0 commit comments

Comments
 (0)