Issues (26)

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/ExceptionRenderer.php (3 issues)

Labels

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
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Console;
11
12
use Railt\Io\Exception\NotReadableException;
13
use Railt\Io\File;
14
15
/**
16
 * Class ExceptionRenderer
17
 */
18
class ExceptionRenderer
19
{
20
    /**
21
     * @var Highlighter
22
     */
23
    private $highlighter;
24
25
    /**
26
     * ExceptionRenderer constructor.
27
     * @param Highlighter $highlighter
28
     */
29
    public function __construct(Highlighter $highlighter)
30
    {
31
        $this->highlighter = $highlighter;
32
    }
33
34
    /**
35
     * @param \Throwable $e
36
     * @return string
37
     */
38
    public function render(\Throwable $e): string
39
    {
40
        return
41
            $this->renderHeader($e) .
42
            $this->renderFileHeader($e) .
43
            $this->renderCode($e) .
44
            $this->renderTrace($e);
45
    }
46
47
    /**
48
     * @param \Throwable $e
49
     * @return string
50
     */
51
    private function renderCode(\Throwable $e): string
52
    {
53
        try {
54
            $file = File::fromPathname($e->getFile());
55
        } catch (NotReadableException $error) {
56
            $file = File::fromSources('', $e->getFile());
57
        }
58
59
        return $this->highlighter->highlight($file, $e->getLine()) .
0 ignored issues
show
It seems like $file defined by \Railt\Io\File::fromPathname($e->getFile()) on line 54 can also be of type object<Railt\Io\File>; however, Railt\Console\Highlighter::highlight() does only seem to accept object<Railt\Io\Readable>, 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...
60
            \PHP_EOL;
61
    }
62
63
    /**
64
     * @param \Throwable $e
65
     * @return string
66
     */
67
    private function renderHeader(\Throwable $e): string
68
    {
69
        [$name, $msg] = [$this->classname($e), $e->getMessage()];
0 ignored issues
show
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $msg does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
70
71
        return \sprintf('<error> %s </error> <comment>%s</comment>', $name, $msg) .
72
            \PHP_EOL . \PHP_EOL;
73
    }
74
75
    /**
76
     * @param string|object $class
77
     * @return string
78
     */
79
    private function classname($class): string
80
    {
81
        $class = \is_object($class) ? \get_class($class) : $class;
82
83
        return \basename(\str_replace('\\', '/', $class));
84
    }
85
86
    /**
87
     * @param \Throwable $e
88
     * @return string
89
     */
90
    private function renderFileHeader(\Throwable $e): string
91
    {
92
        $message = 'in <fg=cyan;options=underscore>%s</> at line <fg=cyan>%d</>';
93
94
        return \sprintf($message, $e->getFile(), $e->getLine()) .
95
            \PHP_EOL . \PHP_EOL;
96
    }
97
98
    /**
99
     * @param \Throwable $e
100
     * @return string
101
     */
102
    private function renderTrace(\Throwable $e): string
103
    {
104
        $result = ['<comment>Stack Trace:</comment>'];
105
106
        foreach ($e->getTrace() as $i => $trace) {
107
            $line = $this->traceInvocation($trace);
108
            $args = $this->traceArguments($trace);
109
110
            $result[] = \sprintf('<fg=blue>%3s</> <comment>%s(%s)</comment>', '#' . $i, $line, $args);
111
            $result[] = \sprintf('    <fg=cyan>%s</>:<fg=cyan>%d</>', $trace['file'], $trace['line']);
112
        }
113
114
        return \implode(\PHP_EOL, $result) .
115
            \PHP_EOL;
116
    }
117
118
    /**
119
     * @param array $trace
120
     * @return string
121
     */
122
    private function traceArguments(array $trace): string
123
    {
124
        $result = [];
125
126
        foreach (($trace['args'] ?? []) as $arg) {
127
            $result[] = \is_object($arg) ? 'Object(' . $this->classname($arg) . ')' : \gettype($arg);
128
        }
129
130
        return \implode(', ', $result);
131
    }
132
133
    /**
134
     * @param array $trace
135
     * @return string
136
     */
137
    private function traceInvocation(array $trace): string
138
    {
139
        return isset($trace['class'])
140
            ? $trace['class'] . $trace['type'] . $trace['function']
141
            : $trace['function'];
142
    }
143
}
144