Issues (5)

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/Console/GeneratorCommand.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
/*
4
 * This file is part of questocat/lumen-request package.
5
 *
6
 * (c) questocat <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Questocat\LumenRequest\Console;
13
14
use Illuminate\Console\Command;
15
use Illuminate\Filesystem\Filesystem;
16
use Illuminate\Support\Str;
17
use Symfony\Component\Console\Input\InputArgument;
18
19
abstract class GeneratorCommand extends Command
20
{
21
    /**
22
     * The filesystem instance.
23
     *
24
     * @var \Illuminate\Filesystem\Filesystem
25
     */
26
    protected $files;
27
28
    /**
29
     * The type of class being generated.
30
     *
31
     * @var string
32
     */
33
    protected $type;
34
35
    /**
36
     * Create a new controller creator command instance.
37
     *
38
     * @param \Illuminate\Filesystem\Filesystem $files
39
     */
40
    public function __construct(Filesystem $files)
41
    {
42
        parent::__construct();
43
44
        $this->files = $files;
45
    }
46
47
    /**
48
     * Execute the console command.
49
     *
50
     * @return bool|null
51
     */
52
    public function handle()
53
    {
54
        $name = $this->qualifyClass($this->getNameInput());
55
56
        $path = $this->getPath($name);
57
58
        // First we will check to see if the class already exists. If it does, we don't want
59
        // to create the class and overwrite the user's code. So, we will bail out so the
60
        // code is untouched. Otherwise, we will continue generating this class' files.
61
        if ($this->alreadyExists($this->getNameInput())) {
62
            $this->error($this->type.' already exists!');
63
64
            return false;
65
        }
66
67
        // Next, we will generate the path to the location where this class' file should get
68
        // written. Then, we will build the class and make the proper replacements on the
69
        // stub files so that it gets the correctly formatted namespace and class name.
70
        $this->makeDirectory($path);
71
72
        $this->files->put($path, $this->buildClass($name));
73
74
        $this->info($this->type.' created successfully.');
75
    }
76
77
    /**
78
     * Get the stub file for the generator.
79
     *
80
     * @return string
81
     */
82
    abstract protected function getStub();
83
84
    /**
85
     * Parse the class name and format according to the root namespace.
86
     *
87
     * @param string $name
88
     *
89
     * @return string
90
     */
91
    protected function qualifyClass($name)
92
    {
93
        $name = ltrim($name, '\\/');
94
95
        $rootNamespace = $this->rootNamespace();
96
97
        if (Str::startsWith($name, $rootNamespace)) {
98
            return $name;
99
        }
100
101
        $name = str_replace('/', '\\', $name);
102
103
        return $this->qualifyClass(
104
            $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name
105
        );
106
    }
107
108
    /**
109
     * Get the default namespace for the class.
110
     *
111
     * @param string $rootNamespace
112
     *
113
     * @return string
114
     */
115
    protected function getDefaultNamespace($rootNamespace)
116
    {
117
        return $rootNamespace;
118
    }
119
120
    /**
121
     * Determine if the class already exists.
122
     *
123
     * @param string $rawName
124
     *
125
     * @return bool
126
     */
127
    protected function alreadyExists($rawName)
128
    {
129
        return $this->files->exists($this->getPath($this->qualifyClass($rawName)));
130
    }
131
132
    /**
133
     * Get the destination class path.
134
     *
135
     * @param string $name
136
     *
137
     * @return string
138
     */
139
    protected function getPath($name)
140
    {
141
        $name = Str::replaceFirst($this->rootNamespace(), '', $name);
142
143
        return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php';
144
    }
145
146
    /**
147
     * Build the directory for the class if necessary.
148
     *
149
     * @param string $path
150
     *
151
     * @return string
152
     */
153
    protected function makeDirectory($path)
154
    {
155
        if (!$this->files->isDirectory(dirname($path))) {
156
            $this->files->makeDirectory(dirname($path), 0777, true, true);
157
        }
158
159
        return $path;
160
    }
161
162
    /**
163
     * Build the class with the given name.
164
     *
165
     * @param string $name
166
     *
167
     * @return string
168
     */
169
    protected function buildClass($name)
170
    {
171
        $stub = $this->files->get($this->getStub());
172
173
        return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
174
    }
175
176
    /**
177
     * Replace the namespace for the given stub.
178
     *
179
     * @param string $stub
180
     * @param string $name
181
     *
182
     * @return $this
183
     */
184
    protected function replaceNamespace(&$stub, $name)
185
    {
186
        $stub = str_replace(
187
            ['DummyNamespace', 'DummyRootNamespace', 'NamespacedDummyUserModel'],
188
            [$this->getNamespace($name), $this->rootNamespace(), config('auth.providers.users.model')],
189
            $stub
190
        );
191
192
        return $this;
193
    }
194
195
    /**
196
     * Get the full namespace for a given class, without the class name.
197
     *
198
     * @param string $name
199
     *
200
     * @return string
201
     */
202
    protected function getNamespace($name)
203
    {
204
        return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
205
    }
206
207
    /**
208
     * Replace the class name for the given stub.
209
     *
210
     * @param string $stub
211
     * @param string $name
212
     *
213
     * @return string
214
     */
215
    protected function replaceClass($stub, $name)
216
    {
217
        $class = str_replace($this->getNamespace($name).'\\', '', $name);
218
219
        return str_replace('DummyClass', $class, $stub);
220
    }
221
222
    /**
223
     * Get the desired class name from the input.
224
     *
225
     * @return string
226
     */
227
    protected function getNameInput()
228
    {
229
        return trim($this->argument('name'));
230
    }
231
232
    /**
233
     * Get the root namespace for the class.
234
     *
235
     * @return string
236
     */
237
    protected function rootNamespace()
238
    {
239
        return $this->laravel->getNamespace();
0 ignored issues
show
The method getNamespace() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
240
    }
241
242
    /**
243
     * Get the console command arguments.
244
     *
245
     * @return array
246
     */
247
    protected function getArguments()
248
    {
249
        return [
250
            ['name', InputArgument::REQUIRED, 'The name of the class'],
251
        ];
252
    }
253
}
254