Skip to content
Open
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
24 changes: 23 additions & 1 deletion src/RedactSensitiveProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ private function traverseArr(array $arr, array $keys): array

private function traverseObj(object $obj, array $keys): object
{
foreach (get_object_vars($obj) as $key => $value) {
$vars = get_object_vars($obj);
if ($this->containsReadOnlyProperties($obj)) {
// create a new object so that redacted copies of readonly variables
// can be stored on it
$obj = new \stdClass();
}
foreach ($vars as $key => $value) {
if (is_scalar($value)) {
if (array_key_exists($key, $keys)) {
$obj->{$key} = $this->redact((string) $value, $keys[$key]);
Expand All @@ -123,4 +129,20 @@ private function traverseObj(object $obj, array $keys): object

return $obj;
}

/**
* Determines if an object has readonly properties.
* @param object $object
* @return bool
*/
private function containsReadOnlyProperties(object $object): bool
{
$reflection = new \ReflectionObject($object);
foreach ($reflection->getProperties() as $property) {
if ($property->isReadOnly()) {
return true;
}
}
return false;
}
}
55 changes: 55 additions & 0 deletions tests/RedactSensitiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,58 @@
$record = $this->getRecord(context: [0 => ['good' => 'value'], 1 => ['test' => 'foobar']]);
expect($processor($record)->context)->toBe([0 => ['good' => 'value'], 1 => ['test' => 'foo***']]);
});

it('creates copies of objects with readonly properties and redacts them', function (): void {
$sensitive_keys = ['test' => 0];
$processor = new RedactSensitiveProcessor($sensitive_keys);

$readonly_properties_object = new class {
public function __construct(
public readonly string $test = 'foobar',
) {}
};

$record = $this->getRecord(context: ['foo' => $readonly_properties_object]);
expect($processor($record)->context['foo']->test)->toBe('******');
});

it('can redact readonly properties on custom object instances', function (): void {
$sensitive_keys = ['test' => 0];
$processor = new RedactSensitiveProcessor($sensitive_keys);

class Foo {
public function __construct(
public readonly string $test = 'foobar',
) {}
}
$f = new Foo;
$record = $this->getRecord(context: ['foo' => $f]);
expect($processor($record)->context['foo']->test)
->toBe('******')
->and($f->test)
->toBe('foobar');
});

it('can redact readonly properties on custom nested objects', function (): void {
$sensitive_keys = ['test' => 0];
$processor = new RedactSensitiveProcessor($sensitive_keys);

class Bar {
public function __construct(
public readonly object $baz = new Baz
) {}
}

class Baz {
public function __construct(
public readonly string $test = 'foobar',
) {}
}

$f = new Bar;
$record = $this->getRecord(context: ['foo' => $f]);
expect($processor($record)->context['foo']->baz->test)
->toBe('******')
->and($f->baz->test)
->toBe('foobar');
});