Skip to content

Commit f6b1745

Browse files
committed
issue #174: fixed key validation
1 parent 3385ee4 commit f6b1745

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

lib/triagens/ArangoDb/Document.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public function setInternalId($id)
495495
}
496496

497497

498-
if (!preg_match('/^\w+\/\w+$/', $id)) {
498+
if (!preg_match('/^[a-zA-Z0-9_-]{1,64}\/[a-zA-Z0-9_:-]{1,254}$/', $id)) {
499499
throw new ClientException('Invalid format for document id');
500500
}
501501

@@ -519,7 +519,7 @@ public function setInternalKey($key)
519519
throw new ClientException('Should not update the key of an existing document');
520520
}
521521

522-
if (!preg_match('/^\w+$/', $key)) {
522+
if (!preg_match('/^[a-zA-Z0-9_:-]{1,254}$/', $key)) {
523523
throw new ClientException('Invalid format for document key');
524524
}
525525

lib/triagens/ArangoDb/DocumentHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ private function getDocumentId($document)
796796
$documentId = $document;
797797
}
798798

799-
if (!$documentId || !(is_string($documentId) || is_double($documentId) || is_int($documentId))) {
799+
if (trim($documentId) === "" || !(is_string($documentId) || is_double($documentId) || is_int($documentId))) {
800800
throw new ClientException('Cannot alter a document without a document id');
801801
}
802802

tests/DocumentBasicTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,66 @@ public function testCreateAndDeleteDocumentUsingDefinedKey()
104104

105105
$documentHandler->delete($document);
106106
}
107+
108+
/**
109+
* Try to create and delete a document with several keys
110+
*/
111+
public function testCreateAndDeleteDocumentWithSeveralKeys()
112+
{
113+
$connection = $this->connection;
114+
$collection = $this->collection;
115+
$documentHandler = new DocumentHandler($connection);
116+
117+
$keys = array("foo", "bar", "bar:bar", "baz", "1", "0", "a-b-c", "a:b", "this-is-a-test", "FOO", "BAR", "Bar", "bAr");
118+
foreach ($keys as $key) {
119+
$document = new Document();
120+
$document->someAttribute = 'someValue';
121+
$document->set('_key', $key);
122+
$documentId = $documentHandler->add($collection->getName(), $document);
123+
124+
$resultingDocument = $documentHandler->get($collection->getName(), $documentId);
125+
126+
$resultingAttribute = $resultingDocument->someAttribute;
127+
$resultingKey = $resultingDocument->getKey();
128+
$this->assertTrue(
129+
$resultingAttribute === 'someValue',
130+
'Resulting Attribute should be "someValue". It\'s :' . $resultingAttribute
131+
);
132+
$this->assertTrue(
133+
$resultingKey === $key,
134+
'Resulting Attribute should be "someValue". It\'s :' . $resultingKey
135+
);
136+
137+
$documentHandler->delete($document);
138+
}
139+
}
140+
141+
142+
/**
143+
* Try to create a document with invalid keys
144+
*/
145+
public function testCreateDocumentWithInvalidKeys()
146+
{
147+
$connection = $this->connection;
148+
$collection = $this->collection;
149+
$documentHandler = new DocumentHandler($connection);
150+
151+
$keys = array("", " ", " bar", "bar ", "/", "?", "abcdef gh", "abcxde&", "mötörhead", "this-key-will-be-too-long-to-be-processed-successfully-would-you-agree-with-me-sure-you-will-because-there-is-a-limit-of-254-characters-per-key-which-this-string-will-not-conform-to-if-you-are-still-reading-this-you-should-probably-do-something-else-right-now-REALLY");
152+
153+
foreach ($keys as $key) {
154+
$document = new Document();
155+
$document->someAttribute = 'someValue';
156+
157+
$caught = false;
158+
try {
159+
$document->set('_key', $key);
160+
} catch (\triagens\ArangoDb\ClientException $exception) {
161+
$caught = true;
162+
}
163+
164+
$this->assertTrue($caught, "expecting exception to be thrown for key ". $key);
165+
}
166+
}
107167

108168

109169
/**

0 commit comments

Comments
 (0)