Issues (4)

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/GitWrapper.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
declare(strict_types=1);
4
5
namespace GitWrapper;
6
7
use GitWrapper\Event\GitOutputEvent;
8
use GitWrapper\EventSubscriber\AbstractOutputEventSubscriber;
9
use GitWrapper\EventSubscriber\GitLoggerEventSubscriber;
10
use GitWrapper\EventSubscriber\StreamOutputEventSubscriber;
11
use GitWrapper\Exception\GitException;
12
use GitWrapper\Process\GitProcess;
13
use GitWrapper\Strings\GitStrings;
14
use Symfony\Component\EventDispatcher\EventDispatcher;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Symfony\Component\Process\ExecutableFinder;
17
18
/**
19
 * A wrapper class around the Git binary.
20
 *
21
 * A GitWrapper object contains the necessary context to run Git commands such
22
 * as the path to the Git binary and environment variables. It also provides
23
 * helper methods to run Git commands as set up the connection to the GIT_SSH
24
 * wrapper script.
25
 */
26
final class GitWrapper
27
{
28
    /**
29
     * Path to the Git binary.
30
     *
31
     * @var string
32
     */
33
    private $gitBinary;
34
35
    /**
36
     * The timeout of the Git command in seconds.
37
     *
38
     * @var int
39
     */
40
    private $timeout = 60;
41
42
    /**
43
     * Environment variables defined in the scope of the Git command.
44
     *
45
     * @var string[]
46
     */
47
    private $env = [];
48
49
    /**
50
     * @var AbstractOutputEventSubscriber
51
     */
52
    private $outputEventSubscriber;
53
54
    /**
55
     * @var EventDispatcherInterface
56
     */
57
    private $eventDispatcher;
58
59
    public function __construct(?string $gitBinary = null)
60
    {
61
        if ($gitBinary === null) {
62
            $finder = new ExecutableFinder();
63
            $gitBinary = $finder->find('git');
64
            if (! $gitBinary) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $gitBinary of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
65
                throw new GitException('Unable to find the Git executable.');
66
            }
67
        }
68
69
        $this->setGitBinary($gitBinary);
70
71
        $this->eventDispatcher = new EventDispatcher();
72
    }
73
74
    public function getDispatcher(): EventDispatcherInterface
75
    {
76
        return $this->eventDispatcher;
77
    }
78
79
    public function setDispatcher(EventDispatcherInterface $eventDispatcher): void
80
    {
81
        $this->eventDispatcher = $eventDispatcher;
82
    }
83
84
    public function setGitBinary(string $gitBinary): void
85
    {
86
        $this->gitBinary = $gitBinary;
87
    }
88
89
    public function getGitBinary(): string
90
    {
91
        return $this->gitBinary;
92
    }
93
94
    public function setEnvVar(string $var, $value): void
95
    {
96
        $this->env[$var] = $value;
97
    }
98
99
    public function unsetEnvVar(string $var): void
100
    {
101
        unset($this->env[$var]);
102
    }
103
104
    /**
105
     * Returns an environment variable that is defined only in the scope of the
106
     * Git command.
107
     *
108
     * @param string $var The name of the environment variable, e.g. "HOME", "GIT_SSH".
109
     * @param mixed $default The value returned if the environment variable is not set, defaults to
110
     *   null.
111
     */
112
    public function getEnvVar(string $var, $default = null)
113
    {
114
        return $this->env[$var] ?? $default;
115
    }
116
117
    /**
118
     * @return string[]
119
     */
120
    public function getEnvVars(): array
121
    {
122
        return $this->env;
123
    }
124
125
    public function setTimeout(int $timeout): void
126
    {
127
        $this->timeout = $timeout;
128
    }
129
130
    public function getTimeout(): int
131
    {
132
        return $this->timeout;
133
    }
134
135
    /**
136
     * Set an alternate private key used to connect to the repository.
137
     *
138
     * This method sets the GIT_SSH environment variable to use the wrapper
139
     * script included with this library. It also sets the custom GIT_SSH_KEY
140
     * and GIT_SSH_PORT environment variables that are used by the script.
141
     *
142
     * @param string|null $wrapper Path the the GIT_SSH wrapper script, defaults to null which uses the
143
     *   script included with this library.
144
     */
145
    public function setPrivateKey(string $privateKey, int $port = 22, ?string $wrapper = null): void
146
    {
147
        if ($wrapper === null) {
148
            $wrapper = __DIR__ . '/../bin/git-ssh-wrapper.sh';
149
        }
150
151
        if (! $wrapperPath = realpath($wrapper)) {
152
            throw new GitException('Path to GIT_SSH wrapper script could not be resolved: ' . $wrapper);
153
        }
154
155
        if (! $privateKeyPath = realpath($privateKey)) {
156
            throw new GitException('Path private key could not be resolved: ' . $privateKey);
157
        }
158
159
        $this->setEnvVar('GIT_SSH', $wrapperPath);
160
        $this->setEnvVar('GIT_SSH_KEY', $privateKeyPath);
161
        $this->setEnvVar('GIT_SSH_PORT', $port);
162
    }
163
164
    /**
165
     * Unsets the private key by removing the appropriate environment variables.
166
     */
167
    public function unsetPrivateKey(): void
168
    {
169
        $this->unsetEnvVar('GIT_SSH');
170
        $this->unsetEnvVar('GIT_SSH_KEY');
171
        $this->unsetEnvVar('GIT_SSH_PORT');
172
    }
173
174
    public function addOutputEventSubscriber(AbstractOutputEventSubscriber $gitOutputEventSubscriber): void
175
    {
176
        $this->getDispatcher()->addSubscriber($gitOutputEventSubscriber);
177
    }
178
179
    public function addLoggerEventSubscriber(GitLoggerEventSubscriber $gitLoggerEventSubscriber): void
180
    {
181
        $this->getDispatcher()->addSubscriber($gitLoggerEventSubscriber);
182
    }
183
184
    public function removeOutputEventSubscriber(AbstractOutputEventSubscriber $gitOutputEventSubscriber): void
185
    {
186
        $this->getDispatcher()->removeSubscriber($gitOutputEventSubscriber);
187
    }
188
189
    /**
190
     * Set whether or not to stream real-time output to STDOUT and STDERR.
191
     */
192
    public function streamOutput(bool $streamOutput = true): void
193
    {
194
        if ($streamOutput && ! isset($this->outputEventSubscriber)) {
195
            $this->outputEventSubscriber = new StreamOutputEventSubscriber();
196
            $this->addOutputEventSubscriber($this->outputEventSubscriber);
197
        }
198
199
        if (! $streamOutput && isset($this->outputEventSubscriber)) {
200
            $this->removeOutputEventSubscriber($this->outputEventSubscriber);
201
            unset($this->outputEventSubscriber);
202
        }
203
    }
204
205
    /**
206
     * Returns an object that interacts with a working copy.
207
     *
208
     * @param string $directory Path to the directory containing the working copy.
209
     */
210
    public function workingCopy(string $directory): GitWorkingCopy
211
    {
212
        return new GitWorkingCopy($this, $directory);
213
    }
214
215
    /**
216
     * Returns the version of the installed Git client.
217
     */
218
    public function version(): string
219
    {
220
        return $this->git('--version');
221
    }
222
223
    /**
224
     * Executes a `git init` command.
225
     *
226
     * Create an empty git repository or reinitialize an existing one.
227
     *
228
     * @param mixed[] $options An associative array of command line options.
229
     */
230
    public function init(string $directory, array $options = []): GitWorkingCopy
231
    {
232
        $git = $this->workingCopy($directory);
233
        $git->init($options);
234
        $git->setCloned(true);
235
236
        return $git;
237
    }
238
239
    /**
240
     * Executes a `git clone` command and returns a working copy object.
241
     *
242
     * Clone a repository into a new directory. Use @see GitWorkingCopy::cloneRepository()
243
     * instead for more readable code.
244
     *
245
     * @param string $directory The directory that the repository will be cloned into. If null is
246
     *   passed, the directory will be generated from the URL with @see GitStrings::parseRepositoryName().
247
     * @param mixed[] $options
248
     */
249
    public function cloneRepository(string $repository, ?string $directory = null, array $options = []): GitWorkingCopy
250
    {
251
        if ($directory === null) {
252
            $directory = GitStrings::parseRepositoryName($repository);
253
        }
254
255
        $git = $this->workingCopy($directory);
256
        $git->cloneRepository($repository, $options);
257
        $git->setCloned(true);
258
        return $git;
259
    }
260
261
    /**
262
     * The command is simply a raw command line entry for everything after the Git binary.
263
     * For example, a `git config -l` command would be passed as `config -l` via the first argument of this method.
264
     *
265
     * @return string The STDOUT returned by the Git command.
266
     */
267
    public function git(string $commandLine, ?string $cwd = null): string
268
    {
269
        $command = new GitCommand($commandLine);
270
        $command->executeRaw(is_string($commandLine));
271
        $command->setDirectory($cwd);
272
        return $this->run($command);
273
    }
274
275
    /**
276
     * @return string The STDOUT returned by the Git command.
277
     */
278
    public function run(GitCommand $gitCommand, ?string $cwd = null): string
279
    {
280
        $process = new GitProcess($this, $gitCommand, $cwd);
281
        $process->run(function ($type, $buffer) use ($process, $gitCommand): void {
282
            $event = new GitOutputEvent($this, $process, $gitCommand, $type, $buffer);
283
            $this->getDispatcher()->dispatch($event);
0 ignored issues
show
$event is of type object<GitWrapper\Event\GitOutputEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
284
        });
285
286
        return $gitCommand->notBypassed() ? $process->getOutput() : '';
287
    }
288
}
289