Issues (25)

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.

lib/Widget/Endpoint.php (3 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 Tacone\Bees\Widget;
4
5
use App;
6
use ArrayAccess;
7
use Countable;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Contracts\Support\Jsonable;
10
use IteratorAggregate;
11
use Tacone\Bees\Base\DelegatedArrayTrait;
12
use Tacone\Bees\Collection\FieldCollection;
13
use Tacone\Bees\Field\Field;
14
use Tacone\Bees\Helper\Error;
15
use Tacone\DataSource\AbstractDataSource;
16
use Tacone\DataSource\DataSource;
17
18
class Endpoint implements Countable, IteratorAggregate, ArrayAccess, Arrayable, Jsonable
19
{
20
    use DelegatedArrayTrait;
21
22
    /**
23
     * @var FieldCollection
24
     */
25
    protected $fields;
26
27
    /**
28
     * @var AbstractDataSource
29
     */
30
    protected $source;
31
32
    protected $auto = false;
33
34
    public function __construct($source = [])
35
    {
36
        $this->fields = new FieldCollection();
37
        $this->initSource($source);
38
    }
39
40
    public static function auto($source)
41
    {
42
        $endpoint = new static($source);
43
        $endpoint->auto = true;
44
45
        return $endpoint;
46
    }
47
48
    protected function initSource($source)
49
    {
50
        $this->source = DataSource::make($source);
51
    }
52
53
    /**
54
     * @param string $name
55
     * @param array  $arguments
56
     *
57
     * @return Field|static|mixed
58
     */
59
    public function __call($name, $arguments)
60
    {
61
        // Is it a field name?
62
        try {
63
            $binding = "bees.$name";
64
            $field = App::make($binding, $arguments);
65
            $this->fields->add($field);
66
67
            return $field;
68
        } catch (\ReflectionException $e) {
69
            // not a field name
70
        }
71
72
        // oh, well, then ...
73
        throw Error::missingMethod($this, $name);
74
    }
75
76
    public function source($newSource = null)
77
    {
78
        if (func_num_args()) {
79
            $this->source = $newSource;
80
81
            return $this;
82
        }
83
84
        return $this->source;
85
    }
86
87
    /**
88
     * Collection containing all the fields in the form.
89
     *
90
     * @return FieldCollection
91
     */
92
    public function fields()
93
    {
94
        return $this->fields;
95
    }
96
97
    /**
98
     * Get a field by name (dotted offset).
99
     *
100
     * (you can also use array notation like:
101
     * <code>$form['author.name']</code>
102
     *
103
     * @param string $name
104
     *
105
     * @return Field
106
     */
107
    public function field($name)
108
    {
109
        return $this->fields->get($name);
110
    }
111
112
    /**
113
     * Get the fields value as an associative array.
114
     * By default a nested array is returned.
115
     * Passing true as the first parameter, a flat
116
     * array will be returned, with dotted offsets
117
     * as the keys.
118
     *
119
     * @param bool $flat
120
     *
121
     * @return array
122
     */
123
    public function toArray($flat = false)
124
    {
125
        return $this->fields()->toArray($flat);
126
    }
127
128
    /**
129
     * Required by DelegatedArrayTrait.
130
     *
131
     * @return FieldCollection
132
     */
133
    protected function getDelegatedStorage()
134
    {
135
        return $this->fields;
136
    }
137
138
    /**
139
     * Sets the fields values back to the models.
140
     */
141
    public function writeSource()
142
    {
143
        foreach ($this->fields as $name => $field) {
144
            $this->source[$name] = $field->cast();
145
        }
146
    }
147
148
    /**
149
     * Fills the form with the values coming from the DB
150
     * and HTTP input.
151
     */
152
    public function populate()
153
    {
154
        $this->fromSource();
155
        $this->fromInput();
156
    }
157
158
    public function fromSource()
159
    {
160
        return $this->from($this->source);
161
    }
162
163
    public function fromInput()
164
    {
165
        $request = method_exists($request = \Request::instance(), 'all')
166
            ? $request
167
            : \Input::instance();
168
169
        return $this->from($request->all());
170
    }
171
172
    public function from($source)
173
    {
174
        if (!$source instanceof AbstractDataSource) {
175
            $source = DataSource::make($source);
176
        }
177
178
        return $this->fields->from($source);
179
    }
180
181
    /**
182
     * Saves the models back to the database.
183
     */
184
    public function save()
185
    {
186
        return $this->source->save();
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class Tacone\DataSource\AbstractDataSource as the method save() does only exist in the following sub-classes of Tacone\DataSource\AbstractDataSource: Tacone\DataSource\AbstractEloquentDataSource, Tacone\DataSource\EloquentCollectionDataSource, Tacone\DataSource\EloquentModelDataSource. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
187
    }
188
189
    /**
190
     * Validates the form, then sets eventual errors on each field.
191
     *
192
     * @return mixed
193
     */
194
    public function validate()
195
    {
196
        $arguments = func_get_args();
197
198
        return call_user_func_array([$this->fields, 'validate'], $arguments);
199
    }
200
201
    /**
202
     * @return mixed
203
     */
204
    public function errors()
205
    {
206
        $arguments = func_get_args();
207
208
        return call_user_func_array([$this->fields, 'errors'], $arguments);
209
    }
210
211
    /**
212
     * Convert the object to its JSON representation.
213
     *
214
     * @param int $options
215
     *
216
     * @return string
217
     */
218
    public function toJson($options = 0)
219
    {
220
        if ($this->auto) {
221
            $this->process();
222
        }
223
224
        return json_encode(
225
            $this->toArray(),
226
            JSON_UNESCAPED_SLASHES
227
        );
228
    }
229
230
    protected function getKey()
231
    {
232
        $parameters = \Route::current()->parameters();
233
234
        return reset($parameters);
235
    }
236
237
    protected function process()
238
    {
239
        $key = $this->getKey();
240
        $this->load($key);
0 ignored issues
show
The call to Endpoint::load() has too many arguments starting with $key.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
241
        $this->fromSource();
242
243
        if (\Request::method() == 'GET') {
244
            if (!$key) {
245
                App::abort(404);
246
            }
247
        } elseif (\Request::method() == 'POST') {
248
            $this->fromInput();
249
            if ($this->validate()) {
250
                $this->writeSource();
251
                $this->save();
252
253
                // we need to update the ID and timestamps
254
                $this->fromSource();
255
            } else {
256
                // TODO: move this to the middleware
257
                // HTTP_UNPROCESSABLE_ENTITY
258
                App::abort(422, $this->errors());
259
            }
260
        } elseif (\Request::method() == 'DELETE') {
261
                $this->source->unwrap()->delete();
262
        }
263
    }
264
265
    protected function load()
266
    {
267
        $key = $this->getKey();
268
        if ($key) {
269
            $instance = $this->source->unwrap()->find($key) or App::abort(404);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
270
            $this->initSource($instance);
271
        }
272
    }
273
}
274