1
0
Fork 0
mirror of synced 2024-05-19 20:22:33 +12:00

Fixed encryption in nested fields

This commit is contained in:
Eldad Fux 2020-11-21 01:31:17 +02:00
parent f657563711
commit 63e442293c
3 changed files with 60 additions and 20 deletions

View file

@ -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 => [

View file

@ -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');
}

View file

@ -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