Lesson 3.4: A theme ready for a Content Security Policy
A Content Security Policy tells the browser which scripts and styles a page may use. The policy Exponential ships
allows only files of the site itself: no <script> with code, no onclick=, no <style> block, no style="". A
theme written that way from the start can be put under the policy at any time; a theme full of inline code cannot.
This lesson makes learners write the first kind.
At a glance
| Course | Theme design |
| Module | Module 3: Stylesheets and scripts through the packer |
| Duration | 45 minutes (15 presenting, 30 exercise) |
| Level | Intermediate |
| Audience | Designers and front-end developers |
| Prerequisites | Lesson 3.1; a browser with developer tools |
Learning objectives
After this lesson, learners can:
- explain the four values of
[ContentSecurityPolicy] Modeand whatautodoes; - name what
script-src 'self'andstyle-src 'self'forbid in a template; - run their siteaccess in report-only mode and read a violation in the browser console;
- replace inline styles and scripts with classes,
data-*attributes and script files.
Trainer's notes
How to present it
Sources: Content Security Policy and Stylesheets and scripts for your site, CSP.
The modes (4 minutes).
site.ini [ContentSecurityPolicy] Mode:Value Effect disabledno policy report-onlythe browser reports what the policy would block and blocks nothing enforcethe browser blocks it auto(default)report-only on administration siteaccesses, disabled on the others What the policy says (4 minutes).
Directives[script-src]='self'allows script files of the site only: no inline<script>, noon...=attributes, nojavascript:links, noeval().Directives[style-src]='self'allows stylesheet files only: no<style>elements and nostyle="". Settingelement.stylefrom a script file stays allowed.The replacements (5 minutes):
Instead of Write <a onclick="openGallery()"><a class="js-gallery">andaddEventListenerin the theme's script file<script>var root = "...";</script>a data-*attribute the script file reads, or a<script type="application/json">data block<div style="width: 40%">a CSS class, or a data-*attribute the script readsa script from another host a copy in the design, or AdditionalSources[]=script-src https://<host>The path to enforce (2 minutes). Report-only first, work with it, fix what is reported, then
enforce. Who switches a live site is the site owner's decision, not the theme's.
Common questions
Is a <script type="application/json"> block not inline code? It is data; the browser does not run it, so the
policy does not block it.
Can editors' content break the policy? Yes, a style="" in content would be reported. That is content, not the
theme; start such sites with report-only.
Does the administration follow these rules? Yes; admin4 runs without inline code apart from documented exceptions (module 4).
Pitfalls
- The console is closed. Report-only violations only show in the developer console (or at
ReportURI). Open it before loading the page. - Leaving report-only on. It costs nothing visible, but end the exercise by removing the setting so the lab is back to its default.
- Third-party widgets. Maps, video players and tag managers usually need hosts in
AdditionalSources[]; check before promising a customer an enforced policy.
Hands-on exercise
On your training installation, with the browser's developer console open. D is your design folder.
Switch the policy to report-only for your siteaccess:
php bin/php/console exp:ini set site.ini/ContentSecurityPolicy/Mode report-only siteaccess:<site> php bin/php/ezcache.php --clear-tag=iniReload a page of
<site>. In the network panel, find the response headerContent-Security-Policy-Report-Onlyof the page and read itsscript-srcandstyle-src.Read the console: note any reported violation.
In
D/templates/page_footer.tpl, addstyle="color: red"to the<footer>element. Clear--clear-tag=templateand reload. Read the new console message.Remove the attribute again. Instead, add to
training.cssthe rule.training_theme-footer { color: #555; }(the class the wizard gave the footer). Run the routine of lesson 3.3 and reload: no new message.Pass the site's root address to a script without inline code: in
D/templates/pagelayout.tpl, add to the<body>element the attributedata-root={'/'|ezurl}.In
D/javascript/site.js, add:document.addEventListener( 'DOMContentLoaded', function () { var root = document.body.getAttribute( 'data-root' ); document.querySelectorAll( '.training_theme-logo' ).forEach( function ( a ) { a.setAttribute( 'title', 'Home: ' + root ); } ); } );Run the routine of lesson 3.3, reload, and hover over the site name in the header: the title shows the root.
Check the console once more: no violation from your theme.
Remove the policy setting again:
php bin/php/console exp:ini rem site.ini/ContentSecurityPolicy/Mode siteaccess:<site> php bin/php/ezcache.php --clear-tag=ini
Expected result
- Step 2: the page sends the report-only header with
script-src 'self'andstyle-src 'self'. - Step 4: the console reports that an inline style violates
style-src; the page still shows red, because nothing is blocked in report-only mode. - Steps 5 to 9: the footer color comes from the stylesheet, the script reads the data attribute, and the console shows no violation from the theme.
- Step 10: the page no longer sends the report-only header.
- The trainer checks one learner's console on the projector after step 9.
Quiz
- What does
Mode=autodo on a public siteaccess? - Which of these does
script-src 'self'block? A.<script src="...">from the site B. anonclick=attribute C. a script file in the design D.ezjsc::jquery - How do you pass a value from a template to a script without inline code?
- In report-only mode, an inline style violates the policy. Does the page still show it?
- A customer wants a map from another host on an enforced site. Which setting allows it?
Answers
- Nothing:
autois disabled on public siteaccesses and report-only on administrations (Content Security Policy). - B.
- In a
data-*attribute (or a JSON data block) that a script file reads. - Yes; report-only reports and blocks nothing.
[ContentSecurityPolicy] AdditionalSources[], for examplescript-src https://<host>.
Further reading
- Content Security Policy: directives, reports and the steps to enforce.
- Content Security Policy for your code: the developer's view.
- Removing inline scripts and styles: cleaning up an older theme.
- CSP audit per release: keeping a theme clean over time.
Related pages
- Module 3: Stylesheets and scripts through the packer
- Theme design: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.