Skip to content

fix(mcp): security hardening for LLM client environments#627

Open
tkaufmann wants to merge 9 commits intocbcoutinho:masterfrom
tkaufmann:fix/security-hardening
Open

fix(mcp): security hardening for LLM client environments#627
tkaufmann wants to merge 9 commits intocbcoutinho:masterfrom
tkaufmann:fix/security-hardening

Conversation

@tkaufmann
Copy link
Copy Markdown

Summary

Defense-in-depth security hardening for deployments where MCP clients may be untrusted (e.g. LLM agents). Eight focused commits, each addressing a specific vulnerability class.

Changes

  1. XML injection preventionescape_xml() helper applied to all user-interpolated values in CalDAV/CardDAV/WebDAV XML bodies
  2. Path traversal protectionsanitize_webdav_path() replaces bare .lstrip('/') at 11 call sites in WebDAV client
  3. XML entity expansiondefusedxml.ElementTree replaces xml.etree.ElementTree for all untrusted XML parsing; secure lxml parser options for CalDAV
  4. Open redirect prevention_validate_redirect_url() validates next parameter in OAuth login/callback/logout routes
  5. Non-root containers — Dockerfiles create and switch to appuser (UID 10001) before ENTRYPOINT
  6. Sharing scope fixnc_share_get/nc_share_list require sharing:read instead of sharing:write
  7. SSL verification defaults — OTEL exporter defaults to verify_ssl=True; Ollama sync calls respect verify_ssl setting
  8. SSRF protectionvalidate_external_url() rejects private/loopback IPs and non-HTTP schemes in cookbook import and news feed creation

Dependencies

  • New dependency: defusedxml>=0.7.1 (commit 3)

Test plan

  • uv run ruff format --check . and uv run ruff check .
  • uv run pytest -v -m unit
  • Verify WebDAV operations still work with normalized paths
  • Verify recipe import and feed creation still accept valid URLs
  • Verify Docker image builds and runs as non-root

User-provided values (display names, search patterns, MIME types, tag
names) were interpolated into XML bodies via f-strings without escaping.
A malicious input could inject XML structures, alter WebDAV search
queries, or cause malformed XML errors.

Add escape_xml() utility using xml.sax.saxutils.escape and apply it to
all user-controlled values in CalDAV, CardDAV, and WebDAV XML bodies.
Replace bare .lstrip('/') with sanitize_webdav_path() that normalizes
paths via posixpath.normpath and rejects any containing '..' components.

Nextcloud blocks traversal server-side, but this adds defense-in-depth
against path manipulation by untrusted MCP clients.
Replace xml.etree.ElementTree with defusedxml.ElementTree in WebDAV and
CardDAV clients to block XXE attacks (billion laughs, external entity
loading). For the lxml-based CalDAV parser, configure XMLParser with
resolve_entities=False and no_network=True.
Validate the 'next' query parameter in OAuth login/logout/callback to
only allow relative paths. Reject absolute URLs, protocol-relative URLs,
and any URL containing '://' to prevent redirect-based phishing attacks.
Add a dedicated appuser (UID 10001) and switch to it before the
entrypoint. Also clean apt lists to reduce image size.
nc_share_get and nc_share_list are read-only operations but incorrectly
required the sharing:write scope. Introduce sharing:read for these tools
so that clients with read-only access can list and inspect shares.
Change OTEL exporter SSL verification default from False to True.
Add verify=self.verify_ssl to synchronous httpx calls in Ollama provider
that were missing it (the async client was already configured correctly).
Validate URLs in cookbook recipe import and news feed creation to reject
non-HTTP schemes and private/loopback/link-local IP addresses. This
prevents MCP clients from using these endpoints to scan internal
services via the Nextcloud server.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant