Issues (12)

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/Rest/RestApiBrowser.php (2 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 Rezzza\RestApiBehatExtension\Rest;
4
5
use Http\Client\HttpClient;
6
use Http\Discovery\HttpClientDiscovery;
7
use Http\Discovery\MessageFactoryDiscovery;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Tolerance\Operation\Callback;
11
use Tolerance\Operation\Runner\RetryOperationRunner;
12
use Tolerance\Operation\Runner\CallbackOperationRunner;
13
use Tolerance\Waiter\SleepWaiter;
14
use Tolerance\Waiter\TimeOut;
15
16
class RestApiBrowser
17
{
18
    /** @var HttpClient */
19
    private $httpClient;
20
21
    /** @var RequestInterface */
22
    private $request;
23
24
    /** @var ResponseInterface */
25
    private $response;
26
27
    /** @var array */
28
    private $requestHeaders = [];
29
30
    /** @var ResponseStorage */
31
    private $responseStorage;
32
33
    /** @var string */
34
    private $host;
35
36
    /** @var MessageFactoryDiscovery */
37
    private $messageFactory;
38
39
    /**
40
     * @param string $host
41
     */
42
    public function __construct($host, HttpClient $httpClient = null)
43
    {
44
        $this->host = $host;
45
        $this->httpClient = $httpClient ?: HttpClientDiscovery::find();
46
        $this->messageFactory = MessageFactoryDiscovery::find();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Http\Discovery\MessageFactoryDiscovery::find() of type object<Http\Message\MessageFactory> is incompatible with the declared type object<Http\Discovery\MessageFactoryDiscovery> of property $messageFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
    }
48
49
    /**
50
     * Allow to override the httpClient to use yours with specific middleware for example.
51
     */
52
    public function useHttpClient(HttpClient $httpClient)
53
    {
54
        $this->httpClient = $httpClient;
55
    }
56
57
    /**
58
     * @param ResponseStorage $responseStorage
59
     */
60
    public function enableResponseStorage(ResponseStorage $responseStorage)
61
    {
62
        $this->responseStorage = $responseStorage;
63
    }
64
65
    /**
66
     * @return ResponseInterface
67
     */
68
    public function getResponse()
69
    {
70
        return $this->response;
71
    }
72
73
    /**
74
     * @param ResponseInterface $response
75
     */
76
    public function setResponse(ResponseInterface $response)
77
    {
78
        $this->response = $response;
79
    }
80
81
    public function getRequest()
82
    {
83
        return $this->request;
84
    }
85
86
    public function getRequestHeaders()
87
    {
88
        return $this->requestHeaders;
89
    }
90
91
    /**
92
     * @param string       $method
93
     * @param string       $uri
94
     * @param string|array $body
95
     */
96
    public function sendRequest($method, $uri, $body = null)
97
    {
98
        if (false === $this->hasHost($uri)) {
99
            $uri = rtrim($this->host, '/').'/'.ltrim($uri, '/');
100
        }
101
102
        $this->request = $this->messageFactory->createRequest($method, $uri, $this->requestHeaders, $body);
0 ignored issues
show
The method createRequest() does not seem to exist on object<Http\Discovery\MessageFactoryDiscovery>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
        $this->response = $this->httpClient->sendRequest($this->request);
104
        $this->requestHeaders = [];
105
106
        if (null !== $this->responseStorage) {
107
            $this->responseStorage->writeRawContent((string) $this->response->getBody());
108
        }
109
    }
110
111
    public function sendRequestUntil($method, $uri, $body, callable $assertion, $maxExecutionTime = 10)
112
    {
113
        $runner = new RetryOperationRunner(
114
            new CallbackOperationRunner(),
115
            new TimeOut(new SleepWaiter(), $maxExecutionTime)
116
        );
117
        $restApiBrowser = $this;
118
        $runner->run(new Callback(function () use ($restApiBrowser, $method, $uri, $body, $assertion) {
119
            $restApiBrowser->sendRequest($method, $uri, $body);
120
121
            return $assertion();
122
        }));
123
    }
124
125
    /**
126
     * @param string $name
127
     * @param string $value
128
     */
129
    public function setRequestHeader($name, $value)
130
    {
131
        $this->removeRequestHeader($name);
132
        $this->addRequestHeader($name, $value);
133
    }
134
135
    /**
136
     * @param string $name
137
     * @param string $value
138
     */
139
    public function addRequestHeader($name, $value)
140
    {
141
        if (isset($this->requestHeaders[$name])) {
142
            $this->requestHeaders[$name] .= ', '.$value;
143
        } else {
144
            $this->requestHeaders[$name] = $value;
145
        }
146
    }
147
148
    /**
149
     * @param string $headerName
150
     */
151
    private function removeRequestHeader($headerName)
152
    {
153
        if (array_key_exists($headerName, $this->requestHeaders)) {
154
            unset($this->requestHeaders[$headerName]);
155
        }
156
    }
157
158
    /**
159
     * @param string $uri
160
     *
161
     * @return bool
162
     */
163
    private function hasHost($uri)
164
    {
165
        return strpos($uri, '://') !== false;
166
    }
167
}
168