Completed
Pull Request — master (#3870)
by Lukas
15:00
created
lib/public/AppFramework/Http/EmptyContentSecurityPolicy.php 2 patches
Indentation   +372 added lines, -372 removed lines patch added patch discarded remove patch
@@ -36,376 +36,376 @@
 block discarded – undo
36 36
  * @since 9.0.0
37 37
  */
38 38
 class EmptyContentSecurityPolicy {
39
-	/** @var bool Whether inline JS snippets are allowed */
40
-	protected $inlineScriptAllowed = null;
41
-	/** @var string Whether JS nonces should be used */
42
-	protected $useJsNonce = null;
43
-	/**
44
-	 * @var bool Whether eval in JS scripts is allowed
45
-	 * TODO: Disallow per default
46
-	 * @link https://github.com/owncloud/core/issues/11925
47
-	 */
48
-	protected $evalScriptAllowed = null;
49
-	/** @var array Domains from which scripts can get loaded */
50
-	protected $allowedScriptDomains = null;
51
-	/**
52
-	 * @var bool Whether inline CSS is allowed
53
-	 * TODO: Disallow per default
54
-	 * @link https://github.com/owncloud/core/issues/13458
55
-	 */
56
-	protected $inlineStyleAllowed = null;
57
-	/** @var array Domains from which CSS can get loaded */
58
-	protected $allowedStyleDomains = null;
59
-	/** @var array Domains from which images can get loaded */
60
-	protected $allowedImageDomains = null;
61
-	/** @var array Domains to which connections can be done */
62
-	protected $allowedConnectDomains = null;
63
-	/** @var array Domains from which media elements can be loaded */
64
-	protected $allowedMediaDomains = null;
65
-	/** @var array Domains from which object elements can be loaded */
66
-	protected $allowedObjectDomains = null;
67
-	/** @var array Domains from which iframes can be loaded */
68
-	protected $allowedFrameDomains = null;
69
-	/** @var array Domains from which fonts can be loaded */
70
-	protected $allowedFontDomains = null;
71
-	/** @var array Domains from which web-workers and nested browsing content can load elements */
72
-	protected $allowedChildSrcDomains = null;
73
-
74
-	/**
75
-	 * Whether inline JavaScript snippets are allowed or forbidden
76
-	 * @param bool $state
77
-	 * @return $this
78
-	 * @since 8.1.0
79
-	 * @deprecated 10.0 CSP tokens are now used
80
-	 */
81
-	public function allowInlineScript($state = false) {
82
-		$this->inlineScriptAllowed = $state;
83
-		return $this;
84
-	}
85
-
86
-	/**
87
-	 * Use the according JS nonce
88
-	 *
89
-	 * @param string $nonce
90
-	 * @return $this
91
-	 * @since 11.0.0
92
-	 */
93
-	public function useJsNonce($nonce) {
94
-		$this->useJsNonce = $nonce;
95
-		return $this;
96
-	}
97
-
98
-	/**
99
-	 * Whether eval in JavaScript is allowed or forbidden
100
-	 * @param bool $state
101
-	 * @return $this
102
-	 * @since 8.1.0
103
-	 */
104
-	public function allowEvalScript($state = true) {
105
-		$this->evalScriptAllowed = $state;
106
-		return $this;
107
-	}
108
-
109
-	/**
110
-	 * Allows to execute JavaScript files from a specific domain. Use * to
111
-	 * allow JavaScript from all domains.
112
-	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
113
-	 * @return $this
114
-	 * @since 8.1.0
115
-	 */
116
-	public function addAllowedScriptDomain($domain) {
117
-		$this->allowedScriptDomains[] = $domain;
118
-		return $this;
119
-	}
120
-
121
-	/**
122
-	 * Remove the specified allowed script domain from the allowed domains.
123
-	 *
124
-	 * @param string $domain
125
-	 * @return $this
126
-	 * @since 8.1.0
127
-	 */
128
-	public function disallowScriptDomain($domain) {
129
-		$this->allowedScriptDomains = array_diff($this->allowedScriptDomains, [$domain]);
130
-		return $this;
131
-	}
132
-
133
-	/**
134
-	 * Whether inline CSS snippets are allowed or forbidden
135
-	 * @param bool $state
136
-	 * @return $this
137
-	 * @since 8.1.0
138
-	 */
139
-	public function allowInlineStyle($state = true) {
140
-		$this->inlineStyleAllowed = $state;
141
-		return $this;
142
-	}
143
-
144
-	/**
145
-	 * Allows to execute CSS files from a specific domain. Use * to allow
146
-	 * CSS from all domains.
147
-	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
148
-	 * @return $this
149
-	 * @since 8.1.0
150
-	 */
151
-	public function addAllowedStyleDomain($domain) {
152
-		$this->allowedStyleDomains[] = $domain;
153
-		return $this;
154
-	}
155
-
156
-	/**
157
-	 * Remove the specified allowed style domain from the allowed domains.
158
-	 *
159
-	 * @param string $domain
160
-	 * @return $this
161
-	 * @since 8.1.0
162
-	 */
163
-	public function disallowStyleDomain($domain) {
164
-		$this->allowedStyleDomains = array_diff($this->allowedStyleDomains, [$domain]);
165
-		return $this;
166
-	}
167
-
168
-	/**
169
-	 * Allows using fonts from a specific domain. Use * to allow
170
-	 * fonts from all domains.
171
-	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
172
-	 * @return $this
173
-	 * @since 8.1.0
174
-	 */
175
-	public function addAllowedFontDomain($domain) {
176
-		$this->allowedFontDomains[] = $domain;
177
-		return $this;
178
-	}
179
-
180
-	/**
181
-	 * Remove the specified allowed font domain from the allowed domains.
182
-	 *
183
-	 * @param string $domain
184
-	 * @return $this
185
-	 * @since 8.1.0
186
-	 */
187
-	public function disallowFontDomain($domain) {
188
-		$this->allowedFontDomains = array_diff($this->allowedFontDomains, [$domain]);
189
-		return $this;
190
-	}
191
-
192
-	/**
193
-	 * Allows embedding images from a specific domain. Use * to allow
194
-	 * images from all domains.
195
-	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
196
-	 * @return $this
197
-	 * @since 8.1.0
198
-	 */
199
-	public function addAllowedImageDomain($domain) {
200
-		$this->allowedImageDomains[] = $domain;
201
-		return $this;
202
-	}
203
-
204
-	/**
205
-	 * Remove the specified allowed image domain from the allowed domains.
206
-	 *
207
-	 * @param string $domain
208
-	 * @return $this
209
-	 * @since 8.1.0
210
-	 */
211
-	public function disallowImageDomain($domain) {
212
-		$this->allowedImageDomains = array_diff($this->allowedImageDomains, [$domain]);
213
-		return $this;
214
-	}
215
-
216
-	/**
217
-	 * To which remote domains the JS connect to.
218
-	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
219
-	 * @return $this
220
-	 * @since 8.1.0
221
-	 */
222
-	public function addAllowedConnectDomain($domain) {
223
-		$this->allowedConnectDomains[] = $domain;
224
-		return $this;
225
-	}
226
-
227
-	/**
228
-	 * Remove the specified allowed connect domain from the allowed domains.
229
-	 *
230
-	 * @param string $domain
231
-	 * @return $this
232
-	 * @since 8.1.0
233
-	 */
234
-	public function disallowConnectDomain($domain) {
235
-		$this->allowedConnectDomains = array_diff($this->allowedConnectDomains, [$domain]);
236
-		return $this;
237
-	}
238
-
239
-	/**
240
-	 * From which domains media elements can be embedded.
241
-	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
242
-	 * @return $this
243
-	 * @since 8.1.0
244
-	 */
245
-	public function addAllowedMediaDomain($domain) {
246
-		$this->allowedMediaDomains[] = $domain;
247
-		return $this;
248
-	}
249
-
250
-	/**
251
-	 * Remove the specified allowed media domain from the allowed domains.
252
-	 *
253
-	 * @param string $domain
254
-	 * @return $this
255
-	 * @since 8.1.0
256
-	 */
257
-	public function disallowMediaDomain($domain) {
258
-		$this->allowedMediaDomains = array_diff($this->allowedMediaDomains, [$domain]);
259
-		return $this;
260
-	}
261
-
262
-	/**
263
-	 * From which domains objects such as <object>, <embed> or <applet> are executed
264
-	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
265
-	 * @return $this
266
-	 * @since 8.1.0
267
-	 */
268
-	public function addAllowedObjectDomain($domain) {
269
-		$this->allowedObjectDomains[] = $domain;
270
-		return $this;
271
-	}
272
-
273
-	/**
274
-	 * Remove the specified allowed object domain from the allowed domains.
275
-	 *
276
-	 * @param string $domain
277
-	 * @return $this
278
-	 * @since 8.1.0
279
-	 */
280
-	public function disallowObjectDomain($domain) {
281
-		$this->allowedObjectDomains = array_diff($this->allowedObjectDomains, [$domain]);
282
-		return $this;
283
-	}
284
-
285
-	/**
286
-	 * Which domains can be embedded in an iframe
287
-	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
288
-	 * @return $this
289
-	 * @since 8.1.0
290
-	 */
291
-	public function addAllowedFrameDomain($domain) {
292
-		$this->allowedFrameDomains[] = $domain;
293
-		return $this;
294
-	}
295
-
296
-	/**
297
-	 * Remove the specified allowed frame domain from the allowed domains.
298
-	 *
299
-	 * @param string $domain
300
-	 * @return $this
301
-	 * @since 8.1.0
302
-	 */
303
-	public function disallowFrameDomain($domain) {
304
-		$this->allowedFrameDomains = array_diff($this->allowedFrameDomains, [$domain]);
305
-		return $this;
306
-	}
307
-
308
-	/**
309
-	 * Domains from which web-workers and nested browsing content can load elements
310
-	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
311
-	 * @return $this
312
-	 * @since 8.1.0
313
-	 */
314
-	public function addAllowedChildSrcDomain($domain) {
315
-		$this->allowedChildSrcDomains[] = $domain;
316
-		return $this;
317
-	}
318
-
319
-	/**
320
-	 * Remove the specified allowed child src domain from the allowed domains.
321
-	 *
322
-	 * @param string $domain
323
-	 * @return $this
324
-	 * @since 8.1.0
325
-	 */
326
-	public function disallowChildSrcDomain($domain) {
327
-		$this->allowedChildSrcDomains = array_diff($this->allowedChildSrcDomains, [$domain]);
328
-		return $this;
329
-	}
330
-
331
-	/**
332
-	 * Get the generated Content-Security-Policy as a string
333
-	 * @return string
334
-	 * @since 8.1.0
335
-	 */
336
-	public function buildPolicy() {
337
-		$policy = "default-src 'none';";
338
-		$policy .= "base-uri 'none';";
339
-
340
-		if(!empty($this->allowedScriptDomains) || $this->inlineScriptAllowed || $this->evalScriptAllowed) {
341
-			$policy .= 'script-src ';
342
-			if(is_string($this->useJsNonce)) {
343
-				$policy .= '\'nonce-'.base64_encode($this->useJsNonce).'\'';
344
-				$allowedScriptDomains = array_flip($this->allowedScriptDomains);
345
-				unset($allowedScriptDomains['\'self\'']);
346
-				$this->allowedScriptDomains = array_flip($allowedScriptDomains);
347
-				if(count($allowedScriptDomains) !== 0) {
348
-					$policy .= ' ';
349
-				}
350
-			}
351
-			if(is_array($this->allowedScriptDomains)) {
352
-				$policy .= implode(' ', $this->allowedScriptDomains);
353
-			}
354
-			if($this->inlineScriptAllowed) {
355
-				$policy .= ' \'unsafe-inline\'';
356
-			}
357
-			if($this->evalScriptAllowed) {
358
-				$policy .= ' \'unsafe-eval\'';
359
-			}
360
-			$policy .= ';';
361
-		}
362
-
363
-		if(!empty($this->allowedStyleDomains) || $this->inlineStyleAllowed) {
364
-			$policy .= 'style-src ';
365
-			if(is_array($this->allowedStyleDomains)) {
366
-				$policy .= implode(' ', $this->allowedStyleDomains);
367
-			}
368
-			if($this->inlineStyleAllowed) {
369
-				$policy .= ' \'unsafe-inline\'';
370
-			}
371
-			$policy .= ';';
372
-		}
373
-
374
-		if(!empty($this->allowedImageDomains)) {
375
-			$policy .= 'img-src ' . implode(' ', $this->allowedImageDomains);
376
-			$policy .= ';';
377
-		}
378
-
379
-		if(!empty($this->allowedFontDomains)) {
380
-			$policy .= 'font-src ' . implode(' ', $this->allowedFontDomains);
381
-			$policy .= ';';
382
-		}
383
-
384
-		if(!empty($this->allowedConnectDomains)) {
385
-			$policy .= 'connect-src ' . implode(' ', $this->allowedConnectDomains);
386
-			$policy .= ';';
387
-		}
388
-
389
-		if(!empty($this->allowedMediaDomains)) {
390
-			$policy .= 'media-src ' . implode(' ', $this->allowedMediaDomains);
391
-			$policy .= ';';
392
-		}
393
-
394
-		if(!empty($this->allowedObjectDomains)) {
395
-			$policy .= 'object-src ' . implode(' ', $this->allowedObjectDomains);
396
-			$policy .= ';';
397
-		}
398
-
399
-		if(!empty($this->allowedFrameDomains)) {
400
-			$policy .= 'frame-src ' . implode(' ', $this->allowedFrameDomains);
401
-			$policy .= ';';
402
-		}
403
-
404
-		if(!empty($this->allowedChildSrcDomains)) {
405
-			$policy .= 'child-src ' . implode(' ', $this->allowedChildSrcDomains);
406
-			$policy .= ';';
407
-		}
408
-
409
-		return rtrim($policy, ';');
410
-	}
39
+    /** @var bool Whether inline JS snippets are allowed */
40
+    protected $inlineScriptAllowed = null;
41
+    /** @var string Whether JS nonces should be used */
42
+    protected $useJsNonce = null;
43
+    /**
44
+     * @var bool Whether eval in JS scripts is allowed
45
+     * TODO: Disallow per default
46
+     * @link https://github.com/owncloud/core/issues/11925
47
+     */
48
+    protected $evalScriptAllowed = null;
49
+    /** @var array Domains from which scripts can get loaded */
50
+    protected $allowedScriptDomains = null;
51
+    /**
52
+     * @var bool Whether inline CSS is allowed
53
+     * TODO: Disallow per default
54
+     * @link https://github.com/owncloud/core/issues/13458
55
+     */
56
+    protected $inlineStyleAllowed = null;
57
+    /** @var array Domains from which CSS can get loaded */
58
+    protected $allowedStyleDomains = null;
59
+    /** @var array Domains from which images can get loaded */
60
+    protected $allowedImageDomains = null;
61
+    /** @var array Domains to which connections can be done */
62
+    protected $allowedConnectDomains = null;
63
+    /** @var array Domains from which media elements can be loaded */
64
+    protected $allowedMediaDomains = null;
65
+    /** @var array Domains from which object elements can be loaded */
66
+    protected $allowedObjectDomains = null;
67
+    /** @var array Domains from which iframes can be loaded */
68
+    protected $allowedFrameDomains = null;
69
+    /** @var array Domains from which fonts can be loaded */
70
+    protected $allowedFontDomains = null;
71
+    /** @var array Domains from which web-workers and nested browsing content can load elements */
72
+    protected $allowedChildSrcDomains = null;
73
+
74
+    /**
75
+     * Whether inline JavaScript snippets are allowed or forbidden
76
+     * @param bool $state
77
+     * @return $this
78
+     * @since 8.1.0
79
+     * @deprecated 10.0 CSP tokens are now used
80
+     */
81
+    public function allowInlineScript($state = false) {
82
+        $this->inlineScriptAllowed = $state;
83
+        return $this;
84
+    }
85
+
86
+    /**
87
+     * Use the according JS nonce
88
+     *
89
+     * @param string $nonce
90
+     * @return $this
91
+     * @since 11.0.0
92
+     */
93
+    public function useJsNonce($nonce) {
94
+        $this->useJsNonce = $nonce;
95
+        return $this;
96
+    }
97
+
98
+    /**
99
+     * Whether eval in JavaScript is allowed or forbidden
100
+     * @param bool $state
101
+     * @return $this
102
+     * @since 8.1.0
103
+     */
104
+    public function allowEvalScript($state = true) {
105
+        $this->evalScriptAllowed = $state;
106
+        return $this;
107
+    }
108
+
109
+    /**
110
+     * Allows to execute JavaScript files from a specific domain. Use * to
111
+     * allow JavaScript from all domains.
112
+     * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
113
+     * @return $this
114
+     * @since 8.1.0
115
+     */
116
+    public function addAllowedScriptDomain($domain) {
117
+        $this->allowedScriptDomains[] = $domain;
118
+        return $this;
119
+    }
120
+
121
+    /**
122
+     * Remove the specified allowed script domain from the allowed domains.
123
+     *
124
+     * @param string $domain
125
+     * @return $this
126
+     * @since 8.1.0
127
+     */
128
+    public function disallowScriptDomain($domain) {
129
+        $this->allowedScriptDomains = array_diff($this->allowedScriptDomains, [$domain]);
130
+        return $this;
131
+    }
132
+
133
+    /**
134
+     * Whether inline CSS snippets are allowed or forbidden
135
+     * @param bool $state
136
+     * @return $this
137
+     * @since 8.1.0
138
+     */
139
+    public function allowInlineStyle($state = true) {
140
+        $this->inlineStyleAllowed = $state;
141
+        return $this;
142
+    }
143
+
144
+    /**
145
+     * Allows to execute CSS files from a specific domain. Use * to allow
146
+     * CSS from all domains.
147
+     * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
148
+     * @return $this
149
+     * @since 8.1.0
150
+     */
151
+    public function addAllowedStyleDomain($domain) {
152
+        $this->allowedStyleDomains[] = $domain;
153
+        return $this;
154
+    }
155
+
156
+    /**
157
+     * Remove the specified allowed style domain from the allowed domains.
158
+     *
159
+     * @param string $domain
160
+     * @return $this
161
+     * @since 8.1.0
162
+     */
163
+    public function disallowStyleDomain($domain) {
164
+        $this->allowedStyleDomains = array_diff($this->allowedStyleDomains, [$domain]);
165
+        return $this;
166
+    }
167
+
168
+    /**
169
+     * Allows using fonts from a specific domain. Use * to allow
170
+     * fonts from all domains.
171
+     * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
172
+     * @return $this
173
+     * @since 8.1.0
174
+     */
175
+    public function addAllowedFontDomain($domain) {
176
+        $this->allowedFontDomains[] = $domain;
177
+        return $this;
178
+    }
179
+
180
+    /**
181
+     * Remove the specified allowed font domain from the allowed domains.
182
+     *
183
+     * @param string $domain
184
+     * @return $this
185
+     * @since 8.1.0
186
+     */
187
+    public function disallowFontDomain($domain) {
188
+        $this->allowedFontDomains = array_diff($this->allowedFontDomains, [$domain]);
189
+        return $this;
190
+    }
191
+
192
+    /**
193
+     * Allows embedding images from a specific domain. Use * to allow
194
+     * images from all domains.
195
+     * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
196
+     * @return $this
197
+     * @since 8.1.0
198
+     */
199
+    public function addAllowedImageDomain($domain) {
200
+        $this->allowedImageDomains[] = $domain;
201
+        return $this;
202
+    }
203
+
204
+    /**
205
+     * Remove the specified allowed image domain from the allowed domains.
206
+     *
207
+     * @param string $domain
208
+     * @return $this
209
+     * @since 8.1.0
210
+     */
211
+    public function disallowImageDomain($domain) {
212
+        $this->allowedImageDomains = array_diff($this->allowedImageDomains, [$domain]);
213
+        return $this;
214
+    }
215
+
216
+    /**
217
+     * To which remote domains the JS connect to.
218
+     * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
219
+     * @return $this
220
+     * @since 8.1.0
221
+     */
222
+    public function addAllowedConnectDomain($domain) {
223
+        $this->allowedConnectDomains[] = $domain;
224
+        return $this;
225
+    }
226
+
227
+    /**
228
+     * Remove the specified allowed connect domain from the allowed domains.
229
+     *
230
+     * @param string $domain
231
+     * @return $this
232
+     * @since 8.1.0
233
+     */
234
+    public function disallowConnectDomain($domain) {
235
+        $this->allowedConnectDomains = array_diff($this->allowedConnectDomains, [$domain]);
236
+        return $this;
237
+    }
238
+
239
+    /**
240
+     * From which domains media elements can be embedded.
241
+     * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
242
+     * @return $this
243
+     * @since 8.1.0
244
+     */
245
+    public function addAllowedMediaDomain($domain) {
246
+        $this->allowedMediaDomains[] = $domain;
247
+        return $this;
248
+    }
249
+
250
+    /**
251
+     * Remove the specified allowed media domain from the allowed domains.
252
+     *
253
+     * @param string $domain
254
+     * @return $this
255
+     * @since 8.1.0
256
+     */
257
+    public function disallowMediaDomain($domain) {
258
+        $this->allowedMediaDomains = array_diff($this->allowedMediaDomains, [$domain]);
259
+        return $this;
260
+    }
261
+
262
+    /**
263
+     * From which domains objects such as <object>, <embed> or <applet> are executed
264
+     * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
265
+     * @return $this
266
+     * @since 8.1.0
267
+     */
268
+    public function addAllowedObjectDomain($domain) {
269
+        $this->allowedObjectDomains[] = $domain;
270
+        return $this;
271
+    }
272
+
273
+    /**
274
+     * Remove the specified allowed object domain from the allowed domains.
275
+     *
276
+     * @param string $domain
277
+     * @return $this
278
+     * @since 8.1.0
279
+     */
280
+    public function disallowObjectDomain($domain) {
281
+        $this->allowedObjectDomains = array_diff($this->allowedObjectDomains, [$domain]);
282
+        return $this;
283
+    }
284
+
285
+    /**
286
+     * Which domains can be embedded in an iframe
287
+     * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
288
+     * @return $this
289
+     * @since 8.1.0
290
+     */
291
+    public function addAllowedFrameDomain($domain) {
292
+        $this->allowedFrameDomains[] = $domain;
293
+        return $this;
294
+    }
295
+
296
+    /**
297
+     * Remove the specified allowed frame domain from the allowed domains.
298
+     *
299
+     * @param string $domain
300
+     * @return $this
301
+     * @since 8.1.0
302
+     */
303
+    public function disallowFrameDomain($domain) {
304
+        $this->allowedFrameDomains = array_diff($this->allowedFrameDomains, [$domain]);
305
+        return $this;
306
+    }
307
+
308
+    /**
309
+     * Domains from which web-workers and nested browsing content can load elements
310
+     * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
311
+     * @return $this
312
+     * @since 8.1.0
313
+     */
314
+    public function addAllowedChildSrcDomain($domain) {
315
+        $this->allowedChildSrcDomains[] = $domain;
316
+        return $this;
317
+    }
318
+
319
+    /**
320
+     * Remove the specified allowed child src domain from the allowed domains.
321
+     *
322
+     * @param string $domain
323
+     * @return $this
324
+     * @since 8.1.0
325
+     */
326
+    public function disallowChildSrcDomain($domain) {
327
+        $this->allowedChildSrcDomains = array_diff($this->allowedChildSrcDomains, [$domain]);
328
+        return $this;
329
+    }
330
+
331
+    /**
332
+     * Get the generated Content-Security-Policy as a string
333
+     * @return string
334
+     * @since 8.1.0
335
+     */
336
+    public function buildPolicy() {
337
+        $policy = "default-src 'none';";
338
+        $policy .= "base-uri 'none';";
339
+
340
+        if(!empty($this->allowedScriptDomains) || $this->inlineScriptAllowed || $this->evalScriptAllowed) {
341
+            $policy .= 'script-src ';
342
+            if(is_string($this->useJsNonce)) {
343
+                $policy .= '\'nonce-'.base64_encode($this->useJsNonce).'\'';
344
+                $allowedScriptDomains = array_flip($this->allowedScriptDomains);
345
+                unset($allowedScriptDomains['\'self\'']);
346
+                $this->allowedScriptDomains = array_flip($allowedScriptDomains);
347
+                if(count($allowedScriptDomains) !== 0) {
348
+                    $policy .= ' ';
349
+                }
350
+            }
351
+            if(is_array($this->allowedScriptDomains)) {
352
+                $policy .= implode(' ', $this->allowedScriptDomains);
353
+            }
354
+            if($this->inlineScriptAllowed) {
355
+                $policy .= ' \'unsafe-inline\'';
356
+            }
357
+            if($this->evalScriptAllowed) {
358
+                $policy .= ' \'unsafe-eval\'';
359
+            }
360
+            $policy .= ';';
361
+        }
362
+
363
+        if(!empty($this->allowedStyleDomains) || $this->inlineStyleAllowed) {
364
+            $policy .= 'style-src ';
365
+            if(is_array($this->allowedStyleDomains)) {
366
+                $policy .= implode(' ', $this->allowedStyleDomains);
367
+            }
368
+            if($this->inlineStyleAllowed) {
369
+                $policy .= ' \'unsafe-inline\'';
370
+            }
371
+            $policy .= ';';
372
+        }
373
+
374
+        if(!empty($this->allowedImageDomains)) {
375
+            $policy .= 'img-src ' . implode(' ', $this->allowedImageDomains);
376
+            $policy .= ';';
377
+        }
378
+
379
+        if(!empty($this->allowedFontDomains)) {
380
+            $policy .= 'font-src ' . implode(' ', $this->allowedFontDomains);
381
+            $policy .= ';';
382
+        }
383
+
384
+        if(!empty($this->allowedConnectDomains)) {
385
+            $policy .= 'connect-src ' . implode(' ', $this->allowedConnectDomains);
386
+            $policy .= ';';
387
+        }
388
+
389
+        if(!empty($this->allowedMediaDomains)) {
390
+            $policy .= 'media-src ' . implode(' ', $this->allowedMediaDomains);
391
+            $policy .= ';';
392
+        }
393
+
394
+        if(!empty($this->allowedObjectDomains)) {
395
+            $policy .= 'object-src ' . implode(' ', $this->allowedObjectDomains);
396
+            $policy .= ';';
397
+        }
398
+
399
+        if(!empty($this->allowedFrameDomains)) {
400
+            $policy .= 'frame-src ' . implode(' ', $this->allowedFrameDomains);
401
+            $policy .= ';';
402
+        }
403
+
404
+        if(!empty($this->allowedChildSrcDomains)) {
405
+            $policy .= 'child-src ' . implode(' ', $this->allowedChildSrcDomains);
406
+            $policy .= ';';
407
+        }
408
+
409
+        return rtrim($policy, ';');
410
+    }
411 411
 }
Please login to merge, or discard this patch.
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -337,72 +337,72 @@
 block discarded – undo
337 337
 		$policy = "default-src 'none';";
338 338
 		$policy .= "base-uri 'none';";
339 339
 
340
-		if(!empty($this->allowedScriptDomains) || $this->inlineScriptAllowed || $this->evalScriptAllowed) {
340
+		if (!empty($this->allowedScriptDomains) || $this->inlineScriptAllowed || $this->evalScriptAllowed) {
341 341
 			$policy .= 'script-src ';
342
-			if(is_string($this->useJsNonce)) {
342
+			if (is_string($this->useJsNonce)) {
343 343
 				$policy .= '\'nonce-'.base64_encode($this->useJsNonce).'\'';
344 344
 				$allowedScriptDomains = array_flip($this->allowedScriptDomains);
345 345
 				unset($allowedScriptDomains['\'self\'']);
346 346
 				$this->allowedScriptDomains = array_flip($allowedScriptDomains);
347
-				if(count($allowedScriptDomains) !== 0) {
347
+				if (count($allowedScriptDomains) !== 0) {
348 348
 					$policy .= ' ';
349 349
 				}
350 350
 			}
351
-			if(is_array($this->allowedScriptDomains)) {
351
+			if (is_array($this->allowedScriptDomains)) {
352 352
 				$policy .= implode(' ', $this->allowedScriptDomains);
353 353
 			}
354
-			if($this->inlineScriptAllowed) {
354
+			if ($this->inlineScriptAllowed) {
355 355
 				$policy .= ' \'unsafe-inline\'';
356 356
 			}
357
-			if($this->evalScriptAllowed) {
357
+			if ($this->evalScriptAllowed) {
358 358
 				$policy .= ' \'unsafe-eval\'';
359 359
 			}
360 360
 			$policy .= ';';
361 361
 		}
362 362
 
363
-		if(!empty($this->allowedStyleDomains) || $this->inlineStyleAllowed) {
363
+		if (!empty($this->allowedStyleDomains) || $this->inlineStyleAllowed) {
364 364
 			$policy .= 'style-src ';
365
-			if(is_array($this->allowedStyleDomains)) {
365
+			if (is_array($this->allowedStyleDomains)) {
366 366
 				$policy .= implode(' ', $this->allowedStyleDomains);
367 367
 			}
368
-			if($this->inlineStyleAllowed) {
368
+			if ($this->inlineStyleAllowed) {
369 369
 				$policy .= ' \'unsafe-inline\'';
370 370
 			}
371 371
 			$policy .= ';';
372 372
 		}
373 373
 
374
-		if(!empty($this->allowedImageDomains)) {
375
-			$policy .= 'img-src ' . implode(' ', $this->allowedImageDomains);
374
+		if (!empty($this->allowedImageDomains)) {
375
+			$policy .= 'img-src '.implode(' ', $this->allowedImageDomains);
376 376
 			$policy .= ';';
377 377
 		}
378 378
 
379
-		if(!empty($this->allowedFontDomains)) {
380
-			$policy .= 'font-src ' . implode(' ', $this->allowedFontDomains);
379
+		if (!empty($this->allowedFontDomains)) {
380
+			$policy .= 'font-src '.implode(' ', $this->allowedFontDomains);
381 381
 			$policy .= ';';
382 382
 		}
383 383
 
384
-		if(!empty($this->allowedConnectDomains)) {
385
-			$policy .= 'connect-src ' . implode(' ', $this->allowedConnectDomains);
384
+		if (!empty($this->allowedConnectDomains)) {
385
+			$policy .= 'connect-src '.implode(' ', $this->allowedConnectDomains);
386 386
 			$policy .= ';';
387 387
 		}
388 388
 
389
-		if(!empty($this->allowedMediaDomains)) {
390
-			$policy .= 'media-src ' . implode(' ', $this->allowedMediaDomains);
389
+		if (!empty($this->allowedMediaDomains)) {
390
+			$policy .= 'media-src '.implode(' ', $this->allowedMediaDomains);
391 391
 			$policy .= ';';
392 392
 		}
393 393
 
394
-		if(!empty($this->allowedObjectDomains)) {
395
-			$policy .= 'object-src ' . implode(' ', $this->allowedObjectDomains);
394
+		if (!empty($this->allowedObjectDomains)) {
395
+			$policy .= 'object-src '.implode(' ', $this->allowedObjectDomains);
396 396
 			$policy .= ';';
397 397
 		}
398 398
 
399
-		if(!empty($this->allowedFrameDomains)) {
400
-			$policy .= 'frame-src ' . implode(' ', $this->allowedFrameDomains);
399
+		if (!empty($this->allowedFrameDomains)) {
400
+			$policy .= 'frame-src '.implode(' ', $this->allowedFrameDomains);
401 401
 			$policy .= ';';
402 402
 		}
403 403
 
404
-		if(!empty($this->allowedChildSrcDomains)) {
405
-			$policy .= 'child-src ' . implode(' ', $this->allowedChildSrcDomains);
404
+		if (!empty($this->allowedChildSrcDomains)) {
405
+			$policy .= 'child-src '.implode(' ', $this->allowedChildSrcDomains);
406 406
 			$policy .= ';';
407 407
 		}
408 408
 
Please login to merge, or discard this patch.