Issues (8)

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/AbstractClient.php (3 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
/*
4
 * This file is part of Caspeco.
5
 *
6
 (c) HOY Multimedia AB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Hoy\Caspeco\Http;
13
14
use GuzzleHttp\Client;
15
use GuzzleHttp\Exception\RequestException;
16
use GuzzleHttp\Psr7\Request;
17
use Hoy\Caspeco\Exceptions\AuthenticationException;
18
use Hoy\Caspeco\Exceptions\HtmlException;
19
use Hoy\Caspeco\Exceptions\HttpException;
20
use Hoy\Caspeco\Exceptions\ValidationException;
21
use Psr\Http\Message\RequestInterface;
22
use Stringy\Stringy;
23
24
/**
25
 * This is the abstract client class.
26
 *
27
 * @author Vincent Klaiber <[email protected]>
28
 */
29
abstract class AbstractClient
30
{
31
    /**
32
     * Create a new abstract api instance.
33
     *
34
     * @param array $config
35
     */
36
    public function __construct(array $config)
37
    {
38
        $this->client = new Client(['base_uri' => $config['url']]);
0 ignored issues
show
The property client 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...
39
        $this->signature = new Signature($config['id'], $config['secret'], $config['url']);
0 ignored issues
show
The property signature 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...
40
    }
41
42
    /**
43
     * Make a get request.
44
     *
45
     * @param string $uri
46
     * @param array $options
47
     *
48
     * @return mixed|\Psr\Http\Message\ResponseInterface
49
     */
50
    public function get($uri, array $options = [])
51
    {
52
        return $this->request(__FUNCTION__, $uri, $options);
53
    }
54
55
    /**
56
     * Make a post request.
57
     *
58
     * @param string $uri
59
     * @param array $options
60
     *
61
     * @return mixed|\Psr\Http\Message\ResponseInterface
62
     */
63
    public function post($uri, array $options = [])
64
    {
65
        return $this->request(__FUNCTION__, $uri, $options);
66
    }
67
68
    /**
69
     * Make a put request.
70
     *
71
     * @param string $uri
72
     * @param array $options
73
     *
74
     * @return mixed|\Psr\Http\Message\ResponseInterface
75
     */
76
    public function put($uri, array $options = [])
77
    {
78
        return $this->request(__FUNCTION__, $uri, $options);
79
    }
80
81
    /**
82
     * Make a delete request.
83
     *
84
     * @param string $uri
85
     * @param array $options
86
     *
87
     * @return mixed|\Psr\Http\Message\ResponseInterface
88
     */
89
    public function delete($uri, array $options = [])
90
    {
91
        return $this->request(__FUNCTION__, $uri, $options);
92
    }
93
94
    /**
95
     * Sign and send request.
96
     *
97
     * @param string $method
98
     * @param string $uri
99
     * @param array $options
100
     *
101
     * @return mixed
102
     */
103
    protected function request($method, $uri, array $options = [])
104
    {
105
        $uri = $this->buildUriFromString($uri);
106
107
        $body = isset($options['json']) ? json_encode($options['json']) : null;
108
109
        $request = new Request($method, $uri, [], $body);
110
        $request = $this->signature->sign($request);
111
112
        $options['headers'] = $request->getHeaders();
113
114
        return $this->send($request, $options);
115
    }
116
117
    /**
118
     * Send the given request.
119
     *
120
     * @param \Psr\Http\Message\RequestInterface $request
121
     * @param array $options
122
     *
123
     * @throws \GuzzleHttp\Exception\RequestException
124
     * @throws \Hoy\Caspeco\Exceptions\HttpException
125
     *
126
     * @return mixed
127
     */
128
    protected function send(RequestInterface $request, array $options = [])
129
    {
130
        try {
131
            $response = $this->client->send($request, $options);
132
133
            $content = $response->getBody()->getContents();
134
            $body = json_decode($content);
135
136
            if (property_exists($body, 'Message')) {
137
                throw new ValidationException(400, $this->formatJsonError($content));
138
            }
139
140
            return $body;
141
        } catch (RequestException $exception) {
142
            $this->handleException($exception);
143
        }
144
    }
145
146
    /**
147
     * Handle Guzzle exception.
148
     *
149
     * @param \GuzzleHttp\Exception\RequestException $exception
150
     *
151
     * @throws \Hoy\Caspeco\Exceptions\HttpException
152
     */
153
    protected function handleException(RequestException $exception)
154
    {
155
        $code = $exception->getResponse()->getStatusCode();
156
        $message = $exception->getResponse()->getBody()->getContents();
157
158
        if (Stringy::create($message)->isJson()) {
159
            throw new HttpException($exception->getResponse()->getStatusCode(), $this->formatJsonError($message));
160
        }
161
162
        if (Stringy::create($message)->contains('html')) {
163
            throw new HtmlException($exception->getResponse()->getStatusCode(), $message);
164
        }
165
166
        throw new AuthenticationException($code, $message);
167
    }
168
169
    /**
170
     * Build API uri from string.
171
     *
172
     * @param string $uri
173
     *
174
     * @return string
175
     */
176
    protected function buildUriFromString($uri)
177
    {
178
        return $uri = '/v1/'.$uri.'/';
0 ignored issues
show
$uri 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...
179
    }
180
181
    /**
182
     * Format JSON error message.
183
     *
184
     * @param string $json
185
     *
186
     * @return string
187
     */
188
    protected function formatJsonError($json)
189
    {
190
        $error = json_decode($json);
191
192
        return sprintf('%s (%s)', $error->Message, $error->Code);
193
    }
194
}
195