From 19cd19e9a703b997d5b22d5ff3c45d906769e2d5 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Sun, 14 Feb 2016 10:01:54 +0100 Subject: [PATCH] Ensure no MongoDB driver classes are serialized --- README.md | 5 ++++ lib/Alcaeus/MongoDbAdapter/AbstractCursor.php | 8 +++++++ lib/Mongo/MongoClient.php | 10 ++++++++ lib/Mongo/MongoCollection.php | 8 +++++++ lib/Mongo/MongoCommandCursor.php | 8 +++++++ lib/Mongo/MongoCursor.php | 24 +++++++++++++++++++ lib/Mongo/MongoDB.php | 8 +++++++ lib/Mongo/MongoGridFS.php | 8 +++++++ .../MongoDbAdapter/Mongo/MongoClientTest.php | 5 ++++ .../Mongo/MongoCollectionTest.php | 5 ++++ .../Mongo/MongoCommandCursorTest.php | 7 ++++++ .../MongoDbAdapter/Mongo/MongoCursorTest.php | 7 ++++++ .../MongoDbAdapter/Mongo/MongoDBTest.php | 5 ++++ .../Mongo/MongoDeleteBatchTest.php | 6 +++++ .../Mongo/MongoGridFSCursorTest.php | 10 ++++++++ .../Mongo/MongoGridFSFileTest.php | 9 +++++++ .../MongoDbAdapter/Mongo/MongoGridFSTest.php | 5 ++++ .../Mongo/MongoInsertBatchTest.php | 6 +++++ .../Mongo/MongoUpdateBatchTest.php | 6 ++++- 19 files changed, 149 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a3cef95d..c5386e63 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,11 @@ counterparts in `ext-mongo`. Do not rely on exception messages being the same. Methods that return a result array containing a `connectionId` field will always return `0` as connection ID. +## Serialization of objects +Serialization of any Mongo* objects (e.g. MongoGridFSFile, MongoCursor, etc.) +will not work properly. The objects can be serialized but are not usable after +unserializing them. + ## Mongo - The Mongo class is deprecated and was not implemented in this library. If you diff --git a/lib/Alcaeus/MongoDbAdapter/AbstractCursor.php b/lib/Alcaeus/MongoDbAdapter/AbstractCursor.php index 5039ffae..fc33584f 100644 --- a/lib/Alcaeus/MongoDbAdapter/AbstractCursor.php +++ b/lib/Alcaeus/MongoDbAdapter/AbstractCursor.php @@ -375,4 +375,12 @@ protected function reset() $this->cursor = null; $this->iterator = null; } + + /** + * @return array + */ + public function __sleep() + { + return ['batchSize', 'connection', 'iterator', 'ns', 'optionNames', 'position', 'startedIterating']; + } } diff --git a/lib/Mongo/MongoClient.php b/lib/Mongo/MongoClient.php index 1f26e062..86021ec5 100644 --- a/lib/Mongo/MongoClient.php +++ b/lib/Mongo/MongoClient.php @@ -334,5 +334,15 @@ private function notImplemented() { throw new \Exception('Not implemented'); } + + /** + * @return array + */ + function __sleep() + { + return [ + 'connected', 'status', 'server', 'persistent' + ]; + } } diff --git a/lib/Mongo/MongoCollection.php b/lib/Mongo/MongoCollection.php index 1d151e7c..4be8cacf 100644 --- a/lib/Mongo/MongoCollection.php +++ b/lib/Mongo/MongoCollection.php @@ -958,5 +958,13 @@ private function checkCollectionName($name) throw new Exception('Collection name cannot contain null bytes'); } } + + /** + * @return array + */ + public function __sleep() + { + return ['db', 'name']; + } } diff --git a/lib/Mongo/MongoCommandCursor.php b/lib/Mongo/MongoCommandCursor.php index f88571a2..c55ebd0c 100644 --- a/lib/Mongo/MongoCommandCursor.php +++ b/lib/Mongo/MongoCommandCursor.php @@ -100,4 +100,12 @@ protected function getIterationInfo() return $iterationInfo; } + + /** + * @return array + */ + public function __sleep() + { + return ['command'] + parent::__sleep(); + } } diff --git a/lib/Mongo/MongoCursor.php b/lib/Mongo/MongoCursor.php index da4346e6..f36434b6 100644 --- a/lib/Mongo/MongoCursor.php +++ b/lib/Mongo/MongoCursor.php @@ -455,4 +455,28 @@ protected function getCursorInfo() 'fields' => $this->projection, ]; } + + /** + * @return array + */ + public function __sleep() + { + return [ + 'allowPartialResults', + 'awaitData', + 'flags', + 'hint', + 'limit', + 'maxTimeMS', + 'noCursorTimeout', + 'optionNames', + 'options', + 'projection', + 'query', + 'skip', + 'snapshot', + 'sort', + 'tailable', + ] + parent::__sleep(); + } } diff --git a/lib/Mongo/MongoDB.php b/lib/Mongo/MongoDB.php index 8bec8992..91f8e9df 100644 --- a/lib/Mongo/MongoDB.php +++ b/lib/Mongo/MongoDB.php @@ -541,4 +541,12 @@ private function getSystemCollectionFilterClosure($includeSystemCollections = fa return $includeSystemCollections || ! preg_match('#^system\.#', $collectionInfo->getName()); }; } + + /** + * @return array + */ + public function __sleep() + { + return ['connection', 'name']; + } } diff --git a/lib/Mongo/MongoGridFS.php b/lib/Mongo/MongoGridFS.php index 47f90395..247b0836 100644 --- a/lib/Mongo/MongoGridFS.php +++ b/lib/Mongo/MongoGridFS.php @@ -444,4 +444,12 @@ private function isOKResult($result) return (is_array($result) && $result['ok'] == 1.0) || (is_bool($result) && $result); } + + /** + * @return array + */ + public function __sleep() + { + return ['chunks', 'chunksName', 'database', 'defaultChunkSize', 'filesName', 'prefix'] + parent::__sleep(); + } } diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoClientTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoClientTest.php index 46f49688..75316829 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoClientTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoClientTest.php @@ -9,6 +9,11 @@ */ class MongoClientTest extends TestCase { + public function testSerialize() + { + $this->assertInternalType('string', serialize($this->getClient())); + } + public function testGetDb() { $client = $this->getClient(); diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php index 4a532d9a..c5ab52de 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php @@ -10,6 +10,11 @@ */ class MongoCollectionTest extends TestCase { + public function testSerialize() + { + $this->assertInternalType('string', serialize($this->getCollection())); + } + public function testGetNestedCollections() { $collection = $this->getCollection()->foo->bar; diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCommandCursorTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCommandCursorTest.php index f5227fb6..dd878f5c 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCommandCursorTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCommandCursorTest.php @@ -10,6 +10,13 @@ */ class MongoCommandCursorTest extends TestCase { + public function testSerialize() + { + $this->prepareData(); + $cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]); + $this->assertInternalType('string', serialize($cursor)); + } + public function testInfo() { $this->prepareData(); diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php index a0fbeb29..06567a94 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php @@ -12,6 +12,13 @@ */ class MongoCursorTest extends TestCase { + public function testSerialize() + { + $this->prepareData(); + $cursor = $this->getCollection()->find(['foo' => 'bar']); + $this->assertInternalType('string', serialize($cursor)); + } + public function testCursorConvertsTypes() { $this->prepareData(); diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php index 25bdf61e..64cc894a 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php @@ -10,6 +10,11 @@ */ class MongoDBTest extends TestCase { + public function testSerialize() + { + $this->assertInternalType('string', serialize($this->getDatabase())); + } + public function testEmptyDatabaseName() { $this->setExpectedException('Exception', 'Database name cannot be empty'); diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoDeleteBatchTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoDeleteBatchTest.php index fc7bb87d..3e25d359 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoDeleteBatchTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoDeleteBatchTest.php @@ -6,6 +6,12 @@ class MongoDeleteBatchTest extends TestCase { + public function testSerialize() + { + $batch = new \MongoDeleteBatch($this->getCollection()); + $this->assertInternalType('string', serialize($batch)); + } + public function testDeleteOne() { $collection = $this->getCollection(); diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSCursorTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSCursorTest.php index 7f589568..81c815b4 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSCursorTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSCursorTest.php @@ -6,6 +6,16 @@ class MongoGridFSCursorTest extends TestCase { + public function testSerialize() + { + $gridfs = $this->getGridFS(); + $gridfs->storeBytes('foo', ['filename' => 'foo.txt']); + $gridfs->storeBytes('bar', ['filename' => 'bar.txt']); + $cursor = $gridfs->find(['filename' => 'foo.txt']); + + $this->assertInternalType('string', serialize($cursor)); + } + public function testCursorItems() { $gridfs = $this->getGridFS(); diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSFileTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSFileTest.php index 47415c8d..ffedb08f 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSFileTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSFileTest.php @@ -6,6 +6,15 @@ class MongoGridFSFileTest extends TestCase { + public function testSerialize() + { + $this->prepareFile('abcd', ['filename' => 'foo']); + $file = $this->getGridFS()->findOne(['filename' => 'foo']); + $this->assertInstanceOf(\MongoGridFSFile::class, $file); + + $this->assertInternalType('string', serialize($file)); + } + public function testFileProperty() { $file = $this->getFile(); diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSTest.php index ffe37056..08fce688 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSTest.php @@ -6,6 +6,11 @@ class MongoGridFSTest extends TestCase { + public function testSerialize() + { + $this->assertInternalType('string', serialize($this->getGridFS())); + } + public function testChunkProperty() { $collection = $this->getGridFS(); diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoInsertBatchTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoInsertBatchTest.php index 7c315fa1..0ffbdae9 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoInsertBatchTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoInsertBatchTest.php @@ -6,6 +6,12 @@ class MongoInsertBatchTest extends TestCase { + public function testSerialize() + { + $batch = new \MongoInsertBatch($this->getCollection()); + $this->assertInternalType('string', serialize($batch)); + } + public function testInsertBatch() { $batch = new \MongoInsertBatch($this->getCollection()); diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoUpdateBatchTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoUpdateBatchTest.php index b5cdbee3..87fc70c0 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoUpdateBatchTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoUpdateBatchTest.php @@ -6,7 +6,11 @@ class MongoUpdateBatchTest extends TestCase { - + public function testSerialize() + { + $batch = new \MongoUpdateBatch($this->getCollection()); + $this->assertInternalType('string', serialize($batch)); + } public function testUpdateOne() {