diff options
author | Rash419 <rashesh.padia@collabora.com> | 2022-06-09 11:39:54 +0530 |
---|---|---|
committer | cobackporter[bot] <cobackporter[bot]@users.noreply.github.com> | 2022-06-16 09:52:59 +0000 |
commit | 8dadc0c279a4d5b05064e1b455e94709e1355043 (patch) | |
tree | 90badb5d6f7cdef886a8eb29020ad70ae92292e8 | |
parent | MessageQueue: don't insert duplicates of tiles into TileCombined. (diff) | |
download | online-backport/4879/distro/collabora/co-21-11.tar.gz online-backport/4879/distro/collabora/co-21-11.zip |
wsd: alias: handle a case when regex is added in host tag of alias_group
backport/4879/distro/collabora/co-21-11
if host has regex and group has no aliases, when we receive first request from host pattern we considered it as original host and all the host following first request which matches the pattern are considered as aliases
for example:
<group>
<host>https://.*:80</host>
</group>
if we receives first request from asustuf then behaviour will similar to the following config:
<group>
<host>https://asustuf:80</host>
<alias>https://.*:80</alias>
</group>
Signed-off-by: Rash419 <rashesh.padia@collabora.com>
Change-Id: I70fb91a4bb7bf38ed79db9efd9fe4bc46db325e1
-rw-r--r-- | common/Util.cpp | 33 | ||||
-rw-r--r-- | common/Util.hpp | 2 | ||||
-rw-r--r-- | wsd/HostUtil.cpp | 14 | ||||
-rw-r--r-- | wsd/HostUtil.hpp | 2 |
4 files changed, 51 insertions, 0 deletions
diff --git a/common/Util.cpp b/common/Util.cpp index 6686d70b33..2d3dba59e9 100644 --- a/common/Util.cpp +++ b/common/Util.cpp @@ -1154,6 +1154,39 @@ namespace Util return std::string(); } + + std::string getValue(const std::set<std::string>& set, const std::string& subject) + { + auto search = set.find(subject); + if (search != set.end()) + { + return *search; + } + + // Not a perfect match, try regex. + for (const auto& value : set) + { + try + { + // Not performance critical to warrant caching. + Poco::RegularExpression re(value, Poco::RegularExpression::RE_CASELESS); + Poco::RegularExpression::Match reMatch; + + // Must be a full match. + if (re.match(subject, reMatch) && reMatch.offset == 0 && + reMatch.length == subject.size()) + { + return value; + } + } + catch (const std::exception& exc) + { + // Nothing to do; skip. + } + } + + return std::string(); + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/common/Util.hpp b/common/Util.hpp index 7556b25392..d403476402 100644 --- a/common/Util.hpp +++ b/common/Util.hpp @@ -997,6 +997,8 @@ int main(int argc, char**argv) /// Mainly used to match WOPI hosts patterns std::string getValue(const std::map<std::string, std::string>& map, const std::string& subject); + std::string getValue(const std::set<std::string>& set, 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 /// matches the allowed list, but not the deny. diff --git a/wsd/HostUtil.cpp b/wsd/HostUtil.cpp index 4a716c9042..f0486c7e07 100644 --- a/wsd/HostUtil.cpp +++ b/wsd/HostUtil.cpp @@ -11,6 +11,7 @@ Util::RegexListMatcher HostUtil::WopiHosts; std::map<std::string, std::string> HostUtil::AliasHosts; +std::set<std::string> HostUtil::hostList; std::string HostUtil::FirstHost; bool HostUtil::WopiEnabled; @@ -94,6 +95,7 @@ void HostUtil::parseAliases(Poco::Util::LayeredConfiguration& conf) try { const Poco::URI realUri(uri); + HostUtil::hostList.insert(realUri.getHost()); HostUtil::addWopiHost(realUri.getHost(), allow); } catch (const Poco::Exception& exc) @@ -148,6 +150,18 @@ std::string HostUtil::getNewUri(const Poco::URI& uri) { newUri.setAuthority(value); } + else + { + // It is allowed for the host to be a regex. + // In that case, the first who connects is treated as the 'host', and stored to the AliasHosts here + const std::string val = Util::getValue(hostList, newUri.getHost()); + // compare incoming request's host with existing hostList , if they are not equal it is regex and we store + // the pair in AliasHosts + if (val.compare(newUri.getHost()) != 0) + { + AliasHosts.insert({ val, newUri.getHost() }); + } + } if (newUri.getAuthority().empty()) { diff --git a/wsd/HostUtil.hpp b/wsd/HostUtil.hpp index 377d08364e..05bf42e6bb 100644 --- a/wsd/HostUtil.hpp +++ b/wsd/HostUtil.hpp @@ -21,6 +21,8 @@ private: static std::map<std::string, std::string> AliasHosts; /// When group configuration is not defined only the firstHost gets access static std::string FirstHost; + /// list of host (not aliases) in alias_groups + static std::set<std::string> hostList; static bool WopiEnabled; |