Skip to content

Additional PHP8.1 hardening #31

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

Merged
merged 2 commits into from
Oct 30, 2023

Conversation

scott-nz
Copy link
Contributor

Uses the same logic as already included in EncryptedText.

NOTE: This is required for Silverstripe 4 and 5 so should be merged in before #30

Uses the same logic as already included in EncryptedText
@@ -42,6 +42,8 @@ public function setValue($value, $record = null, $markChanged = true)

public function getDecryptedValue($value)
{
// Type hardening for PHP 8.1+
$value = (string)$value;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to require it to be a string from the start in the method calls? And require it to be a string
public function getdecryptedvalue(string $value = '') {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Firesphere the getDecryptedValue functions are only ever called from the getValue function within the same class.
The only logic within the getValue functions are to return the getDecryptedValue function call.
Moving the type hardening from getDecryptedValue into getValue splits the logic across the 2 functions and raises the question of if the getDecryptedValue functions are necessary.
Is there any reason that the logic within getDecryptedValue could not be placed directly in getValue()?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why a simple hardening check (value isn't null, and if it is, make it an empty string), moving it to getValue() would suddenly "split the logic".

Given the method is public, hardening at call time instead of at execution time, makes more sense to me, as well as requiring a hardened call.

Further, at the getValue() point, if the value already is null, it is known that there is nothing to decrypt, thus even calling the method is somewhat pointless at that stage.

Unrelated further, the code duplication smells like it could be moved to a Trait to reduce the amount of copy-paste code.

@@ -42,6 +42,8 @@ public function setValue($value, $record = null, $markChanged = true)

public function getDecryptedValue($value)
{
// Type hardening for PHP 8.1+
$value = (string)$value;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why a simple hardening check (value isn't null, and if it is, make it an empty string), moving it to getValue() would suddenly "split the logic".

Given the method is public, hardening at call time instead of at execution time, makes more sense to me, as well as requiring a hardened call.

Further, at the getValue() point, if the value already is null, it is known that there is nothing to decrypt, thus even calling the method is somewhat pointless at that stage.

Unrelated further, the code duplication smells like it could be moved to a Trait to reduce the amount of copy-paste code.

@Firesphere
Copy link
Collaborator

@madmatt I'm pretty happy with the solution this way, and it reduces code smell. Any final thoughts from you¿

@madmatt
Copy link
Owner

madmatt commented Oct 30, 2023

Looks good to me, thanks @scott-nz and @Firesphere!

@madmatt madmatt merged commit a44b789 into madmatt:master Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants