Skip to content

Commit e1439fb

Browse files
committed
wip
1 parent efd57b8 commit e1439fb

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/Context/ContextContextDetector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ private function runningInConsole(): bool
1919
return $_ENV['APP_RUNNING_IN_CONSOLE'] === 'true';
2020
}
2121

22+
if (isset($_ENV['FLARE_FAKE_WEB_REQUEST'])) {
23+
return false;
24+
}
25+
2226
return in_array(php_sapi_name(), ['cli', 'phpdb']);
2327
}
2428
}

src/Flare.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Facade\FlareClient\Http\Client;
1414
use Facade\FlareClient\Middleware\AddGlows;
1515
use Facade\FlareClient\Middleware\AnonymizeIp;
16+
use Facade\FlareClient\Middleware\CensorRequestBodyFields;
1617
use Illuminate\Contracts\Container\Container;
1718
use Illuminate\Pipeline\Pipeline;
1819
use Throwable;
@@ -231,6 +232,10 @@ public function anonymizeIp()
231232
return $this;
232233
}
233234

235+
public function censorRequestBodyFields(array $fieldNames)
236+
{
237+
$this->registerMiddleware(new CensorRequestBodyFields($fieldNames));
238+
}
234239

235240
public function createReport(Throwable $throwable): Report
236241
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Facade\FlareClient\Middleware;
4+
5+
use Facade\FlareClient\Report;
6+
7+
class CensorRequestBodyFields
8+
{
9+
protected $fieldNames = [];
10+
11+
public function __construct(array $fieldNames)
12+
{
13+
$this->fieldNames = $fieldNames;
14+
}
15+
16+
public function handle(Report $report, $next)
17+
{
18+
19+
$context = $report->allContext();
20+
21+
foreach ($this->fieldNames as $fieldName) {
22+
if (isset($context['request_data']['body'][$fieldName])) {
23+
$context['request_data']['body'][$fieldName] = '<CENSORED>';
24+
}
25+
}
26+
27+
$report->userProvidedContext($context);
28+
29+
return $next($report);
30+
}
31+
}

tests/FlareTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ public function it_can_anonymize_the_ip()
126126
]);
127127
}
128128

129+
/** @test */
130+
public function it_can_censor_request_data()
131+
{
132+
$_ENV['FLARE_FAKE_WEB_REQUEST'] = true;
133+
$_POST['user'] = 'john@example.com';
134+
$_POST['password'] = 'secret';
135+
136+
$this->flare->censorRequestBodyFields(['user', 'password']);
137+
138+
$this->reportException();
139+
140+
$this->fakeClient->assertLastRequestContains('context.request_data.body', [
141+
'user' => '<CENSORED>',
142+
'password' => '<CENSORED>',
143+
]);
144+
}
145+
129146
/** @test */
130147
public function it_can_merge_user_provided_context()
131148
{

0 commit comments

Comments
 (0)