Issues (201)

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/Asset/DiscoveryAssetManager.php (13 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/manager 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\Manager\Asset;
13
14
use Puli\Discovery\Binding\ResourceBinding;
15
use Puli\Manager\Api\Asset\AssetManager;
16
use Puli\Manager\Api\Asset\AssetMapping;
17
use Puli\Manager\Api\Asset\DuplicateAssetMappingException;
18
use Puli\Manager\Api\Asset\NoSuchAssetMappingException;
19
use Puli\Manager\Api\Discovery\BindingDescriptor;
20
use Puli\Manager\Api\Discovery\DiscoveryManager;
21
use Puli\Manager\Api\Event\AddAssetMappingEvent;
22
use Puli\Manager\Api\Event\PuliEvents;
23
use Puli\Manager\Api\Event\RemoveAssetMappingEvent;
24
use Puli\Manager\Api\Server\NoSuchServerException;
25
use Puli\Manager\Api\Server\ServerCollection;
26
use Puli\UrlGenerator\DiscoveryUrlGenerator;
27
use Rhumsaa\Uuid\Uuid;
28
use RuntimeException;
29
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
30
use Webmozart\Expression\Expr;
31
use Webmozart\Expression\Expression;
32
33
/**
34
 * An asset manager that uses a {@link DiscoveryManager} as storage backend.
35
 *
36
 * @since  1.0
37
 *
38
 * @author Bernhard Schussek <[email protected]>
39
 */
40
class DiscoveryAssetManager implements AssetManager
41
{
42
    /**
43
     * @var DiscoveryManager
44
     */
45
    private $discoveryManager;
46
47
    /**
48
     * @var ServerCollection
49
     */
50
    private $servers;
51
52
    /**
53
     * @var BindingExpressionBuilder
54
     */
55
    private $exprBuilder;
56
57
    /**
58
     * @var EventDispatcherInterface
59
     */
60
    private $dispatcher;
61
62 36
    public function __construct(DiscoveryManager $discoveryManager, ServerCollection $servers, EventDispatcherInterface $dispatcher = null)
63
    {
64 36
        $this->discoveryManager = $discoveryManager;
65 36
        $this->servers = $servers;
66 36
        $this->exprBuilder = new BindingExpressionBuilder();
67 36
        $this->dispatcher = $dispatcher;
68 36
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 7
    public function addRootAssetMapping(AssetMapping $mapping, $flags = 0)
74
    {
75 7
        if (!$this->discoveryManager->hasTypeDescriptor(DiscoveryUrlGenerator::BINDING_TYPE)) {
76 1
            throw new RuntimeException(sprintf(
77
                'The binding type "%s" was not found. Please install the '.
78
                '"puli/url-generator" module with Composer:'."\n\n".
79 1
                '    $ composer require puli/url-generator',
80 1
                DiscoveryUrlGenerator::BINDING_TYPE
81
            ));
82
        }
83
84 6
        if (!($flags & self::IGNORE_SERVER_NOT_FOUND) && !$this->servers->contains($mapping->getServerName())) {
85 1
            throw NoSuchServerException::forServerName($mapping->getServerName());
86
        }
87
88 5
        if (!($flags & self::OVERRIDE) && $this->hasAssetMapping($mapping->getUuid())) {
89 1
            throw DuplicateAssetMappingException::forUuid($mapping->getUuid());
90
        }
91
92 4
        $binding = new ResourceBinding(
93
            // Match directories as well as all of their contents
94 4
            $mapping->getGlob().'{,/**/*}',
95 4
            DiscoveryUrlGenerator::BINDING_TYPE,
96
            array(
97 4
                DiscoveryUrlGenerator::SERVER_PARAMETER => $mapping->getServerName(),
98 4
                DiscoveryUrlGenerator::PATH_PARAMETER => $mapping->getServerPath(),
99
            ),
100 4
            'glob',
101 4
            $mapping->getUuid()
102
        );
103
104 4
        $this->discoveryManager->addRootBindingDescriptor(
105 4
            new BindingDescriptor($binding),
106 4
            ($flags & self::OVERRIDE) ? DiscoveryManager::OVERRIDE : 0
107
        );
108
109 4
        if ($this->dispatcher && $this->dispatcher->hasListeners(PuliEvents::POST_ADD_ASSET_MAPPING)) {
110 1
            $this->dispatcher->dispatch(
111 1
                PuliEvents::POST_ADD_ASSET_MAPPING,
112 1
                new AddAssetMappingEvent($mapping)
113
            );
114
        }
115 4
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 2
    public function removeRootAssetMapping(Uuid $uuid)
121
    {
122 2
        $mapping = null;
123 2
        $hasListener = $this->dispatcher && $this->dispatcher->hasListeners(PuliEvents::POST_REMOVE_ASSET_MAPPING);
124 2
        $expr = Expr::method('getUuid', Expr::method('toString', Expr::same($uuid->toString())))
125 2
            ->andX($this->exprBuilder->buildExpression());
126
127 2
        if ($hasListener) {
128
            // Query the mapping for the event
129
            try {
130 1
                $mapping = $this->getRootAssetMapping($uuid);
131
            } catch (NoSuchAssetMappingException $e) {
132
                return;
133
            }
134
        }
135
136 2
        $this->discoveryManager->removeRootBindingDescriptors($expr);
137
138 2
        if ($hasListener) {
139 1
            $this->dispatcher->dispatch(
140 1
                PuliEvents::POST_REMOVE_ASSET_MAPPING,
141 1
                new RemoveAssetMappingEvent($mapping)
0 ignored issues
show
It seems like $mapping defined by null on line 122 can be null; however, Puli\Manager\Api\Event\R...ingEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
142
            );
143
        }
144 2
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 2 View Code Duplication
    public function removeRootAssetMappings(Expression $expr)
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...
150
    {
151 2
        $mappings = array();
152 2
        $hasListener = $this->dispatcher && $this->dispatcher->hasListeners(PuliEvents::POST_REMOVE_ASSET_MAPPING);
153
154 2
        if ($hasListener) {
155
            // Query the mappings for the event
156 1
            $mappings = $this->findRootAssetMappings($expr);
157
        }
158
159 2
        $this->discoveryManager->removeRootBindingDescriptors($this->exprBuilder->buildExpression($expr));
160
161 2
        if ($hasListener) {
162 1
            foreach ($mappings as $mapping) {
163 1
                $this->dispatcher->dispatch(
164 1
                    PuliEvents::POST_REMOVE_ASSET_MAPPING,
165 1
                    new RemoveAssetMappingEvent($mapping)
166
                );
167
            }
168
        }
169 2
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174 2 View Code Duplication
    public function clearRootAssetMappings()
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...
175
    {
176 2
        $mappings = array();
177 2
        $hasListener = $this->dispatcher && $this->dispatcher->hasListeners(PuliEvents::POST_REMOVE_ASSET_MAPPING);
178
179 2
        if ($hasListener) {
180
            // Query the mappings for the event
181 1
            $mappings = $this->getRootAssetMappings();
182
        }
183
184 2
        $this->discoveryManager->removeRootBindingDescriptors($this->exprBuilder->buildExpression());
185
186 2
        if ($hasListener) {
187 1
            foreach ($mappings as $mapping) {
188 1
                $this->dispatcher->dispatch(
189 1
                    PuliEvents::POST_REMOVE_ASSET_MAPPING,
190 1
                    new RemoveAssetMappingEvent($mapping)
191
                );
192
            }
193
        }
194 2
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 3 View Code Duplication
    public function getRootAssetMapping(Uuid $uuid)
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...
200
    {
201 3
        $mappings = $this->findRootAssetMappings(Expr::method('getUuid', Expr::method('toString', Expr::same($uuid->toString()))));
202
203 3
        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...
204 1
            throw NoSuchAssetMappingException::forUuid($uuid);
205
        }
206
207 2
        return reset($mappings);
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213 3
    public function getRootAssetMappings()
214
    {
215 3
        return $this->findRootAssetMappings(Expr::true());
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221 2 View Code Duplication
    public function hasRootAssetMapping(Uuid $uuid)
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...
222
    {
223 2
        $expr = Expr::method('getUuid', Expr::method('toString', Expr::same($uuid->toString())))
224 2
            ->andX($this->exprBuilder->buildExpression());
225
226 2
        return $this->discoveryManager->hasRootBindingDescriptors($expr);
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232 3
    public function hasRootAssetMappings(Expression $expr = null)
233
    {
234 3
        return $this->discoveryManager->hasRootBindingDescriptors($this->exprBuilder->buildExpression($expr));
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240 9 View Code Duplication
    public function findRootAssetMappings(Expression $expr)
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...
241
    {
242 9
        $descriptors = $this->discoveryManager->findRootBindingDescriptors($this->exprBuilder->buildExpression($expr));
243 9
        $mappings = array();
244
245 9
        foreach ($descriptors as $descriptor) {
246 6
            $mappings[] = $this->bindingToMapping($descriptor->getBinding());
0 ignored issues
show
$descriptor->getBinding() of type object<Puli\Discovery\Api\Binding\Binding> is not a sub-type of object<Puli\Discovery\Binding\ResourceBinding>. It seems like you assume a concrete implementation of the interface Puli\Discovery\Api\Binding\Binding to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
247
        }
248
249 9
        return $mappings;
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255 2 View Code Duplication
    public function getAssetMapping(Uuid $uuid)
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...
256
    {
257 2
        $mappings = $this->findAssetMappings(Expr::method('getUuid', Expr::method('toString', Expr::same($uuid->toString()))));
258
259 2
        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...
260 1
            throw NoSuchAssetMappingException::forUuid($uuid);
261
        }
262
263 1
        return reset($mappings);
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269 2
    public function getAssetMappings()
270
    {
271 2
        return $this->findAssetMappings(Expr::true());
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277 6 View Code Duplication
    public function findAssetMappings(Expression $expr)
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...
278
    {
279 6
        $descriptors = $this->discoveryManager->findBindingDescriptors($this->exprBuilder->buildExpression($expr));
280 6
        $mappings = array();
281
282 6
        foreach ($descriptors as $descriptor) {
283 3
            $mappings[] = $this->bindingToMapping($descriptor->getBinding());
0 ignored issues
show
$descriptor->getBinding() of type object<Puli\Discovery\Api\Binding\Binding> is not a sub-type of object<Puli\Discovery\Binding\ResourceBinding>. It seems like you assume a concrete implementation of the interface Puli\Discovery\Api\Binding\Binding to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
284
        }
285
286 6
        return $mappings;
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292 6 View Code Duplication
    public function hasAssetMapping(Uuid $uuid)
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...
293
    {
294 6
        $expr = Expr::method('getUuid', Expr::method('toString', Expr::same($uuid->toString())))
295 6
            ->andX($this->exprBuilder->buildExpression());
296
297 6
        return $this->discoveryManager->hasBindingDescriptors($expr);
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303 3
    public function hasAssetMappings(Expression $expr = null)
304
    {
305 3
        return $this->discoveryManager->hasBindingDescriptors($this->exprBuilder->buildExpression($expr));
306
    }
307
308 9
    private function bindingToMapping(ResourceBinding $binding)
309
    {
310 9
        return new AssetMapping(
311
            // Remove "{,/**/*}" suffix
312 9
            substr($binding->getQuery(), 0, -8),
313 9
            $binding->getParameterValue(DiscoveryUrlGenerator::SERVER_PARAMETER),
314 9
            $binding->getParameterValue(DiscoveryUrlGenerator::PATH_PARAMETER),
315 9
            $binding->getUuid()
316
        );
317
    }
318
}
319