Issues (9)

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/Adapter/SymfonyCache.php (1 issue)

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\CacheBundle\Adapter;
15
16
use Sonata\Cache\CacheAdapterInterface;
17
use Sonata\Cache\CacheElementInterface;
18
use Sonata\Cache\Exception\UnsupportedException;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
use Symfony\Component\Filesystem\Filesystem;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
24
use Symfony\Component\Routing\RouterInterface;
25
26
/**
27
 * Handles Symfony cache.
28
 *
29
 * @author Vincent Composieux <[email protected]>
30
 */
31
class SymfonyCache implements CacheAdapterInterface
32
{
33
    /**
34
     * @var RouterInterface
35
     */
36
    protected $router;
37
38
    /**
39
     * @var string
40
     */
41
    protected $cacheDir;
42
43
    /**
44
     * @var string
45
     */
46
    protected $token;
47
48
    /**
49
     * @var string[]
50
     */
51
    protected $types;
52
53
    /**
54
     * @var bool
55
     */
56
    protected $phpCodeCacheEnabled;
57
58
    /**
59
     * @var array
60
     */
61
    protected $servers;
62
63
    /**
64
     * @var array
65
     */
66
    protected $timeouts;
67
68
    /**
69
     * @var Filesystem
70
     */
71
    protected $filesystem;
72
73
    /**
74
     * @var EventDispatcherInterface|null
75
     */
76
    private $eventDispatcher;
77
78
    public function __construct(
79
        RouterInterface $router,
80
        Filesystem $filesystem,
81
        string $cacheDir,
82
        string $token,
83
        bool $phpCodeCacheEnabled,
84
        array $types,
85
        array $servers,
86
        array $timeouts,
87
        EventDispatcherInterface $eventDispatcher = null
88
    ) {
89
        $this->router = $router;
90
        $this->filesystem = $filesystem;
91
        $this->cacheDir = $cacheDir;
92
        $this->token = $token;
93
        $this->types = $types;
94
        $this->phpCodeCacheEnabled = $phpCodeCacheEnabled;
95
        $this->servers = $servers;
96
        $this->timeouts = $timeouts;
97
        $this->eventDispatcher = $eventDispatcher;
98
    }
99
100
    public function flushAll(): bool
101
    {
102
        return $this->flush(['all']);
103
    }
104
105
    /**
106
     * @throws \InvalidArgumentException
107
     * @throws \UnexpectedValueException
108
     */
109
    public function flush(array $keys = ['all']): bool
110
    {
111
        $result = true;
112
113
        foreach ($this->servers as $server) {
114
            foreach ($keys as $type) {
115
                $ip = $server['ip'];
116
117
                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
118
                    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
119
                } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
120
                    $socket = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
121
                } else {
122
                    throw new \InvalidArgumentException(sprintf('"%s" is not a valid ip address', $ip));
123
                }
124
125
                // generate the raw http request
126
                $command = sprintf("GET %s HTTP/1.1\r\n", $this->getUrl($type));
127
                $command .= sprintf("Host: %s\r\n", $server['domain']);
128
129
                if ($server['basic']) {
130
                    $command .= sprintf("Authorization: Basic %s\r\n", $server['basic']);
131
                }
132
133
                $command .= "Connection: Close\r\n\r\n";
134
135
                // setup the default timeout (avoid max execution time)
136
                socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $this->timeouts['SND']);
137
                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $this->timeouts['RCV']);
138
139
                socket_connect($socket, $server['ip'], $server['port']);
140
                socket_write($socket, $command);
141
142
                $content = '';
143
144
                do {
145
                    $buffer = socket_read($socket, 1024);
146
                    $content .= $buffer;
147
                } while (!empty($buffer));
148
149
                if ($result) {
150
                    $result = 'ok' === substr($content, -2);
151
                } else {
152
                    throw new \UnexpectedValueException(sprintf(
153
                        'Server answered with "%s"',
154
                        $content
155
                    ));
156
                }
157
            }
158
        }
159
160
        return $result;
161
    }
162
163
    /**
164
     * @throws AccessDeniedHttpException
165
     * @throws \RuntimeException
166
     */
167
    public function cacheAction(string $token, string $type): Response
168
    {
169
        if ($this->token !== $token) {
170
            throw new AccessDeniedHttpException('Invalid token.');
171
        }
172
173
        if (!\in_array($type, $this->types, true)) {
174
            throw new \RuntimeException(
175
                sprintf('Type "%s" is not defined, allowed types are: "%s"', $type, implode(', ', $this->types))
176
            );
177
        }
178
179
        $path = 'all' === $type ? $this->cacheDir : sprintf('%s/%s', $this->cacheDir, $type);
180
181
        /*
182
         * NEXT_MAJOR: Remove this if condition and else block.
183
         * clean up event dispatcher just before clearing the cache to prevent further events execution
184
         */
185
        if (null !== $this->eventDispatcher) {
186
            foreach ($this->eventDispatcher->getListeners() as $eventName => $eventListenerList) {
187
                foreach ($eventListenerList as $listener) {
188
                    $listener[0] instanceof EventSubscriberInterface ?
189
                        $this->eventDispatcher->removeSubscriber($listener[0]) :
190
                        $this->eventDispatcher->removeListener($eventName, [$listener[0], $listener[1]]);
191
                }
192
            }
193
        } else {
194
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
195
                sprintf(<<<'MESSAGE'
196
Passing no 9th argument to %s is deprecated since version sonata-project/cache-bundle 3.x and will be mandatory in 4.0.
197
Pass Symfony\Component\EventDispatcher\EventDispatcherInterface as 9th argument.
198
MESSAGE
199
                , __CLASS__),
200
                E_USER_DEPRECATED
201
            );
202
        }
203
204
        if ($this->filesystem->exists($path)) {
205
            $movedPath = $path.'_old_'.uniqid();
206
207
            $this->filesystem->rename($path, $movedPath);
208
            $this->filesystem->remove($movedPath);
209
210
            $this->clearPHPCodeCache();
211
        }
212
213
        return new Response('ok', 200, [
214
            'Cache-Control' => 'no-cache, must-revalidate',
215
            'Content-Length' => 2, // to prevent chunked transfer encoding
216
        ]);
217
    }
218
219
    /**
220
     * @throws UnsupportedException
221
     */
222
    public function has(array $keys): bool
223
    {
224
        throw new UnsupportedException('SymfonyCache has() method does not exist.');
225
    }
226
227
    /**
228
     * @throws UnsupportedException
229
     */
230
    public function set(array $keys, $data, int $ttl = 84600, array $contextualKeys = []): CacheElementInterface
231
    {
232
        throw new UnsupportedException('SymfonyCache set() method does not exist.');
233
    }
234
235
    /**
236
     * @throws UnsupportedException
237
     */
238
    public function get(array $keys): CacheElementInterface
239
    {
240
        throw new UnsupportedException('SymfonyCache get() method does not exist.');
241
    }
242
243
    public function isContextual(): bool
244
    {
245
        return false;
246
    }
247
248
    protected function getUrl(string $type): ?string
249
    {
250
        return $this->router->generate('sonata_cache_symfony', [
251
            'token' => $this->token,
252
            'type' => $type,
253
        ]);
254
    }
255
256
    protected function clearPHPCodeCache(): void
257
    {
258
        if (!$this->phpCodeCacheEnabled) {
259
            return;
260
        }
261
262
        if (\function_exists('opcache_reset')) {
263
            opcache_reset();
264
        }
265
    }
266
}
267