Skip to content

Commit

Permalink
Permit empty attributes on decrypt (configurable)
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Jan 15, 2023
1 parent d0e5b68 commit 1200bc3
Show file tree
Hide file tree
Showing 3 changed files with 45 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 @@ -29,6 +29,11 @@ class EncryptedMultiRows
*/
protected $tables = [];

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

/**
* EncryptedFieldSet constructor.
*
Expand Down Expand Up @@ -432,6 +437,9 @@ public function getEncryptedRowObjectForTable($tableName = '')
/** @var EncryptedRow $encryptedRow */
$encryptedRow = $this->tables[$tableName];
$encryptedRow->setTypedIndexes($this->typedIndexes);
if (!is_null($this->permitEmpty)) {
$encryptedRow->setPermitEmtpy($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 @@ -7,6 +7,7 @@
use ParagonIE\CipherSweet\Exception\BlindIndexNotFoundException;
use ParagonIE\CipherSweet\Exception\CipherSweetException;
use ParagonIE\CipherSweet\Exception\CryptoOperationException;
use ParagonIE\CipherSweet\Exception\EmptyFieldException;
use ParagonIE\CipherSweet\Exception\InvalidCiphertextException;
use ParagonIE\ConstantTime\Hex;
use SodiumException;
Expand Down Expand Up @@ -49,6 +50,11 @@ class EncryptedRow
*/
protected $blindIndexes = [];

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

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

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

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

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

class EmptyFieldException extends CipherSweetException
{

}

0 comments on commit 1200bc3

Please sign in to comment.