Issues (135)

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/SourceLocator/Type/ClosureSourceLocator.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 Roave\BetterReflection\SourceLocator\Type;
6
7
use Closure;
8
use PhpParser\Node;
9
use PhpParser\Node\Stmt\Namespace_;
10
use PhpParser\NodeTraverser;
11
use PhpParser\NodeVisitorAbstract;
12
use PhpParser\Parser;
13
use ReflectionFunction as CoreFunctionReflection;
14
use Roave\BetterReflection\Identifier\Identifier;
15
use Roave\BetterReflection\Identifier\IdentifierType;
16
use Roave\BetterReflection\Reflection\Reflection;
17
use Roave\BetterReflection\Reflection\ReflectionFunction;
18
use Roave\BetterReflection\Reflector\Reflector;
19
use Roave\BetterReflection\SourceLocator\Ast\Exception\ParseToAstFailure;
20
use Roave\BetterReflection\SourceLocator\Ast\Strategy\NodeToReflection;
21
use Roave\BetterReflection\SourceLocator\Exception\EvaledClosureCannotBeLocated;
22
use Roave\BetterReflection\SourceLocator\Exception\TwoClosuresOnSameLine;
23
use Roave\BetterReflection\SourceLocator\FileChecker;
24
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;
25
use Roave\BetterReflection\Util\FileHelper;
26
use function array_filter;
27
use function array_values;
28
use function assert;
29
use function file_get_contents;
30
use function is_array;
31
use function strpos;
32
33
/**
34
 * @internal
35
 */
36
final class ClosureSourceLocator implements SourceLocator
37
{
38
    /** @var CoreFunctionReflection */
39
    private $coreFunctionReflection;
40
41
    /** @var Parser */
42
    private $parser;
43
44
    public function __construct(Closure $closure, Parser $parser)
45
    {
46
        $this->coreFunctionReflection = new CoreFunctionReflection($closure);
47
        $this->parser                 = $parser;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     *
53
     * @throws ParseToAstFailure
54
     */
55
    public function locateIdentifier(Reflector $reflector, Identifier $identifier) : ?Reflection
56
    {
57
        return $this->getReflectionFunction($reflector, $identifier->getType());
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     *
63
     * @throws ParseToAstFailure
64
     */
65
    public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType) : array
66
    {
67
        return array_filter([$this->getReflectionFunction($reflector, $identifierType)]);
68
    }
69
70 6
    private function getReflectionFunction(Reflector $reflector, IdentifierType $identifierType) : ?ReflectionFunction
71
    {
72
        if (! $identifierType->isFunction()) {
73
            return null;
74
        }
75
76
        $fileName = $this->coreFunctionReflection->getFileName();
77
78
        if (strpos($fileName, 'eval()\'d code') !== false) {
79
            throw EvaledClosureCannotBeLocated::create();
80
        }
81
82
        FileChecker::assertReadableFile($fileName);
83
84
        $fileName = FileHelper::normalizeWindowsPath($fileName);
85
86
        $nodeVisitor = new class($fileName, $this->coreFunctionReflection->getStartLine()) extends NodeVisitorAbstract
87
        {
88
            /** @var string */
89
            private $fileName;
90
91
            /** @var int */
92
            private $startLine;
93
94
            /** @var (Node|null)[][] */
95
            private $closureNodes = [];
96
97
            /** @var Namespace_|null */
98
            private $currentNamespace;
99
100
            public function __construct(string $fileName, int $startLine)
101
            {
102 6
                $this->fileName  = $fileName;
103 6
                $this->startLine = $startLine;
104 6
            }
105
106
            /**
107
             * {@inheritDoc}
108
             */
109 6
            public function enterNode(Node $node)
110
            {
111 6
                if ($node instanceof Namespace_) {
112 2
                    $this->currentNamespace = $node;
113
114 2
                    return null;
115
                }
116
117 6
                if (! ($node instanceof Node\Expr\Closure)) {
118 6
                    return null;
119
                }
120
121 6
                $this->closureNodes[] = [$node, $this->currentNamespace];
122
123 6
                return null;
124
            }
125
126
            /**
127
             * {@inheritDoc}
128
             */
129 6
            public function leaveNode(Node $node)
130
            {
131 6
                if (! ($node instanceof Namespace_)) {
132 6
                    return null;
133
                }
134
135 2
                $this->currentNamespace = null;
136
137 2
                return null;
138
            }
139
140
            /**
141
             * @return Node[]|null[]|null
142
             *
143
             * @throws TwoClosuresOnSameLine
144
             */
145 6
            public function getClosureNodes() : ?array
146
            {
147
                /** @var (Node|null)[][] $closureNodesDataOnSameLine */
148
                $closureNodesDataOnSameLine = array_values(array_filter($this->closureNodes, function (array $nodes) : bool {
149 6
                    return $nodes[0]->getLine() === $this->startLine;
150 6
                }));
151
152 6
                if (! $closureNodesDataOnSameLine) {
0 ignored issues
show
Bug Best Practice introduced by Jaroslav Hanslík
The expression $closureNodesDataOnSameLine of type array<PhpParser\Node|null>[] 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...
153
                    return null;
154
                }
155
156 6
                if (isset($closureNodesDataOnSameLine[1])) {
157 2
                    throw TwoClosuresOnSameLine::create($this->fileName, $this->startLine);
158
                }
159
160 4
                return $closureNodesDataOnSameLine[0];
161
            }
162
        };
163
164
        $fileContents = file_get_contents($fileName);
165
        $ast          = $this->parser->parse($fileContents);
166
167
        $nodeTraverser = new NodeTraverser();
168
        $nodeTraverser->addVisitor($nodeVisitor);
169
        $nodeTraverser->traverse($ast);
0 ignored issues
show
It seems like $ast defined by $this->parser->parse($fileContents) on line 165 can also be of type null; however, PhpParser\NodeTraverser::traverse() does only seem to accept array<integer,object<PhpParser\Node>>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
170
171
        $closureNodes = $nodeVisitor->getClosureNodes();
172
        assert(is_array($closureNodes));
173
        assert($closureNodes[1] instanceof Namespace_ || $closureNodes[1] === null);
174
175
        $reflectionFunction = (new NodeToReflection())->__invoke(
176
            $reflector,
177
            $closureNodes[0],
178
            new LocatedSource($fileContents, $fileName),
179
            $closureNodes[1]
180
        );
181
        assert($reflectionFunction instanceof ReflectionFunction || $reflectionFunction === null);
182
183
        return $reflectionFunction;
184
    }
185
}
186