Issues (138)

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/Util/FindReflectionOnLine.php (2 issues)

Labels
Severity

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\Util;
6
7
use InvalidArgumentException;
8
use Roave\BetterReflection\Identifier\IdentifierType;
9
use Roave\BetterReflection\Reflection\Reflection;
10
use Roave\BetterReflection\Reflection\ReflectionClass;
11
use Roave\BetterReflection\Reflection\ReflectionConstant;
12
use Roave\BetterReflection\Reflection\ReflectionFunction;
13
use Roave\BetterReflection\Reflection\ReflectionMethod;
14
use Roave\BetterReflection\Reflector\ClassReflector;
15
use Roave\BetterReflection\SourceLocator\Ast\Exception\ParseToAstFailure;
16
use Roave\BetterReflection\SourceLocator\Ast\Locator;
17
use Roave\BetterReflection\SourceLocator\Exception\InvalidFileLocation;
18
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
19
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
20
use Roave\BetterReflection\SourceLocator\Type\SourceLocator;
21
use function array_merge;
22
use function method_exists;
23
24
final class FindReflectionOnLine
25
{
26
    /** @var SourceLocator */
27
    private $sourceLocator;
28
29
    /** @var Locator */
30
    private $astLocator;
31
32 1
    public function __construct(SourceLocator $sourceLocator, Locator $astLocator)
33
    {
34 1
        $this->sourceLocator = $sourceLocator;
35 1
        $this->astLocator    = $astLocator;
36 1
    }
37
38
    /**
39
     * Find a reflection on the specified line number.
40
     *
41
     * Returns null if no reflections found on the line.
42
     *
43
     * @return ReflectionMethod|ReflectionClass|ReflectionFunction|ReflectionConstant|Reflection|null
44
     *
45
     * @throws InvalidFileLocation
46
     * @throws ParseToAstFailure
47
     * @throws InvalidArgumentException
48
     */
49 9
    public function __invoke(string $filename, int $lineNumber)
50
    {
51 9
        $reflections = $this->computeReflections($filename);
52
53 9
        foreach ($reflections as $reflection) {
54 9
            if ($reflection instanceof ReflectionClass && $this->containsLine($reflection, $lineNumber)) {
55 5
                foreach ($reflection->getMethods() as $method) {
56 2
                    if ($this->containsLine($method, $lineNumber)) {
57 1
                        return $method;
58
                    }
59
                }
60
61 4
                return $reflection;
62
            }
63
64 7
            if ($reflection instanceof ReflectionFunction && $this->containsLine($reflection, $lineNumber)) {
65 1
                return $reflection;
66
            }
67
68 7
            if ($reflection instanceof ReflectionConstant && $this->containsLine($reflection, $lineNumber)) {
69 2
                return $reflection;
70
            }
71
        }
72
73 1
        return null;
74
    }
75
76
    /**
77
     * Find all class and function reflections in the specified file
78
     *
79
     * @return Reflection[]
80
     *
81
     * @throws ParseToAstFailure
82
     * @throws InvalidFileLocation
83
     */
84 9
    private function computeReflections(string $filename) : array
85
    {
86 9
        $singleFileSourceLocator = new SingleFileSourceLocator($filename, $this->astLocator);
87 9
        $reflector               = new ClassReflector(new AggregateSourceLocator([$singleFileSourceLocator, $this->sourceLocator]));
88
89 9
        return array_merge(
90 9
            $singleFileSourceLocator->locateIdentifiersByType($reflector, new IdentifierType(IdentifierType::IDENTIFIER_CLASS)),
91 9
            $singleFileSourceLocator->locateIdentifiersByType($reflector, new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION)),
92 9
            $singleFileSourceLocator->locateIdentifiersByType($reflector, new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT))
93
        );
94
    }
95
96
    /**
97
     * Check to see if the line is within the boundaries of the reflection specified.
98
     *
99
     * @param ReflectionMethod|ReflectionClass|ReflectionFunction|Reflection $reflection
100
     *
101
     * @throws InvalidArgumentException
102
     */
103 9
    private function containsLine($reflection, int $lineNumber) : bool
104
    {
105 9
        if (! method_exists($reflection, 'getStartLine')) {
106
            throw new InvalidArgumentException('Reflection does not have getStartLine method');
107
        }
108
109 9
        if (! method_exists($reflection, 'getEndLine')) {
110
            throw new InvalidArgumentException('Reflection does not have getEndLine method');
111
        }
112
113 9
        return $lineNumber >= $reflection->getStartLine() && $lineNumber <= $reflection->getEndLine();
0 ignored issues
show
The method getStartLine does only exist in Roave\BetterReflection\Reflection\ReflectionMethod, but not in Roave\BetterReflection\Reflection\Reflection.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
The method getEndLine does only exist in Roave\BetterReflection\Reflection\ReflectionMethod, but not in Roave\BetterReflection\Reflection\Reflection.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
114
    }
115
}
116