From c2f23a566cb701be8766c3b18ced82dcfc6f3cb4 Mon Sep 17 00:00:00 2001 From: Chris Coutinho Date: Sun, 29 Mar 2026 23:11:42 +0200 Subject: [PATCH] fix: handle internal hostname without port in Login Flow v2 URL rewriting Nextcloud may omit default ports in the login_url (e.g. http://app instead of http://app:80). Extract just scheme+hostname from NEXTCLOUD_HOST for the URL replacement check. Co-Authored-By: Claude Opus 4.6 (1M context) --- nextcloud_mcp_server/auth/provision_routes.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/nextcloud_mcp_server/auth/provision_routes.py b/nextcloud_mcp_server/auth/provision_routes.py index 1ea34eec..16d9563e 100644 --- a/nextcloud_mcp_server/auth/provision_routes.py +++ b/nextcloud_mcp_server/auth/provision_routes.py @@ -205,12 +205,17 @@ async def provision_page(request: Request) -> RedirectResponse | HTMLResponse: ) # Redirect to Nextcloud's Login Flow v2 login page. - # The login_url may use the internal Docker URL (http://app:80/...). + # The login_url may use the internal Docker hostname (http://app/...). # Replace with the public Nextcloud URL for the browser. login_url = init_response.login_url public_issuer = os.getenv("NEXTCLOUD_PUBLIC_ISSUER_URL", "") - if public_issuer and nextcloud_host and nextcloud_host in login_url: - login_url = login_url.replace(nextcloud_host, public_issuer.rstrip("/")) + if public_issuer and nextcloud_host: + # Extract just scheme+host from NEXTCLOUD_HOST for matching + # (NC may omit default ports, e.g. http://app:80 → http://app) + parsed = urlparse(nextcloud_host) + internal_origin = f"{parsed.scheme}://{parsed.hostname}" + if internal_origin in login_url: + login_url = login_url.replace(internal_origin, public_issuer.rstrip("/")) return RedirectResponse(login_url)