Skip to content

Commit 6122c76

Browse files
authored
Merge pull request #560 from ArnabChatterjee20k/feat/block-queries-against-encrypt-filters
added block queries against encrypt fields
2 parents c57cf06 + 900b360 commit 6122c76

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/Database/Validator/Query/Filter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public function __construct(
4141
*/
4242
protected function isValidAttribute(string $attribute): bool
4343
{
44+
if (
45+
\in_array('encrypt', $this->schema[$attribute]['filters'] ?? [])
46+
) {
47+
$this->message = 'Cannot query encrypted attribute: ' . $attribute;
48+
return false;
49+
}
50+
4451
if (\str_contains($attribute, '.')) {
4552
// Check for special symbol `.`
4653
if (isset($this->schema[$attribute])) {

tests/e2e/Adapter/Base.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,6 +2814,54 @@ public function testGetDocument(Document $document): Document
28142814
return $document;
28152815
}
28162816

2817+
public function testEncryptAttributes(): void
2818+
{
2819+
// Add custom encrypt filter
2820+
static::getDatabase()->addFilter(
2821+
'encrypt',
2822+
function (mixed $value) {
2823+
return json_encode([
2824+
'data' => base64_encode($value),
2825+
'method' => 'base64',
2826+
'version' => 'v1',
2827+
]);
2828+
},
2829+
function (mixed $value) {
2830+
if (is_null($value)) {
2831+
return;
2832+
}
2833+
$value = json_decode($value, true);
2834+
return base64_decode($value['data']);
2835+
}
2836+
);
2837+
2838+
$col = static::getDatabase()->createCollection(__FUNCTION__);
2839+
$this->assertNotNull($col->getId());
2840+
2841+
static::getDatabase()->createAttribute($col->getId(), 'title', Database::VAR_STRING, 255, true);
2842+
static::getDatabase()->createAttribute($col->getId(), 'encrypt', Database::VAR_STRING, 128, true, filters: ['encrypt']);
2843+
2844+
static::getDatabase()->createDocument($col->getId(), new Document([
2845+
'title' => 'Sample Title',
2846+
'encrypt' => 'secret',
2847+
]));
2848+
// query against encrypt
2849+
try {
2850+
$queries = [Query::equal('encrypt', ['test'])];
2851+
$doc = static::getDatabase()->find($col->getId(), $queries);
2852+
$this->fail('Queried against encrypt field. Failed to throw exeception.');
2853+
} catch (Throwable $e) {
2854+
$this->assertTrue($e instanceof QueryException);
2855+
}
2856+
2857+
try {
2858+
$queries = [Query::equal('title', ['test'])];
2859+
static::getDatabase()->find($col->getId(), $queries);
2860+
} catch (Throwable $e) {
2861+
$this->fail('Should not have thrown error');
2862+
}
2863+
}
2864+
28172865
/**
28182866
* @depends testCreateDocument
28192867
*/

0 commit comments

Comments
 (0)