Issues (40)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/MongoDbStore.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the vendor/project package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\KeyValueStore;
13
14
use Closure;
15
use Exception;
16
use MongoDB\BSON\Binary;
17
use MongoDB\Collection;
18
use MongoDB\Driver\Exception\UnexpectedValueException;
19
use Webmozart\KeyValueStore\Api\KeyValueStore;
20
use Webmozart\KeyValueStore\Api\NoSuchKeyException;
21
use Webmozart\KeyValueStore\Api\ReadException;
22
use Webmozart\KeyValueStore\Api\UnserializationFailedException;
23
use Webmozart\KeyValueStore\Api\UnsupportedValueException;
24
use Webmozart\KeyValueStore\Api\WriteException;
25
use Webmozart\KeyValueStore\Util\KeyUtil;
26
use Webmozart\KeyValueStore\Util\Serializer;
27
28
/**
29
 * A key-value-store backed by MongoDB.
30
 *
31
 * @since 1.0
32
 *
33
 * @author Bernhard Schussek <[email protected]>
34
 */
35
class MongoDbStore implements KeyValueStore
36
{
37
    /**
38
     * Flag: Disable serialization.
39
     */
40
    const NO_SERIALIZE = 1;
41
42
    /**
43
     * Flag: Support storage of binary data.
44
     */
45
    const SUPPORT_BINARY = 2;
46
47
    private static $typeMap = array(
48
        'root' => 'array',
49
        'document' => 'array',
50
        'array' => 'array',
51
    );
52
53
    /**
54
     * @var Collection
55
     */
56
    private $collection;
57
58
    /**
59
     * @var Closure
60
     */
61
    private $serialize;
62
63
    /**
64
     * @var Closure
65
     */
66
    private $unserialize;
67
68 332
    public function __construct(Collection $collection, $flags = 0)
69
    {
70 332
        $this->collection = $collection;
71
72 332
        if ($flags & self::NO_SERIALIZE) {
73 166
            if ($flags & self::SUPPORT_BINARY) {
74
                $this->serialize = function ($unserialized) {
75 55
                    if (!is_string($unserialized)) {
76 17
                        throw UnsupportedValueException::forValue($unserialized, $this);
77
                    }
78
79 38
                    return new Binary($unserialized, Binary::TYPE_GENERIC);
80
                };
81
                $this->unserialize = function (Binary $serialized) {
82 18
                    return $serialized->getData();
83 83
                };
84
            } else {
85
                $this->serialize = function ($unserialized) {
86 55
                    if (!is_scalar($unserialized) && !is_array($unserialized) && null !== $unserialized) {
87 5
                        throw UnsupportedValueException::forValue($unserialized, $this);
88
                    }
89
90 50
                    return $unserialized;
91
                };
92
                $this->unserialize = function ($serialized) {
93 28
                    return $serialized;
94 166
                };
95
            }
96
        } else {
97 194
            if ($flags & self::SUPPORT_BINARY) {
98
                $this->serialize = function ($unserialized) {
99 55
                    return new Binary(
100 55
                        Serializer::serialize($unserialized),
101 53
                        Binary::TYPE_GENERIC
102
                    );
103
                };
104
                $this->unserialize = function (Binary $serialized) {
105 33
                    return Serializer::unserialize($serialized->getData());
106 83
                };
107
            } else {
108
                $this->serialize = function ($unserialized) {
109 59
                    return Serializer::serialize($unserialized);
110
                };
111 46
                $this->unserialize = function ($serialized) {
112 46
                    return Serializer::unserialize($serialized);
113
                };
114
            }
115
        }
116 332
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 232
    public function set($key, $value)
122
    {
123 232
        KeyUtil::validate($key);
124
125 224
        $serialized = $this->serialize->__invoke($value);
126
127
        try {
128 198
            $this->collection->replaceOne(
129 198
                array('_id' => $key),
130 198
                array('_id' => $key, 'value' => $serialized),
131 198
                array('upsert' => true)
132
            );
133 9
        } catch (UnexpectedValueException $e) {
0 ignored issues
show
The class MongoDB\Driver\Exception\UnexpectedValueException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
134 5
            throw UnsupportedValueException::forType('binary', $this, 0, $e);
135 4
        } catch (Exception $e) {
136 4
            throw WriteException::forException($e);
137
        }
138 189
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 28 View Code Duplication
    public function get($key, $default = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145 28
        KeyUtil::validate($key);
146
147
        try {
148 20
            $document = $this->collection->findOne(
149 20
                array('_id' => $key),
150 20
                array('typeMap' => self::$typeMap)
151
            );
152 4
        } catch (Exception $e) {
153 4
            throw ReadException::forException($e);
154
        }
155
156 16
        if (null === $document) {
157 4
            return $default;
158
        }
159
160 12
        return $this->unserialize->__invoke($document['value']);
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 105 View Code Duplication
    public function getOrFail($key)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168 105
        KeyUtil::validate($key);
169
170
        try {
171 97
            $document = $this->collection->findOne(
172 97
                array('_id' => $key),
173 97
                array('typeMap' => self::$typeMap)
174
            );
175 4
        } catch (Exception $e) {
176 4
            throw ReadException::forException($e);
177
        }
178
179 93
        if (null === $document) {
180 4
            throw NoSuchKeyException::forKey($key);
181
        }
182
183 89
        return $this->unserialize->__invoke($document['value']);
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189 24
    public function getMultiple(array $keys, $default = null)
190
    {
191 24
        KeyUtil::validateMultiple($keys);
192
193 16
        $values = array_fill_keys($keys, $default);
194
195
        try {
196 16
            $cursor = $this->collection->find(
197 16
                array('_id' => array('$in' => array_values($keys))),
198 16
                array('typeMap' => self::$typeMap)
199
            );
200
201 12
            foreach ($cursor as $document) {
202 12
                $values[$document['_id']] = $this->unserialize->__invoke($document['value']);
203
            }
204 8
        } catch (UnserializationFailedException $e) {
205 4
            throw $e;
206 4
        } catch (Exception $e) {
207 4
            throw ReadException::forException($e);
208
        }
209
210 8
        return $values;
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216 105
    public function getMultipleOrFail(array $keys)
217
    {
218 105
        KeyUtil::validateMultiple($keys);
219
220 97
        $values = array();
221
222
        try {
223 97
            $cursor = $this->collection->find(
224 97
                array('_id' => array('$in' => array_values($keys))),
225 97
                array('typeMap' => self::$typeMap)
226
            );
227
228 93
            foreach ($cursor as $document) {
229 93
                $values[$document['_id']] = $this->unserialize->__invoke($document['value']);
230
            }
231 8
        } catch (UnserializationFailedException $e) {
232 4
            throw $e;
233 4
        } catch (Exception $e) {
234 4
            throw ReadException::forException($e);
235
        }
236
237 89
        $notFoundKeys = array_diff($keys, array_keys($values));
238
239 89
        if (count($notFoundKeys) > 0) {
240 4
            throw NoSuchKeyException::forKeys($notFoundKeys);
241
        }
242
243 85
        return $values;
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249 44 View Code Duplication
    public function remove($key)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
250
    {
251 44
        KeyUtil::validate($key);
252
253
        try {
254 36
            $result = $this->collection->deleteOne(array('_id' => $key));
255 32
            $deletedCount = $result->getDeletedCount();
256 8
        } catch (Exception $e) {
257 8
            throw WriteException::forException($e);
258
        }
259
260 28
        return $deletedCount > 0;
261
    }
262
263
    /**
264
     * {@inheritdoc}
265
     */
266 101 View Code Duplication
    public function exists($key)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
267
    {
268 101
        KeyUtil::validate($key);
269
270
        try {
271 93
            $count = $this->collection->count(array('_id' => $key));
272 4
        } catch (Exception $e) {
273 4
            throw ReadException::forException($e);
274
        }
275
276 89
        return $count > 0;
277
    }
278
279
    /**
280
     * {@inheritdoc}
281
     */
282 332
    public function clear()
283
    {
284
        try {
285 332
            $this->collection->drop();
286 4
        } catch (Exception $e) {
287 4
            throw WriteException::forException($e);
288
        }
289 332
    }
290
291
    /**
292
     * {@inheritdoc}
293
     */
294 16
    public function keys()
295
    {
296
        try {
297 16
            $cursor = $this->collection->find(array(), array(
298 16
                'projection' => array('_id' => 1),
299
            ));
300
301 12
            $keys = array();
302
303 12
            foreach ($cursor as $document) {
304 12
                $keys[] = $document['_id'];
305
            }
306 4
        } catch (Exception $e) {
307 4
            throw ReadException::forException($e);
308
        }
309
310 12
        return $keys;
311
    }
312
}
313