Issues (9)

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/Http/Client.php (1 issue)

Labels
Severity

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 Facade\FlareClient\Http;
4
5
use Facade\FlareClient\Http\Exceptions\BadResponseCode;
6
use Facade\FlareClient\Http\Exceptions\InvalidData;
7
use Facade\FlareClient\Http\Exceptions\MissingParameter;
8
use Facade\FlareClient\Http\Exceptions\NotFound;
9
10
class Client
11
{
12
    /** @var null|string */
13
    private $apiToken;
14
15
    /** @var null|string */
16
    private $apiSecret;
17
18
    /** @var string */
19
    private $baseUrl;
20
21
    /** @var int */
22
    private $timeout;
23
24
    public function __construct(
25
        ?string $apiToken,
26
        ?string $apiSecret,
27
        string $baseUrl = 'https://flareapp.io/api',
28
        int $timeout = 10
29
    ) {
30
        $this->apiToken = $apiToken;
31
32
        $this->apiSecret = $apiSecret;
33
34
        if (! $baseUrl) {
35
            throw MissingParameter::create('baseUrl');
36
        }
37
38
        $this->baseUrl = $baseUrl;
39
40
        if (! $timeout) {
41
            throw MissingParameter::create('timeout');
42
        }
43
44
        $this->timeout = $timeout;
45
    }
46
47
    /**
48
     * @param string $url
49
     * @param array  $arguments
50
     *
51
     * @return array|false
52
     */
53
    public function get(string $url, array $arguments = [])
54
    {
55
        return $this->makeRequest('get', $url, $arguments);
56
    }
57
58
    /**
59
     * @param string $url
60
     * @param array  $arguments
61
     *
62
     * @return array|false
63
     */
64
    public function post(string $url, array $arguments = [])
65
    {
66
        return $this->makeRequest('post', $url, $arguments);
67
    }
68
69
    /**
70
     * @param string $url
71
     * @param array  $arguments
72
     *
73
     * @return array|false
74
     */
75
    public function patch(string $url, array $arguments = [])
76
    {
77
        return $this->makeRequest('patch', $url, $arguments);
78
    }
79
80
    /**
81
     * @param string $url
82
     * @param array  $arguments
83
     *
84
     * @return array|false
85
     */
86
    public function put(string $url, array $arguments = [])
87
    {
88
        return $this->makeRequest('put', $url, $arguments);
89
    }
90
91
    /**
92
     * @param string $method
93
     * @param array  $arguments
94
     *
95
     * @return array|false
96
     */
97
    public function delete(string $method, array $arguments = [])
98
    {
99
        return $this->makeRequest('delete', $method, $arguments);
100
    }
101
102
    /**
103
     * @param string $httpVerb
104
     * @param string $url
105
     * @param array $arguments
106
     *
107
     * @return array
108
     */
109
    private function makeRequest(string $httpVerb, string $url, array $arguments = [])
110
    {
111
        $queryString = http_build_query([
112
            'key' => $this->apiToken,
113
            'secret' => $this->apiSecret,
114
        ]);
115
116
        $fullUrl = "{$this->baseUrl}/{$url}?{$queryString}";
117
118
        $headers = [
119
            'x-api-token: '.$this->apiToken,
120
        ];
121
122
        $response = $this->makeCurlRequest($httpVerb, $fullUrl, $headers, $arguments);
123
124
        if ($response->getHttpResponseCode() === 422) {
125
            throw InvalidData::createForResponse($response);
126
        }
127
128
        if ($response->getHttpResponseCode() === 404) {
129
            throw NotFound::createForResponse($response);
130
        }
131
132
        if ($response->getHttpResponseCode() !== 200 && $response->getHttpResponseCode() !== 204) {
133
            throw BadResponseCode::createForResponse($response);
134
        }
135
136
        return $response->getBody();
137
    }
138
139
    public function makeCurlRequest(string $httpVerb, string $fullUrl, array $headers = [], array $arguments = []): Response
140
    {
141
        $curlHandle = $this->getCurlHandle($fullUrl, $headers);
142
143
        switch ($httpVerb) {
144
            case 'post':
145
                curl_setopt($curlHandle, CURLOPT_POST, true);
146
                $this->attachRequestPayload($curlHandle, $arguments);
147
                break;
148
149
            case 'get':
150
                curl_setopt($curlHandle, CURLOPT_URL, $fullUrl.'?'.http_build_query($arguments));
151
                break;
152
153
            case 'delete':
154
                curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'DELETE');
155
                break;
156
157
            case 'patch':
158
                curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'PATCH');
159
                $this->attachRequestPayload($curlHandle, $arguments);
160
                break;
161
162
            case 'put':
163
                curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'PUT');
164
                $this->attachRequestPayload($curlHandle, $arguments);
165
                break;
166
        }
167
168
        $body = json_decode(curl_exec($curlHandle), true);
169
        $headers = curl_getinfo($curlHandle);
170
        $error = curl_error($curlHandle);
171
172
        return new Response($headers, $body, $error);
173
    }
174
175
    private function attachRequestPayload(&$curlHandle, array $data)
176
    {
177
        $encoded = json_encode($data);
178
179
        $this->lastRequest['body'] = $encoded;
0 ignored issues
show
The property lastRequest 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...
180
        curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $encoded);
181
    }
182
183
    /**
184
     * @param string $fullUrl
185
     * @param array $headers
186
     *
187
     * @return resource
188
     */
189
    private function getCurlHandle(string $fullUrl, array $headers = [])
190
    {
191
        $curlHandle = curl_init();
192
193
        curl_setopt($curlHandle, CURLOPT_URL, $fullUrl);
194
195
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array_merge([
196
            'Accept: application/json',
197
            'Content-Type: application/json',
198
        ], $headers));
199
200
        curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Laravel/Flare API 1.0');
201
        curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
202
        curl_setopt($curlHandle, CURLOPT_TIMEOUT, $this->timeout);
203
        curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true);
204
        curl_setopt($curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
205
        curl_setopt($curlHandle, CURLOPT_ENCODING, '');
206
        curl_setopt($curlHandle, CURLINFO_HEADER_OUT, true);
207
208
        return $curlHandle;
209
    }
210
}
211