Issues (234)

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/Filesystem/Replicate.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
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\MediaBundle\Filesystem;
15
16
use Gaufrette\Adapter as AdapterInterface;
17
use Gaufrette\Adapter\MetadataSupporter;
18
use Gaufrette\Filesystem;
19
use Psr\Log\LoggerInterface;
20
21
/**
22
 * @final since sonata-project/media-bundle 3.21.0
23
 */
24
class Replicate implements AdapterInterface, MetadataSupporter
25
{
26
    /**
27
     * @var AdapterInterface
28
     */
29
    protected $master;
30
31
    /**
32
     * @var AdapterInterface
33
     */
34
    protected $slave;
35
36
    /**
37
     * @var LoggerInterface
38
     */
39
    protected $logger;
40
41
    /**
42
     * @param LoggerInterface $logger
43
     */
44
    public function __construct(AdapterInterface $master, AdapterInterface $slave, ?LoggerInterface $logger = null)
45
    {
46
        $this->master = $master;
47
        $this->slave = $slave;
48
        $this->logger = $logger;
49
    }
50
51
    public function delete($key)
52
    {
53
        $ok = true;
54
55
        try {
56
            $this->slave->delete($key);
57
        } catch (\Exception $e) {
58
            if ($this->logger) {
59
                $this->logger->critical(sprintf('Unable to delete %s, error: %s', $key, $e->getMessage()));
60
            }
61
62
            $ok = false;
63
        }
64
65
        try {
66
            $this->master->delete($key);
67
        } catch (\Exception $e) {
68
            if ($this->logger) {
69
                $this->logger->critical(sprintf('Unable to delete %s, error: %s', $key, $e->getMessage()));
70
            }
71
72
            $ok = false;
73
        }
74
75
        return $ok;
76
    }
77
78
    public function mtime($key)
79
    {
80
        return $this->master->mtime($key);
81
    }
82
83
    public function keys()
84
    {
85
        return $this->master->keys();
86
    }
87
88
    public function exists($key)
89
    {
90
        return $this->master->exists($key);
91
    }
92
93
    public function write($key, $content, ?array $metadata = null)
94
    {
95
        $ok = true;
96
        $return = false;
97
98
        try {
99
            $return = $this->master->write($key, $content, $metadata);
0 ignored issues
show
The call to Adapter::write() has too many arguments starting with $metadata.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
        } catch (\Exception $e) {
101
            if ($this->logger) {
102
                $this->logger->critical(sprintf('Unable to write %s, error: %s', $key, $e->getMessage()));
103
            }
104
105
            $ok = false;
106
        }
107
108
        try {
109
            $return = $this->slave->write($key, $content, $metadata);
0 ignored issues
show
The call to Adapter::write() has too many arguments starting with $metadata.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
110
        } catch (\Exception $e) {
111
            if ($this->logger) {
112
                $this->logger->critical(sprintf('Unable to write %s, error: %s', $key, $e->getMessage()));
113
            }
114
115
            $ok = false;
116
        }
117
118
        return $ok && $return;
119
    }
120
121
    public function read($key)
122
    {
123
        return $this->master->read($key);
124
    }
125
126
    public function rename($key, $new)
127
    {
128
        $ok = true;
129
130
        try {
131
            $this->master->rename($key, $new);
132
        } catch (\Exception $e) {
133
            if ($this->logger) {
134
                $this->logger->critical(sprintf('Unable to rename %s, error: %s', $key, $e->getMessage()));
135
            }
136
137
            $ok = false;
138
        }
139
140
        try {
141
            $this->slave->rename($key, $new);
142
        } catch (\Exception $e) {
143
            if ($this->logger) {
144
                $this->logger->critical(sprintf('Unable to rename %s, error: %s', $key, $e->getMessage()));
145
            }
146
147
            $ok = false;
148
        }
149
150
        return $ok;
151
    }
152
153
    /**
154
     * If one of the adapters can allow inserting metadata.
155
     *
156
     * @return bool true if supports metadata, false if not
157
     */
158
    public function supportsMetadata()
159
    {
160
        return $this->master instanceof MetadataSupporter || $this->slave instanceof MetadataSupporter;
161
    }
162
163
    public function setMetadata($key, $metadata): void
164
    {
165
        if ($this->master instanceof MetadataSupporter) {
166
            $this->master->setMetadata($key, $metadata);
167
        }
168
        if ($this->slave instanceof MetadataSupporter) {
169
            $this->slave->setMetadata($key, $metadata);
170
        }
171
    }
172
173
    public function getMetadata($key)
174
    {
175
        if ($this->master instanceof MetadataSupporter) {
176
            return $this->master->getMetadata($key);
177
        } elseif ($this->slave instanceof MetadataSupporter) {
178
            return $this->slave->getMetadata($key);
179
        }
180
181
        return [];
182
    }
183
184
    /**
185
     * Gets the class names as an array for both adapters.
186
     *
187
     * @return string[]
188
     */
189
    public function getAdapterClassNames()
190
    {
191
        return [
192
            \get_class($this->master),
193
            \get_class($this->slave),
194
        ];
195
    }
196
197
    public function createFile($key, Filesystem $filesystem)
198
    {
199
        return $this->master->createFile($key, $filesystem);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Gaufrette\Adapter as the method createFile() does only exist in the following implementations of said interface: Gaufrette\Adapter\Ftp, Gaufrette\Adapter\PhpseclibSftp, Sonata\MediaBundle\Filesystem\Replicate.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
200
    }
201
202
    public function createFileStream($key, Filesystem $filesystem)
203
    {
204
        return $this->master->createFileStream($key, $filesystem);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Gaufrette\Adapter as the method createFileStream() does only exist in the following implementations of said interface: Sonata\MediaBundle\Filesystem\Replicate.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
205
    }
206
207
    public function listDirectory($directory = '')
208
    {
209
        return $this->master->listDirectory($directory);
0 ignored issues
show
The method listDirectory() does not exist on Gaufrette\Adapter. Did you maybe mean isDirectory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
210
    }
211
212
    public function isDirectory($key)
213
    {
214
        return $this->master->isDirectory($key);
215
    }
216
}
217