Issues (36)

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.

Service/Json/JsonReader.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
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\Service\Json;
13
14
use ONGR\ElasticsearchBundle\Service\IndexService;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
17
/**
18
 * Reads records one by one.
19
 *
20
 * Sample input:
21
 * <p>
22
 * [
23
 * {"count":2},
24
 * {"_id":"doc1","title":"Document 1"},
25
 * {"_id":"doc2","title":"Document 2"}
26
 * ]
27
 * </p>
28
 */
29
class JsonReader implements \Countable, \Iterator
30
{
31
    private $filename;
32
    private $handle;
33
    private $key = 0;
34
    private $currentLine;
35
    private $metadata;
36
    private $index;
37
    private $optionsResolver;
38
    private $options;
39
40
    public function __construct(IndexService $index, string $filename, array $options = [])
41
    {
42
        $this->index = $index;
43
        $this->filename = $filename;
44
        $this->options = $options;
45
    }
46
47
    /**
48
     * Destructor. Closes file handler if open.
49
     */
50
    public function __destruct()
51
    {
52
        if ($this->handle !== null) {
53
            @fclose($this->handle);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
54
        }
55
    }
56
57
    public function getIndex(): IndexService
58
    {
59
        return $this->index;
60
    }
61
62
    protected function getFileHandler()
63
    {
64
        //Make sure the gzip option is resolved from a filename.
65
        if ($this->handle === null) {
66
            $isGzip = array_key_exists('gzip', $this->options);
67
68
            $filename = !$isGzip?
69
                $this->filename:
70
                sprintf('compress.zlib://%s', $this->filename);
71
            $fileHandler = @fopen($filename, 'r');
72
73
            if ($fileHandler === false) {
74
                throw new \LogicException('Can not open file.');
75
            }
76
77
            $this->handle = $fileHandler;
78
        }
79
80
        return $this->handle;
81
    }
82
83
    protected function readMetadata()
84
    {
85
        if ($this->metadata !== null) {
86
            return;
87
        }
88
89
        $line = fgets($this->getFileHandler());
90
91
        if (trim($line) !== '[') {
92
            throw new \InvalidArgumentException('Given file does not match expected pattern.');
93
        }
94
95
        $line = trim(fgets($this->getFileHandler()));
96
        $this->metadata = json_decode(rtrim($line, ','), true);
97
    }
98
99
    protected function readLine()
100
    {
101
        $buffer = '';
102
103
        while ($buffer === '') {
104
            $buffer = fgets($this->getFileHandler());
105
106
            if ($buffer === false) {
107
                $this->currentLine = null;
108
109
                return;
110
            }
111
112
            $buffer = trim($buffer);
113
        }
114
115
        if ($buffer === ']') {
116
            $this->currentLine = null;
117
118
            return;
119
        }
120
121
        $data = json_decode(rtrim($buffer, ','), true);
122
        $this->currentLine = $this->getOptionsResolver()->resolve($data);
123
    }
124
125
    protected function configureResolver(OptionsResolver $resolver)
126
    {
127
        $resolver
128
            ->setRequired(['_id', '_source'])
129
            ->setDefaults(['_score' => null, 'fields' => []])
130
            ->addAllowedTypes('_id', ['integer', 'string'])
131
            ->addAllowedTypes('_source', 'array')
132
            ->addAllowedTypes('fields', 'array');
133
    }
134
135
    public function current()
136
    {
137
        if ($this->currentLine === null) {
138
            $this->readLine();
139
        }
140
141
        return $this->currentLine;
142
    }
143
144
    public function next()
145
    {
146
        $this->readLine();
147
148
        $this->key++;
149
    }
150
151
    public function key()
152
    {
153
        return $this->key;
154
    }
155
156
    public function valid()
157
    {
158
        return !feof($this->getFileHandler()) && $this->currentLine;
159
    }
160
161
    public function rewind()
162
    {
163
        rewind($this->getFileHandler());
164
        $this->metadata = null;
165
        $this->readMetadata();
166
        $this->readLine();
167
    }
168
169
    public function count()
170
    {
171
        $metadata = $this->getMetadata();
0 ignored issues
show
Are you sure the assignment to $metadata is correct as $this->getMetadata() (which targets ONGR\ElasticsearchBundle...onReader::getMetadata()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
172
173
        if (!isset($metadata['count'])) {
174
            throw new \LogicException('Given file does not contain count of documents.');
175
        }
176
177
        return $metadata['count'];
178
    }
179
180
    public function getMetadata()
181
    {
182
        $this->readMetadata();
183
184
        return $this->metadata;
185
    }
186
187
    private function getOptionsResolver(): OptionsResolver
188
    {
189
        if (!$this->optionsResolver) {
190
            $this->optionsResolver = new OptionsResolver();
191
            $this->configureResolver($this->optionsResolver);
192
        }
193
194
        return $this->optionsResolver;
195
    }
196
}
197