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

Check sensitive fields case-insensitive #1

Merged
merged 1 commit into from
Nov 6, 2024
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
4 changes: 3 additions & 1 deletion config/request-response-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'prune_after_days' => 30,

'security' => [
// For security reason we should not log sensitive fields.
// For security reason we should not log sensitive fields. These fields will be checked case-insensitive.
'sensitive_fields' => [
'password',
'access_token',
Expand All @@ -22,6 +22,8 @@
'php-auth-digest',
],

// For security reason we should not log sensitive fields for certain vendors. These fields will be checked
// case-insensitive.
'sensitive_fields_per_vendor' => [],
],

Expand Down
7 changes: 5 additions & 2 deletions src/Helpers/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Goedemiddag\RequestResponseLog\Helpers;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use JsonException;

class Sanitizer
Expand Down Expand Up @@ -41,8 +42,10 @@ public static function filterSensitiveData(array $array = [], ?string $vendor =
$sensitiveFieldsPerVendor = Arr::get(config('request-response-log.security.sensitive_fields_per_vendor', []), $vendor, []);

foreach ($array as $key => $value) {
$hasGenericSensitiveKey = in_array($key, $sensitiveFields, true);
$hasVendorSensitiveKey = in_array($key, $sensitiveFieldsPerVendor, true);
$searchKey = Str::lower($key);

$hasGenericSensitiveKey = in_array($searchKey, $sensitiveFields, true);
$hasVendorSensitiveKey = in_array($searchKey, $sensitiveFieldsPerVendor, true);

// When the key is sensitive, mask the value
if ($hasGenericSensitiveKey || $hasVendorSensitiveKey) {
Expand Down
3 changes: 3 additions & 0 deletions tests/Helpers/SanitizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ public function test_it_filters_sensitive_data(): void
$data = [
'password' => 'secret',
'hello' => 'world',
'Authorization' => 'Bearer my-secret-key',
];

$sanitizedData = Sanitizer::filterSensitiveData($data);

$this->assertArrayHasKey('password', $sanitizedData);
$this->assertArrayHasKey('hello', $sanitizedData);
$this->assertArrayHasKey('Authorization', $sanitizedData);
$this->assertSame('********', $sanitizedData['password']);
$this->assertSame('********', $sanitizedData['Authorization']);
$this->assertSame('world', $sanitizedData['hello']);
}

Expand Down