This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
/
ContentSecurityPolicyMiddleware.php
512 lines (481 loc) · 16.4 KB
/
ContentSecurityPolicyMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php
//
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. |
// | Visit https://www.codeinc.fr for more information about licensing. |
// +---------------------------------------------------------------------+
// | NOTICE: All information contained herein is, and remains the |
// | property of Code Inc. SAS. The intellectual and technical concepts |
// | contained herein are proprietary to Code Inc. SAS are protected by |
// | trade secret or copyright law. Dissemination of this information or |
// | reproduction of this material is strictly forbidden unless prior |
// | written permission is obtained from Code Inc. SAS. |
// +---------------------------------------------------------------------+
//
// Author: Joan Fabrégat <joan@codeinc.fr>
// Date: 07/03/2018
// Time: 02:05
// Project: SecurityMiddleware
//
declare(strict_types = 1);
namespace CodeInc\SecurityMiddleware;
use CodeInc\SecurityMiddleware\Tests\ContentSecurityPolicyMiddlewareTest;
/**
* Class ContentSecurityPolicyMiddleware
*
* @see ContentSecurityPolicyMiddlewareTest
* @package CodeInc\SecurityMiddleware
* @author Joan Fabrégat <joan@codeinc.fr>
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy
* @license MIT <https://github.com/CodeIncHQ/SecurityMiddleware/blob/master/LICENSE>
* @link https://github.com/CodeIncHQ/SecurityMiddleware
*/
class ContentSecurityPolicyMiddleware extends AbstractHttpHeaderMiddleware
{
public const SRC_SELF = '\'self\'';
public const SRC_NONE = '\'none\'';
public const SRC_UNSAFE_INLINE = '\'unsafe-inline\'';
public const SRC_UNSAFE_EVAL = '\'unsafe-eval\'';
// values for the referer policy
public const REFERER_POLICY_NONE = 'none';
public const REFERER_POLICY_NO_REFERRER = 'no-referrer';
public const REFERER_POLICY_NONE_WHEN_DOWNGRADE = 'none-when-downgrade';
public const REFERER_POLICY_ORIGIN = 'origin';
public const REFERER_POLICY_ORIGIN_WHEN_CROSS_ORIGIN = 'origin-when-cross-origin';
public const REFERER_POLICY_ORIGIN_WHEN_CROSSORIGIN = 'origin-when-crossorigin';
public const REFERER_POLICY_UNSAFE_URL = 'unsafe-url';
public const REFERER_POLICY_VALUES = [
self::REFERER_POLICY_NONE, self::REFERER_POLICY_NO_REFERRER, self::REFERER_POLICY_NONE_WHEN_DOWNGRADE,
self::REFERER_POLICY_ORIGIN, self::REFERER_POLICY_ORIGIN_WHEN_CROSS_ORIGIN,
self::REFERER_POLICY_ORIGIN_WHEN_CROSSORIGIN, self::REFERER_POLICY_UNSAFE_URL,
];
// values for the sandbox
public const SANDBOX_ALLOW_FORMS = 'allow-forms';
public const SANDBOX_ALLOW_MODALS = 'allow-modals';
public const SANDBOX_ALLOW_ORIENTATION_LOCK = 'allow-orientation-lock';
public const SANDBOX_ALLOW_POINTER_LOCK = 'allow-pointer-lock';
public const SANDBOX_ALLOW_POPUPS = 'allow-popups';
public const SANDBOX_ALLOW_POPUPS_TO_ESCAPE_SANDBOX = 'allow-popups-to-escape-sandbox';
public const SANDBOX_ALLOW_PRESENTATION = 'allow-presentation';
public const SANDBOX_ALLOW_SAME_ORIGIN = 'allow-same-origin';
public const SANDBOX_ALLOW_SCRIPTS = 'allow-scripts';
public const SANDBOX_ALLOW_TOP_NAVIGATION = 'allow-top-navigation';
public const SANDBOX_VALUES = [
self::SANDBOX_ALLOW_FORMS, self::SANDBOX_ALLOW_MODALS, self::SANDBOX_ALLOW_ORIENTATION_LOCK,
self::SANDBOX_ALLOW_POINTER_LOCK, self::SANDBOX_ALLOW_POPUPS, self::SANDBOX_ALLOW_POPUPS_TO_ESCAPE_SANDBOX,
self::SANDBOX_ALLOW_PRESENTATION, self::SANDBOX_ALLOW_SAME_ORIGIN, self::SANDBOX_ALLOW_SCRIPTS,
self::SANDBOX_ALLOW_TOP_NAVIGATION
];
/**
* CSP tags.
*
* @var string[][]|string[]|bool[]|null[]
*/
private $tags = [
'default-src' => [],
'base-uri' => [],
'child-src' => [],
'connect-src' => [],
'font-src' => [],
'form-action' => [],
'frame-ancestors' => [],
'frame-src' => [],
'img-src' => [],
'manifest-src' => [],
'media-src' => [],
'object-src' => [],
'script-src' => [],
'style-src' => [],
'worker-src' => [],
'referer' => null,
'plugin-types' => [],
'report-uri' => [],
'require-sri-for' => [],
'sandbox' => null,
'upgrade-insecure-requests' => false,
'block-all-mixed-content' => false,
];
/**
* ContentSecurityPolicyMiddleware constructor.
*/
public function __construct()
{
parent::__construct('Content-Security-Policy');
}
/**
* Enable the 'upgrade-insecure-requests' flag or the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/upgrade-insecure-requests
*/
public function upgradeInsecureRequests():void
{
$this->tags['upgrade-insecure-requests'] = true;
}
/**
* Enable the 'block-all-mixed-content' flag of the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/block-all-mixed-content
*/
public function blockAllMixedContent():void
{
$this->tags['block-all-mixed-content'] = true;
}
/**
* Sets the 'sandbox' value of the CSP. See the SANDBOX_XYZ class constants for the possible values.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/sandbox
* @param string $sandbox
* @throws MiddlewareException
*/
public function setSandbox(string $sandbox):void
{
if (!in_array($sandbox, self::SANDBOX_VALUES)) {
throw new MiddlewareException(
$this,
sprintf(
"%s is not a correct value for the CSP sandbox, correct values are: %s (see %s)",
$sandbox, implode(', ', self::SANDBOX_VALUES),
'https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/sandbox'
)
);
}
$this->tags['sandbox'] = $sandbox;
}
/**
* Sets the 'referrer' value of the CSP. See the REFERER_POLICY_XYZ class constants for the possible values.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/referrer
* @param string $refererPolicy
* @throws MiddlewareException
*/
public function setRefererPolicy(string $refererPolicy):void
{
if (!in_array($refererPolicy, self::REFERER_POLICY_VALUES)) {
throw new MiddlewareException(
$this,
sprintf(
"%s is not a valid CSP referer policy, correct values are: %s (see %s)",
$refererPolicy, implode(', ', self::REFERER_POLICY_VALUES),
'https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/referrer'
)
);
}
$this->tags['referer'] = $refererPolicy;
}
/**
* Sets the 'require-sri-for' value of the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/require-sri-for
* @param bool $script
* @param bool $style
*/
public function requireSriFor(bool $script, bool $style):void
{
if ($script && $style) {
$this->tags['require-sri-for'] = ['script', 'style'];
}
else if ($script) {
$this->tags['require-sri-for'] = ['script'];
}
else if ($style) {
$this->tags['require-sri-for'] = ['style'];
}
}
/**
* Sets the CSP's report URI.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/report-uri
* @param string $reportUri
* @throws MiddlewareException
* @return bool
*/
public function addReportUri(string $reportUri):bool
{
if (!filter_var($reportUri, FILTER_VALIDATE_URL)) {
throw new MiddlewareException(
$this,
sprintf(
"'%s' is not a valid URI and can not be set as the CSP report URI",
$reportUri
)
);
}
if (!in_array($reportUri, $this->tags['report-uri'])) {
$this->tags['report-uri'][] = $reportUri;
return true;
}
return false;
}
/**
* Add a new plugin media type ot the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/plugin-types
* @param string $mediaType
* @throws MiddlewareException
* @return bool
*/
public function addPluginType(string $mediaType):bool
{
if (!preg_match('#^[-\w]+/[-\w]+$#ui', $mediaType)) {
throw new MiddlewareException(
$this,
sprintf(
"'%s' is not a valid media type and can not be used as a CSP plugin type",
$mediaType
)
);
}
if (!in_array($mediaType, $this->tags['plugin-types'])) {
$this->tags['plugin-types'][] = $mediaType;
return true;
}
return false;
}
/**
* Adds a 'default-src' source to the CSP.
*
* @param string $source
* @return bool
*/
public function addDefaultSrc(string $source):bool
{
if (!in_array($source, $this->tags['default-src'])) {
$this->tags['default-src'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'base-uri' to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/base-uri
* @param string $source
* @return bool
*/
public function addBaseUri(string $source):bool
{
if (!in_array($source, $this->tags['base-uri'])) {
$this->tags['base-uri'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'child-src' source to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/child-src
* @param string $source
* @return bool
*/
public function addChildSrc(string $source):bool
{
if (!in_array($source, $this->tags['child-src'])) {
$this->tags['child-src'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'connect-src' source to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/connect-src
* @param string $source
* @return bool
*/
public function addConnectSrc(string $source):bool
{
if (!in_array($source, $this->tags['connect-src'])) {
$this->tags['connect-src'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'font-src' source to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/font-src
* @param string $source
* @return bool
*/
public function addFontSrc(string $source):bool
{
if (!in_array($source, $this->tags['font-src'])) {
$this->tags['font-src'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'form-action' to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/form-action
* @param string $source
* @return bool
*/
public function addFormAction(string $source):bool
{
if (!in_array($source, $this->tags['form-action'])) {
$this->tags['form-action'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'frame-ancestors' to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors
* @param string $source
* @return bool
*/
public function addFrameAncestors(string $source):bool
{
if (!in_array($source, $this->tags['frame-ancestors'])) {
$this->tags['frame-ancestors'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'frame-src' source to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
* @param string $source
* @return bool
*/
public function addFrameSrc(string $source):bool
{
if (!in_array($source, $this->tags['frame-src'])) {
$this->tags['frame-src'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'img-src' source to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/img-src
* @param string $source
* @return bool
*/
public function addImgSrc(string $source):bool
{
if (!in_array($source, $this->tags['img-src'])) {
$this->tags['img-src'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'manifest-src' source to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/manifest-src
* @param string $source
* @return bool
*/
public function addManifestSrc(string $source):bool
{
if (!in_array($source, $this->tags['manifest-src'])) {
$this->tags['manifest-src'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'media-src' source to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/media-src
* @param string $source
* @return bool
*/
public function addMediaSrc(string $source):bool
{
if (!in_array($source, $this->tags['media-src'])) {
$this->tags['media-src'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'object-src' source to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/object-src
* @param string $source
* @return bool
*/
public function addObjectSrc(string $source):bool
{
if (!in_array($source, $this->tags['object-src'])) {
$this->tags['object-src'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'script-src' source to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/script-src
* @param string $source
* @return bool
*/
public function addScriptSrc(string $source):bool
{
if (!in_array($source, $this->tags['script-src'])) {
$this->tags['script-src'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'style-src' source to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/style-src
* @param string $source
* @return bool
*/
public function addStyleSrc(string $source):bool
{
if (!in_array($source, $this->tags['style-src'])) {
$this->tags['style-src'][] = $source;
return true;
}
return false;
}
/**
* Adds a 'worker-src' source to the CSP.
*
* @link https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/worker-src
* @param string $source
* @return bool
*/
public function addWorkerSrc(string $source):bool
{
if (!in_array($source, $this->tags['worker-src'])) {
$this->tags['worker-src'][] = $source;
return true;
}
return false;
}
/**
* Returns the CSP header value.
*
* @return string|null
*/
public function getHeaderValue():?string
{
$headerValue = [];
foreach ($this->tags as $name => $tagValue) {
if (is_array($tagValue) && !empty($tagValue)) {
$headerValue[] = $name.' '.implode(' ', $tagValue).';';
}
elseif (is_string($tagValue) && !empty($tagValue)) {
$headerValue[] = $name.' '.$tagValue.';';
}
elseif (is_bool($tagValue) && $tagValue) {
$headerValue[] = $name.';';
}
}
return $headerValue ? implode(' ', $headerValue) : null;
}
}