Skip to content

Commit

Permalink
Merge pull request #18 from facade/censor-request-body
Browse files Browse the repository at this point in the history
Add ability to censor request body fields
  • Loading branch information
freekmurze authored Apr 8, 2021
2 parents e72a760 + e0cceaf commit 572600a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Context/ContextContextDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ private function runningInConsole(): bool
return $_ENV['APP_RUNNING_IN_CONSOLE'] === 'true';
}

if (isset($_ENV['FLARE_FAKE_WEB_REQUEST'])) {
return false;
}

return in_array(php_sapi_name(), ['cli', 'phpdb']);
}
}
8 changes: 7 additions & 1 deletion src/Flare.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Facade\FlareClient\Http\Client;
use Facade\FlareClient\Middleware\AddGlows;
use Facade\FlareClient\Middleware\AnonymizeIp;
use Facade\FlareClient\Middleware\CensorRequestBodyFields;
use Illuminate\Contracts\Container\Container;
use Illuminate\Pipeline\Pipeline;
use Throwable;
Expand Down Expand Up @@ -226,11 +227,16 @@ private function applyAdditionalParameters(Report $report)

public function anonymizeIp()
{
$this->registerMiddleware(new AnonymizeIp);
$this->registerMiddleware(new AnonymizeIp());

return $this;
}

public function censorRequestBodyFields(array $fieldNames)
{
$this->registerMiddleware(new CensorRequestBodyFields($fieldNames));
}

public function createReport(Throwable $throwable): Report
{
$report = Report::createForThrowable(
Expand Down
30 changes: 30 additions & 0 deletions src/Middleware/CensorRequestBodyFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Facade\FlareClient\Middleware;

use Facade\FlareClient\Report;

class CensorRequestBodyFields
{
protected $fieldNames = [];

public function __construct(array $fieldNames)
{
$this->fieldNames = $fieldNames;
}

public function handle(Report $report, $next)
{
$context = $report->allContext();

foreach ($this->fieldNames as $fieldName) {
if (isset($context['request_data']['body'][$fieldName])) {
$context['request_data']['body'][$fieldName] = '<CENSORED>';
}
}

$report->userProvidedContext($context);

return $next($report);
}
}
17 changes: 17 additions & 0 deletions tests/FlareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ public function it_can_anonymize_the_ip()
]);
}

/** @test */
public function it_can_censor_request_data()
{
$_ENV['FLARE_FAKE_WEB_REQUEST'] = true;
$_POST['user'] = '[email protected]';
$_POST['password'] = 'secret';

$this->flare->censorRequestBodyFields(['user', 'password']);

$this->reportException();

$this->fakeClient->assertLastRequestContains('context.request_data.body', [
'user' => '<CENSORED>',
'password' => '<CENSORED>',
]);
}

/** @test */
public function it_can_merge_user_provided_context()
{
Expand Down

0 comments on commit 572600a

Please sign in to comment.