-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Feature-Policy header #16613
Merged
Merged
Add Feature-Policy header #16613
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
lib/private/AppFramework/Middleware/Security/FeaturePolicyMiddleware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]> | ||
* | ||
* @author Roeland Jago Douma <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\AppFramework\Middleware\Security; | ||
|
||
use OC\Security\CSP\ContentSecurityPolicyManager; | ||
use OC\Security\CSP\ContentSecurityPolicyNonceManager; | ||
use OC\Security\CSRF\CsrfTokenManager; | ||
use OC\Security\FeaturePolicy\FeaturePolicy; | ||
use OC\Security\FeaturePolicy\FeaturePolicyManager; | ||
use OCP\AppFramework\Controller; | ||
use OCP\AppFramework\Http\ContentSecurityPolicy; | ||
use OCP\AppFramework\Http\EmptyContentSecurityPolicy; | ||
use OCP\AppFramework\Http\EmptyFeaturePolicy; | ||
use OCP\AppFramework\Http\Response; | ||
use OCP\AppFramework\Middleware; | ||
|
||
class FeaturePolicyMiddleware extends Middleware { | ||
|
||
/** @var FeaturePolicyManager */ | ||
private $policyManager; | ||
|
||
public function __construct(FeaturePolicyManager $policyManager) { | ||
$this->policyManager = $policyManager; | ||
} | ||
|
||
/** | ||
* Performs the default FeaturePolicy modifications that may be injected by other | ||
* applications | ||
* | ||
* @param Controller $controller | ||
* @param string $methodName | ||
* @param Response $response | ||
* @return Response | ||
*/ | ||
public function afterController($controller, $methodName, Response $response): Response { | ||
$policy = !is_null($response->getFeaturePolicy()) ? $response->getFeaturePolicy() : new FeaturePolicy(); | ||
|
||
if (get_class($policy) === EmptyFeaturePolicy::class) { | ||
return $response; | ||
} | ||
|
||
$defaultPolicy = $this->policyManager->getDefaultPolicy(); | ||
$defaultPolicy = $this->policyManager->mergePolicies($defaultPolicy, $policy); | ||
$response->setFeaturePolicy($defaultPolicy); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]> | ||
* | ||
* @author Roeland Jago Douma <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\Security\FeaturePolicy; | ||
|
||
class FeaturePolicy extends \OCP\AppFramework\Http\FeaturePolicy { | ||
|
||
public function getAutoplayDomains(): array { | ||
return $this->autoplayDomains; | ||
} | ||
|
||
public function setAutoplayDomains(array $autoplayDomains): void { | ||
$this->autoplayDomains = $autoplayDomains; | ||
} | ||
|
||
public function getCameraDomains(): array { | ||
return $this->cameraDomains; | ||
} | ||
|
||
public function setCameraDomains(array $cameraDomains): void { | ||
$this->cameraDomains = $cameraDomains; | ||
} | ||
|
||
public function getFullscreenDomains(): array { | ||
return $this->fullscreenDomains; | ||
} | ||
|
||
public function setFullscreenDomains(array $fullscreenDomains): void { | ||
$this->fullscreenDomains = $fullscreenDomains; | ||
} | ||
|
||
public function getGeolocationDomains(): array { | ||
return $this->geolocationDomains; | ||
} | ||
|
||
public function setGeolocationDomains(array $geolocationDomains): void { | ||
$this->geolocationDomains = $geolocationDomains; | ||
} | ||
|
||
public function getMicrophoneDomains(): array { | ||
return $this->microphoneDomains; | ||
} | ||
|
||
public function setMicrophoneDomains(array $microphoneDomains): void { | ||
$this->microphoneDomains = $microphoneDomains; | ||
} | ||
|
||
public function getPaymentDomains(): array { | ||
return $this->paymentDomains; | ||
} | ||
|
||
public function setPaymentDomains(array $paymentDomains): void { | ||
$this->paymentDomains = $paymentDomains; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
lib/private/Security/FeaturePolicy/FeaturePolicyManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]> | ||
* | ||
* @author Roeland Jago Douma <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\Security\FeaturePolicy; | ||
|
||
use OCP\AppFramework\Http\EmptyFeaturePolicy; | ||
use OCP\EventDispatcher\IEventDispatcher; | ||
use OCP\Security\FeaturePolicy\AddFeaturePolicyEvent; | ||
|
||
class FeaturePolicyManager { | ||
/** @var EmptyFeaturePolicy[] */ | ||
private $policies = []; | ||
|
||
/** @var IEventDispatcher */ | ||
private $dispatcher; | ||
|
||
public function __construct(IEventDispatcher $dispatcher) { | ||
$this->dispatcher = $dispatcher; | ||
} | ||
|
||
public function addDefaultPolicy(EmptyFeaturePolicy $policy): void { | ||
$this->policies[] = $policy; | ||
} | ||
|
||
public function getDefaultPolicy(): FeaturePolicy { | ||
$event = new AddFeaturePolicyEvent($this); | ||
$this->dispatcher->dispatch(AddFeaturePolicyEvent::class, $event); | ||
|
||
$defaultPolicy = new FeaturePolicy(); | ||
foreach ($this->policies as $policy) { | ||
$defaultPolicy = $this->mergePolicies($defaultPolicy, $policy); | ||
} | ||
return $defaultPolicy; | ||
} | ||
|
||
/** | ||
* Merges the first given policy with the second one | ||
* | ||
*/ | ||
public function mergePolicies(FeaturePolicy $defaultPolicy, | ||
EmptyFeaturePolicy $originalPolicy): FeaturePolicy { | ||
foreach ((object)(array)$originalPolicy as $name => $value) { | ||
$setter = 'set' . ucfirst($name); | ||
if (\is_array($value)) { | ||
$getter = 'get' . ucfirst($name); | ||
$currentValues = \is_array($defaultPolicy->$getter()) ? $defaultPolicy->$getter() : []; | ||
$defaultPolicy->$setter(\array_values(\array_unique(\array_merge($currentValues, $value)))); | ||
} elseif (\is_bool($value)) { | ||
$defaultPolicy->$setter($value); | ||
} | ||
} | ||
|
||
return $defaultPolicy; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could that work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably. But lets leave it for now.