Issues (56)

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/Traits/Rest.php (4 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 WeDevBr\Bankly\Traits;
4
5
use Illuminate\Http\Client\RequestException;
6
use Illuminate\Support\Facades\Http;
7
use Ramsey\Uuid\Uuid;
8
use WeDevBr\Bankly\Auth\Auth;
9
use WeDevBr\Bankly\Inputs\DocumentAnalysis;
10
11
/**
12
 * Trait Rest
13
 * @author Rafael Teixeira <[email protected]>
14
 * @package WeDevBr\Bankly
15
 */
16
trait Rest
17
{
18
    /** @var array */
19
    protected $headers = [];
20
21
    /** @var string */
22
    protected $apiUrl;
23
24
    /** @var string */
25
    protected $apiVersion = '1.0';
26
27
    /**
28
     * @param string $apiUrl
29
     * @return self
30
     */
31
    public function setApiUrl(string $apiUrl)
32
    {
33
        $this->apiUrl = $apiUrl;
34
        return $this;
35
    }
36
37
    /**
38
     * @param string $apiVersion
39
     * @return self
40
     */
41
    public function setApiVersion(string $apiVersion)
42
    {
43
        $this->apiVersion = $apiVersion;
44
        return $this;
45
    }
46
47
    /**
48
     * @param array $header
49
     * @return void
50
     */
51
    public function setHeaders($header)
52
    {
53
        $this->headers = array_merge($this->headers, $header);
54
    }
55
56
    /**
57
     * @param string $endpoint
58
     * @return bool
59
     */
60
    private function requireCorrelationId(string $endpoint)
61
    {
62
        $not_required_endpoints = [
63
            '/banklist',
64
            '/connect/token'
65
        ];
66
67
        return !in_array($endpoint, $not_required_endpoints);
68
    }
69
70
    /**
71
     * @param string $endpoint
72
     * @param array|string|null $query
73
     * @param string|null $correlationId
74
     * @return array|mixed
75
     * @throws RequestException
76
     */
77
    public function get(string $endpoint, $query = null, $correlationId = null)
78
    {
79
        if (is_null($correlationId) && $this->requireCorrelationId($endpoint)) {
80
            $correlationId = Uuid::uuid4()->toString();
81
        }
82
83
        $this->setHeaders([
84
            'API-Version' => $this->apiVersion,
85
            'x-correlation-id' => $correlationId
86
        ]);
87
88
        $token = Auth::login()->getToken();
89
        return Http::withToken($token)
90
            ->withHeaders($this->headers)
91
            ->get($this->getFinalUrl($endpoint), $query)
92
            ->throw()
93
            ->json();
94
    }
95
96
    /**
97
     * @param string $endpoint
98
     * @param array $body
99
     * @param string|null $correlationId
100
     * @param bool $asJson
101
     * @return array|mixed
102
     * @throws RequestException
103
     */
104 View Code Duplication
    private function post(string $endpoint, array $body = [], $correlationId = null, bool $asJson = false)
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...
105
    {
106
        if (is_null($correlationId) && $this->requireCorrelationId($endpoint)) {
107
            $correlationId = Uuid::uuid4()->toString();
108
        }
109
110
        $this->setHeaders([
111
            'API-Version' => $this->apiVersion,
112
            'x-correlation-id' => $correlationId
113
        ]);
114
115
        $bodyFormat = $asJson ? 'json' : 'form_params';
116
        $token = Auth::login()->getToken();
117
118
        return Http::withToken($token)
119
            ->withHeaders($this->headers)
120
            ->bodyFormat($bodyFormat)
121
            ->post($this->getFinalUrl($endpoint), $body)
122
            ->throw()
123
            ->json();
124
    }
125
126
    /**
127
     * @param string $endpoint
128
     * @param array $body
129
     * @param string|null $correlationId
130
     * @param bool $asJson
131
     * @param bool $attachment
132
     * @param DocumentAnalysis $document
133
     * @return array|mixed
134
     * @throws RequestException
135
     */
136
    private function put(
0 ignored issues
show
This method is not used, and could be removed.
Loading history...
137
        string $endpoint,
138
        array $body = [],
139
        $correlationId = null,
140
        bool $asJson = false,
141
        bool $attachment = false,
142
        DocumentAnalysis $document = null
143
    ) {
144
        if (is_null($correlationId) && $this->requireCorrelationId($endpoint)) {
145
            $correlationId = Uuid::uuid4()->toString();
146
        }
147
148
        $this->setHeaders([
149
            'API-Version' => $this->apiVersion,
150
            'x-correlation-id' => $correlationId
151
        ]);
152
153
        $bodyFormat = $asJson ? 'json' : 'form_params';
154
        $token = Auth::login()->getToken();
155
156
        $request = Http::withToken($token)
157
            ->withHeaders($this->headers)
158
            ->bodyFormat($bodyFormat);
159
160
        if ($attachment && !is_null($document)) {
161
            $request->attach($document->getFieldName(), $document->getFileContents(), $document->getFileName());
162
        }
163
164
        return $request->put($this->getFinalUrl($endpoint), $body)
165
            ->throw()
166
            ->json();
167
    }
168
169
    /**
170
     * @param string $endpoint
171
     * @param array $body
172
     * @param string|null $correlationId
173
     * @param bool $asJson
174
     * @return array|mixed
175
     * @throws RequestException
176
     */
177 View Code Duplication
    private function patch(
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...
178
        string $endpoint,
179
        array $body = [],
180
        $correlationId = null,
181
        bool $asJson = false
182
    ) {
183
        if (is_null($correlationId) && $this->requireCorrelationId($endpoint)) {
184
            $correlationId = Uuid::uuid4()->toString();
185
        }
186
187
        $this->setHeaders([
188
            'API-Version' => $this->apiVersion,
189
            'x-correlation-id' => $correlationId
190
        ]);
191
192
        $bodyFormat = $asJson ? 'json' : 'form_params';
193
        $token = Auth::login()->getToken();
194
195
        $request = Http::withToken($token)
196
            ->withHeaders($this->headers)
197
            ->bodyFormat($bodyFormat);
198
199
        return $request->patch($this->getFinalUrl($endpoint), $body)
200
            ->throw()
201
            ->json();
202
    }
203
204
    /**
205
     * Http delete method.
206
     *
207
     * @param string $endpoint
208
     * @return array|mixed
209
     * @throws RequestException
210
     */
211
    private function delete(string $endpoint)
0 ignored issues
show
This method is not used, and could be removed.
Loading history...
212
    {
213
        $token = Auth::login()->getToken();
214
        $request = Http::withToken($token)
215
            ->withHeaders($this->headers);
216
217
        return $request->delete($this->getFinalUrl($endpoint))
218
            ->throw()
219
            ->json();
220
    }
221
222
    /**
223
     * @param string $endpoint
224
     * @return string
225
     */
226
    private function getFinalUrl(string $endpoint)
227
    {
228
        if (is_null($this->apiUrl)) {
229
            $this->apiUrl = config('bankly')['api_url'];
230
        }
231
232
        return $this->apiUrl . $endpoint;
233
    }
234
}
235