How to inventory every origin a Moodle page actually loads before you write a CSP
A generic Content-Security-Policy copied from a hardening blog will break a Moodle™ course page. H5P, TinyMCE, LTI tools, MathJax, CDN fonts and a YouTube embed all load origins that a "scripts from self only" policy nev
A generic Content-Security-Policy copied from a hardening blog will break a Moodle™ course page. H5P, TinyMCE, LTI tools, MathJax, CDN fonts and a YouTube embed all load origins that a "scripts from self only" policy never lists. The browser then fails silently: a grey video box, a quiz that never starts, a login button that does nothing.
Write the policy from what the page actually loads. Here is a method that takes an afternoon on a staging site, not a rewrite of the application.
See the headers you already send
curl -sI https://lms.example.org/login/index.php | grep -iE 'content-security-policy|x-frame-options|strict-transport|x-content-type|referrer-policy|permissions-policy'
An empty grep is common. Moodle does not ship a production CSP. The OWASP Secure Headers Project is the checklist; this article is about collecting the allow-list so you can fill it in without taking the exam week down.
Dump every origin the rendered page uses
Open a course page that is messy on purpose: video, H5P, an LTI activity, a file uploaded by a teacher, the HTML editor. In the browser console:
[...new Set(
[...document.querySelectorAll('script[src], link[href], iframe[src], img[src], video[src], audio[src], embed[src], object[data], source[src]')]
.map((el) => {
const raw = el.src || el.href || el.data;
try { return new URL(raw, location.href).origin; } catch { return null; }
})
.filter(Boolean)
)].sort();
That list is the start of script-src, style-src, img-src, frame-src and media-src. It misses three Moodle-specific sources:
-
Inline scripts and styles. TinyMCE, Atto leftovers, additional HTML in Site administration, and many plugins inject
<script>without asrc. A policy without'unsafe-inline'or a nonce will kill the editor. Moodle core still relies on inline in several flows; nonces are the right long-term fix,'unsafe-inline'is what most sites ship in year one. -
connect-src. LTI postMessage, Behat is not production, but live grade passback, analytics beacons and the notifications poll use XHR/fetch. Watch the Network panel filtered to Fetch/XHR while you click through the same course. -
Web workers and fonts. MathJax and some themes pull fonts from a CDN. Add those origins to
font-srcor host the files yourself.
Repeat the dump on /login/index.php, a dashboard, a quiz attempt, and an LTI launch. Union the arrays. That union is your first report-only policy.
Turn the dump into a report-only header
Do not enforce yet. Send Content-Security-Policy-Report-Only from nginx or Apache so the browser logs what it would have blocked.
nginx, inside the server block:
add_header Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.youtube.com https://www.google.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; frame-src 'self' https://www.youtube.com; font-src 'self' data:; connect-src 'self'; report-uri /csp-report" always;
Replace the YouTube origin with whatever your dump actually contained. 'unsafe-eval' is ugly; some Moodle plugins still need it. Track it as debt.
/csp-report needs an endpoint that accepts application/csp-report and writes the JSON. A 20-line PHP script behind the same vhost is enough for staging. Run this through a normal teaching week, not a quiet Sunday.
Apache equivalent:
Header always set Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; frame-src 'self'; font-src 'self' data:; connect-src 'self'; report-uri /csp-report"
The always keyword matters. Without it, 302s and 404s go out without the header, and a scanner that follows redirects will still call the policy missing.
Moodle settings that fight the header
Site administration → Security → HTTP security has Allow frame embedding. If the LMS is framed by a portal, X-Frame-Options: DENY and a tight frame-ancestors will show a blank portal pane. The most restrictive rule wins. Document the real embedders (portal origin, LTI consumer, mobile wrapper) and put those in frame-ancestors. Keep X-Frame-Options: SAMEORIGIN for older browsers.
Additional HTML in the site head is a CSP leak. Every tracking snippet a marketing team pastes there is a new script-src origin. Prefer a tag manager origin you already listed, or move the snippet behind a plugin you control.
H5P runs in a frame and uses inline scripts. If H5P is in production, frame-src must include your own origin (and any H5P hub you use), and script-src will keep 'unsafe-inline' until you can nonce the player.
Enforce only after the reports go quiet
Switch Content-Security-Policy-Report-Only to Content-Security-Policy when a week of real traffic produces only noise: browser extensions, stray chrome-extension: URIs, one forgotten CDN. Announce the change to people who build course content. Keep a dated note of the policy in the change log so a grey YouTube box on Monday has a cause.
Verify again with curl -sI on a 200, a login redirect, and a 404. Mozilla's HTTP Observatory is a useful second opinion on the rest of the header set (HSTS, X-Content-Type-Options, Referrer-Policy). Those three are cheap; CSP is the one that breaks teaching.
I built GuardLMS to flag when those headers disappear after a plugin install or a reverse-proxy change. The inventory above is still the work you do once, on staging, before any monitor is useful.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.