Skip to content

Commit ec245cd

Browse files
feat(php): masking (#1075)
| 🚥 Resolves [RM-8742](https://linear.app/readme-io/issue/RM-8742) | | :------------------- | ## 🧰 Changes Adds sensitive data masking to the PHP SDK The logic is the same as we will have in Python SDK (PR #955)
1 parent 0fe27da commit ec245cd

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ test-metrics-node-hapi: ## Run Metrics tests against the Node SDK + hapi
5454

5555
test-metrics-php-laravel: ## Run Metrics tests against the PHP SDK + Laravel
5656
docker compose up --build --detach integration_php_laravel
57-
SUPPORTS_MULTIPART=true npm run test:integration-metrics || make cleanup-failure
57+
SUPPORTS_HASHING=true SUPPORTS_MULTIPART=true npm run test:integration-metrics || make cleanup-failure
5858
@make cleanup
5959

6060
test-webhooks-php-laravel: ## Run webhooks tests against the PHP SDK + Laravel
6161
docker compose up --detach integration_php_laravel
62-
SUPPORTS_MULTIPART=true npm run test:integration-webhooks || make cleanup-failure
62+
SUPPORTS_HASHING=true SUPPORTS_MULTIPART=true npm run test:integration-webhooks || make cleanup-failure
6363
@make cleanup
6464

6565
##

packages/php/src/HAR/MaskHelper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace ReadMe\HAR;
4+
5+
class MaskHelper
6+
{
7+
public static function mask(string $data): string
8+
{
9+
$hashBytes = hash('sha512', $data, true);
10+
$base64Hash = base64_encode($hashBytes);
11+
$opts = substr($data, -4);
12+
return 'sha512-' . $base64Hash . '?' . $opts;
13+
}
14+
}

packages/php/src/HAR/Payload.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function create(string $log_id, Request $request, Response $response): ar
3333
if ($api_key_exists) {
3434
// Swap the externally documented `api_key` field into backwards compatible and
3535
// internally used `id` field.
36-
$group['id'] = $group['api_key'];
36+
$group['id'] = MaskHelper::mask($group['api_key']);
3737
unset($group['api_key']);
3838
}
3939

@@ -334,10 +334,14 @@ protected static function convertHeaderBagToArray(HeaderBag $headers): array
334334
/** @psalm-suppress PossiblyNullIterator */
335335
foreach ($values as $value) {
336336
// If the header is empty, don't worry about it.
337-
if ($value === '') {
337+
if ($value === '' || $value === null) {
338338
continue; // @codeCoverageIgnore
339339
}
340340

341+
if (strtolower($name) === 'authorization') {
342+
$value = MaskHelper::mask($value);
343+
}
344+
341345
$output[] = [
342346
'name' => $name,
343347
'value' => $value

packages/php/tests/HAR/PayloadTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ public function testCreate(): void
125125
$this->assertSame('fake-uuid', $har['_id']);
126126

127127
$this->assertEqualsCanonicalizing([
128-
'id' => '123457890',
128+
'id' => 'sha512-UrMmjaetxGbu6QkwzYAH9h4c1dzTNIy3CV1lBuHSb0TNlTmrgUUzTRINiCPah7ObWnOiqVXUlVjQD14gblqlPA=='
129+
. '?7890',
129130
'label' => 'username',
130131
'email' => '[email protected]'
131132
], $har['group']);

0 commit comments

Comments
 (0)