8000 [HttpFoundation] JSONP callback validation · symfony/symfony@1159f8b · GitHub
[go: up one dir, main page]

Skip to content

Commit 1159f8b

Browse files
ro0NLfabpot
authored andcommitted
[HttpFoundation] JSONP callback validation
1 parent df46bb7 commit 1159f8b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,19 @@ public static function create($data = null, $status = 200, $headers = array())
8080
public function setCallback($callback = null)
8181
{
8282
if (null !== $callback) {
83-
// taken from http://www.geekality.net/2011/08/03/valid-javascript-identifier/
84-
$pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u';
83+
// partially token from http://www.geekality.net/2011/08/03/valid-javascript-identifier/
84+
// partially token from https://github.com/willdurand/JsonpCallbackValidator
85+
// JsonpCallbackValidator is released under the MIT License. See https://github.com/willdurand/JsonpCallbackValidator/blob/v1.1.0/LICENSE for details.
86+
// (c) William Durand <william.durand1@gmail.com>
87+
$pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/u';
88+
$reserved = array(
89+
'break', 'do', 'instanceof', 'typeof', 'case', 'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue', 'for', 'switch', 'while',
90+
'debugger', 'function', 'this', 'with', 'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum', 'extends', 'super', 'const', 'export',
91+
'import', 'implements', 'let', 'private', 'public', 'yield', 'interface', 'package', 'protected', 'static', 'null', 'true', 'false',
92+
);
8593
$parts = explode('.', $callback);
8694
foreach ($parts as $part) {
87-
if (!preg_match($pattern, $part)) {
95+
if (!preg_match($pattern, $part) || in_array($part, $reserved, true)) {
8896
throw new \InvalidArgumentException('The callback name is not valid.');
8997
}
9098
}

src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ public function testSetContentJsonSerializeError()
213213

214214
JsonResponse::create($serializable);
215215
}
216+
217+
public function testSetComplexCallback()
218+
{
219+
$response = JsonResponse::fromJsonString('{foo: "bar"}');
220+
$response->setCallback('ಠ_ಠ["foo"].bar[0]');
221+
222+
$this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({foo: "bar"});', $response->getContent());
223+
}
216224
}
217225

218226
if (interface_exists('JsonSerializable')) {

0 commit comments

Comments
 (0)
0