Skip to content

Commit 70c768d

Browse files
authored
Merge pull request #7383 from kenjis/fix-docs-CSP
docs: update CSP description
2 parents 775a99e + a921e66 commit 70c768d

3 files changed

Lines changed: 31 additions & 25 deletions

File tree

user_guide_src/source/outgoing/response.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ call basis, by providing an optional second parameter to the adding method call.
170170
Runtime Configuration
171171
---------------------
172172

173-
If your application needs to make changes at run-time, you can access the instance at ``$this->response->CSP`` in your controllers. The
173+
If your application needs to make changes at run-time, you can access the instance at ``$this->response->getCSP()`` in your controllers. The
174174
class holds a number of methods that map pretty clearly to the appropriate header value that you need to set.
175175
Examples are shown below, with different combinations of parameters, though all accept either a directive
176176
name or an array of them:
@@ -180,7 +180,7 @@ name or an array of them:
180180
The first parameter to each of the "add" methods is an appropriate string value,
181181
or an array of them.
182182

183-
The ``reportOnly`` method allows you to specify the default reporting treatment
183+
The ``reportOnly()`` method allows you to specify the default reporting treatment
184184
for subsequent sources, unless over-ridden. For instance, you could specify
185185
that youtube.com was allowed, and then provide several allowed but reported sources:
186186

@@ -214,7 +214,7 @@ life, and is most secure when generated on the fly. To make this simple, you can
214214

215215
If you don't like this auto replacement functionality, you can turn it off with setting ``$autoNonce = false`` in **app/Config/ContentSecurityPolicy.php**.
216216

217-
In this case, you can use the functions, ``csp_script_nonce()`` and ``csp_style_nonce()``::
217+
In this case, you can use the functions, :php:func:`csp_script_nonce()` and :php:func:`csp_style_nonce()`::
218218

219219
// Original
220220
<script <?= csp_script_nonce() ?>>
Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
<?php
22

3+
// get the CSP instance
4+
$csp = $this->response->getCSP();
5+
36
// specify the default directive treatment
4-
$this->response->CSP->reportOnly(false);
7+
$csp->reportOnly(false);
58

69
// specify the origin to use if none provided for a directive
7-
$this->response->CSP->setDefaultSrc('cdn.example.com');
10+
$csp->setDefaultSrc('cdn.example.com');
811

912
// specify the URL that "report-only" reports get sent to
10-
$this->response->CSP->setReportURI('http://example.com/csp/reports');
13+
$csp->setReportURI('http://example.com/csp/reports');
1114

1215
// specify that HTTP requests be upgraded to HTTPS
13-
$this->response->CSP->upgradeInsecureRequests(true);
16+
$csp->upgradeInsecureRequests(true);
1417

1518
// add types or origins to CSP directives
1619
// assuming that the default treatment is to block rather than just report
17-
$this->response->CSP->addBaseURI('example.com', true); // report only
18-
$this->response->CSP->addChildSrc('https://youtube.com'); // blocked
19-
$this->response->CSP->addConnectSrc('https://*.facebook.com', false); // blocked
20-
$this->response->CSP->addFontSrc('fonts.example.com');
21-
$this->response->CSP->addFormAction('self');
22-
$this->response->CSP->addFrameAncestor('none', true); // report this one
23-
$this->response->CSP->addImageSrc('cdn.example.com');
24-
$this->response->CSP->addMediaSrc('cdn.example.com');
25-
$this->response->CSP->addManifestSrc('cdn.example.com');
26-
$this->response->CSP->addObjectSrc('cdn.example.com', false); // reject from here
27-
$this->response->CSP->addPluginType('application/pdf', false); // reject this media type
28-
$this->response->CSP->addScriptSrc('scripts.example.com', true); // allow but report requests from here
29-
$this->response->CSP->addStyleSrc('css.example.com');
30-
$this->response->CSP->addSandbox(['allow-forms', 'allow-scripts']);
20+
$csp->addBaseURI('example.com', true); // report only
21+
$csp->addChildSrc('https://youtube.com'); // blocked
22+
$csp->addConnectSrc('https://*.facebook.com', false); // blocked
23+
$csp->addFontSrc('fonts.example.com');
24+
$csp->addFormAction('self');
25+
$csp->addFrameAncestor('none', true); // report this one
26+
$csp->addImageSrc('cdn.example.com');
27+
$csp->addMediaSrc('cdn.example.com');
28+
$csp->addManifestSrc('cdn.example.com');
29+
$csp->addObjectSrc('cdn.example.com', false); // reject from here
30+
$csp->addPluginType('application/pdf', false); // reject this media type
31+
$csp->addScriptSrc('scripts.example.com', true); // allow but report requests from here
32+
$csp->addStyleSrc('css.example.com');
33+
$csp->addSandbox(['allow-forms', 'allow-scripts']);
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

3-
$this->response->CSP->addChildSrc('https://youtube.com'); // allowed
4-
$this->response->CSP->reportOnly(true);
5-
$this->response->CSP->addChildSrc('https://metube.com'); // allowed but reported
6-
$this->response->CSP->addChildSrc('https://ourtube.com', false); // allowed
3+
// get the CSP instance
4+
$csp = $this->response->getCSP();
5+
6+
$csp->addChildSrc('https://youtube.com'); // allowed
7+
$csp->reportOnly(true);
8+
$csp->addChildSrc('https://metube.com'); // allowed but reported
9+
$csp->addChildSrc('https://ourtube.com', false); // allowed

0 commit comments

Comments
 (0)