-
-
Notifications
You must be signed in to change notification settings - Fork 450
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 #428 from ste93cry/feature/sanitize-http-headers-p…
…rocessor Add a processor to sanitize the HTTP headers
- Loading branch information
Showing
3 changed files
with
148 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Raven. | ||
* | ||
* (c) Sentry Team | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* This processor sanitizes the configured HTTP headers to ensure no sensitive | ||
* informations are sent to the server. | ||
* | ||
* @author Stefano Arlandini <[email protected]> | ||
*/ | ||
final class Raven_Processor_SanitizeHttpHeadersProcessor extends Raven_Processor | ||
{ | ||
/** | ||
* @var string[] $httpHeadersToSanitize The list of HTTP headers to sanitize | ||
*/ | ||
private $httpHeadersToSanitize = array(); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct(Raven_Client $client) | ||
{ | ||
parent::__construct($client); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setProcessorOptions(array $options) | ||
{ | ||
$this->httpHeadersToSanitize = array_merge($this->getDefaultHeaders(), isset($options['sanitize_http_headers']) ? $options['sanitize_http_headers'] : array()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(&$data) | ||
{ | ||
if (isset($data['request']) && isset($data['request']['headers'])) { | ||
foreach ($data['request']['headers'] as $header => &$value) { | ||
if (in_array($header, $this->httpHeadersToSanitize)) { | ||
$value = self::STRING_MASK; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Gets the list of default headers that must be sanitized. | ||
* | ||
* @return string[] | ||
*/ | ||
private function getDefaultHeaders() | ||
{ | ||
return array('Authorization', 'Proxy-Authorization', 'X-Csrf-Token', 'X-CSRFToken', 'X-XSRF-TOKEN'); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
test/Raven/Tests/Processor/SanitizeHttpHeadersProcessorTest.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,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Raven. | ||
* | ||
* (c) Sentry Team | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
class Raven_SanitizeHttpHeadersProcessorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var \Raven_Processor_SanitizeHttpHeadersProcessor|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected $processor; | ||
|
||
protected function setUp() | ||
{ | ||
/** @var \Raven_Client|\PHPUnit_Framework_MockObject_MockObject $client */ | ||
$client = $this->getMockBuilder('\Raven_Client') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->processor = new Raven_Processor_SanitizeHttpHeadersProcessor($client); | ||
$this->processor->setProcessorOptions(array( | ||
'sanitize_http_headers' => array('User-Defined-Header'), | ||
)); | ||
} | ||
|
||
/** | ||
* @dataProvider processDataProvider | ||
*/ | ||
public function testProcess($inputData, $expectedData) | ||
{ | ||
$this->processor->process($inputData); | ||
|
||
$this->assertArraySubset($expectedData, $inputData); | ||
} | ||
|
||
public function processDataProvider() | ||
{ | ||
return array( | ||
array( | ||
array( | ||
'request' => array( | ||
'headers' => array( | ||
'Authorization' => 'foo', | ||
'AnotherHeader' => 'bar', | ||
), | ||
), | ||
), | ||
array( | ||
'request' => array( | ||
'headers' => array( | ||
'Authorization' => Raven_Processor::STRING_MASK, | ||
'AnotherHeader' => 'bar', | ||
), | ||
), | ||
), | ||
), | ||
array( | ||
array( | ||
'request' => array( | ||
'headers' => array( | ||
'User-Defined-Header' => 'foo', | ||
'AnotherHeader' => 'bar', | ||
), | ||
), | ||
), | ||
array( | ||
'request' => array( | ||
'headers' => array( | ||
'User-Defined-Header' => Raven_Processor::STRING_MASK, | ||
'AnotherHeader' => 'bar', | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |