Skip to content

Commit

Permalink
add masking for php
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiAndreiev committed Sep 18, 2024
1 parent 9781c94 commit 1c3d501
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ test-metrics-node-hapi: ## Run Metrics tests against the Node SDK + hapi

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

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

##
Expand Down
14 changes: 14 additions & 0 deletions packages/php/src/HAR/MaskHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace ReadMe\HAR;

class MaskHelper
{
public static function mask(string $data): string
{
$hashBytes = hash('sha512', $data, true);
$base64Hash = base64_encode($hashBytes);
$opts = substr($data, -4);
return 'sha512-' . $base64Hash . '?' . $opts;
}
}
8 changes: 6 additions & 2 deletions packages/php/src/HAR/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function create(string $log_id, Request $request, Response $response): ar
if ($api_key_exists) {
// Swap the externally documented `api_key` field into backwards compatible and
// internally used `id` field.
$group['id'] = $group['api_key'];
$group['id'] = MaskHelper::mask($group['api_key']);
unset($group['api_key']);
}

Expand Down Expand Up @@ -333,10 +333,14 @@ protected static function convertHeaderBagToArray(HeaderBag $headers): array
/** @psalm-suppress PossiblyNullIterator */
foreach ($values as $value) {
// If the header is empty, don't worry about it.
if ($value === '') {
if ($value === '' || $value === null) {
continue; // @codeCoverageIgnore
}

if (strtolower($name) === 'authorization') {
$value = MaskHelper::mask($value);
}

$output[] = [
'name' => $name,
'value' => $value
Expand Down

0 comments on commit 1c3d501

Please sign in to comment.