Skip to content

Commit c1b39dc

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.5
2 parents a5bd769 + 6c187dd commit c1b39dc

File tree

10 files changed

+140
-125
lines changed

10 files changed

+140
-125
lines changed

system/HTTP/SiteURI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private function normalizeBaseURL(App $configApp): string
197197
// Validate baseURL
198198
if (filter_var($baseURL, FILTER_VALIDATE_URL) === false) {
199199
throw new ConfigException(
200-
'Config\App::$baseURL is invalid.'
200+
'Config\App::$baseURL "' . $baseURL . '" is not a valid URL.'
201201
);
202202
}
203203

system/Test/ControllerTestTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function execute(string $method, ...$params)
215215
/**
216216
* Set controller's config, with method chaining.
217217
*
218-
* @param mixed $appConfig
218+
* @param App $appConfig
219219
*
220220
* @return $this
221221
*/
@@ -229,7 +229,7 @@ public function withConfig($appConfig)
229229
/**
230230
* Set controller's request, with method chaining.
231231
*
232-
* @param mixed $request
232+
* @param IncomingRequest $request
233233
*
234234
* @return $this
235235
*/
@@ -260,7 +260,7 @@ public function withResponse($response)
260260
/**
261261
* Set controller's logger, with method chaining.
262262
*
263-
* @param mixed $logger
263+
* @param LoggerInterface $logger
264264
*
265265
* @return $this
266266
*/

user_guide_src/source/libraries/validation.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,6 @@ To give a labeled error message you can set up as:
320320
.. literalinclude:: validation/007.php
321321
:lines: 2-
322322

323-
.. _validation-withrequest:
324-
325323
.. note:: ``setRules()`` will overwrite any rules that were set previously. To add more than one
326324
rule to an existing set of rules, use ``setRule()`` multiple times.
327325

@@ -351,6 +349,8 @@ For example, data returned by multi select dropdown:
351349
.. literalinclude:: validation/011.php
352350
:lines: 2-
353351

352+
.. _validation-withrequest:
353+
354354
withRequest()
355355
=============
356356

user_guide_src/source/libraries/validation/008.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
$request = \Config\Services::request();
55

66
if ($validation->withRequest($request)->run()) {
7-
// If you want to get the validated data.
7+
// If you use the input data, you should get it from the getValidated() method.
8+
// Otherwise you may create a vulnerability.
89
$validData = $validation->getValidated();
910

1011
// ...
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
.. _content-security-policy:
2+
3+
#######################
4+
Content Security Policy
5+
#######################
6+
7+
.. contents::
8+
:local:
9+
:depth: 2
10+
11+
********************************
12+
What is Content Security Policy?
13+
********************************
14+
15+
One of the best protections you have against XSS attacks is to implement a Content
16+
Security Policy (CSP) on the site. This requires you to specify and authorize each
17+
source of content that is included in your site's HTML, including images,
18+
stylesheets, JavaScript files, and so on. The browser will reject content from
19+
sources that are not explicitly approved. This authorization is defined within
20+
the response's ``Content-Security-Policy`` header and offers various configuration
21+
options.
22+
23+
This sounds complex, and on some sites, can definitely be challenging. For many simple sites, though, where all content
24+
is served by the same domain (http://example.com), it is very simple to integrate.
25+
26+
As this is a complex subject, this user guide will not go over all of the details. For more information, you should
27+
visit the following sites:
28+
29+
* `Content Security Policy main site <https://content-security-policy.com/>`_
30+
* `W3C Specification <https://www.w3.org/TR/CSP>`_
31+
* `Introduction at HTML5Rocks <https://www.html5rocks.com/en/tutorials/security/content-security-policy/>`_
32+
* `Article at SitePoint <https://www.sitepoint.com/improving-web-security-with-the-content-security-policy/>`_
33+
34+
**************
35+
Turning CSP On
36+
**************
37+
38+
.. important:: The :ref:`Debug Toolbar <the-debug-toolbar>` may use Kint, which
39+
outputs inline scripts. Therefore, when CSP is turned on, CSP nonce is
40+
automatically output for the Debug Toolbar. However, if you are not using
41+
CSP nonce, this will change the CSP header to something you do not intend,
42+
and it will behave differently than in production; if you want to verify CSP
43+
behavior, turn off the Debug Toolbar.
44+
45+
By default, support for this is off. To enable support in your application, edit the ``CSPEnabled`` value in
46+
**app/Config/App.php**:
47+
48+
.. literalinclude:: csp/011.php
49+
50+
When enabled, the response object will contain an instance of ``CodeIgniter\HTTP\ContentSecurityPolicy``. The
51+
values set in **app/Config/ContentSecurityPolicy.php** are applied to that instance, and if no changes are
52+
needed during runtime, then the correctly formatted header is sent and you're all done.
53+
54+
With CSP enabled, two header lines are added to the HTTP response: a **Content-Security-Policy** header, with
55+
policies identifying content types or origins that are explicitly allowed for different
56+
contexts, and a **Content-Security-Policy-Report-Only** header, which identifies content types
57+
or origins that will be allowed but which will also be reported to the destination
58+
of your choice.
59+
60+
Our implementation provides for a default treatment, changeable through the ``reportOnly()`` method.
61+
When an additional entry is added to a CSP directive, as shown below, it will be added
62+
to the CSP header appropriate for blocking or preventing. That can be overridden on a per
63+
call basis, by providing an optional second parameter to the adding method call.
64+
65+
*********************
66+
Runtime Configuration
67+
*********************
68+
69+
If your application needs to make changes at run-time, you can access the instance at ``$this->response->getCSP()`` in your controllers. The
70+
class holds a number of methods that map pretty clearly to the appropriate header value that you need to set.
71+
Examples are shown below, with different combinations of parameters, though all accept either a directive
72+
name or an array of them:
73+
74+
.. literalinclude:: csp/012.php
75+
76+
The first parameter to each of the "add" methods is an appropriate string value,
77+
or an array of them.
78+
79+
The ``reportOnly()`` method allows you to specify the default reporting treatment
80+
for subsequent sources, unless over-ridden. For instance, you could specify
81+
that youtube.com was allowed, and then provide several allowed but reported sources:
82+
83+
.. literalinclude:: csp/013.php
84+
85+
**************
86+
Inline Content
87+
**************
88+
89+
It is possible to set a website to not protect even inline scripts and styles on its own pages, since this might have
90+
been the result of user-generated content. To protect against this, CSP allows you to specify a nonce within the
91+
``<style>`` and ``<script>`` tags, and to add those values to the response's header. This is a pain to handle in real
92+
life, and is most secure when generated on the fly. To make this simple, you can include a ``{csp-style-nonce}`` or
93+
``{csp-script-nonce}`` placeholder in the tag and it will be handled for you automatically::
94+
95+
// Original
96+
<script {csp-script-nonce}>
97+
console.log("Script won't run as it doesn't contain a nonce attribute");
98+
</script>
99+
100+
// Becomes
101+
<script nonce="Eskdikejidojdk978Ad8jf">
102+
console.log("Script won't run as it doesn't contain a nonce attribute");
103+
</script>
104+
105+
// OR
106+
<style {csp-style-nonce}>
107+
. . .
108+
</style>
109+
110+
.. warning:: If an attacker injects a string like ``<script {csp-script-nonce}>``, it might become the real nonce attribute with this functionality. You can customize the placeholder string with the ``$scriptNonceTag`` and ``$styleNonceTag`` properties in **app/Config/ContentSecurityPolicy.php**.
111+
112+
If you don't like this auto replacement functionality, you can turn it off with setting ``$autoNonce = false`` in **app/Config/ContentSecurityPolicy.php**.
113+
114+
In this case, you can use the functions, :php:func:`csp_script_nonce()` and :php:func:`csp_style_nonce()`::
115+
116+
// Original
117+
<script <?= csp_script_nonce() ?>>
118+
console.log("Script won't run as it doesn't contain a nonce attribute");
119+
</script>
120+
121+
// Becomes
122+
<script nonce="Eskdikejidojdk978Ad8jf">
123+
console.log("Script won't run as it doesn't contain a nonce attribute");
124+
</script>
125+
126+
// OR
127+
<style <?= csp_style_nonce() ?>>
128+
. . .
129+
</style>

user_guide_src/source/outgoing/response/011.php renamed to user_guide_src/source/outgoing/csp/011.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class App extends BaseConfig
88
{
9-
public bool $CSPEnabled = true;
10-
119
// ...
10+
11+
public bool $CSPEnabled = true;
1212
}

user_guide_src/source/outgoing/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ View components are used to build what is returned to the user.
1616
table
1717
response
1818
api_responses
19+
csp
1920
localization
2021
alternative_php

user_guide_src/source/outgoing/response.rst

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -199,122 +199,6 @@ to the ``Cache-Control`` header. You are free to set all of the options exactly
199199
situation. While most of the options are applied to the ``Cache-Control`` header, it intelligently handles
200200
the ``etag`` and ``last-modified`` options to their appropriate header.
201201

202-
.. _content-security-policy:
203-
204-
Content Security Policy
205-
=======================
206-
207-
One of the best protections you have against XSS attacks is to implement a Content Security Policy on the site.
208-
This forces you to whitelist every single source of content that is pulled in from your site's HTML,
209-
including images, stylesheets, javascript files, etc. The browser will refuse content from sources that don't meet
210-
the whitelist. This whitelist is created within the response's ``Content-Security-Policy`` header and has many
211-
different ways it can be configured.
212-
213-
This sounds complex, and on some sites, can definitely be challenging. For many simple sites, though, where all content
214-
is served by the same domain (http://example.com), it is very simple to integrate.
215-
216-
As this is a complex subject, this user guide will not go over all of the details. For more information, you should
217-
visit the following sites:
218-
219-
* `Content Security Policy main site <https://content-security-policy.com/>`_
220-
* `W3C Specification <https://www.w3.org/TR/CSP>`_
221-
* `Introduction at HTML5Rocks <https://www.html5rocks.com/en/tutorials/security/content-security-policy/>`_
222-
* `Article at SitePoint <https://www.sitepoint.com/improving-web-security-with-the-content-security-policy/>`_
223-
224-
Turning CSP On
225-
--------------
226-
227-
.. important:: The :ref:`Debug Toolbar <the-debug-toolbar>` may use Kint, which
228-
outputs inline scripts. Therefore, when CSP is turned on, CSP nonce is
229-
automatically output for the Debug Toolbar. However, if you are not using
230-
CSP nonce, this will change the CSP header to something you do not intend,
231-
and it will behave differently than in production; if you want to verify CSP
232-
behavior, turn off the Debug Toolbar.
233-
234-
By default, support for this is off. To enable support in your application, edit the ``CSPEnabled`` value in
235-
**app/Config/App.php**:
236-
237-
.. literalinclude:: response/011.php
238-
239-
When enabled, the response object will contain an instance of ``CodeIgniter\HTTP\ContentSecurityPolicy``. The
240-
values set in **app/Config/ContentSecurityPolicy.php** are applied to that instance, and if no changes are
241-
needed during runtime, then the correctly formatted header is sent and you're all done.
242-
243-
With CSP enabled, two header lines are added to the HTTP response: a **Content-Security-Policy** header, with
244-
policies identifying content types or origins that are explicitly allowed for different
245-
contexts, and a **Content-Security-Policy-Report-Only** header, which identifies content types
246-
or origins that will be allowed but which will also be reported to the destination
247-
of your choice.
248-
249-
Our implementation provides for a default treatment, changeable through the ``reportOnly()`` method.
250-
When an additional entry is added to a CSP directive, as shown below, it will be added
251-
to the CSP header appropriate for blocking or preventing. That can be overridden on a per
252-
call basis, by providing an optional second parameter to the adding method call.
253-
254-
Runtime Configuration
255-
---------------------
256-
257-
If your application needs to make changes at run-time, you can access the instance at ``$this->response->getCSP()`` in your controllers. The
258-
class holds a number of methods that map pretty clearly to the appropriate header value that you need to set.
259-
Examples are shown below, with different combinations of parameters, though all accept either a directive
260-
name or an array of them:
261-
262-
.. literalinclude:: response/012.php
263-
264-
The first parameter to each of the "add" methods is an appropriate string value,
265-
or an array of them.
266-
267-
The ``reportOnly()`` method allows you to specify the default reporting treatment
268-
for subsequent sources, unless over-ridden. For instance, you could specify
269-
that youtube.com was allowed, and then provide several allowed but reported sources:
270-
271-
.. literalinclude:: response/013.php
272-
273-
Inline Content
274-
--------------
275-
276-
It is possible to set a website to not protect even inline scripts and styles on its own pages, since this might have
277-
been the result of user-generated content. To protect against this, CSP allows you to specify a nonce within the
278-
``<style>`` and ``<script>`` tags, and to add those values to the response's header. This is a pain to handle in real
279-
life, and is most secure when generated on the fly. To make this simple, you can include a ``{csp-style-nonce}`` or
280-
``{csp-script-nonce}`` placeholder in the tag and it will be handled for you automatically::
281-
282-
// Original
283-
<script {csp-script-nonce}>
284-
console.log("Script won't run as it doesn't contain a nonce attribute");
285-
</script>
286-
287-
// Becomes
288-
<script nonce="Eskdikejidojdk978Ad8jf">
289-
console.log("Script won't run as it doesn't contain a nonce attribute");
290-
</script>
291-
292-
// OR
293-
<style {csp-style-nonce}>
294-
. . .
295-
</style>
296-
297-
.. warning:: If an attacker injects a string like ``<script {csp-script-nonce}>``, it might become the real nonce attribute with this functionality. You can customize the placeholder string with the ``$scriptNonceTag`` and ``$styleNonceTag`` properties in **app/Config/ContentSecurityPolicy.php**.
298-
299-
If you don't like this auto replacement functionality, you can turn it off with setting ``$autoNonce = false`` in **app/Config/ContentSecurityPolicy.php**.
300-
301-
In this case, you can use the functions, :php:func:`csp_script_nonce()` and :php:func:`csp_style_nonce()`::
302-
303-
// Original
304-
<script <?= csp_script_nonce() ?>>
305-
console.log("Script won't run as it doesn't contain a nonce attribute");
306-
</script>
307-
308-
// Becomes
309-
<script nonce="Eskdikejidojdk978Ad8jf">
310-
console.log("Script won't run as it doesn't contain a nonce attribute");
311-
</script>
312-
313-
// OR
314-
<style <?= csp_style_nonce() ?>>
315-
. . .
316-
</style>
317-
318202
Class Reference
319203
===============
320204

0 commit comments

Comments
 (0)