1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpha\Model; |
4
|
|
|
|
5
|
|
|
use Alpha\Model\Type\Text; |
6
|
|
|
use Alpha\Model\Type\SmallText; |
7
|
|
|
use Alpha\Util\Logging\Logger; |
8
|
|
|
use Alpha\Util\Config\ConfigProvider; |
9
|
|
|
use Alpha\Exception\AlphaException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A HTTP request that resulted in a 400 response. The class is only used when the |
13
|
|
|
* security.client.temp.blacklist.filter.enabled setting is set to true to enable the filter. |
14
|
|
|
* |
15
|
|
|
* @since 1.0 |
16
|
|
|
* |
17
|
|
|
* @author John Collins <[email protected]> |
18
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
19
|
|
|
* @copyright Copyright (c) 2020, John Collins (founder of Alpha Framework). |
20
|
|
|
* All rights reserved. |
21
|
|
|
* |
22
|
|
|
* <pre> |
23
|
|
|
* Redistribution and use in source and binary forms, with or |
24
|
|
|
* without modification, are permitted provided that the |
25
|
|
|
* following conditions are met: |
26
|
|
|
* |
27
|
|
|
* * Redistributions of source code must retain the above |
28
|
|
|
* copyright notice, this list of conditions and the |
29
|
|
|
* following disclaimer. |
30
|
|
|
* * Redistributions in binary form must reproduce the above |
31
|
|
|
* copyright notice, this list of conditions and the |
32
|
|
|
* following disclaimer in the documentation and/or other |
33
|
|
|
* materials provided with the distribution. |
34
|
|
|
* * Neither the name of the Alpha Framework nor the names |
35
|
|
|
* of its contributors may be used to endorse or promote |
36
|
|
|
* products derived from this software without specific |
37
|
|
|
* prior written permission. |
38
|
|
|
* |
39
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
40
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
41
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
42
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
43
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
44
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
45
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
46
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
47
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
49
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
50
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
51
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
52
|
|
|
* </pre> |
53
|
|
|
*/ |
54
|
|
|
class BadRequest extends ActiveRecord |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* The HTTP user-agent client string. |
58
|
|
|
* |
59
|
|
|
* @var \Alpha\Model\Type\Text |
60
|
|
|
* |
61
|
|
|
* @since 1.0 |
62
|
|
|
*/ |
63
|
|
|
protected $client; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The IP of the client. |
67
|
|
|
* |
68
|
|
|
* @var \Alpha\Model\Type\SmallText |
69
|
|
|
* |
70
|
|
|
* @since 1.0 |
71
|
|
|
*/ |
72
|
|
|
protected $IP; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The resource that the client requested. |
76
|
|
|
* |
77
|
|
|
* @var \Alpha\Model\Type\SmallText |
78
|
|
|
* |
79
|
|
|
* @since 1.0 |
80
|
|
|
*/ |
81
|
|
|
protected $requestedResource; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* An array of data display labels for the class properties. |
85
|
|
|
* |
86
|
|
|
* @var array |
87
|
|
|
* |
88
|
|
|
* @since 1.0 |
89
|
|
|
*/ |
90
|
|
|
protected $dataLabels = array('ID' => 'Bad request ID#', 'client' => 'Client string', 'IP' => 'IP', 'requestedResource' => 'Requested resource'); |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* The name of the database table for the class. |
94
|
|
|
* |
95
|
|
|
* @var string |
96
|
|
|
* |
97
|
|
|
* @since 1.0 |
98
|
|
|
*/ |
99
|
|
|
const TABLE_NAME = 'BadRequest'; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Trace logger. |
103
|
|
|
* |
104
|
|
|
* @var \Alpha\Util\Logging\Logger |
105
|
|
|
* |
106
|
|
|
* @since 1.0 |
107
|
|
|
*/ |
108
|
|
|
private static $logger = null; |
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Constructor for the class. |
112
|
|
|
* |
113
|
|
|
* @since 1.0 |
114
|
|
|
*/ |
115
|
|
|
public function __construct() |
116
|
|
|
{ |
117
|
|
|
self::$logger = new Logger('BadRequest'); |
118
|
|
|
self::$logger->debug('>>__construct()'); |
119
|
|
|
|
120
|
|
|
// ensure to call the parent constructor |
121
|
|
|
parent::__construct(); |
122
|
|
|
|
123
|
|
|
$this->client = new Text(); |
124
|
|
|
$this->IP = new SmallText(); |
125
|
|
|
$this->requestedResource = new SmallText(); |
126
|
|
|
|
127
|
|
|
self::$logger->debug('<<__construct'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Gets the count of bad requests for the client with this IP and client string in the past |
132
|
|
|
* configurable period (security.client.temp.blacklist.filter.period). |
133
|
|
|
* |
134
|
|
|
* @return int |
135
|
|
|
* |
136
|
|
|
* @since 1.0 |
137
|
|
|
* |
138
|
|
|
* @throws \Alpha\Exception\AlphaException |
139
|
|
|
*/ |
140
|
|
|
public function getBadRequestCount() |
141
|
|
|
{ |
142
|
|
|
$config = ConfigProvider::getInstance(); |
143
|
|
|
|
144
|
|
|
// the datetime interval syntax between MySQL and SQLite3 is a little different |
145
|
|
|
if ($config->get('db.provider.name') == 'Alpha\Model\ActiveRecordProviderMySQL') { |
146
|
|
|
$sqlQuery = 'SELECT COUNT(ID) AS request_count FROM '.$this->getTableName()." WHERE IP = '".$this->IP->getValue()."' AND client = '".$this->client->getValue()."' AND created_ts > NOW()-INTERVAL '".$config->get('security.client.temp.blacklist.filter.period')."' MINUTE"; |
147
|
|
|
} else { |
148
|
|
|
$sqlQuery = 'SELECT COUNT(ID) AS request_count FROM '.$this->getTableName()." WHERE IP = '".$this->IP->getValue()."' AND client = '".$this->client->getValue()."' AND created_ts > datetime('now', '-".$config->get('security.client.temp.blacklist.filter.period')." MINUTES')"; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$result = $this->query($sqlQuery); |
152
|
|
|
|
153
|
|
|
if (isset($result[0])) { |
154
|
|
|
$row = $result[0]; |
155
|
|
|
} else { |
156
|
|
|
throw new AlphaException('No result set returned when querying the bad request table'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (isset($row['request_count'])) { |
160
|
|
|
return $row['request_count']; |
161
|
|
|
} else { |
162
|
|
|
return 0; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|