fix(mcp): security hardening for LLM client environments#627
Open
tkaufmann wants to merge 9 commits intocbcoutinho:masterfrom
Open
fix(mcp): security hardening for LLM client environments#627tkaufmann wants to merge 9 commits intocbcoutinho:masterfrom
tkaufmann wants to merge 9 commits intocbcoutinho:masterfrom
Conversation
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.
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
escape_xml()helper applied to all user-interpolated values in CalDAV/CardDAV/WebDAV XML bodiessanitize_webdav_path()replaces bare.lstrip('/')at 11 call sites in WebDAV clientdefusedxml.ElementTreereplacesxml.etree.ElementTreefor all untrusted XML parsing; secure lxml parser options for CalDAV_validate_redirect_url()validatesnextparameter in OAuth login/callback/logout routesappuser(UID 10001) before ENTRYPOINTnc_share_get/nc_share_listrequiresharing:readinstead ofsharing:writeverify_ssl=True; Ollama sync calls respectverify_sslsettingvalidate_external_url()rejects private/loopback IPs and non-HTTP schemes in cookbook import and news feed creationDependencies
defusedxml>=0.7.1(commit 3)Test plan
uv run ruff format --check .anduv run ruff check .uv run pytest -v -m unit