Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
Local API hardening: refuse requests from web pages (Origin) and non-local Host names; mesh_read's token ties a write to the SubTool, its undo revision and topology; refuse writes to SubTools with sculpt layers. BVH raycast and queryTriangles use a per-thread stack.
islandmedeb5fb952fc1c
diff --git a/src/app/LiveApi.cpp b/src/app/LiveApi.cppindex dc2655c1..6ec824fd 100644--- a/src/app/LiveApi.cpp+++ b/src/app/LiveApi.cpp@@ -77,6 +77,12 @@ void App::setLiveApi(bool on) {const bool started = st->server.start(opt, [weak, token](const link::HttpRequest& req) {auto s = weak.lock();if (!s) return link::HttpResponse::json(503, R"({"ok":false,"message":"stopped"})");+ // Not for web pages: browsers send Origin, and a rebound DNS name shows in Host.+ if (!req.header("origin").empty())+ return link::HttpResponse::json(403, R"({"ok":false,"message":"requests from web pages are refused"})");+ const std::string host = req.header("host");+ if (!host.empty() && host.rfind("127.0.0.1", 0) != 0 && host.rfind("localhost", 0) != 0)+ return link::HttpResponse::json(403, R"({"ok":false,"message":"only 127.0.0.1"})");if (req.header("x-sculptforge-token") != token)return link::HttpResponse::json(401, R"({"ok":false,"message":"missing or wrong X-SculptForge-Token"})");if (req.method == "GET" && req.path == "/info") {diff --git a/src/core/BVH.cpp b/src/core/BVH.cppindex 108a7f53..78c58942 100644--- a/src/core/BVH.cpp+++ b/src/core/BVH.cpp@@ -195,11 +195,14 @@ bool BVH::raycast(const Mesh& mesh, const glm::vec3& origin, const glm::vec3& diuint32_t bestTri = 0;bool found = false;- stack_.clear();- stack_.push_back(0);- while (!stack_.empty()) {- const Node& n = nodes_[stack_.back()];- stack_.pop_back();+ // Per thread: the UI (the surface cursor, picks) casts rays while the predictive-stroke thread sculpts;+ // a shared stack mixed their traversals (strokes then differed from the serial path).+ thread_local std::vector<uint32_t> stack;+ stack.clear();+ stack.push_back(0);+ while (!stack.empty()) {+ const Node& n = nodes_[stack.back()];+ stack.pop_back();if (!rayBox(n.box, origin, invDir, best)) continue;if (n.count > 0) {for (uint32_t i = n.first; i < n.first + n.count; ++i) {@@ -214,8 +217,8 @@ bool BVH::raycast(const Mesh& mesh, const glm::vec3& origin, const glm::vec3& di}}} else if (n.first != kNoLeaf) { // (kNoLeaf: a leaf dynamic edits emptied)- stack_.push_back(n.first);- stack_.push_back(n.first + 1);+ stack.push_back(n.first);+ stack.push_back(n.first + 1);}}if (!found) return false;@@ -234,11 +237,14 @@ void BVH::queryTriangles(const Mesh& mesh, const glm::vec3& center, float radiusout.clear();if (nodes_.empty()) return;const float r2 = radius * radius;- stack_.clear();- stack_.push_back(0);- while (!stack_.empty()) {- const Node& n = nodes_[stack_.back()];- stack_.pop_back();+ // Per thread: the UI (the surface cursor, picks) casts rays while the predictive-stroke thread sculpts;+ // a shared stack mixed their traversals (strokes then differed from the serial path).+ thread_local std::vector<uint32_t> stack;+ stack.clear();+ stack.push_back(0);+ while (!stack.empty()) {+ const Node& n = nodes_[stack.back()];+ stack.pop_back();if (!sphereBox(n.box, center, r2)) continue;if (n.count > 0) {for (uint32_t i = n.first; i < n.first + n.count; ++i) {@@ -250,8 +256,8 @@ void BVH::queryTriangles(const Mesh& mesh, const glm::vec3& center, float radiusif (sphereBox(b, center, r2)) out.push_back(triIndices_[i]);}} else if (n.first != kNoLeaf) {- stack_.push_back(n.first);- stack_.push_back(n.first + 1);+ stack.push_back(n.first);+ stack.push_back(n.first + 1);}}}diff --git a/src/core/LiveApi.cpp b/src/core/LiveApi.cppindex 9692f2ac..0e69e425 100644--- a/src/core/LiveApi.cpp+++ b/src/core/LiveApi.cpp@@ -56,6 +56,13 @@ bool unbase64(const std::string& in, std::string& out) {return true;}+// The snapshot mesh_set_positions checks: this SubTool (its id), its topology and its undo revision, so a write is+// refused after another SubTool became active, a duplicate with the same topology, or any edit since mesh_read.+std::string liveToken(const Document& doc) {+ return std::to_string(doc.activeSubtoolInfo().id) + "-" + std::to_string(doc.undo.revision()) + "-" ++ std::to_string(topologyHash(doc.mesh));+}+} // namespaceuint64_t topologyHash(const Mesh& mesh) {@@ -85,7 +92,7 @@ void registerLiveApiCommands(CommandRegistry& reg) {j.set("subtool_index", double(doc.activeSubtool));j.set("vertices", double(m.vertexCount()));j.set("triangles", double(m.triangleCount()));- j.set("topology", std::to_string(topologyHash(m)));+ j.set("topology", liveToken(doc));j.set("positions", base64(m.positions.data(), m.positions.size() * sizeof(glm::vec3)));if (argBool(a, "indices", true)) {std::vector<uint32_t> idx;@@ -104,8 +111,10 @@ void registerLiveApiCommands(CommandRegistry& reg) {{"label", T::String, "Undo label (default 'Set positions').", false}},[](Document& doc, const Args& a) -> CommandResult {Mesh& m = doc.mesh;- if (argString(a, "topology") != std::to_string(topologyHash(m)))- return {false, "The SubTool's topology changed since mesh_read (or another SubTool is active)"};+ if (argString(a, "topology") != liveToken(doc))+ return {false, "The SubTool changed since mesh_read (another SubTool is active, or it was edited)"};+ if (!m.layers.empty())+ return {false, "This SubTool has sculpt layers: mesh_set_positions only writes SubTools without layers for now"};std::string bytes;if (!unbase64(argString(a, "positions"), bytes)) return {false, "positions is not valid base64"};if (bytes.size() != m.positions.size() * sizeof(glm::vec3))