From 63e442293c9eaeb358c77e33e40948ad67789bfd Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Sat, 21 Nov 2020 01:31:17 +0200 Subject: [PATCH] Fixed encryption in nested fields --- app/config/collections.php | 8 +++ src/Appwrite/Database/Database.php | 52 +++++++++++++++---- .../Projects/ProjectsConsoleClientTest.php | 20 +++---- 3 files changed, 60 insertions(+), 20 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 23d95af71..408e713b7 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -172,6 +172,14 @@ $collections = [ 'required' => false, 'array' => true, ], + [ + '$collection' => Database::SYSTEM_COLLECTION_RULES, + 'label' => 'Filter', + 'key' => 'filter', + 'type' => Database::SYSTEM_VAR_TYPE_TEXT, + 'required' => false, + 'array' => true, + ], ], ], Database::SYSTEM_COLLECTION_USERS => [ diff --git a/src/Appwrite/Database/Database.php b/src/Appwrite/Database/Database.php index e28a57029..e8dd33db8 100644 --- a/src/Appwrite/Database/Database.php +++ b/src/Appwrite/Database/Database.php @@ -455,13 +455,28 @@ class Database foreach ($rules as $key => $rule) { $key = $rule->getAttribute('key', null); - $filters = $rule->getAttribute('filter', null); + $type = $rule->getAttribute('type', null); + $array = $rule->getAttribute('array', false); + $filters = $rule->getAttribute('filter', []); $value = $document->getAttribute($key, null); - if (($value !== null) && is_array($filters)) { - foreach ($filters as $filter) { - $value = $this->encodeAttribute($filter, $value); - $document->setAttribute($key, $value); + if (($value !== null)) { + if ($type === self::SYSTEM_VAR_TYPE_DOCUMENT) { + if($array) { + $list = []; + foreach ($value as $child) { + $list[] = $this->encode($child); + } + + $document->setAttribute($key, $list); + } else { + $document->setAttribute($key, $this->encode($value)); + } + } else { + foreach ($filters as $filter) { + $value = $this->encodeAttribute($filter, $value); + $document->setAttribute($key, $value); + } } } } @@ -476,13 +491,28 @@ class Database foreach ($rules as $key => $rule) { $key = $rule->getAttribute('key', null); - $filters = $rule->getAttribute('filter', null); + $type = $rule->getAttribute('type', null); + $array = $rule->getAttribute('array', false); + $filters = $rule->getAttribute('filter', []); $value = $document->getAttribute($key, null); - if (($value !== null) && is_array($filters)) { - foreach (array_reverse($filters) as $filter) { - $value = $this->decodeAttribute($filter, $value); - $document->setAttribute($key, $value); + if (($value !== null)) { + if ($type === self::SYSTEM_VAR_TYPE_DOCUMENT) { + if($array) { + $list = []; + foreach ($value as $child) { + $list[] = $this->decode($child); + } + + $document->setAttribute($key, $list); + } else { + $document->setAttribute($key, $this->decode($value)); + } + } else { + foreach (array_reverse($filters) as $filter) { + $value = $this->decodeAttribute($filter, $value); + $document->setAttribute($key, $value); + } } } } @@ -499,6 +529,7 @@ class Database static protected function encodeAttribute(string $name, $value) { if (!isset(self::$filters[$name])) { + return $value; throw new Exception('Filter not found'); } @@ -520,6 +551,7 @@ class Database static protected function decodeAttribute(string $name, $value) { if (!isset(self::$filters[$name])) { + return $value; throw new Exception('Filter not found'); } diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index fb571d365..3e2f84d1b 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -221,10 +221,10 @@ class ProjectsConsoleClientTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); $this->assertNotEmpty($response['body']['$id']); $this->assertEquals('Project Test 2', $response['body']['name']); - // $this->assertArrayHasKey('platforms', $response['body']); // TODO enable when response model is enabled - // $this->assertArrayHasKey('webhooks', $response['body']); // TODO enable when response model is enabled - // $this->assertArrayHasKey('keys', $response['body']); // TODO enable when response model is enabled - // $this->assertArrayHasKey('tasks', $response['body']); // TODO enable when response model is enabled + $this->assertArrayHasKey('platforms', $response['body']); + $this->assertArrayHasKey('webhooks', $response['body']); + $this->assertArrayHasKey('keys', $response['body']); + $this->assertArrayHasKey('tasks', $response['body']); $projectId = $response['body']['$id']; @@ -443,7 +443,7 @@ class ProjectsConsoleClientTest extends Scope $this->assertIsBool($response['body']['security']); $this->assertEquals(false, $response['body']['security']); $this->assertEquals('', $response['body']['httpUser']); - // $this->assertEquals('', $response['body']['httpPass']); // TODO add after encrypt refactor + $this->assertEquals('', $response['body']['httpPass']); $response = $this->client->call(Client::METHOD_GET, '/projects/'.$id.'/webhooks/'.$webhookId, array_merge([ 'content-type' => 'application/json', @@ -462,7 +462,7 @@ class ProjectsConsoleClientTest extends Scope $this->assertIsBool($response['body']['security']); $this->assertEquals(false, $response['body']['security']); $this->assertEquals('', $response['body']['httpUser']); - // $this->assertEquals('', $response['body']['httpPass']); // TODO add after encrypt refactor + $this->assertEquals('', $response['body']['httpPass']); /** * Test for FAILURE @@ -759,7 +759,7 @@ class ProjectsConsoleClientTest extends Scope $this->assertContains('demo:value', $response['body']['httpHeaders']); $this->assertCount(1, $response['body']['httpHeaders']); $this->assertEquals('username', $response['body']['httpUser']); - // $this->assertEquals('password', $response['body']['httpPass']); // TODO add after encrypt refactor + $this->assertEquals('password', $response['body']['httpPass']); $data = array_merge($data, ['taskId' => $response['body']['$id']]); @@ -901,7 +901,7 @@ class ProjectsConsoleClientTest extends Scope $this->assertContains('demo:value', $response['body']['httpHeaders']); $this->assertCount(1, $response['body']['httpHeaders']); $this->assertEquals('username', $response['body']['httpUser']); - // $this->assertEquals('password', $response['body']['httpPass']); // TODO add after encrypt refactor + $this->assertEquals('password', $response['body']['httpPass']); /** * Test for FAILURE @@ -952,7 +952,7 @@ class ProjectsConsoleClientTest extends Scope $this->assertContains('demo2:value2', $response['body']['httpHeaders']); $this->assertCount(2, $response['body']['httpHeaders']); $this->assertEquals('username1', $response['body']['httpUser']); - // $this->assertEquals('password1', $response['body']['httpPass']); // TODO add after encrypt refactor + $this->assertEquals('password1', $response['body']['httpPass']); $response = $this->client->call(Client::METHOD_GET, '/projects/'.$id.'/tasks/'.$taskId, array_merge([ 'content-type' => 'application/json', @@ -972,7 +972,7 @@ class ProjectsConsoleClientTest extends Scope $this->assertContains('demo2:value2', $response['body']['httpHeaders']); $this->assertCount(2, $response['body']['httpHeaders']); $this->assertEquals('username1', $response['body']['httpUser']); - // $this->assertEquals('password1', $response['body']['httpPass']); // TODO add after encrypt refactor + $this->assertEquals('password1', $response['body']['httpPass']); /** * Test for FAILURE