Issues (320)

Security Analysis    not enabled

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/php/Apix/Exception.php (4 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
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix;
14
15
use Apix\Service;
16
17
class Exception extends \Exception
18
{
19
    const CRITICAL_ERROR_STRING = '500 Internal Server Error';
20
21
    /**
22
     *  E_RECOVERABLE_ERROR handler.
23
     *
24
     *  Use to re-throw E_RECOVERABLE_ERROR as they occur.
25
     *
26
     * @param  int             $code     The error number.
27
     * @param  string          $msg      The error message.
28
     * @param  string          $file     The filename where the error occured.
29
     * @param  int             $line     The line number where it happened.
30
     * @param  \Exception|null $previous The previous chaining Exception.
31
     * @throws \ErrorException
32
     */
33
    public static function errorHandler(
34
        $code, $msg = '', $file = __FILE__, $line = __LINE__, $previous = null
35
    ) {
36
        if (E_RECOVERABLE_ERROR == $code) {
37
            $msg = preg_replace('@to\s.*::\w+\(\)@', '', $msg, 1);
38
            // $code = 400; // Due to a client error, recoverable.
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
        }
40
        $code = 500; // Set as a HTTP Internal Server Error.
41
42
        if ( null !== $previous && !($previous instanceof \Exception) ) {
43
44
            Service::get('logger')->error(
45
                '{0} - {1}:{2} {3}',
46
                array($msg, $file, $line, $previous)
47
            );
48
49
            $previous = null;
50
        }
51
52
        throw new \ErrorException($msg, $code, 0, $file, $line, $previous);
53
    }
54
55
    /**
56
     * Startup exception handler.
57
     *
58
     * @param \Exception $e
59
     * @return array
60
     */
61
    public static function startupException(\Exception $e)
62
    {
63
        return self::criticalError($e, 'Startup Exception');
64
    }
65
66
    /**
67
     * Shutdown/fatal exceptions handler.
68
     *
69
     * @see criticalError
70
     * @return void
71
     * @codeCoverageIgnore
72
     */
73
    public static function shutdownHandler()
74
    {
75
        if ($e = error_get_last()) {
76
            self::criticalError($e, 'Shutdown Exception');
0 ignored issues
show
$e is of type array, but the function expects a object<Exception>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        }
78
    }
79
80
    /**
81
     * Handles critical errors (output and logging).
82
     *
83
     * @param \Exception $e
84
     * @return array
85
     */
86
    public static function criticalError(\Exception $e, $alt_msg) 
0 ignored issues
show
criticalError uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
87
    {
88
        $err = array(
89
            'msg' => self::CRITICAL_ERROR_STRING,
90
            'ctx' => sprintf(
91
                        '#%d %s @ %s:%d',
92
                        $e->code,
93
                        $e->message ? $e->message : $alt_msg,
94
                        $e->file,
95
                        $e->line
96
                    )
97
        );
98
        printf('<h1>%s</h1>', $err['msg']);
99
100
        Service::get('logger')->critical('{msg} - {ctx}', $err);
101
102
        // @codeCoverageIgnoreStart
103
        // TODO: Move the following crap in the Response object...
104
        if (!defined('UNIT_TEST')) {
105
            $proto = isset($_SERVER['SERVER_PROTOCOL'])
106
                        ? $_SERVER['SERVER_PROTOCOL']
107
                        : 'HTTP/1.1';
108
            header($proto . ' ' . $err['msg'], true, 500);
109
            if(DEBUG) var_dump( $err );
0 ignored issues
show
Security Debugging Code introduced by
var_dump($err); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
110
        }
111
        // @codeCoverageIgnoreEnd
112
113
        return $err;
114
    }
115
116
    /**
117
     * Returns the provided exception as a normalize associative array.
118
     *
119
     * @param  \Exception $e
120
     * @return array
121
     */
122
    public static function toArray(\Exception $e)
123
    {
124
        $array = array(
125
            'message' => $e->getMessage(),
126
            'code'    => $e->getCode(),
127
            'type'    => get_class($e),
128
            'file'    => $e->getFile(),
129
            'line'    => $e->getLine(),
130
            'trace'   => $e->getTraceAsString(),
131
        );
132
133
        if (method_exists($e, 'getPrevious')) {
134
            $p = $e->getPrevious();
135
            if (method_exists($p, 'getTraceAsString')) {
136
                $array['prev'] = $p->getTraceAsString();
137
            }
138
        }
139
140
        return $array;
141
    }
142
143
}
144