fix(webdav): address PR #764 review

Six findings raised in the PR review:

🔴 Blocking
- Fail-open on tag-resolution errors. get_excluded_file_paths now
  wraps each tag's get_tag_by_name and get_files_by_tag call in
  try/except; failures log a warning and the tag is skipped, rather
  than propagating to the caller and disabling all WebDAV tools when
  the systemtags endpoint is degraded. Documented in the docstring as
  the intended fail-open behaviour (threat model is preventing
  accidental exfiltration, not surviving server compromise).

🟡 Important
- nc_webdav_list_directory now raises ToolError when the listed path
  itself is tagged, instead of silently returning an empty listing
  after a wasted PROPFIND. Behaviour now mirrors the mutating tools.
- Destination error messages in move/copy/create_directory said "is
  inside" but is_path_excluded matches exact paths too. Reworded to
  "is or is inside".

🟢 Nits
- get_excluded_file_paths log message clarified: N counts
  directly-tagged paths, not total descendants.
- Test isolation: tests/unit/conftest.py already has an autouse
  _reload_dynaconf_after_test fixture that handles teardown. Removed
  the redundant module-local fixture I had drafted; documented the
  reliance in the module docstring instead.
- Added tests/unit/test_webdav_tools_exclusion.py: 12 server-layer
  tests that register the WebDAV tools on a fresh FastMCP and invoke
  each tool's underlying function with a mocked excluded set, asserting
  ToolError is raised / results filtered as expected. Catches future
  guard-integration regressions (e.g. wrong argument order).

Also added two unit tests for the new fail-open behaviour.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-05-06 12:26:25 +02:00
co-authored by Claude Opus 4.7
parent 22ed9e99a0
commit a6c188abbb
4 changed files with 413 additions and 15 deletions
+20 -11
View File
@@ -37,9 +37,11 @@ def configure_webdav_tools(mcp: FastMCP):
) -> DirectoryListing:
"""List files and directories in the specified NextCloud path.
When ``EXCLUDED_TAGS`` is configured, entries tagged (or whose
ancestor folders are tagged) with an excluded system tag are
omitted from the result.
When ``EXCLUDED_TAGS`` is configured: raises ``ToolError`` if the
listed path itself is tagged (or sits inside a tagged folder),
and otherwise omits any tagged children from the listing. The
early guard is consistent with the mutating tools and avoids a
round-trip to Nextcloud for a known-excluded path.
Args:
path: Directory path to list (empty string for root directory)
@@ -48,10 +50,16 @@ def configure_webdav_tools(mcp: FastMCP):
DirectoryListing with files, total_count, directories_count, files_count, and total_size
"""
client = await get_client(ctx)
# Resolve once and use for both the path-itself guard and the
# children filter below.
excluded = await get_excluded_file_paths(client.webdav)
if is_path_excluded(path, excluded):
raise ToolError(f"Access denied: {path!r} is tagged with an excluded tag")
items = await client.webdav.list_directory(path)
# Filter out files/folders carrying an excluded tag.
excluded = await get_excluded_file_paths(client.webdav)
# Filter out child files/folders carrying an excluded tag.
if excluded:
items = [
i for i in items if not is_path_excluded(i.get("path", ""), excluded)
@@ -221,11 +229,12 @@ def configure_webdav_tools(mcp: FastMCP):
"""
client = await get_client(ctx)
# Block directory creation inside excluded paths.
# Block directory creation at or inside excluded paths.
excluded = await get_excluded_file_paths(client.webdav)
if is_path_excluded(path, excluded):
raise ToolError(
f"Access denied: {path!r} is inside a path tagged with an excluded tag"
f"Access denied: {path!r} is or is inside a path tagged "
"with an excluded tag"
)
return await client.webdav.create_directory(path)
@@ -297,8 +306,8 @@ def configure_webdav_tools(mcp: FastMCP):
)
if is_path_excluded(destination_path, excluded):
raise ToolError(
f"Access denied: destination {destination_path!r} is inside a "
"path tagged with an excluded tag"
f"Access denied: destination {destination_path!r} is or is "
"inside a path tagged with an excluded tag"
)
return await client.webdav.move_resource(
@@ -341,8 +350,8 @@ def configure_webdav_tools(mcp: FastMCP):
)
if is_path_excluded(destination_path, excluded):
raise ToolError(
f"Access denied: destination {destination_path!r} is inside a "
"path tagged with an excluded tag"
f"Access denied: destination {destination_path!r} is or is "
"inside a path tagged with an excluded tag"
)
return await client.webdav.copy_resource(