GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Issues (19)

Branch: master

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/Parser.php (1 issue)

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
namespace Coduo\PHPMatcher;
4
5
use Coduo\PHPMatcher\AST;
6
use Coduo\PHPMatcher\Exception\Exception;
7
use Coduo\PHPMatcher\Exception\PatternException;
8
use Coduo\PHPMatcher\Exception\UnknownExpanderException;
9
use Coduo\PHPMatcher\Matcher\Pattern;
10
use Coduo\PHPMatcher\Parser\ExpanderInitializer;
11
12
final class Parser
13
{
14
    const NULL_VALUE = 'null';
15
16
    /**
17
     * @var Lexer
18
     */
19
    private $lexer;
20
21
    /**
22
     * @var ExpanderInitializer
23
     */
24
    private $expanderInitializer;
25
26
    /**
27
     * @param Lexer $lexer
28
     */
29
    public function __construct(Lexer $lexer, ExpanderInitializer $expanderInitializer)
30
    {
31
        $this->lexer = $lexer;
32
        $this->expanderInitializer = $expanderInitializer;
33
    }
34
35
    /**
36
     * @param string $pattern
37
     * @return bool
38
     */
39
    public function hasValidSyntax($pattern)
40
    {
41
        try {
42
            $this->getAST($pattern);
43
            return true;
44
        } catch (Exception $e) {
45
            return false;
46
        }
47
    }
48
49
    /**
50
     * @param string $pattern
51
     * @throws UnknownExpanderException
52
     * @return Pattern\TypePattern
53
     */
54
    public function parse($pattern)
55
    {
56
        $AST = $this->getAST($pattern);
57
        $pattern = new Pattern\TypePattern((string) $AST->getType());
58
        foreach ($AST->getExpanders() as $expander) {
59
            $pattern->addExpander($this->expanderInitializer->initialize($expander));
60
        }
61
62
        return $pattern;
63
    }
64
65
    /**
66
     * @param $pattern
67
     * @return AST\Pattern
68
     */
69
    public function getAST($pattern)
70
    {
71
        $this->lexer->setInput($pattern);
72
        return $this->getPattern();
73
    }
74
75
    /**
76
     * Create AST root
77
     *
78
     * @return AST\Pattern
79
     */
80
    private function getPattern()
81
    {
82
        $this->lexer->moveNext();
83
84
        switch ($this->lexer->lookahead['type']) {
85
            case Lexer::T_TYPE_PATTERN:
86
                $pattern = new AST\Pattern(new AST\Type($this->lexer->lookahead['value']));
87
                break;
88
            default:
89
                $this->unexpectedSyntaxError($this->lexer->lookahead, "@type@ pattern");
90
                break;
91
        }
92
93
        $this->lexer->moveNext();
94
95
        if (!$this->endOfPattern()) {
96
            $this->addExpanderNodes($pattern);
0 ignored issues
show
The variable $pattern does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
97
        }
98
99
        return $pattern;
100
    }
101
102
    /**
103
     * @param AST\Pattern $pattern
104
     */
105
    private function addExpanderNodes(AST\Pattern $pattern)
106
    {
107
        while(($expander = $this->getNextExpanderNode()) !== null)  {
108
            $pattern->addExpander($expander);
109
        }
110
    }
111
112
    /**
113
     * Try to get next expander, return null if there is no expander left
114
     * @return AST\Expander|null
115
     */
116
    private function getNextExpanderNode()
117
    {
118
        if ($this->endOfPattern()) {
119
            return ;
120
        }
121
122
        $expander = new AST\Expander($this->getExpanderName());
123
124
        if ($this->endOfPattern()) {
125
            $this->unexpectedEndOfString(")");
126
        }
127
128
        $this->addArgumentValues($expander);
129
130
        if ($this->endOfPattern()) {
131
            $this->unexpectedEndOfString(")");
132
        }
133
134
        if (!$this->isNextCloseParenthesis()) {
135
            $this->unexpectedSyntaxError($this->lexer->lookahead, ")");
136
        }
137
138
        return $expander;
139
    }
140
141
    /**
142
     * @return mixed
143
     * @throws PatternException
144
     */
145
    private function getExpanderName()
146
    {
147
        if ($this->lexer->lookahead['type'] !== Lexer::T_EXPANDER_NAME) {
148
            $this->unexpectedSyntaxError($this->lexer->lookahead, ".expanderName(args) definition");
149
        }
150
        $expander = $this->lexer->lookahead['value'];
151
        $this->lexer->moveNext();
152
153
        return $expander;
154
    }
155
156
    /**
157
     * Add arguments to expander
158
     *
159
     * @param AST\Expander $expander
160
     */
161
    private function addArgumentValues(AST\Expander $expander)
162
    {
163
        while(($argument = $this->getNextArgumentValue()) !== null) {
164
            $argument = ($argument === self::NULL_VALUE) ? null : $argument;
165
            $expander->addArgument($argument);
166
            if (!$this->lexer->isNextToken(Lexer::T_COMMA)){
167
                break;
168
            }
169
170
            $this->lexer->moveNext();
171
172
            if ($this->lexer->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) {
173
                $this->unexpectedSyntaxError($this->lexer->lookahead, "string, number, boolean or null argument");
174
            }
175
        }
176
    }
177
178
    /**
179
     * Try to get next argument. Return false if there are no arguments left before ")"
180
     * @return null|mixed
181
     */
182
    private function getNextArgumentValue()
183
    {
184
        $validArgumentTypes = array(
185
            Lexer::T_STRING,
186
            Lexer::T_NUMBER,
187
            Lexer::T_BOOLEAN,
188
            Lexer::T_NULL
189
        );
190
191
        if ($this->lexer->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) {
192
            return ;
193
        }
194
195
        if ($this->lexer->isNextToken(Lexer::T_OPEN_CURLY_BRACE)) {
196
            return $this->getArrayArgument();
197
        }
198
199
        if ($this->lexer->isNextToken(Lexer::T_EXPANDER_NAME)) {
200
            return $this->getNextExpanderNode();
201
        }
202
203
        if (!$this->lexer->isNextTokenAny($validArgumentTypes)) {
204
            $this->unexpectedSyntaxError($this->lexer->lookahead, "string, number, boolean or null argument");
205
        }
206
207
        $tokenType = $this->lexer->lookahead['type'];
208
        $argument = $this->lexer->lookahead['value'];
209
        $this->lexer->moveNext();
210
211
        if ($tokenType === Lexer::T_NULL) {
212
            $argument = self::NULL_VALUE;
213
        }
214
215
        return $argument;
216
    }
217
218
    /**
219
     * return array
220
     */
221
    private function getArrayArgument()
222
    {
223
        $arrayArgument = array();
224
        $this->lexer->moveNext();
225
226
        while ($this->getNextArrayElement($arrayArgument) !== null) {
227
            $this->lexer->moveNext();
228
        }
229
230
        if (!$this->lexer->isNextToken(Lexer::T_CLOSE_CURLY_BRACE)) {
231
            $this->unexpectedSyntaxError($this->lexer->lookahead, "}");
232
        }
233
234
        $this->lexer->moveNext();
235
236
        return $arrayArgument;
237
    }
238
239
    /**
240
     * @param array $array
241
     * @return bool
242
     * @throws PatternException
243
     */
244
    private function getNextArrayElement(array &$array)
245
    {
246
        if ($this->lexer->isNextToken(Lexer::T_CLOSE_CURLY_BRACE)) {
247
            return ;
248
        }
249
250
        $key = $this->getNextArgumentValue();
251
        if ($key === self::NULL_VALUE) {
252
            $key = "";
253
        }
254
255
        if (!$this->lexer->isNextToken(Lexer::T_COLON)) {
256
            $this->unexpectedSyntaxError($this->lexer->lookahead, ":");
257
        }
258
259
        $this->lexer->moveNext();
260
261
        $value = $this->getNextArgumentValue();
262
        if ($value === self::NULL_VALUE) {
263
            $value = null;
264
        }
265
266
        $array[$key] = $value;
267
268
        if (!$this->lexer->isNextToken(Lexer::T_COMMA)) {
269
            return ;
270
        }
271
272
        return true;
273
    }
274
275
    /**
276
     * @return bool
277
     */
278
    private function isNextCloseParenthesis()
279
    {
280
        $isCloseParenthesis = $this->lexer->isNextToken(Lexer::T_CLOSE_PARENTHESIS);
281
        $this->lexer->moveNext();
282
283
        return $isCloseParenthesis;
284
    }
285
286
    /**
287
     * @param $unexpectedToken
288
     * @param null $expected
289
     * @throws PatternException
290
     */
291
    private function unexpectedSyntaxError($unexpectedToken, $expected = null)
292
    {
293
        $tokenPos = (isset($unexpectedToken['position'])) ? $unexpectedToken['position'] : '-1';
294
        $message  = sprintf("line 0, col %d: Error: ", $tokenPos);
295
        $message .= (isset($expected)) ? sprintf("Expected \"%s\", got ", $expected) : "Unexpected";
296
        $message .= sprintf("\"%s\"", $unexpectedToken['value']);
297
298
        throw PatternException::syntaxError($message);
299
    }
300
301
    /**
302
     * @param null $expected
303
     * @throws PatternException
304
     */
305
    private function unexpectedEndOfString($expected = null)
306
    {
307
        $tokenPos = (isset($this->lexer->token['position'])) ? $this->lexer->token['position'] + strlen($this->lexer->token['value']) : '-1';
308
        $message  = sprintf("line 0, col %d: Error: ", $tokenPos);
309
        $message .= (isset($expected)) ? sprintf("Expected \"%s\", got end of string.", $expected) : "Unexpected";
310
        $message .= "end of string";
311
312
        throw PatternException::syntaxError($message);
313
    }
314
315
    /**
316
     * @return bool
317
     */
318
    private function endOfPattern()
319
    {
320
        return is_null($this->lexer->lookahead);
321
    }
322
}
323