summaryrefslogtreecommitdiff
path: root/redfish-core/lib
diff options
context:
space:
mode:
authorEd Tanous <edtanous@google.com>2022-02-09 22:47:27 +0300
committerEd Tanous <ed@tanous.net>2022-04-19 03:04:38 +0300
commiteb1c47d3d98a186164ffb90214037c6062da7937 (patch)
tree82d0d458523645cb0640822b67fa16d4c981e09c /redfish-core/lib
parent357bb8f8034d2c4017062c4479244186fe6ea6a4 (diff)
downloadbmcweb-eb1c47d3d98a186164ffb90214037c6062da7937.tar.xz
Remove regex uses in event service and consolidate
As the patch at https://gerrit.openbmc-project.xyz/c/openbmc/bmcweb/+/50994 can attest, parsing urls with a regex is error prone. We should avoid it where possible, and we have boost::urls that implements a full, correct, and unit tested parser. Ideally, eventually this helper function would devolve into just the parse_uri, and setting defaults portion, and we could rely on the boost::urls::url class to pass into things like http_client. As a side note, because boost url implements port as a proper type-safe uint16, some interfaces that previously accepted port by std::string& needed to be modified, and is included in this patch. Also, once moved, the branch on the ifdef for HTTP push support was failing a clang-tidy validation. This is a known limitation of using ifdefs for our code, and something we've solved with the header file, so move the http push enabler to the header file. Also note that given this reorganization, two EXPECT statements are added to the unit tests for user input behaviors that the old code previously did not handle properly. Tested: Unit tests passing Ran Redfish-Event-Listener, saw subscription create properly: Subcription is successful for https://192.168.7.2, /redfish/v1/EventService/Subscriptions/2197426973 Signed-off-by: Ed Tanous <edtanous@google.com> Change-Id: Ia4127c6cbcde6002fe8a50348792024d1d615e8f
Diffstat (limited to 'redfish-core/lib')
-rw-r--r--redfish-core/lib/event_service.hpp49
1 files changed, 11 insertions, 38 deletions
diff --git a/redfish-core/lib/event_service.hpp b/redfish-core/lib/event_service.hpp
index 04b46e0cdd..2b63955009 100644
--- a/redfish-core/lib/event_service.hpp
+++ b/redfish-core/lib/event_service.hpp
@@ -18,6 +18,8 @@
#include <app.hpp>
#include <boost/beast/http/fields.hpp>
+#include <http/utility.hpp>
+#include <logging.hpp>
#include <query.hpp>
#include <registries/privilege_registry.hpp>
@@ -266,56 +268,27 @@ inline void requestRoutesEventDestinationCollection(App& app)
}
}
- // Validate the URL using regex expression
- // Format: <protocol>://<host>:<port>/<uri>
- // protocol: http/https
- // host: Exclude ' ', ':', '#', '?'
- // port: Empty or numeric value with ':' separator.
- // uri: Start with '/' and Exclude '#', ' '
- // Can include query params(ex: '/event?test=1')
- // TODO: Need to validate hostname extensively(as per rfc)
- const std::regex urlRegex(
- "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/"
- "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)");
- std::cmatch match;
- if (!std::regex_match(destUrl.c_str(), match, urlRegex))
- {
- messages::propertyValueFormatError(asyncResp->res, destUrl,
- "Destination");
- return;
- }
+ std::string host;
+ std::string urlProto;
+ uint16_t port = 0;
+ std::string path;
- std::string uriProto = std::string(match[1].first, match[1].second);
- if (uriProto == "http")
+ if (!crow::utility::validateAndSplitUrl(destUrl, urlProto, host,
+ port, path))
{
-#ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING
+ BMCWEB_LOG_WARNING
+ << "Failed to validate and split destination url";
messages::propertyValueFormatError(asyncResp->res, destUrl,
"Destination");
return;
-#endif
}
- std::string host = std::string(match[2].first, match[2].second);
- std::string port = std::string(match[3].first, match[3].second);
- std::string path = std::string(match[4].first, match[4].second);
- if (port.empty())
- {
- if (uriProto == "http")
- {
- port = "80";
- }
- else
- {
- port = "443";
- }
- }
if (path.empty())
{
path = "/";
}
-
std::shared_ptr<Subscription> subValue =
- std::make_shared<Subscription>(host, port, path, uriProto);
+ std::make_shared<Subscription>(host, port, path, urlProto);
subValue->destinationUrl = destUrl;