Issues (10)

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/Internal/Promise.php (6 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 Http\Adapter\Artax\Internal;
4
5
use Amp\Promise as AmpPromise;
6
use Http\Client\Exception;
7
use Http\Promise\Promise as HttpPromise;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * Promise adapter between artax and php-http, which allow to use the sendAsyncRequest and also the coroutine system by still respecting the Amp Promise.
12
 *
13
 * @internal
14
 */
15
class Promise implements HttpPromise, AmpPromise
16
{
17
    /** @var string */
18
    private $state = HttpPromise::PENDING;
19
20
    /** @var ResponseInterface */
21
    private $response;
22
23
    /** @var Exception */
24
    private $exception;
25
26
    /** @var AmpPromise */
27
    private $promise;
28
29
    /** @var callable|null */
30
    private $onFulfilled;
31
32
    /** @var callable|null */
33
    private $onRejected;
34
35
    /**
36
     * @param AmpPromise $promise Underlying amp promise which MUST resolve with a ResponseInterface or MUST fail with a Http\Client\Exception
37
     */
38
    public function __construct(AmpPromise $promise)
39
    {
40
        $this->promise = $promise;
41
        $this->promise->onResolve(function ($error, $result) {
42
            if (null !== $error) {
43
                if (!$error instanceof Exception) {
44
                    $error = new Exception\TransferException($error->getMessage(), 0, $error);
45
                }
46
47
                $this->reject($error);
48
            } else {
49
                if (!$result instanceof ResponseInterface) {
50
                    $this->reject(new Exception\TransferException('Bad reponse returned'));
51
                } else {
52
                    $this->resolve($result);
53
                }
54
            }
55
        });
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function then(callable $onFulfilled = null, callable $onRejected = null)
62
    {
63
        $deferred = new \Amp\Deferred();
64
        $newPromise = new self($deferred->promise());
65
66
        $onFulfilled = $onFulfilled ?? function (ResponseInterface $response) {
67
            return $response;
68
        };
69
70
        $onRejected = $onRejected ?? function (\Throwable $exception) {
71
            if (!$exception instanceof Exception) {
72
                $exception = new Exception\TransferException($exception->getMessage(), 0, $exception);
73
            }
74
75
            throw $exception;
76
        };
77
78 View Code Duplication
        $this->onFulfilled = function (ResponseInterface $response) use ($onFulfilled, $deferred) {
0 ignored issues
show
This code seems to be duplicated across 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...
79
            try {
80
                $deferred->resolve($onFulfilled($response));
81
            } catch (Exception $exception) {
82
                $deferred->fail($exception);
0 ignored issues
show
$exception is of type object<Http\Client\Exception>, but the function expects a object<Throwable>.

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...
83
            } catch (\Throwable $error) {
84
                $deferred->fail(new Exception\TransferException($error->getMessage(), 0, $error));
85
            }
86
        };
87
88 View Code Duplication
        $this->onRejected = function (Exception $exception) use ($onRejected, $deferred) {
0 ignored issues
show
The parameter $exception is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
This code seems to be duplicated across 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...
89
            try {
90
                $deferred->resolve($onRejected($exception));
91
            } catch (Exception $exception) {
92
                $deferred->fail($exception);
0 ignored issues
show
$exception is of type object<Http\Client\Exception>, but the function expects a object<Throwable>.

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...
93
            } catch (\Throwable $error) {
94
                $deferred->fail(new Exception\TransferException($error->getMessage(), 0, $error));
95
            }
96
        };
97
98
        if (HttpPromise::FULFILLED === $this->state) {
99
            $this->resolve($this->response);
100
        }
101
102
        if (HttpPromise::REJECTED === $this->state) {
103
            $this->reject($this->exception);
104
        }
105
106
        return $newPromise;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getState()
113
    {
114
        return $this->state;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function wait($unwrap = true)
121
    {
122
        try {
123
            AmpPromise\wait($this->promise);
124
        } catch (Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
125
        }
126
127
        if ($unwrap) {
128
            if (HttpPromise::REJECTED === $this->getState()) {
129
                throw $this->exception;
130
            }
131
132
            return $this->response;
133
        }
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function onResolve(callable $onResolved)
140
    {
141
        $this->promise->onResolve($onResolved);
142
    }
143
144
    private function resolve(ResponseInterface $response)
145
    {
146
        $this->state = HttpPromise::FULFILLED;
147
        $this->response = $response;
148
        $onFulfilled = $this->onFulfilled;
149
150
        if (null !== $onFulfilled) {
151
            $onFulfilled($response);
152
        }
153
    }
154
155
    private function reject(Exception $exception)
156
    {
157
        $this->state = HttpPromise::REJECTED;
158
        $this->exception = $exception;
159
        $onRejected = $this->onRejected;
160
161
        if (null !== $onRejected) {
162
            $onRejected($exception);
163
        }
164
    }
165
}
166