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

Cannot modify readonly property #7

Open
deoomen opened this issue Sep 13, 2023 · 0 comments
Open

Cannot modify readonly property #7

deoomen opened this issue Sep 13, 2023 · 0 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@deoomen
Copy link
Contributor

deoomen commented Sep 13, 2023

PHP since version 8.1 supports readonly keyword to mark properties as non modifiable. But there is one problem. When an object has readonly props and it's passed to monolog, Redact will try to overwrite this property, but it's not possible so an exception will be thrown.

Maybe an option is to use reflection to create a clone of a not-readonly object and use that cloned object to overwrite props?

Quick example:

use Monolog\Handler\StreamHandler;
use RedactSensitive\RedactSensitiveProcessor;

class ReadonlyDTO
{
    public function __construct(
        public string $nonReadonlyProp1,
        public readonly string $readonlyProp1,
        public readonly string $readonlyProp2,
    ) {
    }
}

$obj = new ReadonlyDTO('nonreadonly', 'readonly', 'readonly');

$sensitive_keys = [
    'nonReadonlyProp1' => 4,
    'readonlyProp1' => 4,
];

$processor = new RedactSensitiveProcessor($sensitive_keys);

$logger = new \Monolog\Logger('Readme', [new StreamHandler(STDOUT)]);
$logger->pushProcessor($processor);

$logger->info('Hello, World!', ['obj' => $obj]);

Result:

PHP Fatal error:  Uncaught Error: Cannot modify readonly property ReadonlyDTO::$readonlyProp1 in /vendor/leocavalcante/redact-sensitive/src/RedactSensitiveProcessor.php:108
@leocavalcante leocavalcante added bug Something isn't working help wanted Extra attention is needed labels Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants