Issues (457)

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/Http/Controllers/UserController.php (11 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
declare(strict_types=1);
3
4
namespace Tfboe\FmLib\Http\Controllers;
5
6
use Doctrine\ORM\EntityManagerInterface;
7
use Illuminate\Contracts\Hashing\Hasher;
8
use Illuminate\Http\JsonResponse;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Auth;
11
use Laravel\Lumen\Application;
12
use Tfboe\FmLib\Entity\UserInterface;
13
use Tfboe\FmLib\Exceptions\AuthenticationException;
14
use Tfboe\FmLib\Service\ObjectCreatorServiceInterface;
15
use Tymon\JWTAuth\Exceptions\JWTException;
16
17
/**
18
 * Class UserController
19
 * @package App\Http\Controllers
20
 */
21
class UserController extends BaseController
22
{
23
//<editor-fold desc="Public Methods">
24
25
  /** @var ObjectCreatorServiceInterface $objectCreatorService */
26
  private $objectCreatorService;
27
28
  /**
29
   * @inheritDoc
30
   */
31
  public function __construct(EntityManagerInterface $entityManager,
32
                              ObjectCreatorServiceInterface $objectCreatorService)
33
  {
34
    parent::__construct($entityManager);
35
    $this->objectCreatorService = $objectCreatorService;
36
  }
37
38
39
  /**
40
   * login action, checks credentials and returns token
41
   * @param Request $request the http request
42
   * @param Application $app
43
   * @return JsonResponse
44
   * @throws AuthenticationException wrong credentials or errors during creating a token
45
   */
46
  public function login(Request $request, Application $app): JsonResponse
47
  {
48
    $specification = $this->getCredentialSpecification($app);
49
    $this->addAdditionalLoginSpecifications($specification);
50
    $this->validateBySpecification($request, $specification);
51
52
53
    // grab credentials from the request
54
    $credentials = $request->only('email', 'password');
55
56
    /** @var string $token */
57
    $token = null;
58
    try {
59
      // attempt to verify the credentials and create a token for the user
60
      $token = Auth::attempt($credentials);
61
      if (!$token) {
62
        throw new AuthenticationException('invalid credentials');
63
      }
64
    } /** @noinspection PhpRedundantCatchClauseInspection */ catch (JWTException $e) {
65
      // something went wrong whilst attempting to encode the token
66
      throw new AuthenticationException('could not create token');
67
    }
68
    return $this->getLoginResponse($request, $token);
0 ignored issues
show
$token is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
70
  }
71
72
  /**
73
   * register action, registers a new user with email and password
74
   *
75
   * @param Request $request the http request
76
   * @param Application $app
77
   * @return JsonResponse
78
   */
79
  public function register(Request $request, Application $app): JsonResponse
80
  {
81
    $userClass = config('fm-lib')['entityMaps']['Tfboe\FmLib\Entity\UserInterface'];
82
    $specification = [];
83
    $specification['user'] = $this->getCredentialSpecification($app);
84
    $specification['user']['email']['validation'] .= '|unique:' . $userClass . ',email';
85
    $specification['user']['confirmedAGBVersion'] = ['validation' => 'integer-type|integer|min:0'];
86
87
    $this->addAdditionalRegisterSpecifications($specification);
88
89
    $this->validateBySpecification($request, array_merge(...array_values($specification)));
90
91
    $input = $request->input();
92
    /** @var UserInterface $user */
93
94
    $user = $this->setFromSpecification($this->newUser(), $specification['user'], $input);
95
    $this->getEntityManager()->persist($user); //sets the user id
96
97
    $this->createAdditionalRegisterEntities($user, $specification, $input);
98
99
    $this->getEntityManager()->flush();
100
101
    return $this->getRegisterResponse($request, $app, $user);
102
  }
103
104
  /**
105
   * Creates a new user
106
   * @return UserInterface
107
   */
108
  protected function newUser(): UserInterface
109
  {
110
    return $this->objectCreatorService->createObjectFromInterface(UserInterface::class);
111
  }
112
113
  /**
114
   * Gets the response for a successful register action
115
   * @param Request $request the request
116
   * @param Application $app the application
117
   * @param UserInterface $user the newly registered user
118
   * @return JsonResponse the json response
119
   */
120
  protected function getRegisterResponse(/** @noinspection PhpUnusedParameterInspection */
121
    Request $request, /** @noinspection PhpUnusedParameterInspection */
0 ignored issues
show
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
    Application $app, UserInterface $user)
0 ignored issues
show
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
  {
124
    return response()->json(['id' => $user->getId()]);
0 ignored issues
show
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
125
  }
126
127
  /**
128
   * @return JsonResponse
129
   */
130
  public function userId(): JsonResponse
131
  {
132
    /** @noinspection PhpUnhandledExceptionInspection */
133
    return response()->json(['id' => Auth::user()->getAuthIdentifier()]);
0 ignored issues
show
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
134
  }
135
//</editor-fold desc="Public Methods">
136
137
//<editor-fold desc="Protected Methods">
138
  /**
139
   * Gets additional input specifications for the login action
140
   * @param array $specification the specification to add to / modify
141
   */
142
  protected function addAdditionalLoginSpecifications(array &$specification)
0 ignored issues
show
The parameter $specification is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
  {
144
    //do nothing by default
145
  }
146
147
  /**
148
   * adds additional register specifications
149
   * @param array $specification the specification to add to / modify
150
   */
151
  protected function addAdditionalRegisterSpecifications(array &$specification)
0 ignored issues
show
The parameter $specification is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
152
  {
153
    //do nothing by default
154
  }
155
156
  /**
157
   * creates additional entities after registration using the specification and the given input
158
   * @param UserInterface $user the newly registered user
159
   * @param array $specification the specification
160
   * @param array $input the given request input
161
   */
162
  protected function createAdditionalRegisterEntities(UserInterface $user, array $specification, array $input)
0 ignored issues
show
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $specification is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
163
  {
164
    //do nothing by default
165
  }
166
167
  /**
168
   * Gets the response for a successful login action
169
   * @param Request $request the request
170
   * @param string $token the login token
171
   * @return JsonResponse the response
172
   */
173
  protected function getLoginResponse(Request $request, string $token): JsonResponse
174
  {
175
    $user = $request->user();
176
    return response()->json(['id' => $user->getId()], 200, ['jwt-token' => $token]);
0 ignored issues
show
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
177
  }
178
//</editor-fold desc="Protected Methods">
179
180
//<editor-fold desc="Private Methods">
181
  /**
182
   * Gets the specification for the login credentials
183
   * @param Application $app
184
   * @return array
185
   */
186
  private function getCredentialSpecification(Application $app)
187
  {
188
    /** @var Hasher $hasher */
189
    return [
190
      'email' => ['validation' => 'required|email'],
191
      'password' => ['validation' => 'required|string|min:8',
192
        'transformer' => function ($value) use ($app) {
193
          return $app['hash']->make($value);
194
        }]
195
    ];
196
  }
197
//</editor-fold desc="Private Methods">
198
}
199