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/Internal/AsyncClient.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 mpyw\Coutte\Internal;
4
5
use mpyw\Co\CoInterface;
6
use Symfony\Component\BrowserKit\Client as BaseClient;
7
use Symfony\Component\BrowserKit\Request;
8
use Symfony\Component\DomCrawler\Crawler;
9
use Symfony\Component\DomCrawler\Link;
10
use Symfony\Component\DomCrawler\Form;
11
12
abstract class AsyncClient extends BaseClient
13
{
14
    private $maxRedirectsExtended = -1;
15
    private $redirectCountExtended = 0;
16
    private $isMainRequestExtended = true;
17
18
    /**
19
     * Makes an asynchronous request.
20
     *
21
     * @param Request $request An origin request instance
22
     *
23
     * @return \Generator object An origin response instance
24
     */
25
    abstract protected function doRequestAsync($request);
26
27
    /**
28
     * Unsupported action.
29
     *
30
     * @param bool $insulated
31
     *
32
     * @throws \BadMethodCallException
33
     */
34
    public function insulate($insulated = true)
35
    {
36
        throw new \BadMethodCallException('Unsupported action.');
37
    }
38
39
    /**
40
     * Sets the maximum number of requests that crawler can follow.
41
     *
42
     * @param int $maxRedirects
43
     */
44
    public function setMaxRedirects($maxRedirects)
45
    {
46
        $this->maxRedirectsExtended = $maxRedirects < 0 ? -1 : $maxRedirects;
47
        $this->followRedirects = -1 != $this->maxRedirectsExtended;
48
    }
49
    /**
50
     * Returns the maximum number of requests that crawler can follow.
51
     *
52
     * @return int
53
     */
54
    public function getMaxRedirects()
55
    {
56
        return $this->maxRedirectsExtended;
57
    }
58
59
    /**
60
     * Makes a request from a Request object directly asynchronously.
61
     *
62
     * @param Request $request       A Request instance
63
     * @param bool    $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
64
     *
65
     * @return \Generator Crawler
66
     */
67
    protected function requestFromRequestAsync(Request $request, $changeHistory = true)
68
    {
69
        yield CoInterface::RETURN_WITH => $this->requestAsync($request->getMethod(), $request->getUri(), $request->getParameters(), $request->getFiles(), $request->getServer(), $request->getContent(), $changeHistory);
70
    }
71
72
    /**
73
     * Goes back in the browser history asynchronously.
74
     *
75
     * @return \Generator Crawler
76
     */
77
    public function backAsync()
78
    {
79
        return $this->requestFromRequestAsync($this->history->back(), false);
80
    }
81
82
    /**
83
     * Goes forward in the browser history asynchronously.
84
     *
85
     * @return \Generator
86
     */
87
    public function forwardAsync()
88
    {
89
        return $this->requestFromRequestAsync($this->history->forward(), false);
90
    }
91
92
    /**
93
     * Reloads the current browser asynchronously.
94
     *
95
     * @return \Generator
96
     */
97
    public function reloadAsync()
98
    {
99
        return $this->requestFromRequestAsync($this->history->current(), false);
100
    }
101
102
    /**
103
     * Clicks on a given link asynchronously.
104
     *
105
     * @param Link $link A Link instance
106
     *
107
     * @return \Generator Crawler
108
     */
109
    public function clickAsync(Link $link)
110
    {
111
        return $link instanceof Form
112
                ? $this->submitAsync($link)
113
                : $this->requestAsync($link->getMethod(), $link->getUri());
114
    }
115
116
    /**
117
     * Submits a form asynchronously.
118
     *
119
     * @param Form  $form   A Form instance
120
     * @param array $values An array of form field values
121
     *
122
     * @return \Generator Crawler
123
     */
124
    public function submitAsync(Form $form, array $values = [])
125
    {
126
        $form->setValues($values);
127
        return $this->requestAsync($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles());
128
    }
129
130
    /**
131
     * Calls a URI.
132
     *
133
     * @param string $method        The request method
134
     * @param string $uri           The URI to fetch
135
     * @param array  $parameters    The Request parameters
136
     * @param array  $files         The files
137
     * @param array  $server        The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does)
138
     * @param string $content       The raw body data
139
     * @param bool   $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
140
     *
141
     * @return Crawler
142
     */
143 11
    public function request($method, $uri, array $parameters = [], array $files = [], array $server = [], $content = null, $changeHistory = true)
144 11
    {
145 11
        $uri = $this->beforeRequest($method, $uri, $parameters, $files, $server, $content, $changeHistory);
146 11
        $this->response = $this->doRequest($this->request);
147 10
        $this->afterRequest($uri);
148 10
        if ($this->followRedirects && $this->redirect) {
149
            return $this->crawler = $this->followRedirect();
150
        }
151 10
        return $this->crawler = $this->createCrawlerFromContent($this->internalRequest->getUri(), $this->internalResponse->getContent(), $this->internalResponse->getHeader('Content-Type'));
0 ignored issues
show
It seems like $this->internalResponse-...tHeader('Content-Type') targeting Symfony\Component\BrowserKit\Response::getHeader() can also be of type array; however, Symfony\Component\Browse...ateCrawlerFromContent() does only seem to accept string, maybe add an additional type check?

This check looks at variables that 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...
152
    }
153
154
    /**
155
     * Calls a URI asynchronously.
156
     *
157
     * @param string $method        The request method
158
     * @param string $uri           The URI to fetch
159
     * @param array  $parameters    The Request parameters
160
     * @param array  $files         The files
161
     * @param array  $server        The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does)
162
     * @param string $content       The raw body data
163
     * @param bool   $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
164
     *
165
     * @return \Generator Crawler
166
     */
167 2
    public function requestAsync($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true)
168 2
    {
169 2
        $uri = $this->beforeRequest($method, $uri, $parameters, $files, $server, $content, $changeHistory);
170 2
        $this->response = (yield $this->doRequestAsync($this->request));
171 2
        $this->afterRequest($uri);
172 2
        if ($this->followRedirects && $this->redirect) {
173
            yield CoInterface::RETURN_WITH => $this->crawler = (yield $this->followRedirectAsync());
174
        }
175 2
        yield CoInterface::RETURN_WITH => $this->crawler = $this->createCrawlerFromContent($this->internalRequest->getUri(), $this->internalResponse->getContent(), $this->internalResponse->getHeader('Content-Type'));
0 ignored issues
show
It seems like $this->internalResponse-...tHeader('Content-Type') targeting Symfony\Component\BrowserKit\Response::getHeader() can also be of type array; however, Symfony\Component\Browse...ateCrawlerFromContent() does only seem to accept string, maybe add an additional type check?

This check looks at variables that 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...
176
    }
177
178
    /**
179
     * Follow redirects?
180
     *
181
     * @return Crawler
182
     *
183
     * @throws \LogicException If request was not a redirect
184
     */
185 View Code Duplication
    public function followRedirect()
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...
186
    {
187
        $p = $this->beforeRedirect();
188
        $this->isMainRequestExtended = false;
189
        $response = $this->request($p['method'], $this->redirect, $p['parameters'], $p['files'], $p['server'], $p['content']);
0 ignored issues
show
It seems like $this->redirect can also be of type array or null; however, mpyw\Coutte\Internal\AsyncClient::request() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
190
        $this->isMainRequestExtended = true;
191
        return $response;
192
    }
193
194
    /**
195
     * Follow redirects asynchronously?
196
     *
197
     * @return \Generator Crawler
198
     *
199
     * @throws \LogicException If request was not a redirect
200
     */
201 View Code Duplication
    public function followRedirectAsync()
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...
202
    {
203
        $p = $this->beforeRedirect();
204
        $this->isMainRequestExtended = false;
205
        $response = (yield $this->requestAsync($p['method'], $this->redirect, $p['parameters'], $p['files'], $p['server'], $p['content']));
0 ignored issues
show
It seems like $this->redirect can also be of type array or null; however, mpyw\Coutte\Internal\AsyncClient::requestAsync() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
206
        $this->isMainRequestExtended = true;
207
        yield CoInterface::RETURN_WITH => $response;
208
    }
209
210
    /**
211
     * @param string $method
212
     * @param string $uri
213
     * @param array $parameters
214
     * @param array $server
215
     * @param string|null $content
216
     * @param bool $changeHistory
217
     * @return string
218
     */
219 13
    private function beforeRequest($method, $uri, array $parameters, array $files, array $server, $content, $changeHistory)
220 13
    {
221 13
        if ($this->isMainRequestExtended) {
222 13
            $this->redirectCountExtended = 0;
223
        } else {
224
            ++$this->redirectCountExtended;
225
        }
226 13
        $uri = $this->getAbsoluteUri($uri);
227 13
        $server = array_merge($this->server, $server);
228 13
        if (isset($server['HTTPS'])) {
229
            $uri = preg_replace('{^' . parse_url($uri, PHP_URL_SCHEME) . '}', $server['HTTPS'] ? 'https' : 'http', $uri);
230
        }
231 13
        if (!$this->history->isEmpty()) {
232 2
            $server['HTTP_REFERER'] = $this->history->current()->getUri();
233
        }
234 13
        if (empty($server['HTTP_HOST'])) {
235 13
            $server['HTTP_HOST'] = $this->extractHostExtended($uri);
236
        }
237 13
        $server['HTTPS'] = 'https' === parse_url($uri, PHP_URL_SCHEME);
238 13
        $this->internalRequest = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content);
239 13
        $this->request = $this->filterRequest($this->internalRequest);
240 13
        if ($changeHistory) {
241 13
            $this->history->add($this->internalRequest);
242
        }
243 13
        return $uri;
244
    }
245
246
    /**
247
     * @param string $uri
248
     */
249 12
    private function afterRequest($uri)
250 12
    {
251 12
        $this->internalResponse = $this->filterResponse($this->response);
252 12
        $this->cookieJar->updateFromResponse($this->internalResponse, $uri);
253 12
        $status = $this->internalResponse->getStatus();
254 12
        if ($status >= 300 && $status < 400) {
255
            $this->redirect = $this->internalResponse->getHeader('Location');
256
        } else {
257 12
            $this->redirect = null;
258
        }
259 12
    }
260
261
    private function beforeRedirect()
262
    {
263
        if (empty($this->redirect)) {
264
            throw new \LogicException('The request was not redirected.');
265
        }
266
        if (-1 !== $this->maxRedirectsExtended && $this->redirectCountExtended > $this->maxRedirectsExtended) {
267
            throw new \LogicException(sprintf('The maximum number (%d) of redirections was reached.', $this->maxRedirectsExtended));
268
        }
269
        $request = $this->internalRequest;
270
        if (in_array($this->internalResponse->getStatus(), [302, 303])) {
271
            $method = 'GET';
272
            $files = [];
273
            $content = null;
274
        } else {
275
            $method = $request->getMethod();
276
            $files = $request->getFiles();
277
            $content = $request->getContent();
278
        }
279
        if ('GET' === strtoupper($method)) {
280
            // Don't forward parameters for GET request as it should reach the redirection URI
281
            $parameters = [];
282
        } else {
283
            $parameters = $request->getParameters();
284
        }
285
        $server = $this->updateServerFromUriExtended($request->getServer(), $this->redirect);
286
        return compact('method', 'parameters', 'files', 'server', 'content');
287
    }
288
289
    private function updateServerFromUriExtended($server, $uri)
290
    {
291
        $server['HTTP_HOST'] = $this->extractHostExtended($uri);
292
        $scheme = parse_url($uri, PHP_URL_SCHEME);
293
        $server['HTTPS'] = null === $scheme ? $server['HTTPS'] : 'https' === $scheme;
294
        unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']);
295
        return $server;
296
    }
297
298 13
    private function extractHostExtended($uri)
299 13
    {
300 13
        $host = parse_url($uri, PHP_URL_HOST);
301 13
        $port = parse_url($uri, PHP_URL_PORT);
302 13
        return $host . (is_int($port) ? ":$port" : '');
303
    }
304
}
305