Issues (287)

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/API/MAL/MALTrait.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 declare(strict_types=1);
2
/**
3
 * Anime List Client
4
 *
5
 * An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
6
 *
7
 * PHP version 7
8
 *
9
 * @package     AnimeListClient
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2015 - 2017  Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @version     4.0
14
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
15
 */
16
17
namespace Aviat\AnimeClient\API\MAL;
18
19
use Amp\Artax\{Client, FormBody, Request};
20
use Aviat\AnimeClient\API\{
21
	MAL as M,
22
	APIRequestBuilder,
23
	XML
24
};
25
use Aviat\AnimeClient\API\MALRequestBuilder;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Aviat\AnimeClient\API\MAL\MALRequestBuilder.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
26
use Aviat\Ion\Json;
27
use InvalidArgumentException;
28
29
trait MALTrait {
30
31
	/**
32
	 * The request builder for the MAL API
33
	 * @var MALRequestBuilder
34
	 */
35
	protected $requestBuilder;
36
37
	/**
38
	 * The base url for api requests
39
	 * @var string $base_url
40
	 */
41
	protected $baseUrl = M::BASE_URL;
42
43
	/**
44
	 * HTTP headers to send with every request
45
	 *
46
	 * @var array
47
	 */
48
	protected $defaultHeaders = [
49
		'Accept' => 'text/xml',
50
		'Accept-Encoding' => 'gzip',
51
		'Content-type' => 'application/x-www-form-urlencoded',
52
		'User-Agent' => "Tim's Anime Client/4.0"
53
	];
54
55
	/**
56
	 * Set the request builder object
57
	 *
58
	 * @param MALRequestBuilder $requestBuilder
59
	 * @return self
60
	 */
61
	public function setRequestBuilder($requestBuilder): self
62
	{
63
		$this->requestBuilder = $requestBuilder;
64
		return $this;
65
	}
66
67
	/**
68
	 * Unencode the dual-encoded ampersands in the body
69
	 *
70
	 * This is a dirty hack until I can fully track down where
71
	 * the dual-encoding happens
72
	 *
73
	 * @param FormBody $formBody The form builder object to fix
0 ignored issues
show
Should the type for parameter $formBody not be \Amp\Artax\FormBody?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
74
	 * @return string
75
	 */
76
	private function fixBody(FormBody $formBody): string
0 ignored issues
show
This method is not used, and could be removed.
Loading history...
77
	{
78
		$rawBody = \Amp\wait($formBody->getBody());
79
		return html_entity_decode($rawBody, \ENT_HTML5, 'UTF-8');
80
	}
81
82
	/**
83
	 * Create a request object
84
	 *
85
	 * @param string $type
86
	 * @param string $url
87
	 * @param array $options
88
	 * @return \Amp\Artax\Response
89
	 */
90
	public function setUpRequest(string $type, string $url, array $options = [])
91
	{
92
		$config = $this->container->get('config');
0 ignored issues
show
The property container 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...
93
94
		$request = $this->requestBuilder
95
			->newRequest($type, $url)
96
			->setBasicAuth($config->get(['mal','username']), $config->get(['mal','password']));
97
98
		if (array_key_exists('query', $options))
99
		{
100
			$request->setQuery($options['query']);
101
		}
102
103
		if (array_key_exists('body', $options))
104
		{
105
			$request->setBody($options['body']);
106
		}
107
108
		return $request->getFullRequest();
109
	}
110
111
	/**
112
	 * Make a request
113
	 *
114
	 * @param string $type
115
	 * @param string $url
116
	 * @param array $options
117
	 * @return \Amp\Artax\Response
118
	 */
119
	private function getResponse(string $type, string $url, array $options = [])
120
	{
121
		$logger = null;
122
		if ($this->getContainer())
0 ignored issues
show
It seems like getContainer() 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...
123
		{
124
			$logger = $this->container->getLogger('mal-request');
125
		}
126
127
		$request = $this->setUpRequest($type, $url, $options);
128
		$response = \Amp\wait((new Client)->request($request));
129
130
		$logger->debug('MAL api response', [
131
			'status' => $response->getStatus(),
132
			'reason' => $response->getReason(),
133
			'body' => $response->getBody(),
134
			'headers' => $response->getAllHeaders(),
135
			'requestHeaders' => $request->getAllHeaders(),
136
		]);
137
138
		return $response;
139
	}
140
141
	/**
142
	 * Make a request
143
	 *
144
	 * @param string $type
145
	 * @param string $url
146
	 * @param array $options
147
	 * @return array
148
	 */
149 View Code Duplication
	private function request(string $type, string $url, array $options = []): array
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
	{
151
		$logger = null;
152
		if ($this->getContainer())
0 ignored issues
show
It seems like getContainer() 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...
153
		{
154
			$logger = $this->container->getLogger('mal-request');
155
		}
156
157
		$response = $this->getResponse($type, $url, $options);
158
159
		if ((int) $response->getStatus() > 299 || (int) $response->getStatus() < 200)
160
		{
161
			if ($logger)
162
			{
163
				$logger->warning('Non 200 response for api call', $response->getBody());
164
			}
165
		}
166
167
		return XML::toArray((string) $response->getBody());
168
	}
169
170
	/**
171
	 * Remove some boilerplate for get requests
172
	 *
173
	 * @param array $args
174
	 * @return array
175
	 */
176
	protected function getRequest(...$args): array
177
	{
178
		return $this->request('GET', ...$args);
0 ignored issues
show
$args is of type array<integer,array>, 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...
179
	}
180
181
	/**
182
	 * Remove some boilerplate for post requests
183
	 *
184
	 * @param array $args
185
	 * @return array
186
	 */
187 View Code Duplication
	protected function postRequest(...$args): array
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
	{
189
		$logger = null;
190
		if ($this->getContainer())
0 ignored issues
show
It seems like getContainer() 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...
191
		{
192
			$logger = $this->container->getLogger('mal-request');
193
		}
194
195
		$response = $this->getResponse('POST', ...$args);
0 ignored issues
show
$args is of type array<integer,array>, 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...
196
		$validResponseCodes = [200, 201];
197
198
		if ( ! in_array((int) $response->getStatus(), $validResponseCodes))
199
		{
200
			if ($logger)
201
			{
202
				$logger->warning('Non 201 response for POST api call', $response->getBody());
203
			}
204
		}
205
206
		return XML::toArray($response->getBody());
207
	}
208
}