From 5e810c4a5a2b56e664d2bc91b85ffc7b4ae8236e Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Mon, 24 Aug 2026 14:15:01 +0200 Subject: [PATCH] fix(moodle): accept MCP requests under the internal service name Moodle 303-redirects every request whose Host is not $CFG->wwwroot, and an MCP client dialling the internal service name cannot follow that redirect from the container network. The previous accommodation was on the wrong side: the probe sent -H 'Host: ' and -H 'X-Forwarded-Proto: https' and pinned mcp_probe_host to MOODLE_DOMAIN, so it asserted the endpoint works when spoofed and said nothing about how a real client reaches it. Both headers and the probe host are gone. Acceptance moves into config.php, which reads MOODLE_INTERNAL_HOST -- rendered from MOODLE_MCP_SERVICE_KEY -- and, when the incoming HTTP_HOST matches it, rewrites HTTP_HOST and SERVER_NAME to the wwwroot host before Moodle's redirect logic runs. The comparison splits on ':' so a port cannot defeat the match, and a request from any other host is untouched, leaving the canonical-host protection intact for real traffic. None of that could have taken effect on a deployed instance. The bootstrap seeds the code directory with `cp -an`, which never clobbers, so an existing volume keeps its first-run config.php forever and every later config change is silently discarded. moodle_refresh_config now copies the deployment-managed config.php on every start, which is what makes the rewrite above reachable at all. Not verified: no MCP call has been observed succeeding against the internal name since the change. This is also the same failure class that ended the last deploy on another role, where the client already shared the provider's network and had the right alias registered, yet the DNS search suffix still resolved the single-label name to the public ingress. If that applies here too, the Host fix is correct and the request still never arrives -- and removing the spoofing headers will surface it as a failure where the probe previously passed. Two further consequences worth stating: config.php is now overwritten on every start, so an operator edit inside the container is discarded; and the HTTP_HOST rewrite runs in the hot path of every page load, narrow though its condition is. Co-Authored-By: Claude Opus 5 (1M context) --- roles/web-app-moodle/files/build/config.php | 6 ++++++ roles/web-app-moodle/files/build/moodle-entrypoint.sh | 10 ++++++++++ roles/web-app-moodle/tasks/utils/mcp.yml | 2 +- roles/web-app-moodle/tasks/utils/mcp/verify.yml | 2 -- roles/web-app-moodle/templates/env.j2 | 1 + 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/roles/web-app-moodle/files/build/config.php b/roles/web-app-moodle/files/build/config.php index 8f5f7d9270..cef8461f34 100644 --- a/roles/web-app-moodle/files/build/config.php +++ b/roles/web-app-moodle/files/build/config.php @@ -43,6 +43,12 @@ $CFG->directorypermissions = 02770; $CFG->reverseproxy = moodle_env_bool('MOODLE_REVERSEPROXY'); $CFG->sslproxy = moodle_env_bool('MOODLE_SSLPROXY'); +$_moodle_internal_host = moodle_env('MOODLE_INTERNAL_HOST'); +if (explode(':', $_SERVER['HTTP_HOST'] ?? '', 2)[0] === $_moodle_internal_host) { + $_SERVER['HTTP_HOST'] = parse_url($CFG->wwwroot, PHP_URL_HOST); + $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; +} + $_moodle_debug = moodle_env_bool('MOODLE_DEBUG'); $CFG->debug = $_moodle_debug ? 32767 : 0; $CFG->debugdisplay = $_moodle_debug; diff --git a/roles/web-app-moodle/files/build/moodle-entrypoint.sh b/roles/web-app-moodle/files/build/moodle-entrypoint.sh index 025cd512c7..ae9ba95b9d 100644 --- a/roles/web-app-moodle/files/build/moodle-entrypoint.sh +++ b/roles/web-app-moodle/files/build/moodle-entrypoint.sh @@ -37,6 +37,14 @@ moodle_bootstrap_code_dir() { touch "${MOODLE_BOOTSTRAP_SENTINEL}" } +moodle_refresh_config() { + if [ -f "${MOODLE_SOURCE_DIR}/config.php" ]; then + cp -f "${MOODLE_SOURCE_DIR}/config.php" "${MOODLE_CODE_DIR}/config.php" || true + chown "${MOODLE_RUNTIME_USER}:${MOODLE_RUNTIME_USER}" "${MOODLE_CODE_DIR}/config.php" || true + chmod 0640 "${MOODLE_CODE_DIR}/config.php" || true + fi +} + mkdir -p "${MOODLE_CODE_DIR}" while [ ! -f "${MOODLE_BOOTSTRAP_SENTINEL}" ]; do exec 9>>"${MOODLE_BOOTSTRAP_LOCK}" @@ -48,6 +56,8 @@ while [ ! -f "${MOODLE_BOOTSTRAP_SENTINEL}" ]; do exec 9>&- done +moodle_refresh_config + if [ "$(id -u)" -eq 0 ] && [ "${1:-}" != "php-fpm" ]; then exec gosu "${MOODLE_RUNTIME_USER}" "$@" fi diff --git a/roles/web-app-moodle/tasks/utils/mcp.yml b/roles/web-app-moodle/tasks/utils/mcp.yml index f34c27eab1..3fb3fadaf7 100644 --- a/roles/web-app-moodle/tasks/utils/mcp.yml +++ b/roles/web-app-moodle/tasks/utils/mcp.yml @@ -22,4 +22,4 @@ mcp_probe_path_key: "" mcp_probe_read_tool: "core_course_get_courses" mcp_probe_read_args: {} - mcp_probe_host: "{{ MOODLE_DOMAIN }}" + mcp_probe_host: "" diff --git a/roles/web-app-moodle/tasks/utils/mcp/verify.yml b/roles/web-app-moodle/tasks/utils/mcp/verify.yml index 4ca5b1f20e..29ad45fe35 100644 --- a/roles/web-app-moodle/tasks/utils/mcp/verify.yml +++ b/roles/web-app-moodle/tasks/utils/mcp/verify.yml @@ -6,8 +6,6 @@ -X POST \ -H 'Content-Type: application/json' \ -H 'Accept: application/json' \ - -H {{ ('Host: ' ~ MOODLE_DOMAIN) | quote }} \ - -H 'X-Forwarded-Proto: https' \ -H {{ ('Authorization: Bearer ' ~ moodle_mcp_token) | quote }} \ --data-binary {{ MOODLE_MCP_TOOLS_LIST_PAYLOAD | to_json | quote }} \ {{ MOODLE_MCP_URL | quote }} diff --git a/roles/web-app-moodle/templates/env.j2 b/roles/web-app-moodle/templates/env.j2 index 2487b71613..61887a1cac 100644 --- a/roles/web-app-moodle/templates/env.j2 +++ b/roles/web-app-moodle/templates/env.j2 @@ -1,6 +1,7 @@ {% include 'roles/sys-svc-container/templates/env.j2' %} MOODLE_WWWROOT={{ lookup('tls', application_id, 'url.base') | regex_replace('/$', '') }} +MOODLE_INTERNAL_HOST={{ MOODLE_MCP_SERVICE_KEY }} MOODLE_DATAROOT={{ MOODLE_DATA_DIR }} MOODLE_LOCALCACHEDIR={{ MOODLE_LOCAL_CACHE_DIR }}