From 54499eef4e38b717df7eecae0d4fcf5f32b49ed8 Mon Sep 17 00:00:00 2001 From: retoor Date: Sun, 25 Oct 2026 20:06:30 +0100 Subject: [PATCH] fix(realtime): reject WebDAV COPY/MOVE destinations nested inside the source ResourceTree::copy inserted into entries_ (a std::map) while range-iterating that same map. When dst fell inside src (e.g. /dav/a -> /dav/a/b), each newly inserted entry was discovered by the ongoing iteration and copied again, so the loop never converged and memory grew without bound (MOLO-DAV-001, CWE-674/CWE-400). RFC 4918 9.8.3/9.9.3 require servers to reject this shape before mutating anything, so copy() now checks dst == src or dst nested under src and fails immediately with no side effects. move() inherits the fix since it calls copy() first. As defense in depth, copy() also no longer mutates entries_ while iterating it: it collects the (target, resource) pairs into a local vector first, then applies them in a second pass, so the same class of bug cannot recur even if the containment check is ever bypassed. Co-Authored-By: Claude Sonnet 5 --- src/realtime/webdav/server/resource_tree.cpp | 39 ++++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/realtime/webdav/server/resource_tree.cpp b/src/realtime/webdav/server/resource_tree.cpp index dfc0128..a117ac7 100644 --- a/src/realtime/webdav/server/resource_tree.cpp +++ b/src/realtime/webdav/server/resource_tree.cpp @@ -3,6 +3,8 @@ #include "realtime/webdav/server/resource_tree.h" #include +#include +#include namespace molo::realtime::webdav::server { @@ -159,25 +161,40 @@ bool ResourceTree::copy(const std::string& src, const std::string& dst, if (s == entries_.end() || src == dst) { return false; } + // RFC 4918 9.8.3/9.9.3: a destination equal to, or nested inside, the + // source collection must be rejected before anything is mutated. + // Allowing it would insert entries (dst + the relative suffix) that + // this same pass would then have to copy again, forever (MOLO-DAV-001). + std::string prefix = src + "/"; + if (dst.compare(0, prefix.size(), prefix) == 0) { + return false; + } if (entries_.count(dst) > 0 && !overwrite) { return false; } - std::string prefix = src + "/"; + // Collect every (target path, resource) to write in a first pass over + // entries_, then apply them in a second pass below. entries_ must never + // be mutated while it is being range-iterated: inserting into a + // std::map mid-iteration is visible to that same iteration, and an + // insert landing ahead of the cursor would be visited again. + std::vector> pending; for (const auto& [k, v] : entries_) { if (k == src || k.compare(0, prefix.size(), prefix) == 0) { std::string rel = k.substr(src.size()); - std::string target = dst + rel; - if (entries_.count(target) == 0) { - created = true; - } else if (!entries_[target].is_collection) { - total_bytes_ -= entries_[target].body.size(); - } - entries_[target] = v; - if (!v.is_collection) { - total_bytes_ += v.body.size(); - } + pending.emplace_back(dst + rel, v); } } + for (auto& [target, v] : pending) { + if (entries_.count(target) == 0) { + created = true; + } else if (!entries_[target].is_collection) { + total_bytes_ -= entries_[target].body.size(); + } + if (!v.is_collection) { + total_bytes_ += v.body.size(); + } + entries_[target] = std::move(v); + } return true; }