Issues (89)

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

duplicate/similar code.

Duplication Informational

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
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter;
13
14
use Ivory\HttpAdapter\Message\InternalRequestInterface;
15
use Ivory\HttpAdapter\Normalizer\HeadersNormalizer;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
abstract class AbstractHttpAdapter implements HttpAdapterInterface
21
{
22
    use HttpAdapterTrait;
23
24
    /**
25
     * @var ConfigurationInterface
26
     */
27
    private $configuration;
28
29
    /**
30
     * @param ConfigurationInterface|null $configuration
31
     */
32 16133
    public function __construct(ConfigurationInterface $configuration = null)
33
    {
34 16133
        $this->setConfiguration($configuration ?: new Configuration());
35 16133
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 15390
    public function getConfiguration()
41
    {
42 15390
        return $this->configuration;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 16133
    public function setConfiguration(ConfigurationInterface $configuration)
49
    {
50 16133
        $this->configuration = $configuration;
51 16133
    }
52
53
    /**
54
     * @param InternalRequestInterface $internalRequest
55
     * @param bool                     $associative
56
     * @param bool                     $contentType
57
     * @param bool                     $contentLength
58
     *
59
     * @return array the prepared headers
60
     */
61 15345
    protected function prepareHeaders(
62
        InternalRequestInterface &$internalRequest,
63 7
        $associative = true,
64
        $contentType = true,
65
        $contentLength = false
66
    ) {
67 15345
        if (!$internalRequest->hasHeader('Connection')) {
68 15345
            $internalRequest = $internalRequest->withHeader(
69 15345
                'Connection',
70 15345
                $this->configuration->getKeepAlive() ? 'keep-alive' : 'close'
71 11823
            );
72 11823
        }
73
74 15345
        if (!$internalRequest->hasHeader('Content-Type')) {
75 15345
            $rawDatas = (string) $internalRequest->getBody();
76 15345
            $datas = $internalRequest->getDatas();
77 15345
            $files = $internalRequest->getFiles();
78
79 15345
            if ($this->configuration->hasEncodingType()) {
80
                $internalRequest = $internalRequest->withHeader(
81
                    'Content-Type',
82
                    $this->configuration->getEncodingType()
83
                );
84 15345
            } elseif ($contentType && !empty($files)) {
85 1750
                $internalRequest = $internalRequest->withHeader(
86 1750
                    'Content-Type',
87 1750
                    ConfigurationInterface::ENCODING_TYPE_FORMDATA.'; boundary='.$this->configuration->getBoundary()
88 1350
                );
89 14945
            } elseif ($contentType && (!empty($datas) || !empty($rawDatas))) {
90 3150
                $internalRequest = $internalRequest->withHeader(
91 3150
                    'Content-Type',
92 720
                    ConfigurationInterface::ENCODING_TYPE_URLENCODED
93 2430
                );
94 2430
            }
95 11823
        }
96
97 15345
        if ($contentLength && !$internalRequest->hasHeader('Content-Length')
98 15345
            && ($length = strlen($this->prepareBody($internalRequest))) > 0) {
99 504
            $internalRequest = $internalRequest->withHeader('Content-Length', (string) $length);
100 392
        }
101
102 15345
        if (!$internalRequest->hasHeader('User-Agent')) {
103 15345
            $internalRequest = $internalRequest->withHeader('User-Agent', $this->configuration->getUserAgent());
104 11823
        }
105
106 15345
        return HeadersNormalizer::normalize($internalRequest->getHeaders(), $associative);
107
    }
108
109
    /**
110
     * @param InternalRequestInterface $internalRequest
111
     *
112
     * @return string
113
     */
114 14737
    protected function prepareBody(InternalRequestInterface $internalRequest)
115
    {
116 14737
        $body = (string) $internalRequest->getBody();
117
118 14737
        if (!empty($body)) {
119 1281
            return $body;
120
        }
121
122 13822
        $files = $internalRequest->getFiles();
123
124 13822
        if (empty($files)) {
125 12392
            return http_build_query($internalRequest->getDatas(), null, '&');
126
        }
127
128 1430
        $body = '';
129
130 1430
        foreach ($internalRequest->getDatas() as $name => $value) {
131 1430
            $body .= $this->prepareRawBody($name, $value);
132 1090
        }
133
134 1430
        foreach ($internalRequest->getFiles() as $name => $file) {
135 1430
            $body .= $this->prepareRawBody($name, $file, true);
136 1090
        }
137
138 1430
        $body .= '--'.$this->configuration->getBoundary().'--'."\r\n";
139
140 1430
        return $body;
141
    }
142
143
    /**
144
     * @param string $name
145
     * @param string $subName
146
     *
147
     * @return string
148
     */
149 1830
    protected function prepareName($name, $subName)
150
    {
151 1830
        return $name.'['.$subName.']';
152
    }
153
154
    /**
155
     * @param string       $name
156
     * @param array|string $data
157
     * @param bool         $isFile
158
     *
159
     * @return string
160
     */
161 1430
    private function prepareRawBody($name, $data, $isFile = false)
162
    {
163 1430 View Code Duplication
        if (is_array($data)) {
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...
164 1430
            $body = '';
165
166 1430
            foreach ($data as $subName => $subData) {
167 1430
                $body .= $this->prepareRawBody($this->prepareName($name, $subName), $subData, $isFile);
168 1090
            }
169
170 1430
            return $body;
171
        }
172
173 1430
        $body = '--'.$this->configuration->getBoundary()."\r\n".'Content-Disposition: form-data; name="'.$name.'"';
174
175 1430
        if ($isFile) {
176 1430
            $body .= '; filename="'.basename($data).'"';
177 1430
            $data = file_get_contents($data);
178 1090
        }
179
180 1430
        return $body."\r\n\r\n".$data."\r\n";
181
    }
182
}
183