Issues (24)

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.

src/Api/HttpApi.php (5 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 Happyr\ApiClient\Api;
4
5
use Happyr\ApiClient\Exception\Domain as DomainExceptions;
6
use Happyr\ApiClient\Exception\DomainException;
7
use Happyr\ApiClient\Hydrator\Hydrator;
8
use Happyr\ApiClient\Hydrator\NoopHydrator;
9
use Http\Client\HttpClient;
10
use Http\Message\RequestFactory;
11
use Psr\Http\Message\ResponseInterface;
12
13
/**
14
 * @author Tobias Nyholm <[email protected]>
15
 */
16
abstract class HttpApi
17
{
18
    /**
19
     * @var HttpClient
20
     */
21
    protected $httpClient;
22
23
    /**
24
     * @var Hydrator
25
     */
26
    protected $hydrator;
27
28
    /**
29
     * @var RequestFactory
30
     */
31
    protected $requestFactory;
32
33
    /**
34
     * @param HttpClient     $httpClient
35
     * @param RequestFactory $requestFactory
36
     * @param Hydrator       $hydrator
37
     */
38
    public function __construct(HttpClient $httpClient, Hydrator $hydrator, RequestFactory $requestFactory)
39
    {
40
        $this->httpClient = $httpClient;
41
        $this->requestFactory = $requestFactory;
42
        if (!$hydrator instanceof NoopHydrator) {
43
            $this->hydrator = $hydrator;
44
        }
45
    }
46
47
    /**
48
     * @param ResponseInterface $response
49
     * @param string            $class
50
     *
51
     * @throws \Exception
52
     *
53
     * @return mixed|ResponseInterface
54
     */
55
    protected function hydrateResponse(ResponseInterface $response, $class)
56
    {
57
        if (!$this->hydrator) {
58
            return $response;
59
        }
60
61
        if ($response->getStatusCode() === 204) {
62
            return null;
63
        }
64
65
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
66
            $this->handleErrors($response);
67
        }
68
69
        return $this->hydrator->hydrate($response, $class);
70
    }
71
72
    /**
73
     * Send a GET request with query parameters.
74
     *
75
     * @param string $path           Request path
76
     * @param array  $params         GET parameters
77
     * @param array  $requestHeaders Request Headers
78
     *
79
     * @return ResponseInterface
80
     */
81 View Code Duplication
    protected function httpGet($path, array $params = [], array $requestHeaders = [])
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...
82
    {
83
        if (count($params) > 0) {
84
            $path .= '?'.http_build_query($params);
85
        }
86
87
        return $this->httpClient->sendRequest(
88
            $this->requestFactory->createRequest('GET', $path, $requestHeaders)
89
        );
90
    }
91
92
    /**
93
     * Send a POST request with JSON-encoded parameters.
94
     *
95
     * @param string $path           Request path
96
     * @param array  $params         POST parameters to be JSON encoded
97
     * @param array  $requestHeaders Request headers
98
     *
99
     * @return ResponseInterface
100
     */
101
    protected function httpPost($path, array $params = [], array $requestHeaders = [])
102
    {
103
        $requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
104
105
        return $this->httpPostRaw($path, http_build_query($params), $requestHeaders);
106
    }
107
108
    /**
109
     * Send a POST request with raw data.
110
     *
111
     * @param string       $path           Request path
112
     * @param array|string $body           Request body
113
     * @param array        $requestHeaders Request headers
114
     *
115
     * @return ResponseInterface
116
     */
117
    protected function httpPostRaw($path, $body, array $requestHeaders = [])
118
    {
119
        return $response = $this->httpClient->sendRequest(
0 ignored issues
show
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
120
            $this->requestFactory->createRequest('POST', $path, $requestHeaders, $body)
0 ignored issues
show
It seems like $body defined by parameter $body on line 117 can also be of type array; however, Http\Message\RequestFactory::createRequest() does only seem to accept resource|string|object<P...e\StreamInterface>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
121
        );
122
    }
123
124
    /**
125
     * Send a PUT request with JSON-encoded parameters.
126
     *
127
     * @param string $path           Request path
128
     * @param array  $params         POST parameters to be JSON encoded
129
     * @param array  $requestHeaders Request headers
130
     *
131
     * @return ResponseInterface
132
     */
133 View Code Duplication
    protected function httpPut($path, array $params = [], array $requestHeaders = [])
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...
134
    {
135
        $requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
136
137
        return $this->httpClient->sendRequest(
138
            $this->requestFactory->createRequest('PUT', $path, $requestHeaders, http_build_query($params))
139
        );
140
    }
141
142
    /**
143
     * Send a DELETE request with JSON-encoded parameters.
144
     *
145
     * @param string $path           Request path
146
     * @param array  $params         POST parameters to be JSON encoded
147
     * @param array  $requestHeaders Request headers
148
     *
149
     * @return ResponseInterface
150
     */
151 View Code Duplication
    protected function httpDelete($path, array $params = [], array $requestHeaders = [])
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...
152
    {
153
        $requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
154
155
        return $this->httpClient->sendRequest(
156
            $this->requestFactory->createRequest('DELETE', $path, $requestHeaders, http_build_query($params))
157
        );
158
    }
159
160
    /**
161
     * Handle HTTP errors.
162
     *
163
     * Call is controlled by the specific API methods.
164
     *
165
     * @param ResponseInterface $response
166
     *
167
     * @throws DomainException
168
     */
169
    protected function handleErrors(ResponseInterface $response)
170
    {
171
        $statusCode = $response->getStatusCode();
172
        switch ($statusCode) {
173
            case 400:
174
                throw DomainExceptions\HttpClientException::badRequest($response);
175
            case 401:
176
                throw DomainExceptions\HttpClientException::unauthorized($response);
177
            case 402:
178
                throw DomainExceptions\HttpClientException::requestFailed($response);
179
            case 404:
180
                throw DomainExceptions\HttpClientException::notFound($response);
181
            case 404 < $statusCode && $statusCode < 500:
182
                throw new DomainExceptions\HttpClientException('Client error', $statusCode, $response);
183
            case 500 <= $statusCode:
184
                throw DomainExceptions\HttpServerException::serverError($statusCode);
185
            default:
186
                throw new DomainExceptions\UnknownErrorException('Failed to hydrate response from API.', $statusCode);
187
        }
188
    }
189
}
190