Issues (1)

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/Client.php (1 issue)

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\Guzzle5;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\Message\RequestInterface as GuzzleRequest;
8
use GuzzleHttp\Message\ResponseInterface as GuzzleResponse;
9
use Http\Client\HttpClient;
10
use Http\Discovery\MessageFactoryDiscovery;
11
use Http\Message\ResponseFactory;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use Http\Client\Exception as HttplugException;
15
use GuzzleHttp\Exception as GuzzleExceptions;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
class Client implements HttpClient
22
{
23
    /**
24
     * @var ClientInterface
25
     */
26
    private $client;
27
28
    /**
29
     * @var ResponseFactory
30
     */
31
    private $responseFactory;
32
33
    /**
34
     * @param ClientInterface|null $client
35
     * @param ResponseFactory|null $responseFactory
36
     */
37 168
    public function __construct(ClientInterface $client = null, ResponseFactory $responseFactory = null)
38
    {
39 168
        $this->client = $client ?: new GuzzleClient();
40 168
        $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
41 168
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 168
    public function sendRequest(RequestInterface $request): ResponseInterface
47
    {
48 168
        $guzzleRequest = $this->createRequest($request);
49
50
        try {
51 168
            $response = $this->client->send($guzzleRequest);
52 15
        } catch (GuzzleExceptions\TransferException $e) {
53 15
            throw $this->handleException($e, $request);
54
        }
55
56 153
        return $this->createResponse($response);
57
    }
58
59
    /**
60
     * Converts a PSR request into a Guzzle request.
61
     *
62
     * @param RequestInterface $request
63
     *
64
     * @return GuzzleRequest
65
     */
66 168
    private function createRequest(RequestInterface $request)
67
    {
68
        $options = [
69 168
            'exceptions' => false,
70
            'allow_redirects' => false,
71
        ];
72
73 168
        $options['version'] = $request->getProtocolVersion();
74 168
        $options['headers'] = $request->getHeaders();
75 168
        $body = (string) $request->getBody();
76 168
        $options['body'] = '' === $body ? null : $body;
77
78 168
        return $this->client->createRequest(
79 168
            $request->getMethod(),
80 168
            (string) $request->getUri(),
81 168
            $options
82
        );
83
    }
84
85
    /**
86
     * Converts a Guzzle response into a PSR response.
87
     *
88
     * @param GuzzleResponse $response
89
     *
90
     * @return ResponseInterface
91
     */
92 157
    private function createResponse(GuzzleResponse $response)
93
    {
94 157
        $body = $response->getBody();
95
96 157
        return $this->responseFactory->createResponse(
97 157
            $response->getStatusCode(),
98 157
            null,
99 157
            $response->getHeaders(),
100 157
            isset($body) ? $body->detach() : null,
101 157
            $response->getProtocolVersion()
102
        );
103
    }
104
105
    /**
106
     * Converts a Guzzle exception into an Httplug exception.
107
     *
108
     * @param GuzzleExceptions\TransferException $exception
109
     * @param RequestInterface                   $request
110
     *
111
     * @return HttplugException
112
     */
113 15
    private function handleException(GuzzleExceptions\TransferException $exception, RequestInterface $request)
114
    {
115 15
        if ($exception instanceof GuzzleExceptions\ConnectException) {
116 4
            return new HttplugException\NetworkException($exception->getMessage(), $request, $exception);
117
        }
118
119 11
        if ($exception instanceof GuzzleExceptions\RequestException) {
120
            // Make sure we have a response for the HttpException
121 9
            if ($exception->hasResponse()) {
122 4
                $psr7Response = $this->createResponse($exception->getResponse());
0 ignored issues
show
It seems like $exception->getResponse() can be null; however, createResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
123
124 4
                return new HttplugException\HttpException(
125 4
                    $exception->getMessage(),
126 4
                    $request,
127 4
                    $psr7Response,
128 4
                    $exception
129
                );
130
            }
131
132 5
            return new HttplugException\RequestException($exception->getMessage(), $request, $exception);
133
        }
134
135 2
        return new HttplugException\TransferException($exception->getMessage(), 0, $exception);
136
    }
137
}
138