DEV Community

Luuk Verhoeven
Luuk Verhoeven

Posted on

Moodle behind TLS offload needs $CFG->sslproxy

Moodle™ sees the request PHP received. If nginx, HAProxy, an F5, or Cloudflare terminates TLS and the PHP pool speaks HTTP on localhost, $_SERVER['HTTPS'] is empty. $CFG->wwwroot is still https://lms.example.org. Login then 302-loops, cookies drop the Secure flag, and generated URLs flip to http://.

config-dist.php names the switch:

// Enable when using external SSL appliance for performance reasons.
// Please note that site may be accessible via http: or https:, but not both!
$CFG->sslproxy = true;
Enter fullscreen mode Exit fullscreen mode

$CFG->reverseproxy is a different flag. It skips a wwwroot sanity check for load-balanced frontends. Turn both on and Moodle can refuse with “Reverse proxy enabled so the server cannot be accessed directly.” For TLS offload on a single public hostname, sslproxy is the one. Leave reverseproxy off unless you actually cannot hit the origin with that wwwroot.

What breaks when the flag is missing

A GET to /login/index.php that 302s to itself. Mixed-content warnings on SCORM and H5P. pluginfile.php links that start with http:// inside an https:// page. Session cookies without Secure on a site whose wwwroot is https. The pentest that only curls / and sees 200 misses all of that.

Moodle also requires wwwroot to be https:// when sslproxy is true. HTTP wwwroot plus sslproxy is a coding error: “Must use https address in wwwroot when ssl proxy enabled!”

Prove it from the public hostname and from the origin

HOST=https://lms.example.org
ORIGIN=http://127.0.0.1:8080   # whatever the PHP pool actually binds

# public
curl -sI "$HOST/login/index.php" | head
curl -sI "$HOST/" | grep -iE 'location|strict-transport|set-cookie'

# origin, with the public Host header
curl -sI -H "Host: lms.example.org" "$ORIGIN/login/index.php" | head
Enter fullscreen mode Exit fullscreen mode

On the origin you should see HTTP/1.1 from PHP, no TLS. On the public hostname you should see HTTPS, one 200 or a single 303 to a login URL that is still https://. A chain of Location: http://lms.example.org/login/index.php is the missing flag.

Then ask config:

php -r 'define("CLI_SCRIPT", true); require "config.php";
echo "wwwroot=".$CFG->wwwroot."\n";
echo "sslproxy="; var_export(!empty($CFG->sslproxy)); echo "\n";
echo "reverseproxy="; var_export(!empty($CFG->reverseproxy)); echo "\n";
echo "cookiesecure="; var_export($CFG->cookiesecure ?? null); echo "\n";
'
Enter fullscreen mode Exit fullscreen mode

wwwroot must match the URL students type, including https://, with no trailing slash. sslproxy true. reverseproxy false unless the cluster docs say otherwise.

The proxy must pass the original scheme. nginx:

proxy_set_header Host              $host;
proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port  $server_port;
Enter fullscreen mode Exit fullscreen mode

Moodle’s sslproxy path does not require you to rewrite $_SERVER['HTTPS'] by hand. If you patched lib/setup.php in 2012 to force HTTPS, take the patch out and use the config flag. Core upgrades eat local patches.

Cookies and HSTS are a separate layer

sslproxy makes Moodle generate https URLs. It does not set HSTS. Put Strict-Transport-Security on the TLS terminator, on every response including pluginfile.php, not only on the HTML vhost.

curl -sI "$HOST/login/index.php" | grep -i strict-transport
curl -sI "$HOST/pluginfile.php/1/theme_boost/favicon/1/favicon.ico" | grep -i strict-transport
Enter fullscreen mode Exit fullscreen mode

Empty on the file URL means the terminator adds HSTS only to PHP HTML. Students still get a first HTTP hit on the PDF after a cert blip.

$CFG->cookiesecure should follow https wwwroot. If login works over https and the cookie is missing Secure, you are on a browser path Moodle still thinks is HTTP.

Do not run http and https together

The comment in config-dist.php is the policy: the site may be reachable via http or https, not both. Redirect 80 to 443 on the terminator. Do not leave an http vhost that serves the same PHP pool “for the load balancer health check”. Health-check /login/index.php over the internal HTTP port, from the private network, with a Host header. Do not publish that port.

server {
    listen 80;
    server_name lms.example.org;
    return 301 https://$host$request_uri;
}
Enter fullscreen mode Exit fullscreen mode

Re-curl after every proxy change, after cloning the stack into a new cluster, and after someone “temporarily” pointed DNS at the origin IP.

I built GuardLMS to notice when generated Moodle URLs flip back to http. Disclosure: I am the founder. The curls above do not require it. Moodle already documented the flag next to reverseproxy in config-dist.php.

Top comments (0)