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
Merged
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
11 changes: 5 additions & 6 deletions src/FieldType/EncryptedDatetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime;
Expand All @@ -17,6 +18,9 @@
*/
class EncryptedDatetime extends DBDatetime
{

use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -40,7 +44,7 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
Expand All @@ -54,11 +58,6 @@ public function getDecryptedValue($value)
return $value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
}

public function requireField()
{
$values = array(
Expand Down
10 changes: 4 additions & 6 deletions src/FieldType/EncryptedDecimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDecimal;
Expand All @@ -17,6 +18,8 @@
*/
class EncryptedDecimal extends DBDecimal
{
use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -40,7 +43,7 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
Expand All @@ -54,11 +57,6 @@ public function getDecryptedValue($value)
return (float)$value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
}

public function requireField()
{
$values = array(
Expand Down
10 changes: 4 additions & 6 deletions src/FieldType/EncryptedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBEnum;
Expand All @@ -18,6 +19,8 @@
*/
class EncryptedEnum extends DBEnum
{
use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -41,7 +44,7 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
Expand All @@ -55,11 +58,6 @@ public function getDecryptedValue($value)
return $value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value);
}

public function requireField()
{
$values = array(
Expand Down
10 changes: 4 additions & 6 deletions src/FieldType/EncryptedInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBInt;
Expand All @@ -17,6 +18,8 @@
*/
class EncryptedInt extends DBInt
{
use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -40,7 +43,7 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
Expand All @@ -54,11 +57,6 @@ public function getDecryptedValue($value)
return $value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value);
}

public function requireField()
{
$values = array(
Expand Down
12 changes: 4 additions & 8 deletions src/FieldType/EncryptedText.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBText;
use Madmatt\EncryptAtRest\AtRestCryptoService;

class EncryptedText extends DBText
{
use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -33,10 +36,8 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Type hardening for PHP 8.1+
$value = (string)$value;
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
try {
Expand All @@ -49,11 +50,6 @@ public function getDecryptedValue($value)
return $value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
}

public function requireField()
{
$values = array(
Expand Down
19 changes: 9 additions & 10 deletions src/FieldType/EncryptedVarchar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Madmatt\EncryptAtRest\FieldType;

use Exception;
use Madmatt\EncryptAtRest\AtRestCryptoService;
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBVarchar;
use Madmatt\EncryptAtRest\AtRestCryptoService;

/**
* Class EncryptedVarchar
Expand All @@ -17,6 +18,9 @@
*/
class EncryptedVarchar extends DBVarchar
{

use EncryptedFieldGetValueTrait;

/**
* @var AtRestCryptoService
*/
Expand All @@ -40,7 +44,7 @@ public function setValue($value, $record = null, $markChanged = true)
}
}

public function getDecryptedValue($value)
public function getDecryptedValue(string $value = '')
{
// Test if we're actually an encrypted value;
if (ctype_xdigit($value) && strlen($value) > 130) {
Expand All @@ -54,18 +58,13 @@ public function getDecryptedValue($value)
return $value;
}

public function getValue()
{
return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub
}

public function requireField()
{
$values = array(
'type' => 'text',
'type' => 'text',
'parts' => array(
'datatype' => 'text',
'null' => 'not null',
'datatype' => 'text',
'null' => 'not null',
'arrayValue' => $this->arrayValue
)
);
Expand Down
16 changes: 16 additions & 0 deletions src/Traits/EncryptedFieldGetValueTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Madmatt\EncryptAtRest\Traits;

trait EncryptedFieldGetValueTrait
{

public function getValue()
{
// Type hardening for PHP 8.1+
$value = (string)$this->value;

return $this->getDecryptedValue($value);
}

}