-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathban_system.cpp
More file actions
462 lines (378 loc) · 13.9 KB
/
ban_system.cpp
File metadata and controls
462 lines (378 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <curl/curl.h>
#include <map>
#include <memory>
#include <string>
#include "./httpserver.hpp"
#include "httpserver/http_utils.hpp"
#include "./littletest.hpp"
using std::shared_ptr;
using httpserver::webserver;
using httpserver::create_webserver;
using httpserver::http_resource;
using httpserver::http_response;
using httpserver::string_response;
using httpserver::http_request;
using httpserver::http::http_utils;
#ifdef HTTPSERVER_PORT
#define PORT HTTPSERVER_PORT
#else
#define PORT 8080
#endif // PORT
#define STR2(p) #p
#define STR(p) STR2(p)
#define PORT_STRING STR(PORT)
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s) {
s->append(reinterpret_cast<char*>(ptr), size*nmemb);
return size*nmemb;
}
class ok_resource : public http_resource {
public:
shared_ptr<http_response> render_GET(const http_request&) {
return std::make_shared<string_response>("OK", 200, "text/plain");
}
};
LT_BEGIN_SUITE(ban_system_suite)
void set_up() {
}
void tear_down() {
}
LT_END_SUITE(ban_system_suite)
LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks)
webserver ws = create_webserver(PORT).default_policy(http_utils::ACCEPT);
ws.start(false);
ok_resource resource;
LT_ASSERT_EQ(true, ws.register_resource("base", &resource));
curl_global_init(CURL_GLOBAL_ALL);
{
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
{
ws.ban_ip("127.0.0.1");
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0);
curl_easy_cleanup(curl);
}
{
ws.unban_ip("127.0.0.1");
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(accept_default_ban_blocks)
LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes)
webserver ws = create_webserver(PORT).default_policy(http_utils::REJECT);
ws.start(false);
ok_resource resource;
LT_ASSERT_EQ(true, ws.register_resource("base", &resource));
curl_global_init(CURL_GLOBAL_ALL);
{
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0);
curl_easy_cleanup(curl);
}
{
ws.allow_ip("127.0.0.1");
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
{
ws.disallow_ip("127.0.0.1");
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(reject_default_allow_passes)
// Test ACCEPT policy with IP on allow list - allow overrides ban
// In ACCEPT mode: condition is (is_banned && !is_allowed)
// If IP is on allow list, !is_allowed is false, so connection is always allowed
LT_BEGIN_AUTO_TEST(ban_system_suite, accept_policy_allow_overrides_ban)
webserver ws = create_webserver(PORT).default_policy(http_utils::ACCEPT);
ws.start(false);
ok_resource resource;
LT_ASSERT_EQ(true, ws.register_resource("base", &resource));
curl_global_init(CURL_GLOBAL_ALL);
// Add IP to allow list
ws.allow_ip("127.0.0.1");
// Request should work (ACCEPT policy + on allow list)
{
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
// Ban the IP - but in ACCEPT mode, allow list overrides ban
ws.ban_ip("127.0.0.1");
{
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0); // Still allowed - allow list overrides ban in ACCEPT mode
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
// Remove from allow list - now ban should take effect
ws.disallow_ip("127.0.0.1");
{
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0); // Now blocked - ban takes effect without allow list
curl_easy_cleanup(curl);
}
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(accept_policy_allow_overrides_ban)
// Test REJECT policy with IP that is allowed but then banned
// Tests: (!is_allowed || is_banned) - banned overrides allowed
LT_BEGIN_AUTO_TEST(ban_system_suite, reject_policy_allowed_then_banned)
webserver ws = create_webserver(PORT).default_policy(http_utils::REJECT);
ws.start(false);
ok_resource resource;
LT_ASSERT_EQ(true, ws.register_resource("base", &resource));
curl_global_init(CURL_GLOBAL_ALL);
// First, IP is not allowed - should be blocked
{
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0);
curl_easy_cleanup(curl);
}
// Allow the IP - should work
ws.allow_ip("127.0.0.1");
{
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
// Now ban the IP - ban should override allow
ws.ban_ip("127.0.0.1");
{
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0); // Should be blocked
curl_easy_cleanup(curl);
}
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(reject_policy_allowed_then_banned)
// Test REJECT policy with IP that is neither allowed nor banned
// Tests default REJECT behavior
LT_BEGIN_AUTO_TEST(ban_system_suite, reject_policy_neither_allowed_nor_banned)
webserver ws = create_webserver(PORT).default_policy(http_utils::REJECT);
ws.start(false);
ok_resource resource;
LT_ASSERT_EQ(true, ws.register_resource("base", &resource));
curl_global_init(CURL_GLOBAL_ALL);
// IP is not in any list - REJECT policy should block
{
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(reject_policy_neither_allowed_nor_banned)
// Test ban/allow with wildcard then more specific IP
// This tests the weight comparison branch in ban_ip/allow_ip
LT_BEGIN_AUTO_TEST(ban_system_suite, ban_with_weight_comparison)
webserver ws = create_webserver(PORT).default_policy(http_utils::ACCEPT);
ws.start(false);
ok_resource resource;
LT_ASSERT_EQ(true, ws.register_resource("base", &resource));
curl_global_init(CURL_GLOBAL_ALL);
// First ban with wildcard (lower weight)
ws.ban_ip("127.0.0.*");
// Then ban with more specific IP (higher weight)
// This should hit the weight comparison branch
ws.ban_ip("127.0.0.1");
// Request should still be blocked
{
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(ban_with_weight_comparison)
// Test allow with wildcard then more specific IP
LT_BEGIN_AUTO_TEST(ban_system_suite, allow_with_weight_comparison)
webserver ws = create_webserver(PORT).default_policy(http_utils::REJECT);
ws.start(false);
ok_resource resource;
LT_ASSERT_EQ(true, ws.register_resource("base", &resource));
curl_global_init(CURL_GLOBAL_ALL);
// First allow with wildcard (lower weight)
ws.allow_ip("127.0.0.*");
// Then allow with more specific IP (higher weight)
// This should hit the weight comparison branch
ws.allow_ip("127.0.0.1");
// Request should be allowed
{
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(allow_with_weight_comparison)
// Test ban with specific IP first, then wildcard (lower weight replaces higher)
// This tests the t_ip.weight() < (*it).weight() branch
LT_BEGIN_AUTO_TEST(ban_system_suite, ban_specific_then_wildcard)
webserver ws = create_webserver(PORT).default_policy(http_utils::ACCEPT);
ws.start(false);
ok_resource resource;
LT_ASSERT_EQ(true, ws.register_resource("base", &resource));
curl_global_init(CURL_GLOBAL_ALL);
// First ban specific IP (higher weight = 4)
ws.ban_ip("127.0.0.1");
// Then ban with wildcard (lower weight = 3)
// This should trigger the erase-and-insert branch
ws.ban_ip("127.0.0.*");
// Request should still be blocked
{
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(ban_specific_then_wildcard)
// Test allow with specific IP first, then wildcard (lower weight replaces higher)
LT_BEGIN_AUTO_TEST(ban_system_suite, allow_specific_then_wildcard)
webserver ws = create_webserver(PORT).default_policy(http_utils::REJECT);
ws.start(false);
ok_resource resource;
LT_ASSERT_EQ(true, ws.register_resource("base", &resource));
curl_global_init(CURL_GLOBAL_ALL);
// First allow specific IP (higher weight = 4)
ws.allow_ip("127.0.0.1");
// Then allow with wildcard (lower weight = 3)
// This should trigger the erase-and-insert branch
ws.allow_ip("127.0.0.*");
// Request should be allowed
{
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(allow_specific_then_wildcard)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()