33#include "BESCatalogList.h"
34#include "BESCatalogUtils.h"
36#include "TheBESKeys.h"
42#include "AllowedHosts.h"
47#define prolog string("AllowedHosts::").append(__func__).append("() - ")
51AllowedHosts::AllowedHosts() {
58AllowedHosts::theHosts() {
59 static AllowedHosts instance;
73bool AllowedHosts::is_allowed(
const shared_ptr<http::url> &candidate_url) {
75 return is_allowed(*candidate_url, error_msg);
78bool AllowedHosts::is_allowed(
const shared_ptr<http::url> &candidate_url, std::string &why_not) {
79 return AllowedHosts::is_allowed(*candidate_url, why_not);
82bool AllowedHosts::is_allowed(
const http::url &candidate_url) {
84 return is_allowed(candidate_url, error_msg);
89bool AllowedHosts::is_allowed(
const http::url &candidate_url, std::string &why_not) {
90 BESDEBUG(MODULE, prolog <<
"BEGIN candidate_url: " << candidate_url.str() << endl);
92 if (d_allowed_hosts.empty()) {
93 throw BESInternalError(
"The allowed hosts key, '" +
string(ALLOWED_HOSTS_BES_KEY)
94 +
"' has not been configured.", __FILE__, __LINE__);
97 bool isAllowed =
false;
101 if (candidate_url.protocol() == FILE_PROTOCOL) {
105 string file_path = candidate_url.path();
106 BESDEBUG(MODULE, prolog <<
" file_path: '" << file_path <<
107 "' (length: " << file_path.size() <<
" size: " << file_path.size() <<
")" <<endl);
109 BESCatalogList *bcl = BESCatalogList::TheCatalogList();
111 BESDEBUG(MODULE, prolog <<
"Searching for catalog named: '" << default_catalog_name <<
"'" << endl);
112 BESCatalog *bcat = bcl->find_catalog(default_catalog_name);
114 string error_msg =
"INTERNAL_ERROR: Unable to locate default catalog. Check BES configuration.";
115 throw BESInternalError(error_msg, __FILE__, __LINE__);
118 string catalog_root = bcat->
get_root();
119 BESDEBUG(MODULE, prolog <<
"catalog_root: '" << catalog_root <<
120 "' (length: " << catalog_root.size() <<
" size: " << catalog_root.size() <<
")" << endl);
122 string relative_path;
123 if (file_path[0] ==
'/') {
124 if (file_path.size() < catalog_root.size()) {
126 why_not =
"Path is out of scope from configuration.";
130 size_t ret = file_path.find(catalog_root);
131 BESDEBUG(MODULE, prolog <<
"file_path.find(catalog_root): " << ret << endl);
132 isAllowed = (ret == 0);
133 relative_path = file_path.substr(catalog_root.size());
134 BESDEBUG(MODULE, prolog <<
"relative_path: " << relative_path << endl);
135 BESDEBUG(MODULE, prolog <<
"isAllowed: " << (isAllowed?
"true":
"false") << endl);
139 BESDEBUG(MODULE, prolog <<
"Relative path detected");
140 relative_path = file_path;
154 catch (
const BESError &e) {
160 else if (candidate_url.protocol() == HTTPS_PROTOCOL || candidate_url.protocol() == HTTP_PROTOCOL) {
162 isAllowed = candidate_url.is_trusted() || check(candidate_url.str());
163 why_not =
"The URL is not on the AllowedHosts list and is not a known trusted URL";
166 why_not =
"The URL utilizes an unsupported protocol:" + candidate_url.protocol();
173bool AllowedHosts::check(
const std::string &
url)
const {
174 bool isAllowed=
false;
175 for (
auto const &a_regex_pattern: d_allowed_hosts) {
176 BESRegex reg_expr(a_regex_pattern.c_str());
177 int match_result = reg_expr.match(url.c_str(), (
int)url.size());
178 if (match_result >= 0) {
179 if (match_result ==
static_cast<int>(url.size())) {
180 BESDEBUG(MODULE, prolog <<
"FULL MATCH. pattern: " << a_regex_pattern <<
" url: " << url << endl);
183 BESDEBUG(MODULE, prolog <<
"No Match. pattern: " << a_regex_pattern <<
" url: " << url << endl);
virtual std::string default_catalog_name() const
The name of the default catalog.
virtual std::string get_root() const =0
virtual BESCatalogUtils * get_catalog_utils() const
Get a pointer to the utilities, customized for this catalog.
std::string get_message() const
get the error message for this exception
static void check_path(const std::string &path, const std::string &root, bool follow_sym_links)
Is the combination of root + path a pathname the BES can/should access?
static TheBESKeys * TheKeys()
Access to the singleton.
void get_values(const std::string &s, std::vector< std::string > &vals, bool &found)
Retrieve the values of a given key, if set.
Can a given URL be dereferenced given the BES's configuration?
Parse a URL into the protocol, host, path and query parts.
utility class for the HTTP catalog module