-
Notifications
You must be signed in to change notification settings - Fork 13
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 #4 from rezzza/obfuscation
obfuscation
- Loading branch information
Showing
17 changed files
with
1,806 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
require_once __DIR__.'/vendor/autoload.php'; |
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,11 @@ | ||
<?php | ||
|
||
use \mageekguy\atoum; | ||
|
||
$script->bootstrapFile(__DIR__ . DIRECTORY_SEPARATOR . '.atoum.bootstrap.php'); | ||
|
||
$cliReport = $script->addDefaultReport(); | ||
$cliReport->addField(new atoum\report\fields\runner\result\logo()); | ||
|
||
$runner->addReport($cliReport); | ||
$runner->addTestsFromDirectory(__DIR__.'/Tests/Units'); |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
bin | ||
composer.phar | ||
vendor |
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,18 @@ | ||
language: php | ||
|
||
php: | ||
- 5.3 | ||
|
||
before_script: | ||
- wget http://getcomposer.org/composer.phar | ||
- php composer.phar install --dev --prefer-source | ||
|
||
script: | ||
- bin/atoum | ||
|
||
notifications: | ||
email: | ||
recipients: | ||
- [email protected] | ||
on_success: change | ||
on_failure: change |
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,34 @@ | ||
<?php | ||
|
||
namespace Rezzza\SecurityBundle\Controller\Annotations; | ||
|
||
/** | ||
* @Annotation() | ||
* | ||
* ObfuscateRequest | ||
* | ||
* @author Stephane PY <[email protected]> | ||
*/ | ||
class ObfuscateRequest | ||
{ | ||
/** | ||
* @var array<string> | ||
*/ | ||
private $obfuscatedPatterns; | ||
|
||
/** | ||
* @param array $data data | ||
*/ | ||
public function __construct(array $obfuscatedPatterns) | ||
{ | ||
$this->obfuscatedPatterns = $obfuscatedPatterns; | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
public function getObfuscatedPatterns() | ||
{ | ||
return $this->obfuscatedPatterns; | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
namespace Rezzza\SecurityBundle\DataCollector; | ||
|
||
use Doctrine\Common\Annotations\Reader as AnnotationReader; | ||
use Doctrine\Common\Util\ClassUtils; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector as BaseRequestDataCollector; | ||
use Rezzza\SecurityBundle\Controller\Annotations\ObfuscateRequest; | ||
use Rezzza\SecurityBundle\Request\Obfuscator\ObfuscatorInterface; | ||
|
||
class RequestDataCollector extends BaseRequestDataCollector | ||
{ | ||
/** | ||
* @var AnnotationReader | ||
*/ | ||
private $annotationReader; | ||
|
||
/** | ||
* @var Obfuscator | ||
*/ | ||
private $obfuscator; | ||
|
||
/** | ||
* @param AnnotationReader $annotationReader annotationReader | ||
* @param ObfuscatorInterface $obfuscator obfuscator | ||
*/ | ||
public function __construct(AnnotationReader $annotationReader, ObfuscatorInterface $obfuscator) | ||
{ | ||
$this->annotationReader = $annotationReader; | ||
$this->obfuscator = $obfuscator; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
public function collect(Request $request, Response $response, \Exception $exception = null) | ||
{ | ||
parent::collect($request, $response, $exception); | ||
|
||
$controller = explode('::', $request->get('_controller')); | ||
|
||
if (count($controller) !== 2) { | ||
return; | ||
} | ||
|
||
$class = new \ReflectionClass($controller[0]); | ||
$reflectionMethod = $class->getMethod($controller[1]); | ||
$annotation = $this->annotationReader->getMethodAnnotation($reflectionMethod, '\Rezzza\SecurityBundle\Controller\Annotations\ObfuscateRequest'); | ||
|
||
if ($annotation) { | ||
$this->data = $this->obfuscator->obfuscate($this->data, $annotation->getObfuscatedPatterns()); | ||
} | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Rezzza\SecurityBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
|
||
class ObfuscatorCompilerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
// request obfuscator is not enabled. | ||
if (!$container->getParameter('rezzza.security.request_obfuscator.enabled')) { | ||
return; | ||
} | ||
|
||
$container->setParameter('data_collector.request.class', 'Rezzza\SecurityBundle\DataCollector\RequestDataCollector'); | ||
|
||
$container->getDefinition('data_collector.request') | ||
->addArgument(new Reference('annotation_reader')) | ||
->addArgument(new Reference('rezzza.security.request_obfuscator.obfuscator')); | ||
} | ||
} |
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
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,21 @@ | ||
<?php | ||
|
||
namespace Rezzza\SecurityBundle\Request\Obfuscator; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* ObfuscatorInterface | ||
* | ||
* @author Stephane PY <[email protected]> | ||
*/ | ||
interface ObfuscatorInterface | ||
{ | ||
/** | ||
* @param array $data data | ||
* @param array $obfuscatedPatterns obfuscatedPatterns | ||
* | ||
* @return array | ||
*/ | ||
public function obfuscate(array $data, array $obfuscatedPatterns); | ||
} |
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,70 @@ | ||
<?php | ||
|
||
namespace Rezzza\SecurityBundle\Request\Obfuscator; | ||
|
||
use Rezzza\SecurityBundle\Exception\ObfuscateBadPatternException; | ||
|
||
/** | ||
* RequestObfuscator | ||
* | ||
* @uses ObfuscatorInterface | ||
* @author Stephane PY <[email protected]> | ||
*/ | ||
class RequestObfuscator implements ObfuscatorInterface | ||
{ | ||
CONST TOKEN_REPLACE = 'X'; | ||
CONST TOKEN_ALL = '*'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function obfuscate(array $data, array $obfuscatedPatterns) | ||
{ | ||
foreach ($obfuscatedPatterns as $key => $pattern) { | ||
if (isset($data[$key])) { | ||
$data[$key] = $this->obfuscateContentWithPattern($data[$key], $pattern); | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
private function obfuscateContentWithPattern($content, $pattern) | ||
{ | ||
if (!is_array($content)) { | ||
return is_scalar($content) ? $this->obfuscateContent($content) : null; | ||
} | ||
|
||
if ($pattern === self::TOKEN_ALL) { | ||
return self::TOKEN_REPLACE; | ||
} | ||
|
||
$patterns = (array) $pattern; | ||
foreach ($patterns as $pattern) { | ||
$keys = array_map(function($v) { | ||
return str_replace(']', '', $v); | ||
}, explode('[', $pattern)); | ||
|
||
$pattern = array_shift($keys); | ||
|
||
if (array_key_exists($pattern, $content)) { | ||
if (count($keys) === 0) { | ||
$content[$pattern] = $this->obfuscateContent($content[$pattern]); | ||
} else { | ||
$newPattern = array_shift($keys); | ||
foreach ($keys as $key) { | ||
$newPattern .= sprintf('[%s]', $key); | ||
} | ||
$content[$pattern] = $this->obfuscateContentWithPattern($content[$pattern], $newPattern); | ||
} | ||
} | ||
} | ||
|
||
return $content; | ||
} | ||
|
||
private function obfuscateContent($content) | ||
{ | ||
return is_scalar($content) ? str_repeat(self::TOKEN_REPLACE, strlen($content)) : self::TOKEN_REPLACE; | ||
} | ||
} |
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,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<parameters> | ||
<parameter key="rezzza.security.request_obfuscator.obfuscator.class">Rezzza\SecurityBundle\Request\Obfuscator\RequestObfuscator</parameter> | ||
</parameters> | ||
|
||
<services> | ||
<service id="rezzza.security.request_obfuscator.obfuscator" class="%rezzza.security.request_obfuscator.obfuscator.class%"/> | ||
</services> | ||
|
||
</container> |
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
Oops, something went wrong.