diff --git a/common/Util.cpp b/common/Util.cpp index 23885f1bd..7f0a56d7e 100644 --- a/common/Util.cpp +++ b/common/Util.cpp @@ -1138,8 +1138,7 @@ namespace Util std::_Exit(code); } - template - static bool matchRegex(const Type& set, const std::string& subject, const Getter& getter) + bool matchRegex(const std::set& set, const std::string& subject) { if (set.find(subject) != set.end()) { @@ -1152,7 +1151,7 @@ namespace Util try { // Not performance critical to warrant caching. - Poco::RegularExpression re(getter(value), Poco::RegularExpression::RE_CASELESS); + Poco::RegularExpression re(value, Poco::RegularExpression::RE_CASELESS); Poco::RegularExpression::Match reMatch; // Must be a full match. @@ -1171,18 +1170,36 @@ namespace Util return false; } - bool matchRegex(const std::set& set, const std::string& subject) + std::string getValue(const std::map& map, const std::string& subject) { - auto lambda = [] (std::set::key_type x) { return x; }; + if (map.find(subject) != map.end()) + { + return map.at(subject); + } - return matchRegex>(set, subject, lambda); - } + // Not a perfect match, try regex. + for (const auto& value : map) + { + try + { + // Not performance critical to warrant caching. + Poco::RegularExpression re(value.first, Poco::RegularExpression::RE_CASELESS); + Poco::RegularExpression::Match reMatch; - bool matchRegex(const std::map& map, const std::string& subject) - { - auto lambda = [] (std::map::value_type x) { return x.first; }; + // Must be a full match. + if (re.match(subject, reMatch) && reMatch.offset == 0 && + reMatch.length == subject.size()) + { + return value.second; + } + } + catch (const std::exception& exc) + { + // Nothing to do; skip. + } + } - return matchRegex>(map, subject, lambda); + return std::string(); } } diff --git a/common/Util.hpp b/common/Util.hpp index f63785a6a..5a2675db6 100644 --- a/common/Util.hpp +++ b/common/Util.hpp @@ -1110,9 +1110,9 @@ int main(int argc, char**argv) /// Mainly used to match WOPI hosts patterns bool matchRegex(const std::set& set, const std::string& subject); - /// Return true if the subject matches in given map. It uses regex + /// Return value from key:value pair if the subject matches in given map. It uses regex /// Mainly used to match WOPI hosts patterns - bool matchRegex(const std::map& map, const std::string& subject); + std::string getValue(const std::map& map, const std::string& subject); /// Given one or more patterns to allow, and one or more to deny, /// the match member will return true if, and only if, the subject diff --git a/wsd/HostUtil.cpp b/wsd/HostUtil.cpp index 2e2bdfa46..0bbdc53d9 100644 --- a/wsd/HostUtil.cpp +++ b/wsd/HostUtil.cpp @@ -77,7 +77,7 @@ bool HostUtil::allowedAlias(const Poco::URI& uri) else if (!Util::matchRegex(AllHosts, uri.getAuthority())) { LOG_ERR("Host: " << uri.getAuthority() - << " is not allowed, It is not part of alias_groups configuration"); + << " is denied, It is not defined in alias_groups configuration"); return false; } return true; @@ -147,10 +147,18 @@ void HostUtil::parseAliases(Poco::Util::LayeredConfiguration& conf) { continue; } + const std::string host = aliasUri.getHost(); + + std::vector strVec = Util::splitStringToVector(host, '|'); const Poco::URI realUri(uri); - AliasHosts.insert({ aliasUri.getAuthority(), realUri.getAuthority() }); - AllHosts.insert(aliasUri.getAuthority()); - HostUtil::addWopiHost(aliasUri.getHost(), allow); + for (auto& x : strVec) + { + const Poco::URI aUri(aliasUri.getScheme() + "://" + x + ':' + + std::to_string(aliasUri.getPort())); + AliasHosts.insert({ aUri.getAuthority(), realUri.getAuthority() }); + AllHosts.insert(aUri.getAuthority()); + HostUtil::addWopiHost(aUri.getHost(), allow); + } } catch (const Poco::Exception& exc) { @@ -167,10 +175,10 @@ std::string HostUtil::getNewUri(const Poco::URI& uri) return uri.getPath(); } Poco::URI newUri(uri); - const std::string key = newUri.getAuthority(); - if (Util::matchRegex(AliasHosts, key)) + const std::string value = Util::getValue(AliasHosts, newUri.getAuthority()); + if (!value.empty()) { - newUri.setAuthority(AliasHosts[key]); + newUri.setAuthority(value); } if (newUri.getAuthority().empty()) @@ -184,10 +192,10 @@ std::string HostUtil::getNewUri(const Poco::URI& uri) const Poco::URI HostUtil::getNewLockedUri(Poco::URI& uri) { Poco::URI newUri(uri); - const std::string key = newUri.getAuthority(); - if (Util::matchRegex(AliasHosts, key)) + const std::string value = Util::getValue(AliasHosts, newUri.getAuthority()); + if (!value.empty()) { - newUri.setAuthority(AliasHosts[key]); + newUri.setAuthority(value); LOG_WRN("The locked_host: " << uri.getAuthority() << " is alias of " << newUri.getAuthority() << ",Applying " << newUri.getAuthority() << " locked_host settings.");