diff options
| -rw-r--r-- | src/zencompute/httporchestrator.cpp | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/src/zencompute/httporchestrator.cpp b/src/zencompute/httporchestrator.cpp index 4ae19303e..31251849c 100644 --- a/src/zencompute/httporchestrator.cpp +++ b/src/zencompute/httporchestrator.cpp @@ -10,25 +10,29 @@ namespace zen::compute { -HttpOrchestratorService::HttpOrchestratorService(std::filesystem::path DataDir) -: m_Service(std::make_unique<OrchestratorService>(std::move(DataDir))) +// Worker IDs must be 3-64 characters and can only contain letters, numbers, underscores, and dashes +static bool +IsValidWorkerId(std::string_view Id) { - m_Router.AddMatcher("workerid", [](std::string_view Segment) { - // Worker IDs must be 3-64 characters and can only contain letters, numbers, underscores, and dashes - if (Segment.size() < 3 || Segment.size() > 64) - { - return false; - } - for (char c : Segment) + if (Id.size() < 3 || Id.size() > 64) + { + return false; + } + for (char c : Id) + { + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-') { - if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-') - { - continue; - } - return false; + continue; } - return true; - }); + return false; + } + return true; +} + +HttpOrchestratorService::HttpOrchestratorService(std::filesystem::path DataDir) +: m_Service(std::make_unique<OrchestratorService>(std::move(DataDir))) +{ + m_Router.AddMatcher("workerid", [](std::string_view Segment) { return IsValidWorkerId(Segment); }); // dummy endpoint for websocket clients m_Router.RegisterRoute( @@ -51,14 +55,18 @@ HttpOrchestratorService::HttpOrchestratorService(std::filesystem::path DataDir) std::string_view WorkerId = Data["id"].AsString(""); std::string_view WorkerUri = Data["uri"].AsString(""); - if (WorkerId.empty() || WorkerUri.empty()) + if (!IsValidWorkerId(WorkerId)) { - return HttpReq.WriteResponse(HttpResponseCode::BadRequest); + return HttpReq.WriteResponse(HttpResponseCode::BadRequest, + HttpContentType::kText, + "Invalid worker id: must be 3-64 alphanumeric, underscore, or dash characters"); } if (!WorkerUri.starts_with("http://") && !WorkerUri.starts_with("https://")) { - return HttpReq.WriteResponse(HttpResponseCode::BadRequest); + return HttpReq.WriteResponse(HttpResponseCode::BadRequest, + HttpContentType::kText, + "Invalid uri: must start with http:// or https://"); } OrchestratorService::WorkerAnnouncement Ann; |