@@ -80,11 +80,19 @@ public static function create($data = null, $status = 200, $headers = array())
80
80
public function setCallback ($ callback = null )
81
81
{
82
82
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
+ );
85
93
$ parts = explode ('. ' , $ callback );
86
94
foreach ($ parts as $ part ) {
87
- if (!preg_match ($ pattern , $ part )) {
95
+ if (!preg_match ($ pattern , $ part ) || in_array ( $ part , $ reserved , true ) ) {
88
96
throw new \InvalidArgumentException ('The callback name is not valid. ' );
89
97
}
90
98
}
0 commit comments