2019-10-09 20:29:41 +00:00
|
|
|
#include <re2/re2.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2020-01-18 20:19:10 +00:00
|
|
|
#include <Common/RemoteHostFilter.h>
|
2019-10-09 20:29:41 +00:00
|
|
|
#include <Common/StringUtils/StringUtils.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
2020-01-18 20:19:10 +00:00
|
|
|
|
2019-10-09 20:29:41 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int UNACCEPTABLE_URL;
|
|
|
|
}
|
|
|
|
|
2019-10-21 14:36:24 +00:00
|
|
|
void RemoteHostFilter::checkURL(const Poco::URI & uri) const
|
2019-10-09 20:29:41 +00:00
|
|
|
{
|
2019-11-05 12:40:49 +00:00
|
|
|
if (!checkForDirectEntry(uri.getHost()) &&
|
|
|
|
!checkForDirectEntry(uri.getHost() + ":" + toString(uri.getPort())))
|
2021-10-26 17:17:02 +00:00
|
|
|
throw Exception("URL \"" + uri.toString() + "\" is not allowed in configuration file, see <remote_url_allow_hosts>", ErrorCodes::UNACCEPTABLE_URL);
|
2019-10-09 20:29:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 14:36:24 +00:00
|
|
|
void RemoteHostFilter::checkHostAndPort(const std::string & host, const std::string & port) const
|
2019-10-09 20:29:41 +00:00
|
|
|
{
|
2019-11-05 12:40:49 +00:00
|
|
|
if (!checkForDirectEntry(host) &&
|
|
|
|
!checkForDirectEntry(host + ":" + port))
|
2021-10-26 17:17:02 +00:00
|
|
|
throw Exception("URL \"" + host + ":" + port + "\" is not allowed in configuration file, see <remote_url_allow_hosts>", ErrorCodes::UNACCEPTABLE_URL);
|
2019-10-09 20:29:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 12:58:06 +00:00
|
|
|
void RemoteHostFilter::setValuesFromConfig(const Poco::Util::AbstractConfiguration & config)
|
2019-10-09 20:29:41 +00:00
|
|
|
{
|
|
|
|
if (config.has("remote_url_allow_hosts"))
|
|
|
|
{
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
config.keys("remote_url_allow_hosts", keys);
|
2022-03-15 13:00:31 +00:00
|
|
|
|
|
|
|
std::lock_guard guard(hosts_mutex);
|
2022-03-15 14:08:56 +00:00
|
|
|
primary_hosts.clear();
|
|
|
|
regexp_hosts.clear();
|
2022-03-15 13:00:31 +00:00
|
|
|
|
2020-03-08 21:18:53 +00:00
|
|
|
for (const auto & key : keys)
|
2019-10-09 20:29:41 +00:00
|
|
|
{
|
|
|
|
if (startsWith(key, "host_regexp"))
|
|
|
|
regexp_hosts.push_back(config.getString("remote_url_allow_hosts." + key));
|
|
|
|
else if (startsWith(key, "host"))
|
|
|
|
primary_hosts.insert(config.getString("remote_url_allow_hosts." + key));
|
|
|
|
}
|
2022-03-15 13:00:31 +00:00
|
|
|
|
|
|
|
is_initialized = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
is_initialized = false;
|
|
|
|
std::lock_guard guard(hosts_mutex);
|
|
|
|
primary_hosts.clear();
|
|
|
|
regexp_hosts.clear();
|
2019-10-09 20:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:40:49 +00:00
|
|
|
bool RemoteHostFilter::checkForDirectEntry(const std::string & str) const
|
2019-10-09 20:29:41 +00:00
|
|
|
{
|
2022-03-15 13:00:31 +00:00
|
|
|
if (!is_initialized)
|
|
|
|
/// Allow everything by default.
|
2019-10-09 20:29:41 +00:00
|
|
|
return true;
|
2022-03-15 13:00:31 +00:00
|
|
|
|
|
|
|
std::lock_guard guard(hosts_mutex);
|
|
|
|
|
|
|
|
if (primary_hosts.contains(str))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (const auto & regexp : regexp_hosts)
|
|
|
|
if (re2::RE2::FullMatch(str, regexp))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2019-10-09 20:29:41 +00:00
|
|
|
}
|
2019-10-10 13:53:52 +00:00
|
|
|
}
|