Issues (62)

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/Handler/PublishCommandHandler.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
/*
4
 * This file is part of the puli/asset-plugin package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Cli\Handler;
13
14
use Puli\Manager\Api\Asset\AssetManager;
15
use Puli\Manager\Api\Asset\AssetMapping;
16
use Puli\Manager\Api\Installation\InstallationManager;
17
use Puli\Manager\Api\Installation\InstallationParams;
18
use Puli\Manager\Api\Server\Server;
19
use Puli\Manager\Api\Server\ServerManager;
20
use RuntimeException;
21
use Webmozart\Console\Api\Args\Args;
22
use Webmozart\Console\Api\IO\IO;
23
use Webmozart\Console\UI\Component\Table;
24
use Webmozart\Console\UI\Style\TableStyle;
25
use Webmozart\Expression\Expr;
26
use Webmozart\PathUtil\Path;
27
28
/**
29
 * @since  1.0
30
 *
31
 * @author Bernhard Schussek <[email protected]>
32
 */
33
class PublishCommandHandler
34
{
35
    /**
36
     * @var AssetManager
37
     */
38
    private $assetManager;
39
40
    /**
41
     * @var InstallationManager
42
     */
43
    private $installationManager;
44
45
    /**
46
     * @var ServerManager
47
     */
48
    private $serverManager;
49
50
    /**
51
     * @var string
52
     */
53
    private $currentPath = '/';
54
55 21
    public function __construct(AssetManager $assetManager, InstallationManager $installationManager, ServerManager $serveRManager)
56
    {
57 21
        $this->assetManager = $assetManager;
58 21
        $this->installationManager = $installationManager;
59 21
        $this->serverManager = $serveRManager;
60 21
    }
61
62 4
    public function handleList(Args $args, IO $io)
0 ignored issues
show
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        /** @var AssetMapping[][] $mappingsByServer */
65 4
        $mappingsByServer = array();
66
67
        /** @var Server[] $servers */
68 4
        $servers = array();
69 4
        $nonExistingServers = array();
70
71
        // Assemble mappings and validate servers
72 4
        foreach ($this->assetManager->getAssetMappings() as $mapping) {
73 3
            $serverName = $mapping->getServerName();
74
75 3
            if (!isset($mappingsByServer[$serverName])) {
76 3
                $mappingsByServer[$serverName] = array();
77
78 3
                if ($this->serverManager->hasServer($serverName)) {
79 2
                    $servers[$serverName] = $this->serverManager->getServer($serverName);
80
                } else {
81 2
                    $nonExistingServers[$serverName] = true;
82
                }
83
            }
84
85 3
            $mappingsByServer[$serverName][] = $mapping;
86
        }
87
88 4
        if (empty($mappingsByServer)) {
89 1
            $io->writeLine('No public resources. Use "puli publish <path> <server>" to publish resources.');
90
91 1
            return 0;
92
        }
93
94 3
        if (count($servers) > 0) {
95 2
            $io->writeLine('The following resources are published:');
96 2
            $io->writeLine('');
97
98 2
            foreach ($servers as $serverName => $server) {
99 2
                $serverTitle = 'Server <bu>'.$serverName.'</bu>';
100
101 2
                $io->writeLine(sprintf('    <b>%s</b>', $serverTitle));
102 2
                $io->writeLine(sprintf('    Location:   <c2>%s</c2>', $server->getDocumentRoot()));
103 2
                $io->writeLine(sprintf('    Installer:  %s', $server->getInstallerName()));
104 2
                $io->writeLine(sprintf('    URL Format: <c1>%s</c1>', $server->getUrlFormat()));
105 2
                $io->writeLine('');
106
107 2
                $this->printMappingTable($io, $mappingsByServer[$serverName]);
108 2
                $io->writeLine('');
109
            }
110
111 2
            $io->writeLine('Use "puli publish --install" to install the resources on your servers.');
112
        }
113
114 3
        if (count($servers) > 0 && count($nonExistingServers) > 0) {
115 1
            $io->writeLine('');
116
        }
117
118 3
        if (count($nonExistingServers) > 0) {
119 2
            $io->writeLine('The following public resources are disabled since their server does not exist:');
120 2
            $io->writeLine('');
121
122 2
            foreach ($nonExistingServers as $serverName => $_) {
123 2
                $io->writeLine(sprintf('    <b>Server <bu>%s</bu></b>', $serverName));
124 2
                $io->writeLine('');
125
126 2
                $this->printMappingTable($io, $mappingsByServer[$serverName], false);
127 2
                $io->writeLine('');
128
            }
129
130 2
            $io->writeLine('Use "puli server add <name> <document-root>" to add a server.');
131
        }
132
133 3
        return 0;
134
    }
135
136 5 View Code Duplication
    public function handleAdd(Args $args)
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...
137
    {
138 5
        $flags = $args->isOptionSet('force') ? AssetManager::IGNORE_SERVER_NOT_FOUND : 0;
139 5
        $path = Path::makeAbsolute($args->getArgument('path'), $this->currentPath);
140
141 5
        $this->assetManager->addRootAssetMapping(new AssetMapping(
142
            $path,
143 5
            $args->getArgument('server'),
144 5
            $args->getArgument('server-path')
145
        ), $flags);
146
147 5
        return 0;
148
    }
149
150 5
    public function handleUpdate(Args $args)
151
    {
152 5
        $flags = $args->isOptionSet('force')
153 1
            ? AssetManager::OVERRIDE | AssetManager::IGNORE_SERVER_NOT_FOUND
154 5
            : AssetManager::OVERRIDE;
155 5
        $mappingToUpdate = $this->getMappingByUuidPrefix($args->getArgument('uuid'));
156 5
        $path = $mappingToUpdate->getGlob();
157 5
        $serverPath = $mappingToUpdate->getServerPath();
158 5
        $serverName = $mappingToUpdate->getServerName();
159
160 5
        if ($args->isOptionSet('path')) {
161 3
            $path = Path::makeAbsolute($args->getOption('path'), $this->currentPath);
162
        }
163
164 5
        if ($args->isOptionSet('server-path')) {
165 2
            $serverPath = $args->getOption('server-path');
166
        }
167
168 5
        if ($args->isOptionSet('server')) {
169 1
            $serverName = $args->getOption('server');
170
        }
171
172 5
        $updatedMapping = new AssetMapping($path, $serverName, $serverPath, $mappingToUpdate->getUuid());
173
174 5
        if ($this->mappingsEqual($mappingToUpdate, $updatedMapping)) {
175 1
            throw new RuntimeException('Nothing to update.');
176
        }
177
178 4
        $this->assetManager->addRootAssetMapping($updatedMapping, $flags);
179
180 4
        return 0;
181
    }
182
183 3
    public function handleDelete(Args $args)
184
    {
185 3
        $mapping = $this->getMappingByUuidPrefix($args->getArgument('uuid'));
186
187 1
        $this->assetManager->removeRootAssetMapping($mapping->getUuid());
188
189 1
        return 0;
190
    }
191
192 4
    public function handleInstall(Args $args, IO $io)
193
    {
194 4
        if ($args->isArgumentSet('server')) {
195 1
            $expr = Expr::method('getServerName', Expr::same($args->getArgument('server')));
196 1
            $mappings = $this->assetManager->findAssetMappings($expr);
197
        } else {
198 3
            $mappings = $this->assetManager->getAssetMappings();
199
        }
200
201 4
        if (!$mappings) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mappings of type Puli\Manager\Api\Asset\AssetMapping[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
202 1
            $io->writeLine('Nothing to install.');
203
204 1
            return 0;
205
        }
206
207
        /** @var InstallationParams[] $paramsToInstall */
208 3
        $paramsToInstall = array();
209
210
        // Prepare and validate the installation of all matching mappings
211 3
        foreach ($mappings as $mapping) {
212 3
            $paramsToInstall[] = $this->installationManager->prepareInstallation($mapping);
213
        }
214
215 2
        foreach ($paramsToInstall as $params) {
216 2
            foreach ($params->getResources() as $resource) {
217 2
                $serverPath = rtrim($params->getDocumentRoot(), '/').$params->getServerPathForResource($resource);
218
219 2
                $io->writeLine(sprintf(
220 2
                    'Installing <c1>%s</c1> into <c2>%s</c2> via <u>%s</u>...',
221 2
                    $resource->getRepositoryPath(),
222 2
                    trim($serverPath, '/'),
223 2
                    $params->getInstallerDescriptor()->getName()
224
                ));
225
226 2
                $this->installationManager->installResource($resource, $params);
227
            }
228
        }
229
230 2
        return 0;
231
    }
232
233
    /**
234
     * @param IO             $io
235
     * @param AssetMapping[] $mappings
236
     * @param bool           $enabled
237
     */
238 3
    private function printMappingTable(IO $io, array $mappings, $enabled = true)
239
    {
240 3
        $table = new Table(TableStyle::borderless());
241
242 3
        $globTag = $enabled ? 'c1' : 'bad';
243 3
        $pathTag = $enabled ? 'c2' : 'bad';
244
245 3
        foreach ($mappings as $mapping) {
246 3
            $uuid = substr($mapping->getUuid()->toString(), 0, 6);
247 3
            $glob = $mapping->getGlob();
248 3
            $serverPath = $mapping->getServerPath();
249
250 3
            if (!$enabled) {
251 2
                $uuid = sprintf('<bad>%s</bad>', $uuid);
252
            }
253
254 3
            $table->addRow(array(
255 3
                $uuid,
256 3
                sprintf('<%s>%s</%s>', $globTag, $glob, $globTag),
257 3
                sprintf('<%s>%s</%s>', $pathTag, $serverPath, $pathTag),
258
            ));
259
        }
260
261 3
        $table->render($io, 8);
262 3
    }
263
264
    /**
265
     * @param string $uuidPrefix
266
     *
267
     * @return AssetMapping
268
     */
269 8 View Code Duplication
    private function getMappingByUuidPrefix($uuidPrefix)
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...
270
    {
271 8
        $expr = Expr::method('getUuid', Expr::startsWith($uuidPrefix));
272
273 8
        $mappings = $this->assetManager->findAssetMappings($expr);
274
275 8
        if (!$mappings) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mappings of type Puli\Manager\Api\Asset\AssetMapping[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
276 1
            throw new RuntimeException(sprintf(
277 1
                'The mapping with the UUID prefix "%s" does not exist.',
278
                $uuidPrefix
279
            ));
280
        }
281
282 7
        if (count($mappings) > 1) {
283 1
            throw new RuntimeException(sprintf(
284 1
                'More than one mapping matches the UUID prefix "%s".',
285
                $uuidPrefix
286
            ));
287
        }
288
289 6
        return reset($mappings);
290
    }
291
292 5
    private function mappingsEqual(AssetMapping $mapping1, AssetMapping $mapping2)
293
    {
294 5
        return $mapping1->getUuid() === $mapping2->getUuid() &&
295 5
            $mapping1->getGlob() === $mapping2->getGlob() &&
296 5
            $mapping1->getServerPath() === $mapping2->getServerPath() &&
297 5
            $mapping1->getServerName() === $mapping2->getServerName();
298
    }
299
}
300