Issues (9)

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/Defender.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 Hongliang\Defender;
4
5
use Hongliang\Defender\Voter\VoterInterface;
6
use Hongliang\Defender\Voter\IpRangeVoter;
7
use Hongliang\Defender\Voter\UriKeywordVoter;
8
use Hongliang\Defender\Voter\SpiderVoter;
9
10
class Defender
11
{
12
    const NORMAL = 0;
13
    const NOTICE = 1;
14
    const FORBIDDEN = 2;
15
    const DENY = 3;
16
    const REVENGE = 4;
17
    const REVENGE_PERMANENT = 5;
18
19
    private $voters = null;
20
    private $redirectUrl = 'http://localhost';
21
22
    public static function defend()
23
    {
24
        $defender = new self();
25
        $defender->addVoter(new IpRangeVoter(), self::DENY)
26
            ->addVoter(new UriKeywordVoter(), self::FORBIDDEN)
27
            ->addVoter(new SpiderVoter(), self::DENY)
28
            ->react();
29
    }
30
31
    public function addVoter(VoterInterface $voter, $level = self::FORBIDDEN)
32
    {
33
        if (null === $this->voters) {
34
            $this->voters = [];
35
        }
36
        $this->voters [] = ['voter' => $voter, 'level' => $level];
37
38
        return $this;
39
    }
40
41
    public function setRedirectUrl($url)
42
    {
43
        $this->redirectUrl = $url;
44
45
        return $this;
46
    }
47
48
    public function getRedirectUrl()
49
    {
50
        return $this->redirectUrl;
51
    }
52
53
    public function exam()
54
    {
55
        $level = self::NORMAL;
56
        if (null === $this->voters) {
57
            return $level;
58
        }
59
        $this->sortVoters();
60
        foreach ($this->voters as $voter) {
61
            if ($voter['voter']->vote()) {
62
                return $voter['level'];
63
            }
64
        }
65
66
        return $level;
67
    }
68
69
    private function sortVoters()
70
    {
71
        if (is_array($this->voters)) {
72
            usort($this->voters, array($this, 'sortVotersFunc'));
73
        }
74
    }
75
76
    private function sortVotersFunc($a, $b)
77
    {
78
        return $a['level'] < $b['level'];
79
    }
80
81
    public function react($level = null)
82
    {
83
        if (null === $level) {
84
            $level = $this->exam();
85
        }
86
        $exit = false;
87
        switch ($level) {
88 View Code Duplication
            case self::REVENGE_PERMANENT:
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...
89
                header('HTTP/1.1 301 Moved Permanently');
90
                header('Location: '.$this->getRedirectUrl());
91
                $exit = true;
92
                break;
93 View Code Duplication
            case self::REVENGE:
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...
94
                header('HTTP/1.1 302 Moved Temporarily');
95
                header('Location: '.$this->getRedirectUrl());
96
                $exit = true;
97
                break;
98
            case self::DENY:
99
                header('HTTP/1.1 500 Internal Server Error');
100
                $exit = true;
101
                break;
102
            case self::FORBIDDEN:
103
                header('HTTP/1.0 403 Forbidden');
104
                $exit = true;
105
                break;
106
            case self::NOTICE:
107
            default:
108
                break;
109
        }
110
111
        if ($exit) {
112
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method react() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
113
        }
114
    }
115
}
116