Issues (89)

Security Analysis    not enabled

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/Event/Cache/Cache.php (2 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 Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter\Event\Cache;
13
14
use Ivory\HttpAdapter\Event\Cache\Adapter\CacheAdapterInterface;
15
use Ivory\HttpAdapter\Event\Formatter\Formatter;
16
use Ivory\HttpAdapter\Event\Formatter\FormatterInterface;
17
use Ivory\HttpAdapter\HttpAdapterException;
18
use Ivory\HttpAdapter\Message\InternalRequestInterface;
19
use Ivory\HttpAdapter\Message\MessageFactoryInterface;
20
use Ivory\HttpAdapter\Message\ResponseInterface;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class Cache implements CacheInterface
26
{
27
    /**
28
     * @var CacheAdapterInterface
29
     */
30
    private $adapter;
31
32
    /**
33
     * @var FormatterInterface
34
     */
35
    private $formatter;
36
37
    /**
38
     * @var int|null
39
     */
40
    private $lifetime;
41
42
    /**
43
     * @var bool
44
     */
45
    private $cacheException;
46
47
    /**
48
     * @param CacheAdapterInterface   $adapter
49
     * @param FormatterInterface|null $formatter
50
     * @param int|null                $lifetime
51
     * @param bool                    $cacheException
52
     */
53 135
    public function __construct(
54
        CacheAdapterInterface $adapter,
55
        FormatterInterface $formatter = null,
56
        $lifetime = null,
57
        $cacheException = true
58
    ) {
59 135
        $this->setAdapter($adapter);
60 135
        $this->setFormatter($formatter ?: new Formatter());
61 135
        $this->setlifetime($lifetime);
62 135
        $this->cacheException($cacheException);
63 135
    }
64
65
    /**
66
     * @return CacheAdapterInterface
67
     */
68 18
    public function getAdapter()
69
    {
70 18
        return $this->adapter;
71
    }
72
73
    /**
74
     * @param CacheAdapterInterface $adapter
75
     */
76 135
    public function setAdapter(CacheAdapterInterface $adapter)
77
    {
78 135
        $this->adapter = $adapter;
79 135
    }
80
81
    /**
82
     * @return FormatterInterface
83
     */
84 18
    public function getFormatter()
85
    {
86 18
        return $this->formatter;
87
    }
88
89
    /**
90
     * @param FormatterInterface $formatter
91
     */
92 135
    public function setFormatter(FormatterInterface $formatter)
93
    {
94 135
        $this->formatter = $formatter;
95 135
    }
96
97
    /**
98
     * @return int|null
99
     */
100 18
    public function getLifetime()
101
    {
102 18
        return $this->lifetime;
103
    }
104
105
    /**
106
     * @param int|null $lifetime
107
     */
108 135
    public function setLifetime($lifetime = null)
109
    {
110 135
        $this->lifetime = $lifetime;
111 135
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 135
    public function cacheException($cacheException = null)
117
    {
118 135
        if ($cacheException !== null) {
119 135
            $this->cacheException = $cacheException;
120 105
        }
121
122 135
        return $this->cacheException;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 18 View Code Duplication
    public function getResponse(InternalRequestInterface $internalRequest, MessageFactoryInterface $messageFactory)
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...
129
    {
130 18
        if (!$this->adapter->has($id = $this->getIdentifier($internalRequest, 'response'))) {
131 9
            return;
132
        }
133
134 9
        $response = $this->unserializeResponse($this->adapter->get($id), $messageFactory);
135
136 9
        return $response->withParameter('request', $internalRequest);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 27 View Code Duplication
    public function getException(InternalRequestInterface $internalRequest, MessageFactoryInterface $messageFactory)
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...
143
    {
144 27
        if (!$this->cacheException || !$this->adapter->has($id = $this->getIdentifier($internalRequest, 'exception'))) {
145 18
            return;
146
        }
147
148 9
        $exception = $this->unserializeException($this->adapter->get($id));
149 9
        $exception->setRequest($internalRequest);
150
151 9
        return $exception;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 18
    public function saveResponse(ResponseInterface $response, InternalRequestInterface $internalRequest)
158
    {
159 18
        if (!$this->adapter->has($id = $this->getIdentifier($internalRequest, 'response'))) {
160 9
            $this->adapter->set($id, $this->serializeResponse($response), $this->lifetime);
161 7
        }
162 18
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 27
    public function saveException(HttpAdapterException $exception, InternalRequestInterface $internalRequest)
168
    {
169 27
        if ($this->cacheException && !$this->adapter->has($id = $this->getIdentifier($internalRequest, 'exception'))) {
170 9
            $this->adapter->set($id, $this->serializeException($exception), $this->lifetime);
171 7
        }
172 27
    }
173
174
    /**
175
     * @param InternalRequestInterface $internalRequest
176
     * @param string                   $context
177
     *
178
     * @return string
179
     */
180 72
    private function getIdentifier(InternalRequestInterface $internalRequest, $context)
181
    {
182 72
        return sha1($context.$this->serializeInternalRequest($internalRequest));
183
    }
184
185
    /**
186
     * @param InternalRequestInterface $internalRequest
187
     *
188
     * @return string
189
     */
190 72
    private function serializeInternalRequest(InternalRequestInterface $internalRequest)
191
    {
192 72
        $formattedInternalRequest = $this->formatter->formatRequest($internalRequest);
193 72
        unset($formattedInternalRequest['parameters']);
194
195 72
        return $this->serialize($formattedInternalRequest);
196
    }
197
198
    /**
199
     * @param ResponseInterface $response
200
     *
201
     * @return string
202
     */
203 9
    private function serializeResponse(ResponseInterface $response)
204
    {
205 9
        return $this->serialize($this->formatter->formatResponse($response));
206
    }
207
208
    /**
209
     * @param HttpAdapterException $exception
210
     *
211
     * @return string
212
     */
213 9
    private function serializeException(HttpAdapterException $exception)
214
    {
215 9
        return $this->serialize($this->formatter->formatException($exception));
216
    }
217
218
    /**
219
     * @param string                  $serialized
220
     * @param MessageFactoryInterface $messageFactory
221
     *
222
     * @return ResponseInterface
223
     */
224 9
    private function unserializeResponse($serialized, MessageFactoryInterface $messageFactory)
225
    {
226 9
        return $this->createResponse($this->unserialize($serialized), $messageFactory);
227
    }
228
229
    /**
230
     * @param string $serialized
231
     *
232
     * @return HttpAdapterException
233
     */
234 9
    private function unserializeException($serialized)
235
    {
236 9
        return $this->createException($this->unserialize($serialized));
237
    }
238
239
    /**
240
     * @param array                   $unserialized
241
     * @param MessageFactoryInterface $messageFactory
242
     *
243
     * @return ResponseInterface
244
     */
245 9
    private function createResponse(array $unserialized, MessageFactoryInterface $messageFactory)
246
    {
247 9
        return $messageFactory->createResponse(
248 9
            $unserialized['status_code'],
249 9
            $unserialized['protocol_version'],
250 9
            $unserialized['headers'],
251 9
            $unserialized['body'],
252 9
            $unserialized['parameters']
253 7
        );
254
    }
255
256
    /**
257
     * @param array $unserialized
258
     *
259
     * @return HttpAdapterException
260
     */
261 9
    private function createException(array $unserialized)
262
    {
263 9
        return new HttpAdapterException($unserialized['message']);
264
    }
265
266
    /**
267
     * @param array $data
268
     *
269
     * @return string
270
     */
271 72
    private function serialize(array $data)
272
    {
273 72
        return json_encode($data);
274
    }
275
276
    /**
277
     * @param string $data
278
     *
279
     * @return array
280
     */
281 18
    private function unserialize($data)
282
    {
283 18
        return json_decode($data, true);
284
    }
285
}
286