Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid Utf chars #339

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,9 +787,9 @@ public function createDocument(string $collection, Document $document): Document
$stmtPermissions->execute();
}
} catch (Throwable $e) {
$this->getPDO()->rollBack();
switch ($e->getCode()) {
case 23505:
$this->getPDO()->rollBack();
throw new Duplicate('Duplicated document: ' . $e->getMessage());
default:
throw $e;
Expand Down
34 changes: 34 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,40 @@ public function testCreateDocumentDefaults(): void
static::getDatabase()->deleteCollection('defaults');
}

public function testInvalidUtfCharacters(): void
{
// This test is a reminder we can not insert non UTF chars to UTF field
$collection = 'invalid_characters';

static::getDatabase()->createCollection($collection);
static::getDatabase()->createAttribute($collection, 'str', Database::VAR_STRING, 128, true);

// Test insert of null
$str = "\x00"; // Use double quotes!
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str
])));

// Test insert of null
$str = "\u0000"; // Use double quotes!
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str
])));

// Test insert of null Use double quotes
$str = "\000"; // Use double quotes!
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str,
])));

// Suggestion for fix: cleanup non-utf chars
$str = "\xE2\x94"; // Use double quotes!
$str = mb_convert_encoding($str, 'UTF-8', 'UTF-8');
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str
])));
}

/**
* @throws AuthorizationException|LimitException|DuplicateException|StructureException|Exception|Throwable
*/
Expand Down
Loading