Issues (183)

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/Ftp/Driver/Sftp.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
namespace FMUP\Ftp\Driver;
3
4
use FMUP\Ftp\FtpAbstract;
5
use FMUP\Ftp\Exception as FtpException;
6
use FMUP\Logger;
7
8
class Sftp extends FtpAbstract implements Logger\LoggerInterface
9
{
10
    use Logger\LoggerTrait;
11
12
    const METHODS = 'methods';
13
    const CALLBACKS = 'callbacks';
14
    const USE_INCLUDE_PATH = 'use_include_path';
15
    const GET_CONTENT_CONTEXT = 'get_content_context';
16
    const PUT_CONTENT_CONTEXT = 'put_content_context';
17
    const PUT_CONTENT_FLAGS = 'put_content_flags';
18
    const OFFSET = 'offset';
19
    const MAXLEN = 'maxlen';
20
    const CREATE_MODE = 'create_mode';
21
22
    private $sftpSession;
23
24
    /**
25
     * @return resource
26
     * @throws FtpException
27
     */
28 1
    protected function getSftpSession()
29
    {
30 1
        if (!$this->sftpSession) {
31 1
            $this->sftpSession = $this->ssh2Sftp($this->getSession());
32
        }
33 1
        return $this->sftpSession;
34
    }
35
36
    /**
37
     * @return array|null
38
     */
39 2
    protected function getMethods()
40
    {
41 2
        return isset($this->settings[self::METHODS]) ? $this->settings[self::METHODS] : null;
42
    }
43
44
    /**
45
     * @return array|null
46
     */
47 2
    protected function getCallbacks()
48
    {
49 2
        return isset($this->settings[self::CALLBACKS]) ? $this->settings[self::CALLBACKS] : null;
50
    }
51
52
    /**
53
     * Optional param for file_get_content
54
     * @return bool
55
     */
56 1
    protected function getUseIncludePath()
57
    {
58 1
        return isset($this->settings[self::USE_INCLUDE_PATH]) ? $this->settings[self::USE_INCLUDE_PATH] : false;
59
    }
60
61
    /**
62
     * Optional param for file_get_content
63
     * @return resource|null
64
     */
65 1
    protected function getGetContentContext()
66
    {
67 1
        return isset($this->settings[self::GET_CONTENT_CONTEXT]) ? $this->settings[self::GET_CONTENT_CONTEXT] : null;
68
    }
69
70
    /**
71
     * Optional param for file_get_content
72
     * @return int
73
     */
74 1
    protected function getOffset()
75
    {
76 1
        return isset($this->settings[self::OFFSET]) ? $this->settings[self::OFFSET] : 0;
77
    }
78
79
    /**
80
     * Optional param for file_get_content
81
     * @return int|null
82
     */
83 1
    protected function getMaxLen()
84
    {
85 1
        return isset($this->settings[self::MAXLEN]) ? $this->settings[self::MAXLEN] : null;
86
    }
87
88
    /**
89
     * Optional param for file_put_content
90
     * @return int
91
     */
92 1
    protected function getPutContentFlags()
93
    {
94 1
        return isset($this->settings[self::PUT_CONTENT_FLAGS]) ? $this->settings[self::PUT_CONTENT_FLAGS] : 0;
95
    }
96
97
    /**
98
     * Optional param for file_put_content
99
     * @return resource|null
100
     */
101 1
    protected function getPutContentContext()
102
    {
103 1
        return isset($this->settings[self::PUT_CONTENT_CONTEXT]) ? $this->settings[self::PUT_CONTENT_CONTEXT] : null;
104
    }
105
106
    /**
107
     * Optional param for file_get_content
108
     * @return int
109
     */
110 1
    protected function getCreateMode()
111
    {
112 1
        return isset($this->settings[self::CREATE_MODE]) ? $this->settings[self::CREATE_MODE] : 0644;
113
    }
114
115
    /**
116
     * @param string $host
117
     * @param int $port
118
     * @return $this
119
     * @throws FtpException
120
     */
121 1
    public function connect($host, $port = 22)
122
    {
123
124 1
        $this->setSession($this->ssh2Connect($host, $port, $this->getMethods(), $this->getCallbacks()));
125 1
        return $this;
126
    }
127
128
    /**
129
     * @param string $host
130
     * @param int $port
131
     * @param array|null $methods
132
     * @param array|null $callbacks
133
     * @return resource
134
     * @codeCoverageIgnore
135
     */
136
    protected function ssh2Connect($host, $port = 22, array $methods = null, array $callbacks = null)
137
    {
138
        return ssh2_connect($host, $port, $methods, $callbacks);
139
    }
140
141
    /**
142
     * @param string $user
143
     * @param string $pass
144
     * @return bool
145
     * @throws FtpException
146
     */
147 2
    public function login($user, $pass)
148
    {
149 2
        $ret = $this->ssh2AuthPassword($this->getSession(), $user, $pass);
150 2
        if (!$ret) {
151 1
            $this->log(Logger::ERROR, "Unable to login to SFTP server", (array)$this->getSettings());
152 1
            throw new FtpException('Unable to login to the SFTP server');
153
        }
154 1
        return $ret;
155
    }
156
157
    /**
158
     * @param resource $session
159
     * @param string $username
160
     * @param string $password
161
     * @return bool
162
     * @codeCoverageIgnore
163
     */
164
    protected function ssh2AuthPassword($session, $username, $password)
165
    {
166
        return ssh2_auth_password($session, $username, $password);
167
    }
168
169
    /**
170
     * @param string $localFile
171
     * @param string $remoteFile
172
     * @return int
173
     */
174 2
    public function get($localFile, $remoteFile)
175
    {
176 2
        if ($this->getMaxLen() === null) {
177 1
            $fileContent = $this->fileGetContents(
178 1
                'ssh2.sftp://' . $this->getSftpSession() . '/' . $remoteFile,
179 1
                $this->getUseIncludePath(),
180 1
                $this->getGetContentContext(),
181 1
                $this->getOffset()
182
            );
183
        } else {
184 1
            $fileContent = $this->fileGetContents(
185 1
                'ssh2.sftp://' . $this->getSftpSession() . '/' . $remoteFile,
186 1
                $this->getUseIncludePath(),
187 1
                $this->getGetContentContext(),
188 1
                $this->getOffset(),
189 1
                $this->getMaxLen()
190
            );
191
        }
192 2
        return $this->filePutContents(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->filePutCon...etPutContentContext()); (integer) is incompatible with the return type declared by the interface FMUP\Ftp\FtpInterface::get of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
193 2
            $localFile,
194 2
            $fileContent,
195 2
            $this->getPutContentFlags(),
196 2
            $this->getPutContentContext()
197
        );
198
    }
199
200
    /**
201
     * Put file on ftp server
202
     * @param string $remoteFile
203
     * @param string $localFile
204
     * @return bool
205
     */
206 2
    public function put($remoteFile, $localFile)
207
    {
208 2
        if ($this->getMaxLen() === null) {
209 1
            $fileContent = $this->fileGetContents(
210 1
                $localFile,
211 1
                $this->getUseIncludePath(),
212 1
                $this->getGetContentContext(),
213 1
                $this->getOffset()
214
            );
215
        } else {
216 1
            $fileContent = $this->fileGetContents(
217 1
                $localFile,
218 1
                $this->getUseIncludePath(),
219 1
                $this->getGetContentContext(),
220 1
                $this->getOffset(),
221 1
                $this->getMaxLen()
222
            );
223
        }
224 2
        return $this->filePutContents(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->filePutCon...etPutContentContext()); (integer) is incompatible with the return type declared by the interface FMUP\Ftp\FtpInterface::put of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
225 2
            'ssh2.sftp://' . intval($this->getSftpSession()) . '/' . $remoteFile,
226 2
            $fileContent,
227 2
            $this->getPutContentFlags(),
228 2
            $this->getPutContentContext()
229
        );
230
    }
231
232
    /**
233
     * @param resource $session
234
     * @param string $localFile
235
     * @param string $remoteFile
236
     * @param int $createMode
237
     * @return bool
238
     * @codeCoverageIgnore
239
     */
240
    protected function ssh2ScpSend($session, $localFile, $remoteFile, $createMode)
241
    {
242
        return ssh2_scp_send($session, $localFile, $remoteFile, $createMode);
243
    }
244
245
    /**
246
     * @return string
247
     * @codeCoverageIgnore
248
     */
249
    protected function fileGetContents()
250
    {
251
        return call_user_func_array('file_get_contents', func_get_args());
252
    }
253
254
    /**
255
     * @param string $fileName
256
     * @param mixed $data
257
     * @param int $flags
258
     * @param resource $context
259
     * @return int
260
     * @codeCoverageIgnore
261
     */
262
    protected function filePutContents($fileName, $data, $flags = 0, $context = null)
263
    {
264
        return file_put_contents($fileName, $data, $flags, $context);
265
    }
266
267
    /**
268
     * @param string $file
269
     * @return bool
270
     */
271 1
    public function delete($file)
272
    {
273 1
        return $this->ssh2SftpUnlink($this->getSftpSession(), $file);
274
    }
275
276
    /**
277
     * @param resource $sftpSession
278
     * @param string $path
279
     * @return bool
280
     * @codeCoverageIgnore
281
     */
282
    protected function ssh2SftpUnlink($sftpSession, $path)
283
    {
284
        return ssh2_sftp_unlink($sftpSession, $path);
285
    }
286
287
    /**
288
     *
289
     * @return bool
290
     */
291 1
    public function close()
292
    {
293 1
        return true;
294
    }
295
296
    /**
297
     * @param resource $session
298
     * @return resource
299
     * @codeCoverageIgnore
300
     */
301
    protected function ssh2Sftp($session)
302
    {
303
        return ssh2_sftp($session);
304
    }
305
}
306