mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-19 11:15:50 +00:00
Add hostname resolving to dosguard (#1000)
Fixes #983. Cassandra, ETL sorces and cache already support hostname resolving. Also added config to show missing includes by clangd.
This commit is contained in:
@@ -21,40 +21,44 @@
|
||||
#include "web/WhitelistHandler.h"
|
||||
|
||||
#include <boost/json/parse.hpp>
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
using namespace util;
|
||||
using namespace web;
|
||||
|
||||
constexpr static auto JSONDataIPV4 = R"JSON(
|
||||
{
|
||||
"dos_guard": {
|
||||
"whitelist": [
|
||||
"127.0.0.1",
|
||||
"192.168.0.1/22",
|
||||
"10.0.0.1"
|
||||
]
|
||||
}
|
||||
}
|
||||
)JSON";
|
||||
|
||||
constexpr static auto JSONDataIPV6 = R"JSON(
|
||||
{
|
||||
"dos_guard": {
|
||||
"whitelist": [
|
||||
"2002:1dd8:85a7:0000:0000:8a6e:0000:1111",
|
||||
"2001:0db8:85a3:0000:0000:8a2e:0000:0000/22"
|
||||
]
|
||||
}
|
||||
}
|
||||
)JSON";
|
||||
|
||||
class WhitelistHandlerTest : public NoLoggerFixture {};
|
||||
struct WhitelistHandlerTest : NoLoggerFixture {};
|
||||
|
||||
TEST_F(WhitelistHandlerTest, TestWhiteListIPV4)
|
||||
{
|
||||
struct MockResolver {
|
||||
MOCK_METHOD(std::vector<std::string>, resolve, (std::string_view, std::string_view));
|
||||
};
|
||||
|
||||
testing::StrictMock<MockResolver> mockResolver;
|
||||
|
||||
constexpr static auto JSONDataIPV4 = R"JSON(
|
||||
{
|
||||
"dos_guard": {
|
||||
"whitelist": [
|
||||
"127.0.0.1",
|
||||
"192.168.0.1/22",
|
||||
"10.0.0.1"
|
||||
]
|
||||
}
|
||||
}
|
||||
)JSON";
|
||||
|
||||
EXPECT_CALL(mockResolver, resolve(testing::_, ""))
|
||||
.Times(3)
|
||||
.WillRepeatedly([](auto hostname, auto) -> std::vector<std::string> { return {std::string{hostname}}; });
|
||||
|
||||
Config const cfg{boost::json::parse(JSONDataIPV4)};
|
||||
WhitelistHandler const whitelistHandler{cfg};
|
||||
WhitelistHandler const whitelistHandler{cfg, mockResolver};
|
||||
|
||||
EXPECT_TRUE(whitelistHandler.isWhiteListed("192.168.1.10"));
|
||||
EXPECT_FALSE(whitelistHandler.isWhiteListed("193.168.0.123"));
|
||||
@@ -62,8 +66,41 @@ TEST_F(WhitelistHandlerTest, TestWhiteListIPV4)
|
||||
EXPECT_FALSE(whitelistHandler.isWhiteListed("10.0.0.2"));
|
||||
}
|
||||
|
||||
TEST_F(WhitelistHandlerTest, TestWhiteListResolvesHostname)
|
||||
{
|
||||
constexpr static auto JSONDataIPV4 = R"JSON(
|
||||
{
|
||||
"dos_guard": {
|
||||
"whitelist": [
|
||||
"localhost",
|
||||
"10.0.0.1"
|
||||
]
|
||||
}
|
||||
}
|
||||
)JSON";
|
||||
|
||||
Config const cfg{boost::json::parse(JSONDataIPV4)};
|
||||
WhitelistHandler const whitelistHandler{cfg};
|
||||
|
||||
EXPECT_TRUE(whitelistHandler.isWhiteListed("127.0.0.1"));
|
||||
EXPECT_FALSE(whitelistHandler.isWhiteListed("193.168.0.123"));
|
||||
EXPECT_TRUE(whitelistHandler.isWhiteListed("10.0.0.1"));
|
||||
EXPECT_FALSE(whitelistHandler.isWhiteListed("10.0.0.2"));
|
||||
}
|
||||
|
||||
TEST_F(WhitelistHandlerTest, TestWhiteListIPV6)
|
||||
{
|
||||
constexpr static auto JSONDataIPV6 = R"JSON(
|
||||
{
|
||||
"dos_guard": {
|
||||
"whitelist": [
|
||||
"2002:1dd8:85a7:0000:0000:8a6e:0000:1111",
|
||||
"2001:0db8:85a3:0000:0000:8a2e:0000:0000/22"
|
||||
]
|
||||
}
|
||||
}
|
||||
)JSON";
|
||||
|
||||
Config const cfg{boost::json::parse(JSONDataIPV6)};
|
||||
WhitelistHandler const whitelistHandler{cfg};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user