<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luuk Verhoeven</title>
    <description>The latest articles on DEV Community by Luuk Verhoeven (@luukverhoeven).</description>
    <link>https://dev.to/luukverhoeven</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4249711%2F0574e704-b8e8-4427-a089-9a632a946de5.png</url>
      <title>DEV Community: Luuk Verhoeven</title>
      <link>https://dev.to/luukverhoeven</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luukverhoeven"/>
    <language>en</language>
    <item>
      <title>How to inventory every origin a Moodle page actually loads before you write a CSP</title>
      <dc:creator>Luuk Verhoeven</dc:creator>
      <pubDate>Tue, 17 Nov 2026 11:34:26 +0000</pubDate>
      <link>https://dev.to/luukverhoeven/how-to-inventory-every-origin-a-moodle-page-actually-loads-before-you-write-a-csp-5gkb</link>
      <guid>https://dev.to/luukverhoeven/how-to-inventory-every-origin-a-moodle-page-actually-loads-before-you-write-a-csp-5gkb</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  See the headers you already send
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -sI https://lms.example.org/login/index.php &lt;span class="o"&gt;|&lt;/span&gt; grep -iE &lt;span class="s1"&gt;'content-security-policy|x-frame-options|strict-transport|x-content-type|referrer-policy|permissions-policy'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An empty grep is common. Moodle does not ship a production CSP. The &lt;a href="https://owasp.org/www-project-secure-headers/" rel="noopener noreferrer"&gt;OWASP Secure Headers Project&lt;/a&gt; is the checklist; this article is about collecting the allow-list so you can fill it in without taking the exam week down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dump every origin the rendered page uses
&lt;/h2&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'script[src], link[href], iframe[src], img[src], video[src], audio[src], embed[src], object[data], source[src]'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;href&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;href&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That list is the start of &lt;code&gt;script-src&lt;/code&gt;, &lt;code&gt;style-src&lt;/code&gt;, &lt;code&gt;img-src&lt;/code&gt;, &lt;code&gt;frame-src&lt;/code&gt; and &lt;code&gt;media-src&lt;/code&gt;. It misses three Moodle-specific sources:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Inline scripts and styles.&lt;/strong&gt; TinyMCE, Atto leftovers, additional HTML in Site administration, and many plugins inject &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; without a &lt;code&gt;src&lt;/code&gt;. A policy without &lt;code&gt;'unsafe-inline'&lt;/code&gt; or a nonce will kill the editor. Moodle core still relies on inline in several flows; nonces are the right long-term fix, &lt;code&gt;'unsafe-inline'&lt;/code&gt; is what most sites ship in year one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;connect-src&lt;/code&gt;.&lt;/strong&gt; 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web workers and fonts.&lt;/strong&gt; MathJax and some themes pull fonts from a CDN. Add those origins to &lt;code&gt;font-src&lt;/code&gt; or host the files yourself.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Repeat the dump on &lt;code&gt;/login/index.php&lt;/code&gt;, a dashboard, a quiz attempt, and an LTI launch. Union the arrays. That union is your first report-only policy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turn the dump into a report-only header
&lt;/h2&gt;

&lt;p&gt;Do not enforce yet. Send &lt;code&gt;Content-Security-Policy-Report-Only&lt;/code&gt; from nginx or Apache so the browser logs what it &lt;em&gt;would&lt;/em&gt; have blocked.&lt;/p&gt;

&lt;p&gt;nginx, inside the &lt;code&gt;server&lt;/code&gt; block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;add_header&lt;/span&gt; &lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Security&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Policy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Report&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Only&lt;/span&gt; &lt;span class="s2"&gt;"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"&lt;/span&gt; &lt;span class="n"&gt;always&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace the YouTube origin with whatever your dump actually contained. &lt;code&gt;'unsafe-eval'&lt;/code&gt; is ugly; some Moodle plugins still need it. Track it as debt.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/csp-report&lt;/code&gt; needs an endpoint that accepts &lt;code&gt;application/csp-report&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;Apache equivalent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight apache"&gt;&lt;code&gt;&lt;span class="nc"&gt;Header&lt;/span&gt; &lt;span class="n"&gt;always&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Security&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Policy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Report&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Only&lt;/span&gt; &lt;span class="s2"&gt;"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"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;always&lt;/code&gt; keyword matters. Without it, 302s and 404s go out without the header, and a scanner that follows redirects will still call the policy missing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Moodle settings that fight the header
&lt;/h2&gt;

&lt;p&gt;Site administration → Security → HTTP security has &lt;strong&gt;Allow frame embedding&lt;/strong&gt;. If the LMS is framed by a portal, &lt;code&gt;X-Frame-Options: DENY&lt;/code&gt; and a tight &lt;code&gt;frame-ancestors&lt;/code&gt; 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 &lt;code&gt;frame-ancestors&lt;/code&gt;. Keep &lt;code&gt;X-Frame-Options: SAMEORIGIN&lt;/code&gt; for older browsers.&lt;/p&gt;

&lt;p&gt;Additional HTML in the site head is a CSP leak. Every tracking snippet a marketing team pastes there is a new &lt;code&gt;script-src&lt;/code&gt; origin. Prefer a tag manager origin you already listed, or move the snippet behind a plugin you control.&lt;/p&gt;

&lt;p&gt;H5P runs in a frame and uses inline scripts. If H5P is in production, &lt;code&gt;frame-src&lt;/code&gt; must include your own origin (and any H5P hub you use), and &lt;code&gt;script-src&lt;/code&gt; will keep &lt;code&gt;'unsafe-inline'&lt;/code&gt; until you can nonce the player.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enforce only after the reports go quiet
&lt;/h2&gt;

&lt;p&gt;Switch &lt;code&gt;Content-Security-Policy-Report-Only&lt;/code&gt; to &lt;code&gt;Content-Security-Policy&lt;/code&gt; when a week of real traffic produces only noise: browser extensions, stray &lt;code&gt;chrome-extension:&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;Verify again with &lt;code&gt;curl -sI&lt;/code&gt; on a 200, a login redirect, and a 404. Mozilla's &lt;a href="https://developer.mozilla.org/en-US/observatory" rel="noopener noreferrer"&gt;HTTP Observatory&lt;/a&gt; is a useful second opinion on the rest of the header set (HSTS, &lt;code&gt;X-Content-Type-Options&lt;/code&gt;, &lt;code&gt;Referrer-Policy&lt;/code&gt;). Those three are cheap; CSP is the one that breaks teaching.&lt;/p&gt;

&lt;p&gt;I built &lt;a href="https://guardlms.com" rel="noopener noreferrer"&gt;GuardLMS&lt;/a&gt; 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.&lt;/p&gt;

</description>
      <category>moodle</category>
      <category>php</category>
      <category>security</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
