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 <noreply@anthropic.com>
This commit is contained in:
2026-10-25 20:06:30 +01:00
co-authored by Claude Sonnet 5
parent 16a0bb0de4
commit 54499eef4e
+28 -11
View File
@@ -3,6 +3,8 @@
#include "realtime/webdav/server/resource_tree.h"
#include <cstdio>
#include <utility>
#include <vector>
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<std::pair<std::string, DavResource>> 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;
}