-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from FriendsOfSymfony/callback-validator
Callback validator
- Loading branch information
Showing
5 changed files
with
147 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace FOS\JsRoutingBundle\Tests\Validator; | ||
|
||
use FOS\JsRoutingBundle\Validator\CallbackValidator; | ||
|
||
class CallbackValidatorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
const IS_VALID = true; | ||
|
||
const IS_INVALID = false; | ||
|
||
/** | ||
* @dataProvider dataProviderForTestIsValid | ||
*/ | ||
public function testIsValid($callback, $expected) | ||
{ | ||
$validator = new CallbackValidator(); | ||
$this->assertEquals($expected, $validator->isValid($callback)); | ||
} | ||
|
||
public static function dataProviderForTestIsValid() | ||
{ | ||
return array( | ||
array('foo', self::IS_VALID), | ||
array('foo123', self::IS_VALID), | ||
array('fos.Router.data', self::IS_VALID), | ||
array('$.callback', self::IS_VALID), | ||
array('_.callback', self::IS_VALID), | ||
array('hello', self::IS_VALID), | ||
array('foo23', self::IS_VALID), | ||
array('$210', self::IS_VALID), | ||
array('_bar', self::IS_VALID), | ||
array('some_var', self::IS_VALID), | ||
array('$', self::IS_VALID), | ||
array('somevar', self::IS_VALID), | ||
array('$.ajaxHandler', self::IS_VALID), | ||
array('array_of_functions[42]', self::IS_VALID), | ||
array('array_of_functions[42][1]', self::IS_VALID), | ||
array('$.ajaxHandler[42][1].foo', self::IS_VALID), | ||
array('array_of_functions["key"]', self::IS_VALID), | ||
array('_function', self::IS_VALID), | ||
array('petersCallback1412331422[12]', self::IS_VALID), | ||
array('(function xss(x){evil()})', self::IS_INVALID), | ||
array('', self::IS_INVALID), | ||
array('alert()', self::IS_INVALID), | ||
array('test()', self::IS_INVALID), | ||
array('a-b', self::IS_INVALID), | ||
array('23foo', self::IS_INVALID), | ||
array('function', self::IS_INVALID), | ||
array(' somevar', self::IS_INVALID), | ||
array('$.23', self::IS_INVALID), | ||
array('array_of_functions[42]foo[1]', self::IS_INVALID), | ||
array('array_of_functions[]', self::IS_INVALID), | ||
array('myFunction[123].false', self::IS_INVALID), | ||
array('myFunction .tester', self::IS_INVALID), | ||
array(':myFunction', self::IS_INVALID), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSJsRoutingBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\JsRoutingBundle\Validator; | ||
|
||
/** | ||
* Most of the code above took inspiration from: | ||
* https://gist.github.com/ptz0n/1217080. | ||
*/ | ||
class CallbackValidator | ||
{ | ||
private $reservedKeywords = array( | ||
'break', | ||
'do', | ||
'instanceof', | ||
'typeof', | ||
'case', | ||
'else', | ||
'new', | ||
'var', | ||
'catch', | ||
'finally', | ||
'return', | ||
'void', | ||
'continue', | ||
'for', | ||
'switch', | ||
'while', | ||
'debugger', | ||
'function', | ||
'this', | ||
'with', | ||
'default', | ||
'if', | ||
'throw', | ||
'delete', | ||
'in', | ||
'try', | ||
'class', | ||
'enum', | ||
'extends', | ||
'super', | ||
'const', | ||
'export', | ||
'import', | ||
'implements', | ||
'let', | ||
'private', | ||
'public', | ||
'yield', | ||
'interface', | ||
'package', | ||
'protected', | ||
'static', | ||
'null', | ||
'true', | ||
'false', | ||
); | ||
|
||
public function isValid($callback) | ||
{ | ||
foreach (explode('.', $callback) as $identifier) { | ||
if (!preg_match('/^[a-zA-Z_$][0-9a-zA-Z_$]*(?:\[(?:".+"|\'.+\'|\d+)\])*?$/', $identifier)) { | ||
return false; | ||
} | ||
|
||
if (in_array($identifier, $this->reservedKeywords)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters