Issues (8)

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/Reader.php (3 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 SP\Crawler;
4
5
use SP\Spiderling\CrawlerInterface;
6
use SP\Crawler\Element\ClickRequestInterface;
7
use SP\Crawler\Element\ClickableInterface;
8
use SP\Crawler\Element\SelectableInterface;
9
use SP\Crawler\Element\File;
10
use Psr\Http\Message\ServerRequestInterface;
11
use SP\Spiderling\Query\AbstractQuery;
12
use GuzzleHttp\Psr7\Uri;
13
use Psr\Http\Message\UriInterface;
14
use DOMDocument;
15
use DOMElement;
16
use InvalidArgumentException;
17
use BadMethodCallException;
18
19
/**
20
 * @author    Ivan Kerin <[email protected]>
21
 * @copyright 2015, Clippings Ltd.
22
 * @license   http://spdx.org/licenses/BSD-3-Clause
23
 */
24
class Reader implements CrawlerInterface
25
{
26
    /**
27
     * @var DOMDocument
28
     */
29
    private $document;
30
31
    /**
32
     * @var SafeXPath
33
     */
34
    private $xpath;
35
36
    /**
37
     * @var ElementMap
38
     */
39
    private $inputMap;
40
41
    /**
42
     * @param DOMDocument $document
43
     */
44 1
    public function __construct(DOMDocument $document)
45
    {
46 1
        $this->document = $document;
47
48 1
        $this->xpath = new SafeXPath($document);
49
50 1
        $this->inputMap = new InputMap($this);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SP\Crawler\InputMap($this) of type object<SP\Crawler\InputMap> is incompatible with the declared type object<SP\Crawler\ElementMap> of property $inputMap.

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...
51 1
    }
52
53
    /**
54
     * @param  string $content
55
     * @return self
56
     */
57 1
    public function setDocumentContent($content)
58
    {
59 1
        $this->document->loadHtml((string) $content);
60 1
        $this->xpath = new SafeXPath($this->document);
61
62 1
        return $this;
63
    }
64
65
    /**
66
     * @return DOMDocument
67
     */
68 1
    public function getDocument()
69
    {
70 1
        return $this->document;
71
    }
72
73
    /**
74
     * @return SafeXPath
75
     */
76 1
    public function getXPath()
77
    {
78 1
        return $this->xpath;
79
    }
80
81
    /**
82
     * @return ElementMap
83
     */
84 1
    public function getInputMap()
85
    {
86 1
        return $this->inputMap;
87
    }
88
89
    /**
90
     * @param  string $id
91
     * @throws BadMethodCallException
92
     */
93 3
    public function click($id)
94
    {
95 3
        $input = $this->getInput($this->getElement($id));
96
97 3
        if ($input instanceof ClickableInterface) {
98 1
            $input->click();
99 3
        } elseif ($input instanceof ClickRequestInterface) {
100 1
            $request = $input->clickRequest();
101 1
            $this->sendRequest($request);
102 1
        } else {
103 1
            throw new BadMethodCallException(
104 1
                sprintf('Cannot click on %s, %s', get_class($input), $id)
105 1
            );
106
        }
107 2
    }
108
109
    /**
110
     * @param  string $id
111
     * @throws BadMethodCallException
112
     */
113 2
    public function select($id)
114
    {
115 2
        $input = $this->getInput($this->getElement($id));
116
117 2
        if ($input instanceof SelectableInterface) {
118 1
            $input->select();
119 1
        } else {
120 1
            throw new BadMethodCallException(
121 1
                sprintf('Cannot select on %s, %s', get_class($input), $id)
122 1
            );
123
        }
124 1
    }
125
126
    /**
127
     * @param  ServerRequestInterface $input
0 ignored issues
show
There is no parameter named $input. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
128
     * @throws BadMethodCallException
129
     */
130 1
    public function sendRequest(ServerRequestInterface $request)
131
    {
132 1
        throw new BadMethodCallException(
133 1
            sprintf('Cannot send request to %s', $request->getUri())
134 1
        );
135
    }
136
137
    /**
138
     * @param  string $url
139
     * @throws BadMethodCallException
140
     */
141 1
    public function open(UriInterface $url)
142
    {
143 1
        throw new BadMethodCallException(
144 1
            sprintf('Method %s not supported by %s', __METHOD__, __CLASS__)
145 1
        );
146
    }
147
148
    /**
149
     * @return Psr\Http\Message\UriInterface
150
     */
151 1
    public function getUri()
152
    {
153 1
        return new Uri('');
154
    }
155
156
    /**
157
     * @param  string          $xpath
158
     * @param  DOMElement|null $scope
159
     * @throws InvalidArgumentException If xpath is not valid
160
     * @return DOMNodeList
161
     */
162 1
    public function query($xpath, DOMElement $scope = null)
163
    {
164 1
        return $this->getXpath()->query($xpath, $scope);
165
    }
166
167
    /**
168
     * @param  string $xpath
169
     * @throws InvalidArgumentException when id not found
170
     * @return DOMElement
171
     */
172 1
    public function getElement($xpath)
173
    {
174 1
        $items = $this->query($xpath);
175
176 1
        if (0 === $items->length) {
177 1
            throw new InvalidArgumentException(
178 1
                sprintf('Node with id %s does not exist', $xpath)
179 1
            );
180
        }
181
182 1
        return $items->item(0);
183
    }
184
185
    /**
186
     * @param  string $id
187
     * @throws InvalidArgumentException when id not found
188
     * @return string
189
     */
190 1
    public function getText($id)
191
    {
192 1
        $element = $this->getElement($id);
193
194 1
        return trim(preg_replace('/[ \s\f\n\r\t\v ]+/u', ' ', $element->textContent));
195
    }
196
197
    /**
198
     * @param  string $id
199
     * @throws InvalidArgumentException when id not found
200
     * @return string
201
     */
202 1
    public function getTagName($id)
203
    {
204 1
        return $this->getElement($id)->tagName;
205
    }
206
207
    /**
208
     * @param  string $id
209
     * @param  string $name
210
     * @throws InvalidArgumentException when id not found
211
     * @return string
212
     */
213 1
    public function getAttribute($id, $name)
214
    {
215 1
        return $this->getElement($id)->getAttribute($name);
216
    }
217
218
    /**
219
     * @param  string $id
220
     * @throws InvalidArgumentException when id not found
221
     * @return string
222
     */
223 1
    public function getHtml($id)
224
    {
225 1
        return $this->document->saveXml($this->getElement($id));
226
    }
227
228
    /**
229
     * @return string
230
     */
231 1
    public function getFullHtml()
232
    {
233 1
        return $this->document->saveHtml();
234
    }
235
236
    /**
237
     * @param  string $id
238
     * @throws InvalidArgumentException when id not found
239
     * @return boolean
240
     */
241 3
    public function isVisible($id)
242
    {
243 3
        $element = $this->getElement($id);
244
245
        $conditions = [
246 3
            "contains(@style, 'display:none')",
247 3
            "contains(@style, 'display: none')",
248 3
            "self::script",
249 3
            "self::head",
250 3
        ];
251
252 3
        $hidden = $this->xpath->query(
253 3
            './ancestor-or-self::*['.join(' or ', $conditions).']',
254
            $element
255 3
        );
256
257 3
        return $hidden->length == 0;
258
    }
259
260
    /**
261
     * @param  string $id
262
     * @throws InvalidArgumentException when id not found
263
     * @return boolean
264
     */
265 4
    public function isSelected($id)
266
    {
267 4
        return $this->getElement($id)->hasAttribute('selected');
268
    }
269
270
    /**
271
     * @param  string $id
272
     * @throws InvalidArgumentException when id not found
273
     * @return boolean
274
     */
275 6
    public function isChecked($id)
276
    {
277 6
        return $this->getElement($id)->hasAttribute('checked');
278
    }
279
280
    /**
281
     * @param  DOMElement $element
282
     * @throws InvalidArgumentException when id not found
283
     * @return Element\AbstractElement
284
     */
285 9
    public function getInput(DOMElement $element)
286
    {
287 9
        return $this->inputMap->get($element);
288
    }
289
290
    /**
291
     * @param  string $id
292
     * @throws InvalidArgumentException when id not found
293
     * @return mixed
294
     */
295 1
    public function getValue($id)
296
    {
297 1
        return $this->getInput($this->getElement($id))->getValue();
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class SP\Crawler\Element\AbstractElement as the method getValue() does only exist in the following sub-classes of SP\Crawler\Element\AbstractElement: SP\Crawler\Element\Checkbox, SP\Crawler\Element\File, SP\Crawler\Element\Input, SP\Crawler\Element\Option, SP\Crawler\Element\Radio, SP\Crawler\Element\Select, SP\Crawler\Element\Textarea. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
298
    }
299
300
    /**
301
     * @param  string $id
302
     * @param  string $value
303
     * @throws InvalidArgumentException when id not found
304
     */
305 1
    public function setValue($id, $value)
306
    {
307 1
        $input = $this->getInput($this->getElement($id));
308
309 1
        if (false === $input->isDisabled()) {
310 1
            $input->setValue($value);
311 1
        }
312 1
    }
313
314
    /**
315
     * @param  string $id
316
     * @param  string $file
317
     * @throws InvalidArgumentException when id not found or not a file
318
     */
319 2
    public function setFile($id, $file)
320
    {
321 2
        $input = $this->getInput($this->getElement($id));
322
323 2
        if (false === ($input instanceof File)) {
324 1
            throw new InvalidArgumentException(
325 1
                sprintf('Node with id %s is not a file', $id)
326 1
            );
327
        }
328
329 1
        if (false === $input->isDisabled()) {
330 1
            $input->setValue($file);
331 1
        }
332 1
    }
333
334
    /**
335
     * @param  AbstractQuery $query
336
     * @param  string        $parent
337
     * @return array
338
     */
339 1
    public function queryIds(AbstractQuery $query, $parent = null)
340
    {
341 1
        $xpath = $parent.$query->getXPath();
342
343 1
        $ids = [];
344
345 1
        foreach ($this->query($xpath) as $index => $element) {
346 1
            $ids []= "($xpath)[".($index+1)."]";
347 1
        }
348
349 1
        return $query->getFilters()->matchAll($this, $ids);
350
    }
351
}
352