Issues (6)

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/Parser.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
namespace Vlaswinkel\Lua;
4
5
use Vlaswinkel\Lua\AST\ASTNode;
6
use Vlaswinkel\Lua\AST\NilASTNode;
7
use Vlaswinkel\Lua\AST\NumberASTNode;
8
use Vlaswinkel\Lua\AST\StringASTNode;
9
use Vlaswinkel\Lua\AST\TableASTNode;
10
use Vlaswinkel\Lua\AST\TableEntryASTNode;
11
12
/**
13
 * Class Parser
14
 *
15
 * @see     http://lisperator.net/pltut/parser/the-parser
16
 *
17
 * @author  Koen Vlaswinkel <[email protected]>
18
 * @package Vlaswinkel\Lua
19
 */
20
class Parser {
21
    /**
22
     * @var TokenStream
23
     */
24
    private $input;
25
26
    /**
27
     * Parser constructor.
28
     *
29
     * @param TokenStream $input
30
     */
31 24
    public function __construct(TokenStream $input) {
32 24
        $this->input = $input;
33 24
    }
34
35
    /**
36
     * @return ASTNode
37
     *
38
     * @throws ParseException
39
     */
40 24
    public function parse() {
41 24
        $result = $this->parseInternal();
42
43 22
        if (!$this->input->eof()) {
44 2
            if ($result instanceof StringASTNode && $this->isPunctuation('=')) {
45 2
                $this->skipPunctuation('=');
46 2
                $value = $this->parseInternal();
47
48 2
                return new TableASTNode([new TableEntryASTNode($value, $result)]);
0 ignored issues
show
It seems like $value defined by $this->parseInternal() on line 46 can be null; however, Vlaswinkel\Lua\AST\Table...yASTNode::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
49
            }
50
            
51
            $this->input->error('Parser has finished parsing, but end of file was not reached. Next character is ' . $this->input->peek()->getValue());
52
        }
53
54 20
        return $result;
55
    }
56
57
    /**
58
     * @return ASTNode
59
     *
60
     * @throws ParseException
61
     */
62 24
    protected function parseInternal() {
63 24
        if ($this->isPunctuation('{')) {
64 13
            return $this->parseTable();
65
        }
66 23
        if ($this->isPunctuation('[')) {
67 4
            return $this->parseTableKey();
68
        }
69 23
        $token = $this->input->next();
70 23
        if ($token->getType() == Token::TYPE_NUMBER) {
71 8
            return new NumberASTNode($token->getValue());
72
        }
73 21
        if ($token->getType() == Token::TYPE_STRING || $token->getType() == Token::TYPE_IDENTIFIER) {
74 18
            return new StringASTNode($token->getValue());
75
        }
76 5
        if ($token->getType() == Token::TYPE_KEYWORD) {
77 5
            if ($token->getValue() === 'nil') {
78 4
                return new NilASTNode();
79
            } else {
80 1
                $this->input->error('Unexpected keyword: ' . $token->getValue());
81
            }
82
        }
83
        $this->unexpected();
84
    }
85
86
    /**
87
     * @return TableASTNode
88
     */
89 13
    protected function parseTable() {
90 13
        return new TableASTNode(
91 13
            $this->delimited(
92 13
                '{',
93 13
                '}',
94 13
                ',',
95 13
                [$this, 'parseTableEntry']
96 13
            )
97 12
        );
98
    }
99
100
    /**
101
     * @return TableEntryASTNode
102
     */
103 12
    protected function parseTableEntry() {
104 12
        $token = $this->parseInternal();
105 12
        if ($this->isPunctuation('=')) {
106 11
            $this->skipPunctuation('=');
107 11
            $value = $this->parseInternal();
108 11
            return new TableEntryASTNode(
109 11
                $value,
0 ignored issues
show
It seems like $value defined by $this->parseInternal() on line 107 can be null; however, Vlaswinkel\Lua\AST\Table...yASTNode::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
110
                $token
111 11
            );
112
        }
113 5
        return new TableEntryASTNode($token);
0 ignored issues
show
It seems like $token defined by $this->parseInternal() on line 104 can be null; however, Vlaswinkel\Lua\AST\Table...yASTNode::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
114
    }
115
116
    /**
117
     * @return ASTNode
0 ignored issues
show
Should the return type not be ASTNode|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
118
     */
119 4
    protected function parseTableKey() {
120 4
        $this->skipPunctuation('[');
121 4
        $token = $this->parseInternal();
122 4
        $this->skipPunctuation(']');
123 4
        return $token;
124
    }
125
126
    /**
127
     * @param string   $start
128
     * @param string   $stop
129
     * @param string   $separator
130
     * @param callable $parser
131
     *
132
     * @return array
133
     */
134 13
    protected function delimited($start, $stop, $separator, callable $parser) {
135 13
        $a     = [];
136 13
        $first = true;
137 13
        $this->skipPunctuation($start);
138 13
        while (!$this->input->eof()) {
139 13
            if ($this->isPunctuation($stop)) {
140 12
                break;
141
            }
142 12
            if ($first) {
143 12
                $first = false;
144 12
            } else {
145 5
                $this->skipPunctuation($separator);
146
            }
147 12
            if ($this->isPunctuation($stop)) {
148 2
                break;
149
            }
150 12
            $a[] = $parser();
151 12
        }
152 12
        $this->skipPunctuation($stop);
153 12
        return $a;
154
    }
155
156
    /**
157
     * @param string|null $char
158
     *
159
     * @return bool
160
     */
161 24
    protected function isPunctuation($char = null) {
162 24
        $token = $this->input->peek();
163 24
        return $token && $token->getType() == Token::TYPE_PUNCTUATION && ($char === null || $token->getValue(
164 24
            ) == $char);
165
    }
166
167
    /**
168
     * @param string|null $char
169
     *
170
     * @throws ParseException
171
     */
172 14
    protected function skipPunctuation($char = null) {
173 14
        if ($this->isPunctuation($char)) {
174 14
            $this->input->next();
175 14
        } else {
176 1
            $this->input->error('Expecting punctuation: "' . $char . '"');
177
        }
178 14
    }
179
180
    /**
181
     * @throws ParseException
182
     */
183
    protected function unexpected() {
184
        $this->input->error('Unexpected token: ' . json_encode($this->input->peek()));
185
    }
186
}