Skip to content

Commit

Permalink
Merge pull request #81 from paragonie/permit-empty
Browse files Browse the repository at this point in the history
Permit empty attributes on decrypt (configurable)
  • Loading branch information
paragonie-security authored Jan 15, 2023
2 parents 53afa7f + b2e48fc commit 7459af6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/EncryptedMultiRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class EncryptedMultiRows
*/
protected array $tables = [];

/**
* @var bool|null $permitEmpty
*/
protected ?bool $permitEmpty = null;

/**
* EncryptedFieldSet constructor.
*
Expand Down Expand Up @@ -406,6 +411,9 @@ public function getEncryptedRowObjectForTable(string $tableName = ''): Encrypted
/** @var EncryptedRow $encryptedRow */
$encryptedRow = $this->tables[$tableName];
$encryptedRow->setTypedIndexes($this->typedIndexes);
if (!is_null($this->permitEmpty)) {
$encryptedRow->setPermitEmpty($this->permitEmpty);
}
return $encryptedRow;
}

Expand Down
30 changes: 30 additions & 0 deletions src/EncryptedRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
BlindIndexNotFoundException,
CipherSweetException,
CryptoOperationException,
EmptyFieldException,
InvalidCiphertextException
};
use ParagonIE\ConstantTime\Hex;
Expand Down Expand Up @@ -52,6 +53,11 @@ class EncryptedRow
*/
protected array $blindIndexes = [];

/**
* @var bool $permitEmpty
*/
protected bool $permitEmpty = false;

/**
* @var bool $typedIndexes
*/
Expand Down Expand Up @@ -404,6 +410,12 @@ public function decryptRow(
$this->tableName,
$field
);
if (!array_key_exists($field, $row)) {
if (!$this->permitEmpty) {
throw new EmptyFieldException('Field is not defined in row: ' . $field);
}
continue;
}
if (\is_null($row[$field])) {
$return[$field] = null;
continue;
Expand Down Expand Up @@ -801,6 +813,24 @@ public function setFlatIndexes(bool $bool): static
return $this;
}

/**
* @return bool
*/
public function getPermitEmpty(): bool
{
return $this->permitEmpty;
}

/**
* @param bool $permitted
* @return $this
*/
public function setPermitEmpty(bool $permitted): static
{
$this->permitEmpty = $permitted;
return $this;
}

/**
* @return bool
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Exception/EmptyFieldException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace ParagonIE\CipherSweet\Exception;

class EmptyFieldException extends CipherSweetException
{

}

0 comments on commit 7459af6

Please sign in to comment.