Skip to content
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
7 changes: 7 additions & 0 deletions src/Database/Validator/Query/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public function __construct(
*/
protected function isValidAttribute(string $attribute): bool
{
if (
\in_array('encrypt', $this->schema[$attribute]['filters'] ?? [])
) {
$this->message = 'Cannot query encrypted attribute: ' . $attribute;
return false;
}

if (\str_contains($attribute, '.')) {
// Check for special symbol `.`
if (isset($this->schema[$attribute])) {
Expand Down
48 changes: 48 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2814,6 +2814,54 @@ public function testGetDocument(Document $document): Document
return $document;
}

public function testEncryptAttributes(): void
{
// Add custom encrypt filter
static::getDatabase()->addFilter(
'encrypt',
function (mixed $value) {
return json_encode([
'data' => base64_encode($value),
'method' => 'base64',
'version' => 'v1',
]);
},
function (mixed $value) {
if (is_null($value)) {
return;
}
$value = json_decode($value, true);
return base64_decode($value['data']);
}
);

$col = static::getDatabase()->createCollection(__FUNCTION__);
$this->assertNotNull($col->getId());

static::getDatabase()->createAttribute($col->getId(), 'title', Database::VAR_STRING, 255, true);
static::getDatabase()->createAttribute($col->getId(), 'encrypt', Database::VAR_STRING, 128, true, filters: ['encrypt']);

static::getDatabase()->createDocument($col->getId(), new Document([
'title' => 'Sample Title',
'encrypt' => 'secret',
]));
// query against encrypt
try {
$queries = [Query::equal('encrypt', ['test'])];
$doc = static::getDatabase()->find($col->getId(), $queries);
$this->fail('Queried against encrypt field. Failed to throw exeception.');
} catch (Throwable $e) {
$this->assertTrue($e instanceof QueryException);
}

try {
$queries = [Query::equal('title', ['test'])];
static::getDatabase()->find($col->getId(), $queries);
} catch (Throwable $e) {
$this->fail('Should not have thrown error');
}
}

/**
* @depends testCreateDocument
*/
Expand Down