Issues (127)

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 constant;
14
use function defined;
15
use function dirname;
16
use function realpath;
17
use function reset;
18
19
class CompileNodeToValue
20
{
21
    /**
22
     * Compile an expression from a node into a value.
23
     *
24
     * @param Node\Stmt\Expression|Node\Expr $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
25
     *
26
     * @return mixed
27
     *
28
     * @throws Exception\UnableToCompileNode
29
     */
30 112
    public function __invoke(Node $node, CompilerContext $context)
31
    {
32 112
        if ($node instanceof Node\Stmt\Expression) {
33 94
            return $this($node->expr, $context);
34
        }
35
36
        $constExprEvaluator = new ConstExprEvaluator(function (Node\Expr $node) use ($context) {
37 19
            if ($node instanceof Node\Expr\ConstFetch) {
38 4
                return $this->compileConstFetch($node, $context);
39
            }
40
41 15
            if ($node instanceof Node\Expr\ClassConstFetch) {
42 14
                return $this->compileClassConstFetch($node, $context);
43
            }
44
45 1
            if ($node instanceof Node\Scalar\MagicConst\Dir) {
46
                return $this->compileDirConstant($context);
47
            }
48
49 1
            if ($node instanceof Node\Scalar\MagicConst\Class_) {
50
                return $this->compileClassConstant($context);
51
            }
52
53 1
            throw Exception\UnableToCompileNode::forUnRecognizedExpressionInContext($node, $context);
54 112
        });
55
56 112
        return $constExprEvaluator->evaluateDirectly($node);
0 ignored issues
show
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Expr>. It seems like you assume a concrete implementation of the interface PhpParser\Node 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...
57
    }
58
59
    /**
60
     * Compile constant expressions
61
     *
62
     * @return bool|mixed|null
63
     *
64
     * @throws Exception\UnableToCompileNode
65
     */
66 4
    private function compileConstFetch(Node\Expr\ConstFetch $constNode, CompilerContext $context)
67
    {
68 4
        $firstName = reset($constNode->name->parts);
69 4
        switch ($firstName) {
70 4
            case 'null':
71
                return null;
72 4
            case 'false':
73
                return false;
74 4
            case 'true':
75
                return true;
76
            default:
77 4
                if (! defined($firstName)) {
78 1
                    throw Exception\UnableToCompileNode::becauseOfNotFoundConstantReference($context, $constNode);
79
                }
80
81 3
                return constant($firstName);
82
        }
83
    }
84
85
    /**
86
     * Compile class constants
87
     *
88
     * @return string|int|float|bool|mixed[]|null
89
     *
90
     * @throws IdentifierNotFound
91
     * @throws Exception\UnableToCompileNode If a referenced constant could not be located on the expected referenced class.
92
     */
93 14
    private function compileClassConstFetch(Node\Expr\ClassConstFetch $node, CompilerContext $context)
94
    {
95
        /** @var Node\Identifier $node->name */
96 14
        $nodeName = $node->name->name;
97
        /** @var Node\Name $node->class */
98 14
        $className = $node->class->toString();
99
100 14
        if ($nodeName === 'class') {
101 5
            return $this->resolveClassNameForClassNameConstant($className, $context);
102
        }
103
104
        /** @var ReflectionClass|null $classInfo */
105 10
        $classInfo = null;
106
107 10
        if ($className === 'self' || $className === 'static') {
108 6
            $classInfo = $context->getSelf()->hasConstant($nodeName) ? $context->getSelf() : null;
109 5
        } elseif ($className === 'parent') {
110 1
            $classInfo = $context->getSelf()->getParentClass();
111
        }
112
113 10
        if ($classInfo === null) {
114
            /** @var ReflectionClass $classInfo */
115 4
            $classInfo = $context->getReflector()->reflect($className);
116
        }
117
118 10
        $reflectionConstant = $classInfo->getReflectionConstant($nodeName);
119
120 10
        if (! $reflectionConstant instanceof ReflectionClassConstant) {
121 1
            throw Exception\UnableToCompileNode::becauseOfNotFoundClassConstantReference($context, $classInfo, $node);
122
        }
123
124 9
        return $this->__invoke(
125 9
            $reflectionConstant->getAst()->consts[$reflectionConstant->getPositionInAst()]->value,
126 9
            new CompilerContext($context->getReflector(), $classInfo)
127
        );
128
    }
129
130
    /**
131
     * Compile a __DIR__ node
132
     */
133
    private function compileDirConstant(CompilerContext $context) : string
134
    {
135
        return FileHelper::normalizeWindowsPath(dirname(realpath($context->getFileName())));
136
    }
137
138
    /**
139
     * Compiles magic constant __CLASS__
140
     */
141
    private function compileClassConstant(CompilerContext $context) : string
142
    {
143
        return $context->hasSelf() ? $context->getSelf()->getName() : '';
144
    }
145
146 5
    private function resolveClassNameForClassNameConstant(string $className, CompilerContext $context) : string
147
    {
148 5
        if ($className === 'self' || $className === 'static') {
149 1
            return $context->getSelf()->getName();
150
        }
151
152 5
        if ($className === 'parent') {
153
            /** @var ReflectionClass $parentClass */
154 1
            $parentClass = $context->getSelf()->getParentClass();
155
156 1
            return $parentClass->getName();
157
        }
158
159 4
        return $className;
160
    }
161
}
162