Issues (3)

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/Response.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
 * cURL transport for yii2-hiart
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart-curl
6
 * @package   yii2-hiart-curl
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\curl;
12
13
use hiqdev\hiart\AbstractRequest;
14
use hiqdev\hiart\AbstractResponse;
15
use hiqdev\hiart\ResponseErrorException;
16
17
/**
18
 * Class Response represents response through cURL library.
19
 */
20
class Response extends AbstractResponse
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $rawData;
26
27
    /**
28
     * @var array[]
29
     */
30
    protected $headers = [];
31
32
    /**
33
     * @var string
34
     */
35
    protected $statusCode;
36
37
    /**
38
     * @var string
39
     */
40
    protected $reasonPhrase;
41
42
    /**
43
     * Response constructor.
44
     *
45
     * @param AbstractRequest $request
46
     * @param string $rawBody the raw response, returned by `curl_exec()` method
47
     * @param array $info the cURL information, returned by `curl_getinfo()` method
48
     * @param string $error the cURL error message, if present. Empty string otherwise.
49
     * @param int $errorCode the cURL error code, if present. Integer `0` otherwise.
50
     * @throws ResponseErrorException
51
     */
52 2
    public function __construct(AbstractRequest $request, $rawBody, $info, $error, $errorCode)
53
    {
54 2
        $this->request = $request;
55
56 2
        $this->checkTransportError($error, $errorCode);
57
58 2
        $parsedResponse = $this->parseRawResponse($rawBody, $info);
59 2
        $this->headers = $parsedResponse['headers'];
60 2
        $this->statusCode = $parsedResponse['statusCode'];
61 2
        $this->rawData = $parsedResponse['data'];
62 2
        $this->reasonPhrase = $parsedResponse['reasonPhrase'];
63 2
    }
64
65
    /**
66
     * @return mixed|string
67
     */
68 2
    public function getRawData()
69
    {
70 2
        return $this->rawData;
71
    }
72
73
    /**
74
     * @param string $name the header name
75
     * @return array|null
76
     */
77 2
    public function getHeader($name)
78
    {
79 2
        return isset($this->headers[$name]) ? $this->headers[$name] : null;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 2
    public function getStatusCode()
86
    {
87 2
        return $this->statusCode;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getReasonPhrase()
94
    {
95
        return $this->reasonPhrase;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->reasonPhrase; (string) is incompatible with the return type declared by the interface hiqdev\hiart\ResponseInterface::getReasonPhrase of type hiqdev\hiart\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
96
    }
97
98
    /**
99
     * Parses raw response and returns parsed information.
100
     *
101
     * @param string $data the raw response
102
     * @param array $info the curl information (result of `gurl_getinfo` call)
103
     * @return array array with the following keys will be returned:
104
     *  - data: string, response data;
105
     *  - headers: array, response headers;
106
     *  - statusCode: string, the response status-code;
107
     *  - reasonPhrase: string, the response reason phrase (OK, NOT FOUND, etc)
108
     */
109 2
    protected function parseRawResponse($data, $info)
110
    {
111 2
        $result = [];
112
113 2
        $headerSize = $info['header_size'];
114 2
        $result['data'] = substr($data, $headerSize);
115
116 2
        $rawHeaders = explode("\r\n", substr($data, 0, $headerSize));
117
        // First line is status-code HTTP/1.1 200 OK
118 2
        list(, $result['statusCode'], $result['reasonPhrase']) = explode(' ', array_shift($rawHeaders), 3);
119 2
        foreach ($rawHeaders as $line) {
120 2
            if ($line === '') {
121 2
                continue;
122
            }
123
124 2
            list($key, $value) = explode(': ', $line);
125 2
            $result['headers'][$key][] = $value;
126
        }
127
128 2
        return $result;
129
    }
130
131
    /**
132
     * Checks $error and $errorCode for transport errors.
133
     *
134
     * @param string $error the cURL error message, if present. Empty string otherwise.
135
     * @param int $errorCode the cURL error code, if present. Integer `0` otherwise.
136
     * @throws ResponseErrorException when the error is present
137
     */
138 2
    protected function checkTransportError($error, $errorCode)
139
    {
140 2
        if ($error !== '' || $errorCode !== 0) {
141
            throw new ResponseErrorException($error, $this, $errorCode);
142
        }
143 2
    }
144
145
    /**
146
     * Returns array of all headers.
147
     * Key - Header name
148
     * Value - array of header values. For example:.
149
     *
150
     * ```php
151
     * ['Location' => ['http://example.com'], 'Expires' => ['Thu, 01 Jan 1970 00:00:00 GMT']]
152
     * @return array
153
     */
154
    public function getHeaders()
155
    {
156
        return $this->headers;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->headers; (array[]) is incompatible with the return type declared by the interface hiqdev\hiart\ResponseInterface::getHeaders of type string[][].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
157
    }
158
}
159