Passed
Push — master ( ec7e83...51197a )
by Roeland
10:19 queued 12s
created

CSPMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC\AppFramework\Middleware\Security;
26
27
use OC\Security\CSP\ContentSecurityPolicyManager;
28
use OC\Security\CSP\ContentSecurityPolicyNonceManager;
29
use OC\Security\CSRF\CsrfTokenManager;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Http\ContentSecurityPolicy;
32
use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
33
use OCP\AppFramework\Http\Response;
34
use OCP\AppFramework\Middleware;
35
36
class CSPMiddleware extends Middleware {
37
38
	/** @var ContentSecurityPolicyManager */
39
	private $contentSecurityPolicyManager;
40
	/** @var ContentSecurityPolicyNonceManager */
41
	private $cspNonceManager;
42
	/** @var CsrfTokenManager */
43
	private $csrfTokenManager;
44
45
	public function __construct(ContentSecurityPolicyManager $policyManager,
46
								ContentSecurityPolicyNonceManager $cspNonceManager,
47
								CsrfTokenManager $csrfTokenManager) {
48
		$this->contentSecurityPolicyManager = $policyManager;
49
		$this->cspNonceManager = $cspNonceManager;
50
		$this->csrfTokenManager = $csrfTokenManager;
51
	}
52
53
	/**
54
	 * Performs the default CSP modifications that may be injected by other
55
	 * applications
56
	 *
57
	 * @param Controller $controller
58
	 * @param string $methodName
59
	 * @param Response $response
60
	 * @return Response
61
	 */
62
	public function afterController($controller, $methodName, Response $response): Response {
63
		$policy = !is_null($response->getContentSecurityPolicy()) ? $response->getContentSecurityPolicy() : new ContentSecurityPolicy();
64
65
		if (get_class($policy) === EmptyContentSecurityPolicy::class) {
66
			return $response;
67
		}
68
69
		$defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy();
70
		$defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy);
71
72
		if($this->cspNonceManager->browserSupportsCspV3()) {
73
			$defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue());
74
		}
75
76
		$response->setContentSecurityPolicy($defaultPolicy);
77
78
		return $response;
79
	}
80
}
81