Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a processor to remove all stacktraces from reported exceptions #429

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Collect User.ip_address automatically (#419).
- Added a processor to remove web cookies and another to remove HTTP body data for POST, PUT, PATCH and DELETE requests. They will be enabled by default in ``2.0`` (#405).
- Added a processor to sanitize HTTP headers (e.g. the Authorization header) (#428).
- Added a processor to remove ``pre_context``, ``context_line`` and ``post_context`` informations from reported exceptions (#429).

1.6.2
-----
Expand Down
39 changes: 39 additions & 0 deletions lib/Raven/Processor/SanitizeStacktraceProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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 removes the `pre_context`, `context_line` and `post_context`
* informations from all exceptions captured by an event.
*
* @author Stefano Arlandini <[email protected]>
*/
class Raven_Processor_SanitizeStacktraceProcessor extends Raven_Processor
{
/**
* {@inheritdoc}
*/
public function process(&$data)
{
if (!isset($data['exception'], $data['exception']['values'])) {
return;
}

foreach ($data['exception']['values'] as &$exception) {
if (!isset($exception['stacktrace'])) {
continue;
}

foreach ($exception['stacktrace']['frames'] as &$frame) {
unset($frame['pre_context'], $frame['context_line'], $frame['post_context']);
}
}
}
}
113 changes: 113 additions & 0 deletions test/Raven/Tests/Processor/SanitizeStacktraceProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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_Tests_SanitizeStacktraceProcessorTest extends PHPUnit_Framework_TestCase
{
/**
* @var Raven_Client|PHPUnit_Framework_MockObject_MockObject
*/
protected $client;

/**
* @var Raven_Processor_SanitizeStacktraceProcessor
*/
protected $processor;

protected function setUp()
{
$this->client = $this->getMockBuilder('Raven_Client')
->setMethods(array_diff($this->getClassMethods('Raven_Client'), array('captureException', 'capture', 'get_default_data', 'get_http_data', 'get_user_data', 'get_extra_data')))
->getMock();

$this->client->store_errors_for_bulk_send = true;

$this->processor = new Raven_Processor_SanitizeStacktraceProcessor($this->client);
}

public function testProcess()
{
try {
throw new \Exception();
} catch (\Exception $exception) {
$this->client->captureException($exception);
}

foreach ($this->client->_pending_events[0]['exception']['values'] as $exceptionValue) {
foreach ($exceptionValue['stacktrace']['frames'] as $frame) {
$this->assertArrayHasKey('pre_context', $frame);
$this->assertArrayHasKey('context_line', $frame);
$this->assertArrayHasKey('post_context', $frame);
}
}

$this->processor->process($this->client->_pending_events[0]);

foreach ($this->client->_pending_events[0]['exception']['values'] as $exceptionValue) {
foreach ($exceptionValue['stacktrace']['frames'] as $frame) {
$this->assertArrayNotHasKey('pre_context', $frame);
$this->assertArrayNotHasKey('context_line', $frame);
$this->assertArrayNotHasKey('post_context', $frame);
}
}
}

public function testProcessWithPreviousException()
{
try {
try {
throw new \Exception('foo');
} catch (\Exception $exception) {
throw new \Exception('bar', 0, $exception);
}
} catch (\Exception $exception) {
$this->client->captureException($exception);
}

foreach ($this->client->_pending_events[0]['exception']['values'] as $exceptionValue) {
foreach ($exceptionValue['stacktrace']['frames'] as $frame) {
$this->assertArrayHasKey('pre_context', $frame);
$this->assertArrayHasKey('context_line', $frame);
$this->assertArrayHasKey('post_context', $frame);
}
}

$this->processor->process($this->client->_pending_events[0]);

foreach ($this->client->_pending_events[0]['exception']['values'] as $exceptionValue) {
foreach ($exceptionValue['stacktrace']['frames'] as $frame) {
$this->assertArrayNotHasKey('pre_context', $frame);
$this->assertArrayNotHasKey('context_line', $frame);
$this->assertArrayNotHasKey('post_context', $frame);
}
}
}

/**
* Gets all the public and abstracts methods of a given class.
*
* @param string $className The FCQN of the class
*
* @return array
*/
private function getClassMethods($className)
{
$class = new ReflectionClass($className);
$methods = array();

foreach ($class->getMethods() as $method) {
if ($method->isPublic() || $method->isAbstract()) {
$methods[] = $method->getName();
}
}

return $methods;
}
}