Issues (286)

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.

TestsTraits/PhpUnit/TestsAuthHelperTrait.php (2 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 Apiato\Core\Traits\TestsTraits\PhpUnit;
4
5
use App;
6
use App\Containers\Authentication\Tasks\ApiLoginThisUserObjectTask;
7
use App\Containers\User\Models\User;
8
use Artisan;
9
use Illuminate\Support\Facades\Hash;
10
11
/**
12
 * Class TestsAuthHelperTrait.
13
 *
14
 * Tests helper for authentication and authorization.
15
 *
16
 * @author  Mahmoud Zalt <[email protected]>
17
 */
18
trait TestsAuthHelperTrait
19
{
20
21
    /**
22
     * Logged in user object.
23
     *
24
     * @var User
25
     */
26
    protected $testingUser;
27
28
    /**
29
     * Roles and permissions, to be attached on the user
30
     *
31
     * @var array
32
     */
33
    protected $access = [
34
        'permissions' => '',
35
        'roles'       => '',
36
    ];
37
38
    /**
39
     * Try to get the last logged in User, if not found then create new one.
40
     * Note: if $userDetails are provided it will always create new user, even
41
     * if another one was previously created during the execution of your test.
42
     *
43
     * By default Users will be given the Roles and Permissions found int he class
44
     * `$access` property. But the $access parameter can be used to override the
45
     * defined roles and permissions in the `$access` property of your class.
46
     *
47
     * @param null $access      roles and permissions you'd like to provide this user with
48
     * @param null $userDetails what to be attached on the User object
49
     *
50
     * @return  \App\Containers\User\Models\User
51
     */
52
    public function getTestingUser($userDetails = null, $access = null)
53
    {
54
        return is_null($userDetails) ? $this->findOrCreateTestingUser($userDetails, $access)
55
            : $this->createTestingUser($userDetails, $access);
56
    }
57
58
    /**
59
     * Same as `getTestingUser()` but always overrides the User Access
60
     * (roles and permissions) with null. So the user can be used to test
61
     * if unauthorized user tried to access your protected endpoint.
62
     *
63
     * @param null $userDetails
64
     *
65
     * @return  \App\Containers\User\Models\User
66
     */
67
    public function getTestingUserWithoutAccess($userDetails = null)
68
    {
69
        return $this->getTestingUser($userDetails, $this->getNullAccess());
70
    }
71
72
    /**
73
     * @param $userDetails
74
     * @param $access
75
     *
76
     * @return  \App\Containers\User\Models\User
77
     */
78
    private function findOrCreateTestingUser($userDetails, $access)
79
    {
80
        return $this->testingUser ? : $this->createTestingUser($userDetails, $access);
81
    }
82
83
    /**
84
     * @param null $access
85
     * @param null $userDetails
86
     *
87
     * @return  User
88
     */
89
    private function createTestingUser($userDetails = null, $access = null)
90
    {
91
        // "inject" the confirmed status, if userdetails are submitted
92
        if(is_array($userDetails)) {
93
            $defaults = [
94
                'confirmed' => true,
95
            ];
96
97
            $userDetails = array_merge($defaults, $userDetails);
98
        }
99
100
        // create new user
101
        $user = $this->factoryCreateUser($userDetails);
102
103
        // assign user roles and permissions based on the access property
104
        $user = $this->setupTestingUserAccess($user, $access);
105
106
        // authentication the user
107
        $this->actingAs($user, 'api');
0 ignored issues
show
It seems like actingAs() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
108
109
        // set the created user
110
        return $this->testingUser = $user;
111
    }
112
113
    /**
114
     * @param null $userDetails
115
     *
116
     * @return  User
117
     */
118
    private function factoryCreateUser($userDetails = null)
119
    {
120
        return factory(User::class)->create($this->prepareUserDetails($userDetails));
121
    }
122
123
    /**
124
     * @param null $userDetails
125
     *
126
     * @return  array
127
     */
128
    private function prepareUserDetails($userDetails = null)
129
    {
130
        $defaultUserDetails = [
131
            'name'     => $this->faker->name,
0 ignored issues
show
The property faker does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
132
            'email'    => $this->faker->email,
133
            'password' => 'testing-password',
134
        ];
135
136
        // if no user detail provided, use the default details, to find the password or generate one before encoding it
137
        return $this->prepareUserPassword($userDetails ? : $defaultUserDetails);;
138
    }
139
140
    /**
141
     * @param $userDetails
142
     *
143
     * @return  null
144
     */
145
    private function prepareUserPassword($userDetails)
146
    {
147
        // get password from the user details or generate one
148
        $password = isset($userDetails['password']) ? $userDetails['password'] : $this->faker->password;
149
150
        // hash the password and set it back at the user details
151
        $userDetails['password'] = Hash::make($password);
152
153
        return $userDetails;
154
    }
155
156
157
    /**
158
     * @return  array|null
159
     */
160
    private function getAccess()
161
    {
162
        return isset($this->access) ? $this->access : null;
163
    }
164
165
    /**
166
     * @param $user
167
     * @param $access
168
     *
169
     * @return  mixed
170
     */
171
    private function setupTestingUserAccess($user, $access = null)
172
    {
173
        $access = $access ? : $this->getAccess();
174
175
        $user = $this->setupTestingUserPermissions($user, $access);
176
        $user = $this->setupTestingUserRoles($user, $access);
177
178
        return $user;
179
    }
180
181
    /**
182
     * @param $user
183
     * @param $access
184
     *
185
     * @return  mixed
186
     */
187 View Code Duplication
    private function setupTestingUserRoles($user, $access)
188
    {
189
        if (isset($access['roles']) && !empty($access['roles'])) {
190
            if (!$user->hasRole($access['roles'])) {
191
                $user->assignRole($access['roles']);
192
                $user = $user->fresh();
193
            }
194
        }
195
196
        return $user;
197
    }
198
199
    /**
200
     * @param $user
201
     * @param $access
202
     *
203
     * @return  mixed
204
     */
205 View Code Duplication
    private function setupTestingUserPermissions($user, $access)
206
    {
207
        if (isset($access['permissions']) && !empty($access['permissions'])) {
208
            $user->givePermissionTo($access['permissions']);
209
            $user = $user->fresh();
210
        }
211
212
        return $user;
213
    }
214
215
216
    /**
217
     * @return  array
218
     */
219
    private function getNullAccess()
220
    {
221
        return [
222
            'permissions' => null,
223
            'roles'       => null
224
        ];
225
    }
226
227
}
228