|
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 OC\Security\FeaturePolicy\FeaturePolicy; |
|
31
|
|
|
use OC\Security\FeaturePolicy\FeaturePolicyManager; |
|
32
|
|
|
use OCP\AppFramework\Controller; |
|
33
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy; |
|
34
|
|
|
use OCP\AppFramework\Http\EmptyContentSecurityPolicy; |
|
35
|
|
|
use OCP\AppFramework\Http\EmptyFeaturePolicy; |
|
36
|
|
|
use OCP\AppFramework\Http\Response; |
|
37
|
|
|
use OCP\AppFramework\Middleware; |
|
38
|
|
|
|
|
39
|
|
|
class FeaturePolicyMiddleware extends Middleware { |
|
40
|
|
|
|
|
41
|
|
|
/** @var FeaturePolicyManager */ |
|
42
|
|
|
private $policyManager; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(FeaturePolicyManager $policyManager) { |
|
45
|
|
|
$this->policyManager = $policyManager; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Performs the default FeaturePolicy modifications that may be injected by other |
|
50
|
|
|
* applications |
|
51
|
|
|
* |
|
52
|
|
|
* @param Controller $controller |
|
53
|
|
|
* @param string $methodName |
|
54
|
|
|
* @param Response $response |
|
55
|
|
|
* @return Response |
|
56
|
|
|
*/ |
|
57
|
|
|
public function afterController($controller, $methodName, Response $response): Response { |
|
58
|
|
|
$policy = !is_null($response->getFeaturePolicy()) ? $response->getFeaturePolicy() : new FeaturePolicy(); |
|
59
|
|
|
|
|
60
|
|
|
if (get_class($policy) === EmptyFeaturePolicy::class) { |
|
61
|
|
|
return $response; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$defaultPolicy = $this->policyManager->getDefaultPolicy(); |
|
65
|
|
|
$defaultPolicy = $this->policyManager->mergePolicies($defaultPolicy, $policy); |
|
66
|
|
|
$response->setFeaturePolicy($defaultPolicy); |
|
67
|
|
|
|
|
68
|
|
|
return $response; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|