Issues (197)

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/Commands/CheckCompact.php (6 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
namespace Imanghafoori\LaravelMicroscope\Commands;
4
5
use Illuminate\Console\Command;
6
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
7
use Imanghafoori\LaravelMicroscope\Analyzers\FunctionCall;
8
use Imanghafoori\LaravelMicroscope\Analyzers\Ifs;
9
use Imanghafoori\LaravelMicroscope\Analyzers\TokenManager;
10
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
11
use Imanghafoori\LaravelMicroscope\ErrorTypes\CompactCall;
12
use Imanghafoori\LaravelMicroscope\LaravelPaths\FilePath;
13
use Imanghafoori\LaravelMicroscope\SpyClasses\RoutePaths;
14
15
class CheckCompact extends Command
16
{
17
    protected $signature = 'check:compact';
18
19
    protected $description = 'Checks that compact() function calls are correct';
20
21
    public function handle()
22
    {
23
        event('microscope.start.command');
24
        $this->info('Checking compact() calls, fast and furious!  mm(~_~)mm  ');
25
26
        $this->checkRoutePaths(RoutePaths::get());
27
        $this->checkPsr4Classes();
28
29
        event('microscope.finished.checks', [$this]);
30
31
        return app(ErrorPrinter::class)->hasErrors() ? 1 : 0;
32
    }
33
34
    private function checkPathForCompact($absPath)
35
    {
36
        $tokens = token_get_all(file_get_contents($absPath));
37
38
        foreach ($tokens as $i => $token) {
39
            if ($tokens[$i][0] != T_FUNCTION) {
40
                continue;
41
            }
42
43
            $methodBody = $this->readMethodBodyAsTokens($tokens, $i);
44
45
            if ($methodBody === false) {
46
                continue;
47
            }
48
49
            $signatureVars = $this->collectSignatureVars($tokens, $i);
50
            $this->checkMethodBodyForCompact($absPath, $methodBody, $signatureVars);
51
        }
52
    }
53
54
    private function checkRoutePaths($paths)
55
    {
56
        foreach ($paths as $filePath) {
57
            $this->checkPathForCompact($filePath);
58
        }
59
    }
60
61
    private function checkPsr4Classes()
62
    {
63
        $psr4 = ComposerJson::readAutoload();
64
65
        foreach ($psr4 as $_namespace => $dirPath) {
66
            foreach (FilePath::getAllPhpFiles($dirPath) as $filePath) {
67
                $this->checkPathForCompact($filePath->getRealPath());
68
            }
69
        }
70
    }
71
72
    private function checkMethodBodyForCompact($absPath, $methodBody, $vars)
73
    {
74
        foreach ($methodBody as $c => $token) {
75
            ($token[0] == T_VARIABLE) && $vars[$token[1]] = null;
76
77
            if (! ($pp = FunctionCall::isGlobalCall('compact', $methodBody, $c))) {
78
                continue;
79
            }
80
81
            [, $compactedVars,] = Ifs::readCondition($methodBody, $pp);
0 ignored issues
show
The variable $compactedVars 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...
82
            $compactVars = [];
83
            foreach ($compactedVars as $uu => $var) {
84
                $var[0] == T_CONSTANT_ENCAPSED_STRING && $compactVars[] = '$'.\trim($var[1], '\'\"');
85
            }
86
            $compactVars = array_flip($compactVars);
87
88
            unset($vars['$this']);
89
            $missingVars = array_diff_key($compactVars, $vars);
90
            $missingVars && CompactCall::isMissing($absPath, $methodBody[$pp][2], $missingVars);
0 ignored issues
show
Bug Best Practice introduced by
The expression $missingVars of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
91
        }
92
    }
93
94
    private function collectSignatureVars($tokens, $i)
95
    {
96
        [, $signatures,] = Ifs::readCondition($tokens, $i);
0 ignored issues
show
The variable $signatures 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...
97
98
        $vars = [];
99
        foreach ($signatures as $sig) {
100
            ($sig[0] == T_VARIABLE) && $vars[$sig[1]] = null;
101
        }
102
103
        return $vars;
104
    }
105
106
    private function readMethodBodyAsTokens($tokens, $i)
107
    {
108
        // fast-forward to the start of function body
109
        [$char, $methodBodyStartIndex] = TokenManager::forwardTo($tokens, $i, ['{', ';']);
0 ignored issues
show
The variable $char 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 $methodBodyStartIndex 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...
110
111
        // in order to avoid checking abstract methods (with no body) and do/while
112
        if ($char === ';') {
113
            return false;
114
        }
115
116
        try {
117
            // fast-forward to the end of function body
118
            [$methodBody,] = TokenManager::readBody($tokens, $methodBodyStartIndex);
0 ignored issues
show
The variable $methodBody 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...
119
120
            return $methodBody;
121
        } catch (\Exception $e) {
122
            return false;
123
        }
124
    }
125
}
126