@@ -89,7 +89,84 @@ the ``etag`` and ``last-modified`` options to their appropriate header.
8989Content Security Policy
9090=======================
9191
92- TODO
92+ One of the best protections you have against XSS attacks is to implement a Content Security Policy on the site.
93+ This forces you to whitelist every single source of content that is pulled in from your site's HTML,
94+ including images, stylesheets, javascript files, etc. The browser will refuse content from sources that don't meet
95+ the whitelist. This whitelist is created within the response's ``Content-Security-Policy `` header and has many
96+ different ways it can be configured.
97+
98+ This sounds complex, and on some sites, can definitely be challenging. For many simple sites, though, where all content
99+ is served by the same domain (http://example.com), it is very simple to integrate.
100+
101+ As this is a complex subject, this user guide will not go over all of the details. For more information, you should
102+ visit the following sites:
103+
104+ * `Content Security Policy main site <http://content-security-policy.com/ >`_
105+ * `W3C Specification <https://www.w3.org/TR/CSP >`_
106+ * `Introduction at HTML5Rocks <http://www.html5rocks.com/en/tutorials/security/content-security-policy/ >`_
107+ * `Article at SitePoint <https://www.sitepoint.com/improving-web-security-with-the-content-security-policy/ >`_
108+
109+ Turning CSP On
110+ --------------
111+
112+ By default, support for this is off. To enable support in your application, edit the ``CSPEnabled `` value in
113+ **application/Config/App.php **::
114+
115+ public $CSPEnabled = true;
116+
117+ When enabled, the response object will contain an instance of ``CodeIgniter\HTTP\ContentSecurityPolicy ``. The
118+ values set in **application/Config/ContentSecurityPolicy.php ** are applied to that instance and, if no changes are
119+ needed during runtime, then the correctly formatted header is sent and you're all done.
120+
121+ Runtime Configuration
122+ ---------------------
123+
124+ If your application needs to make changes at run-time, you can access the instance at ``$response->CSP ``. The
125+ class holds a number of methods that map pretty clearly to the appropriate header value that you need to set::
126+
127+ $reportOnly = true;
128+
129+ $response->CSP->reportOnly($reportOnly);
130+ $response->CSP->setBaseURI('example.com', true);
131+ $response->CSP->setDefaultSrc('cdn.example.com', $reportOnly);
132+ $response->CSP->setReportURI('http://example.com/csp/reports');
133+ $response->CSP->setSandbox(true, ['allow-forms', 'allow-scripts']);
134+ $response->CSP->upgradeInsecureRequests(true);
135+ $response->CSP->addChildSrc('https://youtube.com', $reportOnly);
136+ $response->CSP->addConnectSrc('https://*.facebook.com', $reportOnly);
137+ $response->CSP->addFontSrc('fonts.example.com', $reportOnly);
138+ $response->CSP->addFormAction('self', $reportOnly);
139+ $response->CSP->addFrameAncestor('none', $reportOnly);
140+ $response->CSP->addImageSrc('cdn.example.com', $reportOnly);
141+ $response->CSP->addMediaSrc('cdn.example.com', $reportOnly);
142+ $response->CSP->addObjectSrc('cdn.example.com', $reportOnly);
143+ $response->CSP->addPluginType('application/pdf', $reportOnly);
144+ $response->CSP->addScriptSrc('scripts.example.com', $reportOnly);
145+ $response->CSP->addStyleSrc('css.example.com', $reportOnly);
146+
147+ Inline Content
148+ --------------
149+
150+ It is possible to set a website to not protect even inline scripts and styles on its own pages, since this might have
151+ been the result of user-generated content. To protect against this, CSP allows you to specify a nonce within the
152+ ``<style> `` and ``<script> `` tags, and to add those values to the response's header. This is a pain to handle in real
153+ life, and is most secure when generated on the fly. To make this simple, you can include a ``{csp-style-nonce} `` or
154+ ``{csp-script-nonce} `` placeholder in the tag and it will be handled for you automatically::
155+
156+ // Original
157+ <script {csp-script-nonce}>
158+ console.log("Script won't run as it doesn't contain a nonce attribute");
159+ </script>
160+
161+ // Becomes
162+ <script nonce="Eskdikejidojdk978Ad8jf">
163+ console.log("Script won't run as it doesn't contain a nonce attribute");
164+ </script>
165+
166+ // OR
167+ <style {csp-style-nonce}>
168+ . . .
169+ </style>
93170
94171***************
95172Class Reference
0 commit comments