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/NodeCompiler/CompileNodeToValue.php (1 issue)

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\NodeCompiler;
6
7
use PhpParser\ConstExprEvaluator;
8
use PhpParser\Node;
9
use Roave\BetterReflection\Reflection\ReflectionClass;
10
use Roave\BetterReflection\Reflection\ReflectionClassConstant;
11
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
12
use Roave\BetterReflection\Util\FileHelper;
13
use function assert;
14
use function constant;
15
use function defined;
16
use function dirname;
17
use function realpath;
18
use function reset;
19
20
class CompileNodeToValue
21
{
22
    /**
23
     * Compile an expression from a node into a value.
24
     *
25
     * @param Node\Stmt\Expression|Node\Expr $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
26
     *
27
     * @return bool|int|float|string|array|null
28
     *
29
     * @throws Exception\UnableToCompileNode
30
     *
31
     * @psalm-return scalar|array<scalar>|null
32
     */
33 112
    public function __invoke(Node $node, CompilerContext $context)
34
    {
35 112
        if ($node instanceof Node\Stmt\Expression) {
36 94
            return $this($node->expr, $context);
37
        }
38
39
        $constExprEvaluator = new ConstExprEvaluator(function (Node\Expr $node) use ($context) {
40 19
            if ($node instanceof Node\Expr\ConstFetch) {
41 4
                return $this->compileConstFetch($node, $context);
42
            }
43
44 15
            if ($node instanceof Node\Expr\ClassConstFetch) {
45 14
                return $this->compileClassConstFetch($node, $context);
46
            }
47
48 1
            if ($node instanceof Node\Scalar\MagicConst\Dir) {
49
                return $this->compileDirConstant($context);
50
            }
51
52 1
            if ($node instanceof Node\Scalar\MagicConst\Class_) {
53
                return $this->compileClassConstant($context);
54
            }
55
56 1
            throw Exception\UnableToCompileNode::forUnRecognizedExpressionInContext($node, $context);
57 112
        });
58
59 112
        return $constExprEvaluator->evaluateDirectly($node);
60
    }
61
62
    /**
63
     * Compile constant expressions
64
     *
65
     * @return bool|int|float|string|array|null
66
     *
67
     * @throws Exception\UnableToCompileNode
68
     *
69
     * @psalm-return scalar|array<scalar>|null
70
     */
71 4
    private function compileConstFetch(Node\Expr\ConstFetch $constNode, CompilerContext $context)
72
    {
73 4
        $firstName = reset($constNode->name->parts);
74 4
        switch ($firstName) {
75 4
            case 'null':
76
                return null;
77 4
            case 'false':
78
                return false;
79 4
            case 'true':
80
                return true;
81
            default:
82 4
                if (! defined($firstName)) {
83 1
                    throw Exception\UnableToCompileNode::becauseOfNotFoundConstantReference($context, $constNode);
84
                }
85
86 3
                return constant($firstName);
87
        }
88
    }
89
90
    /**
91
     * Compile class constants
92
     *
93
     * @return bool|int|float|string|array|null
94
     *
95
     * @throws IdentifierNotFound
96
     * @throws Exception\UnableToCompileNode If a referenced constant could not be located on the expected referenced class.
97
     *
98
     * @psalm-return scalar|array<scalar>|null
99
     */
100 14
    private function compileClassConstFetch(Node\Expr\ClassConstFetch $node, CompilerContext $context)
101
    {
102 14
        assert($node->name instanceof Node\Identifier);
103 14
        $nodeName = $node->name->name;
104 14
        assert($node->class instanceof Node\Name);
105 14
        $className = $node->class->toString();
0 ignored issues
show
The method toString does only exist in PhpParser\Node\Name, but not in PhpParser\Node\Expr.

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...
106
107 14
        if ($nodeName === 'class') {
108 5
            return $this->resolveClassNameForClassNameConstant($className, $context);
109
        }
110
111 10
        $classInfo = null;
112
113 10
        if ($className === 'self' || $className === 'static') {
114 6
            $classInfo = $context->getSelf()->hasConstant($nodeName) ? $context->getSelf() : null;
115 5
        } elseif ($className === 'parent') {
116 1
            $classInfo = $context->getSelf()->getParentClass();
117
        }
118
119 10
        if ($classInfo === null) {
120 4
            $classInfo = $context->getReflector()->reflect($className);
121 4
            assert($classInfo instanceof ReflectionClass);
122
        }
123
124 10
        $reflectionConstant = $classInfo->getReflectionConstant($nodeName);
125
126 10
        if (! $reflectionConstant instanceof ReflectionClassConstant) {
127 1
            throw Exception\UnableToCompileNode::becauseOfNotFoundClassConstantReference($context, $classInfo, $node);
128
        }
129
130 9
        return $this->__invoke(
131 9
            $reflectionConstant->getAst()->consts[$reflectionConstant->getPositionInAst()]->value,
132 9
            new CompilerContext($context->getReflector(), $classInfo)
133
        );
134
    }
135
136
    /**
137
     * Compile a __DIR__ node
138
     */
139
    private function compileDirConstant(CompilerContext $context) : string
140
    {
141
        return FileHelper::normalizeWindowsPath(dirname(realpath($context->getFileName())));
142
    }
143
144
    /**
145
     * Compiles magic constant __CLASS__
146
     */
147
    private function compileClassConstant(CompilerContext $context) : string
148
    {
149
        return $context->hasSelf() ? $context->getSelf()->getName() : '';
150
    }
151
152 5
    private function resolveClassNameForClassNameConstant(string $className, CompilerContext $context) : string
153
    {
154 5
        if ($className === 'self' || $className === 'static') {
155 1
            return $context->getSelf()->getName();
156
        }
157
158 5
        if ($className === 'parent') {
159 1
            $parentClass = $context->getSelf()->getParentClass();
160 1
            assert($parentClass instanceof ReflectionClass);
161
162 1
            return $parentClass->getName();
163
        }
164
165 4
        return $className;
166
    }
167
}
168