Issues (45)

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/Rule/FilesRule.php (4 issues)

Labels
Severity

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 project-quality-inspector.
4
 *
5
 * (c) Alexandre GESLIN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace ProjectQualityInspector\Rule;
12
13
use ProjectQualityInspector\Application\ProcessHelper;
14
use ProjectQualityInspector\Exception\ExpectationFailedException;
15
16
/**
17
 * Class FilesRule
18
 *
19
 * @package ProjectQualityInspector\Rule
20
 */
21
class FilesRule extends AbstractRule
22
{
23
    public function __construct(array $config, $baseDir)
24
    {
25
        parent::__construct($config, $baseDir);
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function evaluate()
32
    {
33
        $expectationsFailedExceptions = [];
34
35
        foreach ($this->config as $fileConf) {
36
            try {
37
                $files = $this->expectsFilesGlobExists($fileConf, $this->baseDir);
38
                if (isset($fileConf['grep']) && count($files)) {
39
                    $this->expectsFilesGrep($files, $fileConf['grep'], $this->getReason($fileConf));
0 ignored issues
show
It seems like $this->getReason($fileConf) targeting ProjectQualityInspector\...stractRule::getReason() can also be of type array or null; however, ProjectQualityInspector\...ule::expectsFilesGrep() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
40
                }
41
                $this->addAssertion($this->getValue($fileConf));
0 ignored issues
show
It seems like $this->getValue($fileConf) targeting ProjectQualityInspector\...bstractRule::getValue() can also be of type array; however, ProjectQualityInspector\...actRule::addAssertion() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
42
            } catch (ExpectationFailedException $e) {
43
                $expectationsFailedExceptions[] = $e;
44
                $this->addAssertion($this->getValue($fileConf), [['message' => $e->getMessage() . $e->getReason(), 'type' => 'expectsFilesGlobExists|expectsFilesGrep']]);
0 ignored issues
show
It seems like $this->getValue($fileConf) targeting ProjectQualityInspector\...bstractRule::getValue() can also be of type array; however, ProjectQualityInspector\...actRule::addAssertion() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
45
            }
46
        }
47
48
        if (count($expectationsFailedExceptions)) {
49
            $this->throwRuleViolationException($expectationsFailedExceptions);
50
        }
51
    }
52
53
    /**
54
     * @param $fileConf
55
     * @param $baseDir
56
     * @return array
57
     */
58
    private function expectsFilesGlobExists($fileConf, $baseDir)
59
    {
60
        $files = [];
61
        $fileName = $this->getValue($fileConf);
62
        $reason = $this->getReason($fileConf);
63
64
        $filePath = $baseDir . DIRECTORY_SEPARATOR . $fileName;
65
66
        if ($fileName[0] == '!') {
67
            $fileName = ltrim($fileName, '!');
68
            $filePath = $baseDir . DIRECTORY_SEPARATOR . $fileName;
69
            $this->globShouldNotFind($filePath, $reason);
0 ignored issues
show
It seems like $reason defined by $this->getReason($fileConf) on line 62 can also be of type array or null; however, ProjectQualityInspector\...le::globShouldNotFind() does only seem to accept string, 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...
70
        } else {
71
            $files = $this->globShouldFind($filePath, $reason);
72
        }
73
74
        return $files;
75
    }
76
77
    /**
78
     * @param $filePathGlob
79
     * @param $reason
80
     * @return array
81
     *
82
     * @throws ExpectationFailedException
83
     */
84
    private function globShouldFind($filePathGlob, $reason)
85
    {
86
        $message = sprintf('file <fg=green>"%s"</> should exists', $filePathGlob);
87
        $files = glob($filePathGlob);
88
        if (!count($files)) {
89
            throw new ExpectationFailedException($filePathGlob, $message, $reason);
90
        }
91
92
        return $files;
93
    }
94
95
    /**
96
     * @param string $filePathGlob
97
     * @param string $reason
98
     *
99
     * @throws ExpectationFailedException
100
     */
101
    private function globShouldNotFind($filePathGlob, $reason)
102
    {
103
        $message = sprintf('file <fg=green>"%s"</> should not exists', $filePathGlob);
104
105
        if (count(glob($filePathGlob))) {
106
            throw new ExpectationFailedException($filePathGlob, $message, $reason);
107
        }
108
    }
109
110
    /**
111
     * @param array $files
112
     * @param array|string $greps
113
     * @param string $reason
114
     */
115
    private function expectsFilesGrep(array $files, $greps, $reason)
116
    {
117
        $greps = (!is_array($greps)) ? [$greps] : $greps;
118
119
        foreach ($files as $file) {
120
            foreach ($greps as $grep) {
121
                $this->fileShouldGrep($file, $grep, $reason);
122
            }
123
        }
124
    }
125
126
    /**
127
     * @param string $filePath
128
     * @param string $grep
129
     * @param string $reason
130
     *
131
     * @throws ExpectationFailedException
132
     */
133
    private function fileShouldGrep($filePath, $grep, $reason)
134
    {
135
        $message = sprintf('file <fg=green>"%s"</> should contain <fg=green>"%s"</> string', $filePath, $grep);
136
        $negation = false;
137
        $recursiveOption = '';
138
139
        if ($grep[0] == '!') {
140
            $grep = ltrim($grep, '!');
141
            $negation = true;
142
            $message = sprintf('file <fg=green>"%s"</> should not contain <fg=green>"%s"</> string', $filePath, $grep);
143
        }
144
145
        if (is_dir($filePath)) {
146
            $recursiveOption = '-r';
147
        }
148
149
        $result = ProcessHelper::execute(sprintf('grep %s %s %s', $recursiveOption, escapeshellarg($grep), $filePath), $this->baseDir, true);
150
151
        if (($negation && count($result)) || (!$negation && !count($result))) {
152
            throw new ExpectationFailedException($filePath, $message, $reason);
153
        }
154
    }
155
}