aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2026-02-28 15:56:28 +0100
committerStefan Boberg <[email protected]>2026-02-28 15:56:28 +0100
commit007210cee9b43aff9d8219659f45254195dd8e83 (patch)
tree93eff4899cd6fd18dfe059f351c43df6f4a3f971
parentadd basic validation of announced endpoints (diff)
downloadzen-sb/orch.tar.xz
zen-sb/orch.zip
added worker id validationsb/orch
-rw-r--r--src/zencompute/httporchestrator.cpp46
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;