summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRash419 <rashesh.padia@collabora.com>2022-04-01 16:14:53 +0530
committerAndras Timar <andras.timar@collabora.com>2022-04-05 22:10:52 +0200
commit67b576ea0ef1ef789ed3c4f996bb7055bc8cc0be (patch)
tree29bb7c9b29f202e9995101c13f88a8815344c3b1
parentdocker: added start-collabora-online.pl in docker files (diff)
downloadonline-67b576ea0ef1ef789ed3c4f996bb7055bc8cc0be.tar.gz
online-67b576ea0ef1ef789ed3c4f996bb7055bc8cc0be.zip
wsd: add: support to define multiple domain with '|'
fix: in regex, escaping special character fails to find host of the alias Signed-off-by: Rash419 <rashesh.padia@collabora.com> Change-Id: I19bfacc5e45af26832cb8c6a7d249e0c7de56624
-rw-r--r--common/Util.cpp39
-rw-r--r--common/Util.hpp4
-rw-r--r--wsd/HostUtil.cpp28
3 files changed, 48 insertions, 23 deletions
diff --git a/common/Util.cpp b/common/Util.cpp
index c4140d9495..9c314d59e9 100644
--- a/common/Util.cpp
+++ b/common/Util.cpp
@@ -1137,8 +1137,7 @@ namespace Util
std::_Exit(code);
}
- template <class Type, typename Getter>
- static bool matchRegex(const Type& set, const std::string& subject, const Getter& getter)
+ bool matchRegex(const std::set<std::string>& set, const std::string& subject)
{
if (set.find(subject) != set.end())
{
@@ -1151,7 +1150,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.
@@ -1170,18 +1169,36 @@ namespace Util
return false;
}
- bool matchRegex(const std::set<std::string>& set, const std::string& subject)
+ std::string getValue(const std::map<std::string, std::string>& map, const std::string& subject)
{
- auto lambda = [] (std::set<std::string>::key_type x) { return x; };
+ if (map.find(subject) != map.end())
+ {
+ return map.at(subject);
+ }
- return matchRegex<std::set<std::string>>(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<std::string, std::string>& map, const std::string& subject)
- {
- auto lambda = [] (std::map<std::string, std::string>::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<std::map<std::string, std::string>>(map, subject, lambda);
+ return std::string();
}
}
diff --git a/common/Util.hpp b/common/Util.hpp
index e5a4e96960..bd2bb1ec32 100644
--- a/common/Util.hpp
+++ b/common/Util.hpp
@@ -1084,9 +1084,9 @@ int main(int argc, char**argv)
/// Mainly used to match WOPI hosts patterns
bool matchRegex(const std::set<std::string>& 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<std::string, std::string>& map, const std::string& subject);
+ std::string getValue(const std::map<std::string, std::string>& 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 2e2bdfa46b..0bbdc53d94 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<std::string> 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.");