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
This commit is contained in:
Rash419 2022-04-01 16:14:53 +05:30 committed by Jan Holesovsky
parent ff8236740f
commit c2be0b3a48
3 changed files with 48 additions and 23 deletions

View file

@ -1138,8 +1138,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())
{
@ -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<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();
}
}

View file

@ -1110,9 +1110,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

View file

@ -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.");